package game import ( "encoding/json" "fmt" "maps" "github.com/google/uuid" ) type TechSet map[Tech]float64 func (ts TechSet) Value(t Tech) float64 { if v, ok := ts[t]; ok { return v } else { panic(fmt.Sprintf("TechSet: Value: %s's value not set", t.String())) } } func (ts TechSet) Set(t Tech, v float64) TechSet { m := maps.Clone(ts) m[t] = v return m } type Game struct { ID uuid.UUID `json:"id"` Age uint `json:"turn"` // Game's turn number Map Map `json:"map"` Race []Race `json:"races"` ShipGroups []ShipGroup `json:"shipGroup,omitempty"` Fleets []Fleet `json:"fleet,omitempty"` } func (g Game) Votes(raceID uuid.UUID) float64 { // XXX: calculate [Race]Population once when loading Game from Storage? var pop float64 for i := range g.Map.Planet { if g.Map.Planet[i].Owner == raceID { pop += g.Map.Planet[i].Population } } return pop / 1000. } func (g Game) MarshalBinary() (data []byte, err error) { return json.Marshal(&g) } func (g *Game) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, g) }