From 877edb34f53a484d865f7c0d19dca78e2b470d12 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 8 Mar 2022 16:11:15 -0500 Subject: [PATCH] use headers for player_id --- api.go | 76 +++++++++++++++++++++------------------------------- api_types.go | 15 +---------- 2 files changed, 31 insertions(+), 60 deletions(-) diff --git a/api.go b/api.go index 9eee459..2b8cf15 100644 --- a/api.go +++ b/api.go @@ -63,18 +63,7 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "Invalid game ID") return } - var gr gameReq - decoder := json.NewDecoder(req.Body) - if err := decoder.Decode(&gr); err != nil { - log.Println(err) - respondWithError(res, http.StatusBadRequest, "Invalid request payload") - return - } - defer req.Body.Close() - if gr.PlayerID != "red" && gr.PlayerID != "blue" { - respondWithError(res, http.StatusBadRequest, "Bad player ID") - return - } + pid := req.Header.Get("Player-id") var p *Player s, isset := a.games[id] @@ -82,10 +71,13 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "No such game") return } - if gr.PlayerID == "red" { + if pid == "red" { p = s.redPlayer - } else { + } else if pid == "blue" { p = s.bluePlayer + } else { + respondWithError(res, http.StatusBadRequest, "Bad player ID") + return } log.Println("sending game state") @@ -101,23 +93,22 @@ func (a *API) GetGameStatus(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "Invalid game ID") return } - var gr gameReq - decoder := json.NewDecoder(req.Body) - if err := decoder.Decode(&gr); err != nil { - respondWithError(res, http.StatusBadRequest, "Invalid resquest payload") - return - } - defer req.Body.Close() - if gr.PlayerID != "red" && gr.PlayerID != "blue" { - respondWithError(res, http.StatusBadRequest, "Bad player ID") - return - } s, isset := a.games[id] if !isset { respondWithError(res, http.StatusBadRequest, "No such game") return } - log.Println("sending game status") + pid := req.Header.Get("Player-id") + var p *Player + if pid == "red" { + p = s.redPlayer + } else if pid == "blue" { + p = s.bluePlayer + } else { + respondWithError(res, http.StatusBadRequest, "Bad player ID") + return + } + log.Printf("sending game status to player %v", p.Team) respondWithJSON(res, http.StatusOK, gameStatusResp{s.simulator.State, s.moveNum}) } @@ -132,14 +123,11 @@ func (a *API) PostMove(res http.ResponseWriter, req *http.Request) { var gr gameMovePostReq decoder := json.NewDecoder(req.Body) if err := decoder.Decode(&gr); err != nil { - respondWithError(res, http.StatusBadRequest, "Invalid resquest payload") + respondWithError(res, http.StatusBadRequest, "Invalid request payload") return } defer req.Body.Close() - if gr.PlayerID != "red" && gr.PlayerID != "blue" { - respondWithError(res, http.StatusBadRequest, "Bad player ID") - return - } + pid := req.Header.Get("Player-id") var p *Player s, isset := a.games[id] @@ -147,10 +135,13 @@ func (a *API) PostMove(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "No such game") return } - if gr.PlayerID == "red" { + if pid == "red" { p = s.redPlayer - } else { + } else if pid == "blue" { p = s.bluePlayer + } else { + respondWithError(res, http.StatusBadRequest, "Bad player ID") + return } parsed, err := s.tryMove(p, gr.Move) if err != nil { @@ -180,17 +171,6 @@ func (a *API) GetMove(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "Invalid move number") return } - var gr gameMoveReq - decoder := json.NewDecoder(req.Body) - if err := decoder.Decode(&gr); err != nil { - respondWithError(res, http.StatusBadRequest, "Invalid resquest payload") - return - } - defer req.Body.Close() - if gr.PlayerID != "red" && gr.PlayerID != "blue" { - respondWithError(res, http.StatusBadRequest, "Bad player ID") - return - } var p *Player s, isset := a.games[id] @@ -198,10 +178,14 @@ func (a *API) GetMove(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "No such game") return } - if gr.PlayerID == "red" { + pid := req.Header.Get("Player-id") + if pid == "red" { p = s.redPlayer - } else { + } else if pid == "blue" { p = s.bluePlayer + } else { + respondWithError(res, http.StatusBadRequest, "Bad player ID") + return } moveRes, err := s.getMove(p, move) if err != nil { diff --git a/api_types.go b/api_types.go index ab159c5..a6f586d 100644 --- a/api_types.go +++ b/api_types.go @@ -9,26 +9,17 @@ type newGameResp struct { PlayerID string `json:"player_id"` } -type gameReq struct { - PlayerID string `json:"player_id"` -} - type gameResp struct { GameBoard [8][8]*ViewTile `json:"board"` } -type gameStatusReq struct { - PlayerID string `json:"player_id"` -} - type gameStatusResp struct { GameStatus freego.GameState `json:"game_status"` Move int `json:"move"` } type gameMovePostReq struct { - PlayerID string `json:"player_id"` - Move string `json:"move"` + Move string `json:"move"` } type gameMovePostRes struct { @@ -38,10 +29,6 @@ type gameMovePostRes struct { Error error `json:"error"` } -type gameMoveReq struct { - PlayerID string `json:"player_id"` -} - type gameMoveRes struct { Move string `json:"move"` }