Files
galaxy-game/internal/model/game/game.go
T
2026-01-04 19:22:06 +02:00

151 lines
3.6 KiB
Go

package game
import (
"encoding/json"
"fmt"
"iter"
"maps"
"slices"
"strings"
"github.com/google/uuid"
e "github.com/iliadenisov/galaxy/internal/error"
)
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) PlanetByNumber(number uint) (Planet, error) {
pi := slices.IndexFunc(g.Map.Planet, func(p Planet) bool { return p.Number == number })
if pi < 0 {
return Planet{}, e.NewGameStateError("PlanetByNumber: planet with number=%d not found", number)
}
return g.Map.Planet[pi], nil
}
func (g Game) ShipsInUpgrade(planetNumber uint) iter.Seq[ShipGroup] {
return func(yield func(ShipGroup) bool) {
for sg := range g.ShipGroups {
if g.ShipGroups[sg].Destination == planetNumber && g.ShipGroups[sg].State() == StateUpgrade {
if !yield(g.ShipGroups[sg]) {
break
}
}
}
}
}
func (g Game) raceIndex(name string) (int, error) {
i := slices.IndexFunc(g.Race, func(r Race) bool { return r.Name == name })
if i < 0 {
return i, e.NewRaceUnknownError(name)
}
return i, nil
}
func (g Game) UpdateRelation(race, opponent string, rel Relation) error {
ri, err := g.raceIndex(race)
if err != nil {
return err
}
var other int
if race == opponent {
other = ri
} else if other, err = g.raceIndex(opponent); err != nil {
return err
}
if err != nil {
return err
}
return g.updateRelationInternal(ri, other, rel)
}
func (g Game) updateRelationInternal(ri, other int, rel Relation) error {
for o := range g.Race[ri].Relations {
switch {
case ri == other:
g.Race[ri].Relations[o].Relation = rel
case g.Race[ri].Relations[o].RaceID == g.Race[other].ID:
g.Race[ri].Relations[o].Relation = rel
return nil
}
}
if ri != other {
return e.NewGameStateError("UpdateRelation: opponent not found")
}
return nil
}
func (g Game) Relation(hostRace, opponentRace string) (RaceRelation, error) {
ri, err := g.raceIndex(hostRace)
if err != nil {
return RaceRelation{}, err
}
other, err := g.raceIndex(opponentRace)
if err != nil {
return RaceRelation{}, err
}
return g.relationInternal(ri, other)
}
func (g Game) relationInternal(ri, other int) (RaceRelation, error) {
rel := slices.IndexFunc(g.Race[ri].Relations, func(r RaceRelation) bool { return r.RaceID == g.Race[other].ID })
if rel < 0 {
return RaceRelation{}, e.NewGameStateError("Relation: opponent not found")
}
return g.Race[ri].Relations[rel], nil
}
// -----------------------------------------------------------------------------
// validateTypeName always return v without leading and trailing spaces
func validateTypeName(v string) (string, bool) {
s := strings.TrimSpace(v)
if len(s) > 0 {
return s, true
}
// TODO: special symbols AND include error check in all user-input test
return s, false
}
func (g Game) MarshalBinary() (data []byte, err error) {
return json.Marshal(&g)
}
func (g *Game) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, g)
}