actually use new handlers

This commit is contained in:
stryan 2022-03-07 15:04:13 -05:00
parent 38c21448cc
commit fa019a4de2
2 changed files with 8 additions and 3 deletions

7
api.go
View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
@ -26,6 +27,7 @@ func NewAPI() *API {
func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
for i, g := range a.games {
if !g.redPlayer.Ready {
log.Println("red player somehow not ready")
g.redPlayer.Ready = true
if g.bluePlayer.Ready {
g.simulator.Setup()
@ -44,7 +46,9 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
return
}
}
log.Printf("creating new game %v", a.nextInt)
a.games[a.nextInt] = NewSession()
a.games[a.nextInt].redPlayer.Ready = true
respondWithJSON(res, http.StatusOK, newGameResp{a.nextInt, "red"})
a.nextInt = a.nextInt + 1
}
@ -60,7 +64,8 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
var gr gameReq
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&gr); err != nil {
respondWithError(res, http.StatusBadRequest, "Invalid resquest payload")
log.Println(err)
respondWithError(res, http.StatusBadRequest, "Invalid request payload")
return
}
defer req.Body.Close()

View File

@ -14,8 +14,8 @@ func main() {
router.HandleFunc("/game", api.NewGame).Methods("POST")
router.HandleFunc("/game/{id}", api.GetGame).Methods("GET")
router.HandleFunc("/game/{id}/status", api.GetGameStatus).Methods("GET")
router.HandleFunc("/game/{id}/move", nil).Methods("POST")
router.HandleFunc("/game/{id}/move/{movenum}", nil).Methods("GET")
router.HandleFunc("/game/{id}/move", api.PostMove).Methods("POST")
router.HandleFunc("/game/{id}/move/{movenum}", api.GetMove).Methods("GET")
log.Fatal(http.ListenAndServe(":1379", router))
}