fix: remove fleet when deleting last group

This commit is contained in:
IliaDenisov
2026-02-10 21:14:15 +03:00
parent 56998d4c2d
commit 43dc8ab3f9
7 changed files with 29 additions and 18 deletions
@@ -22,7 +22,7 @@ func (c *Cache) CreateShips(ri int, shipTypeName string, planetNumber uint, quan
return e.NewEntityNotOwnedError("planet #%d", planetNumber) return e.NewEntityNotOwnedError("planet #%d", planetNumber)
} }
c.createShipsUnsafe(ri, class.ID, p.Number, uint(quantity)) c.unsafeCreateShips(ri, class.ID, p.Number, uint(quantity))
return nil return nil
} }
@@ -137,7 +137,7 @@ func VotingWinners(calc []*VoteGroup, gameVotes float64) []int {
} }
func (c *Cache) CreateShipsUnsafe_T(ri int, classID uuid.UUID, planet uint, quantity uint) { func (c *Cache) CreateShipsUnsafe_T(ri int, classID uuid.UUID, planet uint, quantity uint) {
c.createShipsUnsafe(ri, classID, planet, quantity) c.unsafeCreateShips(ri, classID, planet, quantity)
} }
func (c *Cache) WipeRace(ri int) { func (c *Cache) WipeRace(ri int) {
+4 -4
View File
@@ -140,7 +140,7 @@ func (c *Cache) ShipGroupJoinFleet(ri int, fleetName string, groupID uuid.UUID,
} }
if quantity > 0 && quantity < c.ShipGroup(sgi).Number { if quantity > 0 && quantity < c.ShipGroup(sgi).Number {
nsgi, err := c.breakGroupSafe(ri, groupID, quantity) nsgi, err := c.breakGroup(ri, groupID, quantity)
if err != nil { if err != nil {
return err return err
} }
@@ -162,7 +162,7 @@ func (c *Cache) ShipGroupJoinFleet(ri int, fleetName string, groupID uuid.UUID,
if !ok { if !ok {
return e.NewGameStateError("old fleet index not found by ID=%v", *oldFleetID) return e.NewGameStateError("old fleet index not found by ID=%v", *oldFleetID)
} }
if err := c.deleteFleetSafe(ri, c.g.Fleets[oldFleetIndex].Name); err != nil { if err := c.deleteFleet(ri, c.g.Fleets[oldFleetIndex].Name); err != nil {
return err return err
} }
} }
@@ -191,7 +191,7 @@ func (c *Cache) fleetMerge(ri int, fleetSourceName, fleetTargetName string) (err
sg.FleetID = &c.g.Fleets[fiTarget].ID sg.FleetID = &c.g.Fleets[fiTarget].ID
} }
} }
return c.deleteFleetSafe(ri, fleetSourceName) return c.deleteFleet(ri, fleetSourceName)
} }
func (c *Cache) createFleet(ri int, name string) (int, error) { func (c *Cache) createFleet(ri int, name string) (int, error) {
@@ -217,7 +217,7 @@ func (c *Cache) createFleet(ri int, name string) (int, error) {
return i, nil return i, nil
} }
func (c *Cache) deleteFleetSafe(ri int, name string) error { func (c *Cache) deleteFleet(ri int, name string) error {
fi, ok := c.fleetIndex(ri, name) fi, ok := c.fleetIndex(ri, name)
if !ok { if !ok {
return e.NewEntityNotExistsError("fleet %s", name) return e.NewEntityNotExistsError("fleet %s", name)
+1 -1
View File
@@ -194,7 +194,7 @@ func (c *Cache) TurnPlanetProductions() {
case game.ProductionShip: case game.ProductionShip:
st := c.MustShipType(ri, *p.Production.SubjectID) st := c.MustShipType(ri, *p.Production.SubjectID)
if ships := ProduceShip(p, productionAvailable, st.EmptyMass()); ships > 0 { if ships := ProduceShip(p, productionAvailable, st.EmptyMass()); ships > 0 {
c.createShipsUnsafe(ri, st.ID, p.Number, ships) c.unsafeCreateShips(ri, st.ID, p.Number, ships)
} }
case game.ResearchScience: case game.ResearchScience:
sc := c.mustScience(ri, *p.Production.SubjectID) sc := c.mustScience(ri, *p.Production.SubjectID)
+1 -1
View File
@@ -130,7 +130,7 @@ func (c *Cache) SendRoutedGroups() {
if toLoad > sgCapacity { if toLoad > sgCapacity {
toLoad = sgCapacity toLoad = sgCapacity
} else if maxShips := uint(math.Ceil(toLoad / (sgCapacity / float64(ships)))); maxShips < ships { } else if maxShips := uint(math.Ceil(toLoad / (sgCapacity / float64(ships)))); maxShips < ships {
newGroupIdx := c.breakGroupUnsafe(c.RaceIndex(sg.OwnerID), sgi, maxShips) newGroupIdx := c.unsafeBreakGroup(c.RaceIndex(sg.OwnerID), sgi, maxShips)
sgi = newGroupIdx sgi = newGroupIdx
sg = c.ShipGroup(newGroupIdx) sg = c.ShipGroup(newGroupIdx)
} }
+19 -8
View File
@@ -174,7 +174,7 @@ func (c *Cache) shipGroupDismantle(ri int, groupIndex uuid.UUID, quantity uint)
if quantity > 0 && quantity < c.ShipGroup(sgi).Number { if quantity > 0 && quantity < c.ShipGroup(sgi).Number {
// make new group for disassembly // make new group for disassembly
nsgi, err := c.breakGroupSafe(ri, groupIndex, quantity) nsgi, err := c.breakGroup(ri, groupIndex, quantity)
if err != nil { if err != nil {
return err return err
} }
@@ -236,7 +236,7 @@ func (c *Cache) shipGroupLoad(ri int, groupID uuid.UUID, ct game.CargoType, ship
return e.NewCargoLoadNotEqualError("cargo: %v", *c.ShipGroup(sgi).CargoType) return e.NewCargoLoadNotEqualError("cargo: %v", *c.ShipGroup(sgi).CargoType)
} }
if ships > 0 && ships < c.ShipGroup(sgi).Number { if ships > 0 && ships < c.ShipGroup(sgi).Number {
nsgi, err := c.breakGroupSafe(ri, groupID, ships) nsgi, err := c.breakGroup(ri, groupID, ships)
if err != nil { if err != nil {
return err return err
} }
@@ -305,7 +305,7 @@ func (c *Cache) shipGroupUnload(ri int, groupID uuid.UUID, ships uint, quantity
return e.NewEntityNotOwnedError("planet #%d unload %v", p.Number, ct) return e.NewEntityNotOwnedError("planet #%d unload %v", p.Number, ct)
} }
if ships > 0 && ships < c.ShipGroup(sgi).Number { if ships > 0 && ships < c.ShipGroup(sgi).Number {
nsgi, err := c.breakGroupSafe(ri, groupID, ships) nsgi, err := c.breakGroup(ri, groupID, ships)
if err != nil { if err != nil {
return err return err
} }
@@ -437,7 +437,7 @@ func (c *Cache) ShipGroupBreak(ri int, groupID uuid.UUID, quantity uint) error {
if quantity == 0 || quantity == c.ShipGroup(sgi).Number { if quantity == 0 || quantity == c.ShipGroup(sgi).Number {
c.internalShipGroupJoinFleet(sgi, nil) c.internalShipGroupJoinFleet(sgi, nil)
} else { } else {
if _, err := c.breakGroupSafe(ri, groupID, quantity); err != nil { if _, err := c.breakGroup(ri, groupID, quantity); err != nil {
return err return err
} }
} }
@@ -445,7 +445,7 @@ func (c *Cache) ShipGroupBreak(ri int, groupID uuid.UUID, quantity uint) error {
return nil return nil
} }
func (c *Cache) breakGroupSafe(ri int, groupID uuid.UUID, newGroupShips uint) (int, error) { func (c *Cache) breakGroup(ri int, groupID uuid.UUID, newGroupShips uint) (int, error) {
c.validateRaceIndex(ri) c.validateRaceIndex(ri)
sgi, ok := c.raceShipGroupIndex(ri, groupID) sgi, ok := c.raceShipGroupIndex(ri, groupID)
if !ok { if !ok {
@@ -454,10 +454,10 @@ func (c *Cache) breakGroupSafe(ri int, groupID uuid.UUID, newGroupShips uint) (i
if c.ShipGroup(sgi).Number < newGroupShips { if c.ShipGroup(sgi).Number < newGroupShips {
return -1, e.NewBreakGroupIllegalNumberError("group=%s ships: %d -> %d", c.ShipGroup(sgi).ID, c.ShipGroup(sgi).Number, newGroupShips) return -1, e.NewBreakGroupIllegalNumberError("group=%s ships: %d -> %d", c.ShipGroup(sgi).ID, c.ShipGroup(sgi).Number, newGroupShips)
} }
return c.breakGroupUnsafe(ri, sgi, newGroupShips), nil return c.unsafeBreakGroup(ri, sgi, newGroupShips), nil
} }
func (c *Cache) breakGroupUnsafe(ri, sgi int, newGroupShips uint) int { func (c *Cache) unsafeBreakGroup(ri, sgi int, newGroupShips uint) int {
newGroup := *c.ShipGroup(sgi) newGroup := *c.ShipGroup(sgi)
if c.ShipGroup(sgi).CargoType != nil { if c.ShipGroup(sgi).CargoType != nil {
newGroup.Load = game.F(float64(c.ShipGroup(sgi).Load) / float64(c.ShipGroup(sgi).Number) * float64(newGroupShips)) newGroup.Load = game.F(float64(c.ShipGroup(sgi).Load) / float64(c.ShipGroup(sgi).Number) * float64(newGroupShips))
@@ -526,6 +526,17 @@ func (c *Cache) shipGroupsInUpgrade(planetNumber uint) iter.Seq[*game.ShipGroup]
func (c *Cache) unsafeDeleteShipGroup(sgi int) { func (c *Cache) unsafeDeleteShipGroup(sgi int) {
c.validateShipGroupIndex(sgi) c.validateShipGroupIndex(sgi)
sg := c.ShipGroup(sgi)
if sg.FleetID != nil {
fi := c.MustFleetIndex(*sg.FleetID)
fleetGroups := slices.Collect(c.fleetGroupIds(c.RaceIndex(sg.OwnerID), fi))
if len(fleetGroups) == 1 {
// remove fleet when deleting last group in the fleet
c.unsafeDeleteFleet(fi)
}
}
c.g.ShipGroups = append(c.g.ShipGroups[:sgi], c.g.ShipGroups[sgi+1:]...) c.g.ShipGroups = append(c.g.ShipGroups[:sgi], c.g.ShipGroups[sgi+1:]...)
c.invalidateShipGroupCache() c.invalidateShipGroupCache()
} }
@@ -536,7 +547,7 @@ func (c *Cache) validateShipGroupIndex(i int) {
} }
} }
func (c *Cache) createShipsUnsafe(ri int, classID uuid.UUID, planet uint, quantity uint) { func (c *Cache) unsafeCreateShips(ri int, classID uuid.UUID, planet uint, quantity uint) {
c.appendShipGroup(ri, &game.ShipGroup{ c.appendShipGroup(ri, &game.ShipGroup{
OwnerID: c.g.Race[ri].ID, OwnerID: c.g.Race[ri].ID,
TypeID: classID, TypeID: classID,
+1 -1
View File
@@ -43,7 +43,7 @@ func (c *Cache) shipGroupSend(ri int, groupID uuid.UUID, planetNumber, quantity
} }
if quantity > 0 && quantity < c.ShipGroup(sgi).Number { if quantity > 0 && quantity < c.ShipGroup(sgi).Number {
nsgi, err := c.breakGroupSafe(ri, groupID, quantity) nsgi, err := c.breakGroup(ri, groupID, quantity)
if err != nil { if err != nil {
return err return err
} }
+1 -1
View File
@@ -118,7 +118,7 @@ func (c *Cache) shipGroupUpgrade(ri int, groupID uuid.UUID, techInput string, li
// break group if needed // break group if needed
if maxUpgradableShips < sg.Number { if maxUpgradableShips < sg.Number {
nsgi, err := c.breakGroupSafe(ri, groupID, maxUpgradableShips) nsgi, err := c.breakGroup(ri, groupID, maxUpgradableShips)
if err != nil { if err != nil {
return err return err
} }