From 751c2b0dbe9801eee83048859af23a829a13a192 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 7 Mar 2022 15:21:37 -0500 Subject: [PATCH] moves work --- api.go | 18 +++++++++++++++--- session.go | 22 ++++++++++++---------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/api.go b/api.go index 3fd89f0..581b894 100644 --- a/api.go +++ b/api.go @@ -32,6 +32,7 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) { if g.bluePlayer.Ready { g.simulator.Setup() initDummy(g.simulator) + log.Println("dummy game started") } respondWithJSON(res, http.StatusOK, newGameResp{i, "red"}) return @@ -41,6 +42,7 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) { if g.redPlayer.Ready { g.simulator.Setup() initDummy(g.simulator) + log.Println("dummy game started") } respondWithJSON(res, http.StatusOK, newGameResp{i, "blue"}) return @@ -79,6 +81,7 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) { return } //TODO filter based off player info + log.Println("sending game state") respondWithJSON(res, http.StatusOK, gameResp{s.simulator}) return } @@ -107,6 +110,7 @@ func (a *API) GetGameStatus(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "No such game") return } + log.Println("sending game status") respondWithJSON(res, http.StatusOK, gameStatusResp{s.simulator.State, s.moveNum}) } @@ -143,9 +147,17 @@ func (a *API) PostMove(res http.ResponseWriter, req *http.Request) { } parsed, err := s.tryMove(p, gr.Move) if err != nil { - respondWithJSON(res, http.StatusOK, gameMovePostRes{false, false, parsed.String(), err}) - } + respondWithError(res, http.StatusBadRequest, err.Error()) + } + result, err := s.mutate(parsed) + if err != nil { + respondWithError(res, http.StatusBadRequest, err.Error()) + } + if result == "" { + respondWithJSON(res, http.StatusOK, gameMovePostRes{true, false, "", err}) + } + respondWithJSON(res, http.StatusOK, gameMovePostRes{true, true, result, err}) } //GetMove returns the move made at turn X @@ -156,7 +168,7 @@ func (a *API) GetMove(res http.ResponseWriter, req *http.Request) { respondWithError(res, http.StatusBadRequest, "Invalid game ID") return } - move, err := strconv.Atoi(vars["move_number"]) + move, err := strconv.Atoi(vars["movenum"]) if err != nil { respondWithError(res, http.StatusBadRequest, "Invalid move number") return diff --git a/session.go b/session.go index b972e66..fea9906 100644 --- a/session.go +++ b/session.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "log" "git.saintnet.tech/stryan/freego" ) @@ -39,8 +40,8 @@ func NewSession() *Session { simulator: sim, redPlayer: &Player{false, freego.Red}, bluePlayer: &Player{false, freego.Blue}, - moveNum: 0, - moveList: []freego.ParsedCommand{}, + moveNum: 1, + moveList: make([]freego.ParsedCommand, 20), } } @@ -56,21 +57,22 @@ func (s *Session) tryMove(player *Player, move string) (*freego.ParsedCommand, e return p, nil } -func (s *Session) mutate(p *freego.ParsedCommand) error { +func (s *Session) mutate(p *freego.ParsedCommand) (string, error) { success, err := s.simulator.Mutate(p) if err != nil { - return err + return "", err } - if !success { - return errors.New("invalid move") + if success { + s.moveList[s.moveNum] = *p + s.moveNum++ + return fmt.Sprintf("%v %v", s.moveNum-1, p.String()), nil } - s.moveList[s.moveNum] = *p - s.moveNum++ - return nil + return "", nil } func (s *Session) getMove(p *Player, num int) (string, error) { - if num < 0 || num > s.moveNum { + if num <= 0 || num >= s.moveNum { + log.Printf("tried to get move number %v when move is %v", num, s.moveNum) return "", errors.New("invalid move number") } return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil