diff --git a/api.go b/api.go index 581b894..9eee459 100644 --- a/api.go +++ b/api.go @@ -75,14 +75,21 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "Bad player ID") return } + var p *Player + s, isset := a.games[id] if !isset { respondWithError(res, http.StatusBadRequest, "No such game") return } - //TODO filter based off player info + if gr.PlayerID == "red" { + p = s.redPlayer + } else { + p = s.bluePlayer + } + log.Println("sending game state") - respondWithJSON(res, http.StatusOK, gameResp{s.simulator}) + respondWithJSON(res, http.StatusOK, gameResp{s.getBoard(p)}) return } diff --git a/api_types.go b/api_types.go index 67b9508..ab159c5 100644 --- a/api_types.go +++ b/api_types.go @@ -14,7 +14,7 @@ type gameReq struct { } type gameResp struct { - GameBoard *freego.Game `json:"board"` + GameBoard [8][8]*ViewTile `json:"board"` } type gameStatusReq struct { diff --git a/go.mod b/go.mod index 78a1c2c..21b461a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module git.saintnet.tech/freego_api go 1.17 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 ) diff --git a/go.sum b/go.sum index d733f1b..62dddc6 100644 --- a/go.sum +++ b/go.sum @@ -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-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-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/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= diff --git a/session.go b/session.go index fea9906..5474124 100644 --- a/session.go +++ b/session.go @@ -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 } + +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 +} diff --git a/view_tile.go b/view_tile.go new file mode 100644 index 0000000..a038653 --- /dev/null +++ b/view_tile.go @@ -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{} +}