fix game output

This commit is contained in:
stryan 2022-03-18 15:21:10 -04:00
parent 48ea6e62a1
commit 677a6e9e4d
6 changed files with 44 additions and 18 deletions

16
api.go
View File

@ -49,7 +49,7 @@ func (a *API) NewGame(res http.ResponseWriter, req *http.Request) {
}
}
log.Printf("creating new game %v", a.nextInt)
a.games[a.nextInt] = NewSession()
a.games[a.nextInt] = NewSession(8)
a.games[a.nextInt].redPlayer.Ready = true
respondWithJSON(res, http.StatusOK, newGameResp{a.nextInt, "red"})
a.nextInt = a.nextInt + 1
@ -81,18 +81,16 @@ func (a *API) GetGame(res http.ResponseWriter, req *http.Request) {
}
log.Println("sending game state")
rotatedBoard := s.getBoard(p)
board := s.getBoard(p)
rotate := req.Header.Get("Rotate")
if rotate == "true" {
i := 0
temp := rotatedBoard[0]
for ; i < len(rotatedBoard)-1; i++ {
rotatedBoard[i] = rotatedBoard[i+1]
}
log.Println("rotating output")
//rotateBoard(board)
//rotateBoard(board)
//rotateBoard(board)
rotatedBoard[i] = temp
}
respondWithJSON(res, http.StatusOK, gameResp{rotatedBoard})
respondWithJSON(res, http.StatusOK, gameResp{board})
return
}

View File

@ -23,7 +23,7 @@ func TestNewAPI(t *testing.T) {
func dummyGame(a *API) int {
i := a.nextInt
a.games[i] = NewSession()
a.games[i] = NewSession(8)
a.games[i].redPlayer.Ready = true
a.games[i].bluePlayer.Ready = true
a.games[i].simulator.Setup()
@ -107,7 +107,7 @@ func TestGetGame(t *testing.T) {
}
for j := range respStruct.GameBoard {
for i, vt := range respStruct.GameBoard[j] {
curr, err := a.games[gid].simulator.Board.GetPiece(j, i)
curr, err := a.games[gid].simulator.Board.GetPiece(i, j)
if err != nil {
t.Fatalf("Strange board position: %v", err)
}

View File

@ -10,7 +10,7 @@ type newGameResp struct {
}
type gameResp struct {
GameBoard [8][8]*ViewTile `json:"board"`
GameBoard [][]*ViewTile `json:"board"`
}
type gameStatusResp struct {

View File

@ -15,6 +15,7 @@ type Session struct {
bluePlayer *Player
moveNum int
moveList []freego.ParsedCommand
boardSize int
}
//Player is a player in a match
@ -34,7 +35,7 @@ func (p *Player) Colour() freego.Colour {
}
//NewSession creates a new game session
func NewSession() *Session {
func NewSession(size int) *Session {
sim := freego.NewGame()
return &Session{
simulator: sim,
@ -42,6 +43,7 @@ func NewSession() *Session {
bluePlayer: &Player{false, freego.Blue},
moveNum: 1,
moveList: make([]freego.ParsedCommand, 20),
boardSize: size,
}
}
@ -78,10 +80,13 @@ func (s *Session) getMove(p *Player, num int) (string, error) {
return fmt.Sprintf("%v %v", num, s.moveList[num].String()), nil
}
func (s *Session) getBoard(p *Player) [8][8]*ViewTile {
var res [8][8]*ViewTile
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
func (s *Session) getBoard(p *Player) [][]*ViewTile {
res := make([][]*ViewTile, s.boardSize)
for i := range res {
res[i] = make([]*ViewTile, s.boardSize)
}
for i := 0; i < s.boardSize; i++ {
for j := 0; j < s.boardSize; j++ {
cur := NewViewTile()
terrain, err := s.simulator.Board.IsTerrain(i, j)
if err != nil {
@ -109,7 +114,7 @@ func (s *Session) getBoard(p *Player) [8][8]*ViewTile {
cur.Empty = true
}
}
res[i][j] = cur
res[j][i] = cur
}
}
return res

10
util.go
View File

@ -21,6 +21,16 @@ func respondWithJSON(res http.ResponseWriter, code int, payload interface{}) {
res.Write(response)
}
func rotateBoard(board [][]*ViewTile) {
i := 0
temp := board[0]
for ; i < len(board)-1; i++ {
board[i] = board[i+1]
}
board[i] = temp
}
//TODO remove this when you can actually setup a game
func initDummy(g *freego.Game) {
//Setup terrain

View File

@ -12,3 +12,16 @@ type ViewTile struct {
func NewViewTile() *ViewTile {
return &ViewTile{}
}
func (vt *ViewTile) String() string {
if vt.Piece != "" {
return vt.Piece
}
if vt.Hidden {
return "?"
}
if vt.Terrain {
return "X"
}
return " "
}