cmd: break group
This commit is contained in:
@@ -122,6 +122,51 @@ func (g *Game) JoinEqualGroups(raceName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) BreakGroup(raceName string, groupIndex, quantity uint) error {
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.breakGroupInternal(ri, groupIndex, quantity)
|
||||
}
|
||||
|
||||
func (g *Game) breakGroupInternal(ri int, groupIndex, quantity uint) error {
|
||||
sgi := -1
|
||||
var maxIndex uint
|
||||
for i, sg := range g.listIndexShipGroups(ri) {
|
||||
if sgi < 0 && sg.Index == groupIndex {
|
||||
sgi = i
|
||||
}
|
||||
if sg.Index > maxIndex {
|
||||
maxIndex = sg.Index
|
||||
}
|
||||
}
|
||||
if sgi < 0 {
|
||||
return e.NewEntityNotExistsError("group #%d", groupIndex)
|
||||
}
|
||||
|
||||
if g.ShipGroups[sgi].State != "In_Orbit" || g.ShipGroups[sgi].Origin != nil || g.ShipGroups[sgi].Range != nil {
|
||||
return e.NewShipsBusyError()
|
||||
}
|
||||
|
||||
if g.ShipGroups[sgi].Number < quantity {
|
||||
return e.NewBeakGroupNumberNotEnoughError("%d<%d", g.ShipGroups[sgi].Number, quantity)
|
||||
}
|
||||
|
||||
if quantity == 0 || quantity == g.ShipGroups[sgi].Number {
|
||||
g.ShipGroups[sgi].FleetID = nil
|
||||
} else {
|
||||
newGroup := g.ShipGroups[sgi]
|
||||
newGroup.Number = quantity
|
||||
g.ShipGroups[sgi].Number -= quantity
|
||||
newGroup.Index = maxIndex + 1
|
||||
newGroup.FleetID = nil
|
||||
g.ShipGroups = append(g.ShipGroups, newGroup)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) joinEqualGroupsInternal(ri int) {
|
||||
shipGroups := slices.Collect(maps.Values(maps.Collect(g.listIndexShipGroups(ri))))
|
||||
origin := len(shipGroups)
|
||||
|
||||
Reference in New Issue
Block a user