use headers for player_id
This commit is contained in:
parent
56e561c91e
commit
877edb34f5
76
api.go
76
api.go
@ -63,18 +63,7 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "Invalid game ID")
|
respondWithError(res, http.StatusBadRequest, "Invalid game ID")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var gr gameReq
|
pid := req.Header.Get("Player-id")
|
||||||
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
|
|
||||||
}
|
|
||||||
var p *Player
|
var p *Player
|
||||||
|
|
||||||
s, isset := a.games[id]
|
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")
|
respondWithError(res, http.StatusBadRequest, "No such game")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if gr.PlayerID == "red" {
|
if pid == "red" {
|
||||||
p = s.redPlayer
|
p = s.redPlayer
|
||||||
} else {
|
} else if pid == "blue" {
|
||||||
p = s.bluePlayer
|
p = s.bluePlayer
|
||||||
|
} else {
|
||||||
|
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("sending game state")
|
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")
|
respondWithError(res, http.StatusBadRequest, "Invalid game ID")
|
||||||
return
|
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]
|
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
|
||||||
}
|
}
|
||||||
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})
|
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
|
var gr gameMovePostReq
|
||||||
decoder := json.NewDecoder(req.Body)
|
decoder := json.NewDecoder(req.Body)
|
||||||
if err := decoder.Decode(&gr); err != nil {
|
if err := decoder.Decode(&gr); err != nil {
|
||||||
respondWithError(res, http.StatusBadRequest, "Invalid resquest payload")
|
respondWithError(res, http.StatusBadRequest, "Invalid request payload")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer req.Body.Close()
|
defer req.Body.Close()
|
||||||
if gr.PlayerID != "red" && gr.PlayerID != "blue" {
|
pid := req.Header.Get("Player-id")
|
||||||
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var p *Player
|
var p *Player
|
||||||
|
|
||||||
s, isset := a.games[id]
|
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")
|
respondWithError(res, http.StatusBadRequest, "No such game")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if gr.PlayerID == "red" {
|
if pid == "red" {
|
||||||
p = s.redPlayer
|
p = s.redPlayer
|
||||||
} else {
|
} else if pid == "blue" {
|
||||||
p = s.bluePlayer
|
p = s.bluePlayer
|
||||||
|
} else {
|
||||||
|
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
parsed, err := s.tryMove(p, gr.Move)
|
parsed, err := s.tryMove(p, gr.Move)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -180,17 +171,6 @@ func (a *API) GetMove(res http.ResponseWriter, req *http.Request) {
|
|||||||
respondWithError(res, http.StatusBadRequest, "Invalid move number")
|
respondWithError(res, http.StatusBadRequest, "Invalid move number")
|
||||||
return
|
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
|
var p *Player
|
||||||
|
|
||||||
s, isset := a.games[id]
|
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")
|
respondWithError(res, http.StatusBadRequest, "No such game")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if gr.PlayerID == "red" {
|
pid := req.Header.Get("Player-id")
|
||||||
|
if pid == "red" {
|
||||||
p = s.redPlayer
|
p = s.redPlayer
|
||||||
} else {
|
} else if pid == "blue" {
|
||||||
p = s.bluePlayer
|
p = s.bluePlayer
|
||||||
|
} else {
|
||||||
|
respondWithError(res, http.StatusBadRequest, "Bad player ID")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
moveRes, err := s.getMove(p, move)
|
moveRes, err := s.getMove(p, move)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
15
api_types.go
15
api_types.go
@ -9,26 +9,17 @@ type newGameResp struct {
|
|||||||
PlayerID string `json:"player_id"`
|
PlayerID string `json:"player_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameReq struct {
|
|
||||||
PlayerID string `json:"player_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gameResp struct {
|
type gameResp struct {
|
||||||
GameBoard [8][8]*ViewTile `json:"board"`
|
GameBoard [8][8]*ViewTile `json:"board"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameStatusReq struct {
|
|
||||||
PlayerID string `json:"player_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gameStatusResp struct {
|
type gameStatusResp struct {
|
||||||
GameStatus freego.GameState `json:"game_status"`
|
GameStatus freego.GameState `json:"game_status"`
|
||||||
Move int `json:"move"`
|
Move int `json:"move"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameMovePostReq struct {
|
type gameMovePostReq struct {
|
||||||
PlayerID string `json:"player_id"`
|
Move string `json:"move"`
|
||||||
Move string `json:"move"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameMovePostRes struct {
|
type gameMovePostRes struct {
|
||||||
@ -38,10 +29,6 @@ type gameMovePostRes struct {
|
|||||||
Error error `json:"error"`
|
Error error `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type gameMoveReq struct {
|
|
||||||
PlayerID string `json:"player_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gameMoveRes struct {
|
type gameMoveRes struct {
|
||||||
Move string `json:"move"`
|
Move string `json:"move"`
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user