freego_api/util.go

69 lines
1.6 KiB
Go
Raw Normal View History

2022-03-03 13:28:00 -05:00
package main
import (
"encoding/json"
"errors"
"fmt"
2022-03-03 13:28:00 -05:00
"net/http"
"git.saintnet.tech/stryan/freego"
2022-03-03 13:28:00 -05:00
)
func respondWithError(res http.ResponseWriter, code int, message string) {
respondWithJSON(res, code, map[string]string{"error": message})
}
func respondWithJSON(res http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
res.Header().Set("Content-Type", "application/json")
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()
}