From 86883cff71274d4cd9d1b5a8f09a9ea274c8be72 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 7 Mar 2022 14:47:35 -0500 Subject: [PATCH] rest of the routes, dummy game creation --- api.go | 21 ++++++++++++++++++++- go.mod | 2 +- go.sum | 2 ++ session.go | 3 ++- util.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 3 deletions(-) diff --git a/api.go b/api.go index 3412246..3783311 100644 --- a/api.go +++ b/api.go @@ -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 diff --git a/go.mod b/go.mod index a8bdfe4..af7a33d 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index b23cdb3..ab08d89 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/session.go b/session.go index 69b2f45..b972e66 100644 --- a/session.go +++ b/session.go @@ -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, diff --git a/util.go b/util.go index 1528156..78a0023 100644 --- a/util.go +++ b/util.go @@ -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() +}