fix game view

This commit is contained in:
stryan 2022-03-07 16:14:30 -05:00
parent 09831f53bf
commit 08dedb24d2
6 changed files with 68 additions and 4 deletions

11
api.go
View File

@ -75,14 +75,21 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
respondWithError(res, http.StatusBadRequest, "Bad player ID") respondWithError(res, http.StatusBadRequest, "Bad player ID")
return return
} }
var p *Player
s, isset := a.games[id] s, isset := a.games[id]
if !isset { if !isset {
respondWithError(res, http.StatusBadRequest, "No such game") respondWithError(res, http.StatusBadRequest, "No such game")
return return
} }
//TODO filter based off player info if gr.PlayerID == "red" {
p = s.redPlayer
} else {
p = s.bluePlayer
}
log.Println("sending game state") log.Println("sending game state")
respondWithJSON(res, http.StatusOK, gameResp{s.simulator}) respondWithJSON(res, http.StatusOK, gameResp{s.getBoard(p)})
return return
} }

View File

@ -14,7 +14,7 @@ type gameReq struct {
} }
type gameResp struct { type gameResp struct {
GameBoard *freego.Game `json:"board"` GameBoard [8][8]*ViewTile `json:"board"`
} }
type gameStatusReq struct { type gameStatusReq struct {

2
go.mod
View File

@ -3,6 +3,6 @@ module git.saintnet.tech/freego_api
go 1.17 go 1.17
require ( require (
git.saintnet.tech/stryan/freego v0.0.0-20220307202314-dd8dce886942 // indirect git.saintnet.tech/stryan/freego v0.0.0-20220307210942-38a41634101a // indirect
github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/mux v1.8.0 // indirect
) )

6
go.sum
View File

@ -4,5 +4,11 @@ git.saintnet.tech/stryan/freego v0.0.0-20220307194514-b5fc90339454 h1:KK8YuhJYMj
git.saintnet.tech/stryan/freego v0.0.0-20220307194514-b5fc90339454/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc= git.saintnet.tech/stryan/freego v0.0.0-20220307194514-b5fc90339454/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc=
git.saintnet.tech/stryan/freego v0.0.0-20220307202314-dd8dce886942 h1:fgVoPchS1ETLuPKMtu4pO6zU5JRqXp+O4iy49uIaxhE= git.saintnet.tech/stryan/freego v0.0.0-20220307202314-dd8dce886942 h1:fgVoPchS1ETLuPKMtu4pO6zU5JRqXp+O4iy49uIaxhE=
git.saintnet.tech/stryan/freego v0.0.0-20220307202314-dd8dce886942/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc= git.saintnet.tech/stryan/freego v0.0.0-20220307202314-dd8dce886942/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc=
git.saintnet.tech/stryan/freego v0.0.0-20220307205125-093ef4caf221 h1:OfBW5x9SAjpk+dGRbdmw0qmh8Ru3eJl6wjDTD4QwxAU=
git.saintnet.tech/stryan/freego v0.0.0-20220307205125-093ef4caf221/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc=
git.saintnet.tech/stryan/freego v0.0.0-20220307205900-a33244481774 h1:V5V4/SEX3HjeilyO7gi9fp7DpevkDIrKPukdgLOMEXw=
git.saintnet.tech/stryan/freego v0.0.0-20220307205900-a33244481774/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc=
git.saintnet.tech/stryan/freego v0.0.0-20220307210942-38a41634101a h1:y/UwFZN0yZS0OYcdXnjvbdiwY4ZKIi8+K8x2xRwJ1L8=
git.saintnet.tech/stryan/freego v0.0.0-20220307210942-38a41634101a/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=

View File

@ -77,3 +77,40 @@ func (s *Session) getMove(p *Player, num int) (string, error) {
} }
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil
} }
func (s *Session) getBoard(p *Player) [8][8]*ViewTile {
var res [8][8]*ViewTile
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
cur := NewViewTile()
terrain, err := s.simulator.Board.IsTerrain(i, j)
if err != nil {
panic(err)
}
if terrain {
cur.Terrain = true
} else {
piece, err := s.simulator.Board.GetPiece(i, j)
if err != nil {
panic(err)
}
if piece != nil {
if piece.Hidden {
cur.Hidden = true
if piece.Owner == p.Colour() {
cur.Piece = piece.Rank.String()
} else {
cur.Piece = "Unknown"
}
} else {
cur.Piece = piece.Rank.String()
}
} else {
cur.Empty = true
}
}
res[i][j] = cur
}
}
return res
}

14
view_tile.go Normal file
View File

@ -0,0 +1,14 @@
package main
//ViewTile is a json friendly version of a tile
type ViewTile struct {
Piece string `json:"piece"`
Terrain bool `json:"terrain"`
Hidden bool `json:"hidden"`
Empty bool `json:"empty"`
}
//NewViewTile creates a new ViewTile
func NewViewTile() *ViewTile {
return &ViewTile{}
}