refactor: game funcs moved to controller

This commit is contained in:
Ilia Denisov
2026-01-15 14:42:04 +02:00
parent fe8a8d4150
commit 16aba8435d
47 changed files with 1023 additions and 3093 deletions
-44
View File
@@ -3,13 +3,9 @@ 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
@@ -48,46 +44,6 @@ func (g Game) Votes(raceID uuid.UUID) float64 {
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
}
// -----------------------------------------------------------------------------
// 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)
}