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 {
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

View File

@ -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