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
+1 -90
View File
@@ -1,13 +1,5 @@
package game
import (
"fmt"
"slices"
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/iliadenisov/galaxy/internal/util"
)
type RouteType string
const (
@@ -18,7 +10,7 @@ const (
)
var (
routeTypeSet map[string]RouteType = map[string]RouteType{
RouteTypeSet map[string]RouteType = map[string]RouteType{
RouteMaterial.String(): RouteMaterial,
RouteCapital.String(): RouteCapital,
RouteColonist.String(): RouteColonist,
@@ -33,84 +25,3 @@ func (rt RouteType) Ref() *RouteType {
func (rt RouteType) String() string {
return string(rt)
}
func (g *Game) SetRoute(raceName, loadType string, origin, destination uint) error {
ri, err := g.raceIndex(raceName)
if err != nil {
return err
}
rt, ok := routeTypeSet[loadType]
if !ok {
return e.NewCargoTypeInvalidError(loadType)
}
return g.setRouteInternal(ri, rt, origin, destination)
}
func (g *Game) RemoveRoute(raceName, loadType string, origin uint) error {
ri, err := g.raceIndex(raceName)
if err != nil {
return err
}
rt, ok := routeTypeSet[loadType]
if !ok {
return e.NewCargoTypeInvalidError(loadType)
}
return g.removeRouteInternal(ri, rt, origin)
}
func (g *Game) setRouteInternal(ri int, rt RouteType, origin, destination uint) error {
p1, ok := PlanetByNum(g, origin)
if !ok {
return e.NewEntityNotExistsError("origin planet #%d", origin)
}
if p1.Owner != g.Race[ri].ID {
return e.NewEntityNotOwnedError("planet #%d", origin)
}
p2, ok := PlanetByNum(g, destination)
if !ok {
return e.NewEntityNotExistsError("destination planet #%d", destination)
}
rangeToDestination := util.ShortDistance(g.Map.Width, g.Map.Height, p1.X, p1.Y, p2.X, p2.Y)
if rangeToDestination > g.Race[ri].FlightDistance() {
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
}
SetPlanetRoute(g, rt, origin, destination)
return nil
}
func (g *Game) removeRouteInternal(ri int, rt RouteType, origin uint) error {
p1, ok := PlanetByNum(g, origin)
if !ok {
return e.NewEntityNotExistsError("origin planet #%d", origin)
}
if p1.Owner != g.Race[ri].ID {
return e.NewEntityNotOwnedError("planet #%d", origin)
}
RemovePlanetRoute(g, rt, origin)
return nil
}
func SetPlanetRoute(g *Game, rt RouteType, origin, destination uint) {
pi := slices.IndexFunc(g.Map.Planet, func(p Planet) bool { return p.Number == origin })
if pi < 0 {
panic(fmt.Sprintf("SetPlanetRoute: origin planet #%d not found", origin))
}
if g.Map.Planet[pi].Route == nil {
g.Map.Planet[pi].Route = make(map[RouteType]uint)
}
g.Map.Planet[pi].Route[rt] = destination
}
func RemovePlanetRoute(g *Game, rt RouteType, origin uint) {
pi := slices.IndexFunc(g.Map.Planet, func(p Planet) bool { return p.Number == origin })
if pi < 0 {
panic(fmt.Sprintf("RemovePlanetRoute: origin planet #%d not found", origin))
}
if g.Map.Planet[pi].Route != nil {
delete(g.Map.Planet[pi].Route, rt)
}
}