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
+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 {
// make new group for disassembly
nsgi, err := c.breakGroupSafe(ri, groupIndex, quantity)
nsgi, err := c.breakGroup(ri, groupIndex, quantity)
if err != nil {
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)
}
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 {
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)
}
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 {
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 {
c.internalShipGroupJoinFleet(sgi, nil)
} else {
if _, err := c.breakGroupSafe(ri, groupID, quantity); err != nil {
if _, err := c.breakGroup(ri, groupID, quantity); err != nil {
return err
}
}
@@ -445,7 +445,7 @@ func (c *Cache) ShipGroupBreak(ri int, groupID uuid.UUID, quantity uint) error {
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)
sgi, ok := c.raceShipGroupIndex(ri, groupID)
if !ok {
@@ -454,10 +454,10 @@ func (c *Cache) breakGroupSafe(ri int, groupID uuid.UUID, newGroupShips uint) (i
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 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)
if c.ShipGroup(sgi).CargoType != nil {
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) {
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.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{
OwnerID: c.g.Race[ri].ID,
TypeID: classID,