moves work

This commit is contained in:
stryan 2022-03-07 15:21:37 -05:00
parent fa019a4de2
commit 751c2b0dbe
2 changed files with 27 additions and 13 deletions

18
api.go
View File

@ -32,6 +32,7 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
if g.bluePlayer.Ready { if g.bluePlayer.Ready {
g.simulator.Setup() g.simulator.Setup()
initDummy(g.simulator) initDummy(g.simulator)
log.Println("dummy game started")
} }
respondWithJSON(res, http.StatusOK, newGameResp{i, "red"}) respondWithJSON(res, http.StatusOK, newGameResp{i, "red"})
return return
@ -41,6 +42,7 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
if g.redPlayer.Ready { if g.redPlayer.Ready {
g.simulator.Setup() g.simulator.Setup()
initDummy(g.simulator) initDummy(g.simulator)
log.Println("dummy game started")
} }
respondWithJSON(res, http.StatusOK, newGameResp{i, "blue"}) respondWithJSON(res, http.StatusOK, newGameResp{i, "blue"})
return return
@ -79,6 +81,7 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
return return
} }
//TODO filter based off player info //TODO filter based off player info
log.Println("sending game state")
respondWithJSON(res, http.StatusOK, gameResp{s.simulator}) respondWithJSON(res, http.StatusOK, gameResp{s.simulator})
return return
} }
@ -107,6 +110,7 @@ func (a *API) GetGameStatus(res http.ResponseWriter, req *http.Request) {
respondWithError(res, http.StatusBadRequest, "No such game") respondWithError(res, http.StatusBadRequest, "No such game")
return return
} }
log.Println("sending game status")
respondWithJSON(res, http.StatusOK, gameStatusResp{s.simulator.State, s.moveNum}) respondWithJSON(res, http.StatusOK, gameStatusResp{s.simulator.State, s.moveNum})
} }
@ -143,10 +147,18 @@ func (a *API) PostMove(res http.ResponseWriter, req *http.Request) {
} }
parsed, err := s.tryMove(p, gr.Move) parsed, err := s.tryMove(p, gr.Move)
if err != nil { 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 //GetMove returns the move made at turn X
func (a *API) GetMove(res http.ResponseWriter, req *http.Request) { func (a *API) GetMove(res http.ResponseWriter, req *http.Request) {
@ -156,7 +168,7 @@ func (a *API) GetMove(res http.ResponseWriter, req *http.Request) {
respondWithError(res, http.StatusBadRequest, "Invalid game ID") respondWithError(res, http.StatusBadRequest, "Invalid game ID")
return return
} }
move, err := strconv.Atoi(vars["move_number"]) move, err := strconv.Atoi(vars["movenum"])
if err != nil { if err != nil {
respondWithError(res, http.StatusBadRequest, "Invalid move number") respondWithError(res, http.StatusBadRequest, "Invalid move number")
return return

View File

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"git.saintnet.tech/stryan/freego" "git.saintnet.tech/stryan/freego"
) )
@ -39,8 +40,8 @@ func NewSession() *Session {
simulator: sim, simulator: sim,
redPlayer: &Player{false, freego.Red}, redPlayer: &Player{false, freego.Red},
bluePlayer: &Player{false, freego.Blue}, bluePlayer: &Player{false, freego.Blue},
moveNum: 0, moveNum: 1,
moveList: []freego.ParsedCommand{}, moveList: make([]freego.ParsedCommand, 20),
} }
} }
@ -56,21 +57,22 @@ func (s *Session) tryMove(player *Player, move string) (*freego.ParsedCommand, e
return p, nil 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) success, err := s.simulator.Mutate(p)
if err != nil { if err != nil {
return err return "", err
}
if !success {
return errors.New("invalid move")
} }
if success {
s.moveList[s.moveNum] = *p s.moveList[s.moveNum] = *p
s.moveNum++ s.moveNum++
return nil return fmt.Sprintf("%v %v", s.moveNum-1, p.String()), nil
}
return "", nil
} }
func (s *Session) getMove(p *Player, num int) (string, error) { 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 "", errors.New("invalid move number")
} }
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil