rest of the routes, dummy game creation

This commit is contained in:
stryan 2022-03-07 14:47:35 -05:00
parent 06e2175021
commit 86883cff71
5 changed files with 75 additions and 3 deletions

21
api.go
View File

@ -22,8 +22,27 @@ func NewAPI() *API {
}
}
//NewGame takes a POST and creates a new game
//NewGame takes a POST and creates a new game or returns an open one
func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
for i, g := range a.games {
if !g.redPlayer.Ready {
g.redPlayer.Ready = true
if g.bluePlayer.Ready {
g.simulator.Setup()
initDummy(g.simulator)
}
respondWithJSON(res, http.StatusOK, newGameResp{i, "red"})
}
if !g.bluePlayer.Ready {
g.bluePlayer.Ready = true
if g.redPlayer.Ready {
g.simulator.Setup()
initDummy(g.simulator)
}
respondWithJSON(res, http.StatusOK, newGameResp{i, "blue"})
return
}
}
a.games[a.nextInt] = NewSession()
respondWithJSON(res, http.StatusOK, newGameResp{a.nextInt, "red"})
a.nextInt = a.nextInt + 1

2
go.mod
View File

@ -3,6 +3,6 @@ module git.saintnet.tech/freego_api
go 1.17
require (
git.saintnet.tech/stryan/freego v0.0.0-20220307180035-64bab97c38d6 // indirect
git.saintnet.tech/stryan/freego v0.0.0-20220307194514-b5fc90339454 // indirect
github.com/gorilla/mux v1.8.0 // indirect
)

2
go.sum
View File

@ -1,4 +1,6 @@
git.saintnet.tech/stryan/freego v0.0.0-20220307180035-64bab97c38d6 h1:0GzkfU8R4Rj7SVxTpkOf9oZN4YpW2xe/IjYbNP5Zxjs=
git.saintnet.tech/stryan/freego v0.0.0-20220307180035-64bab97c38d6/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc=
git.saintnet.tech/stryan/freego v0.0.0-20220307194514-b5fc90339454 h1:KK8YuhJYMjbmyREbCDX+DshFaarrzxhjRTyPDIDhzDs=
git.saintnet.tech/stryan/freego v0.0.0-20220307194514-b5fc90339454/go.mod h1:NXXisQVSPklkvs2Qg6Iv3LqXNaJwwEtho/2WmzhvAZc=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=

View File

@ -34,8 +34,9 @@ func (p *Player) Colour() freego.Colour {
//NewSession creates a new game session
func NewSession() *Session {
sim := freego.NewGame()
return &Session{
simulator: freego.NewGame(),
simulator: sim,
redPlayer: &Player{false, freego.Red},
bluePlayer: &Player{false, freego.Blue},
moveNum: 0,

50
util.go
View File

@ -2,7 +2,11 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"git.saintnet.tech/stryan/freego"
)
func respondWithError(res http.ResponseWriter, code int, message string) {
@ -16,3 +20,49 @@ func respondWithJSON(res http.ResponseWriter, code int, payload interface{}) {
res.WriteHeader(code)
res.Write(response)
}
//TODO remove this when you can actually setup a game
func initDummy(g *freego.Game) {
//Setup terrain
terrain := []struct {
x, y, t int
}{
{1, 1, 1},
{2, 2, 1},
}
for _, tt := range terrain {
res, err := g.Board.AddTerrain(tt.x, tt.y, tt.t)
if err != nil {
panic(err)
}
if !res {
panic(errors.New("Error creating terrain"))
}
}
pieces := []struct {
x, y int
p *freego.Piece
}{
{0, 0, freego.NewPiece(freego.Flag, freego.Blue)},
{3, 0, freego.NewPiece(freego.Spy, freego.Blue)},
{2, 0, freego.NewPiece(freego.Captain, freego.Blue)},
{3, 1, freego.NewPiece(freego.Marshal, freego.Blue)},
{0, 1, freego.NewPiece(freego.Bomb, freego.Blue)},
{1, 6, freego.NewPiece(freego.Flag, freego.Red)},
{3, 6, freego.NewPiece(freego.Spy, freego.Red)},
{2, 7, freego.NewPiece(freego.Captain, freego.Red)},
{0, 6, freego.NewPiece(freego.Marshal, freego.Red)},
{0, 7, freego.NewPiece(freego.Bomb, freego.Red)},
}
for _, tt := range pieces {
res, err := g.SetupPiece(tt.x, tt.y, tt.p)
if err != nil {
panic(fmt.Errorf("Piece %v,%v:%v", tt.x, tt.y, err))
}
if !res {
panic(errors.New("error placing dummy piece"))
}
}
g.Start()
}