wip: refactor controller

This commit is contained in:
Ilia Denisov
2026-01-14 22:17:24 +02:00
parent 1bfc9242af
commit fe8a8d4150
43 changed files with 4710 additions and 2188 deletions
+89
View File
@@ -0,0 +1,89 @@
package controller
import (
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/iliadenisov/galaxy/internal/model/game"
"github.com/iliadenisov/galaxy/internal/util"
)
func (c *Controller) SendFleet(raceName, fleetName string, planetNumber uint) error {
ri, err := c.Cache.raceIndex(raceName)
if err != nil {
return err
}
fi, ok := c.Cache.fleetIndex(ri, fleetName)
if !ok {
return e.NewEntityNotExistsError("fleet %q", fleetName)
}
return c.Cache.SendFleet(ri, fi, planetNumber)
}
func (c *Cache) SendFleet(ri, fi int, planetNumber uint) error {
c.validateRaceIndex(ri)
c.validateFleetIndex(fi)
state, sourcePlanet, _ := c.FleetState(c.g.Fleets[fi].ID)
if game.StateInOrbit != state && game.StateLaunched != state {
return e.NewShipsBusyError()
}
p1, ok := c.Planet(*sourcePlanet)
// p1, ok := PlanetByNum(g, *sourcePlanet)
if !ok {
return e.NewGameStateError("source planet #%d does not exists", sourcePlanet)
}
p2, ok := c.Planet(planetNumber)
// p2, ok := PlanetByNum(g, planetNumber)
if !ok {
return e.NewEntityNotExistsError("destination planet #%d", planetNumber)
}
rangeToDestination := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X, p1.Y, p2.X, p2.Y)
if rangeToDestination > c.g.Race[ri].FlightDistance() {
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
}
for sg := range c.FleetGroups(ri, fi) {
st := c.ShipType(ri, sg.TypeID)
// st, ok := ShipClass(g, ri, sg.TypeID)
// if !ok {
// return e.NewGameStateError("not found: ShipType ID=%v", sg.TypeID)
// }
if st.DriveBlockMass() == 0 {
return e.NewSendShipHasNoDrivesError("Class=%s", st.Name)
}
}
if *sourcePlanet == planetNumber {
c.UnsendFleet(ri, fi)
return nil
}
c.LaunchFleet(ri, fi, planetNumber)
return nil
}
func (c *Cache) LaunchFleet(ri, fi int, destination uint) {
c.validateRaceIndex(ri)
c.validateFleetIndex(fi)
for sg := range c.FleetGroups(ri, fi) {
c.LaunchShips(*sg, destination)
// sgi := slices.IndexFunc(g.ShipGroups, func(s ShipGroup) bool { return sg.Index == s.Index })
// if sgi < 0 {
// panic(fmt.Sprintf("LauncgFleet: cannot find ship group index=%d", sg.Index))
// }
// g.ShipGroups[sgi] = c.LaunchShips(sg, destination)
}
}
func (c *Cache) UnsendFleet(ri, fi int) {
c.validateRaceIndex(ri)
c.validateFleetIndex(fi)
for sg := range c.FleetGroups(ri, fi) {
c.UnsendShips(*sg)
// sgi := slices.IndexFunc(g.ShipGroups, func(s ShipGroup) bool { return sg.Index == s.Index })
// if sgi < 0 {
// panic(fmt.Sprintf("UnsendFleet: cannot find ship group index=%d", sg.Index))
// }
// g.ShipGroups[sgi] = c.UnsendShips(sg)
}
}