add JSON DTOs for game state

This commit is contained in:
2024-01-21 00:56:45 -06:00
parent 5466785bbc
commit 5fa8de142b
3 changed files with 67 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"git.sunturtle.xyz/studio/shotgun/player"
"git.sunturtle.xyz/studio/shotgun/serve"
)
type Game struct {
@@ -105,7 +106,7 @@ func (g *Game) PopShell() bool {
// Peek returns the current turn's shell if it is revealed for the player with
// the given ID, or nil otherwise.
func (g *Game) Peek(id player.ID) *bool {
if id != g.CurrentPlayer().ID || !g.Reveal {
if len(g.Shells) == 0 || id != g.CurrentPlayer().ID || !g.Reveal {
return nil
}
return &g.Shells[0]
@@ -150,4 +151,20 @@ func (g *Game) Shoot(id player.ID, self bool) error {
return nil
}
// DTO returns the current game state as viewed by the given player.
func (g *Game) DTO(id player.ID) serve.Game {
return serve.Game{
Players: [2]serve.Player{
g.PP[0].DTO(),
g.PP[1].DTO(),
},
Round: g.Round,
Group: g.Group,
Turn: g.Turn,
Damage: g.Damage,
Shell: g.Peek(id),
Previous: g.Prev,
}
}
var ErrWrongTurn = errors.New("not your turn")

View File

@@ -4,6 +4,7 @@ import (
"slices"
"git.sunturtle.xyz/studio/shotgun/player"
"git.sunturtle.xyz/studio/shotgun/serve"
)
type Player struct {
@@ -29,6 +30,22 @@ func (p *Player) StartGroup(rng *RNG, items int) {
p.Cuffs = Uncuffed // TODO(zeph): or is this startround?
}
func (p *Player) DTO() serve.Player {
r := serve.Player{
HP: p.HP,
Items: make([]string, 0, 8),
Cuffs: p.Cuffs != Uncuffed,
}
for _, i := range p.Items {
s := i.String()
if s == "" {
continue
}
r.Items = append(r.Items, s)
}
return r
}
type CuffState uint8
const (