cmd: giveaway group

This commit is contained in:
Ilia Denisov
2025-12-09 22:25:37 +03:00
parent 7002f3d299
commit 52bd71d1be
6 changed files with 187 additions and 7 deletions
+94 -1
View File
@@ -20,8 +20,12 @@ const (
CargoCapital CargoType = "CAP" // Промышленность
)
func (ct CargoType) Ref() *CargoType {
return &ct
}
type ShipGroup struct {
Index uint `json:"index"` // Group index (ordered)
Index uint `json:"index"` // FIXME: use UUID for Group Index (ordered)
OwnerID uuid.UUID `json:"ownerId"` // Race link
TypeID uuid.UUID `json:"typeId"` // ShipType link
FleetID *uuid.UUID `json:"fleetId,omitempty"` // Fleet link
@@ -130,6 +134,95 @@ func (g *Game) BreakGroup(raceName string, groupIndex, quantity uint) error {
return g.breakGroupInternal(ri, groupIndex, quantity)
}
func (g *Game) GiveawayGroup(raceName, raceAcceptor string, groupIndex, quantity uint) error {
ri, err := g.raceIndex(raceName)
if err != nil {
return err
}
riAccept, err := g.raceIndex(raceAcceptor)
if err != nil {
return err
}
return g.giveawayGroupInternal(ri, riAccept, groupIndex, quantity)
}
func (g *Game) giveawayGroupInternal(ri, riAccept int, groupIndex, quantity uint) (err error) {
if ri == riAccept {
return e.NewInputSameRaceError(g.Race[riAccept].Name)
}
sgi := -1
for i, sg := range g.listIndexShipGroups(ri) {
if sgi < 0 && sg.Index == groupIndex {
sgi = i
}
}
if sgi < 0 {
return e.NewEntityNotExistsError("group #%d", groupIndex)
}
if g.ShipGroups[sgi].Number < quantity {
return e.NewBeakGroupNumberNotEnoughError("%d<%d", g.ShipGroups[sgi].Number, quantity)
}
var sti int
if sti = slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.ID == g.ShipGroups[sgi].TypeID }); sti < 0 {
// hard to test, need manual game data invalidation
return e.NewGameStateError("not found: ShipType ID=%v", g.ShipGroups[sgi].TypeID)
}
var stAcc int
if stAcc = slices.IndexFunc(g.Race[riAccept].ShipTypes, func(st ShipType) bool { return st.Name == g.Race[ri].ShipTypes[sti].Name }); stAcc >= 0 &&
!g.Race[ri].ShipTypes[sti].Equal(g.Race[riAccept].ShipTypes[stAcc]) {
return e.NewGiveawayGroupShipsTypeNotEqualError("race %w, ship type %w", g.Race[riAccept].Name, g.Race[riAccept].ShipTypes[stAcc].Name)
}
if stAcc < 0 {
stAcc, err = g.createShipTypeInternal(riAccept,
g.Race[ri].ShipTypes[sti].Name,
g.Race[ri].ShipTypes[sti].Drive,
g.Race[ri].ShipTypes[sti].Weapons,
g.Race[ri].ShipTypes[sti].Shields,
g.Race[ri].ShipTypes[sti].Cargo,
int(g.Race[ri].ShipTypes[sti].Armament))
if err != nil {
return err
}
}
var maxIndex uint
for sg := range g.listShipGroups(riAccept) {
if sg.Index > maxIndex {
maxIndex = sg.Index
}
}
g.ShipGroups = append(g.ShipGroups, ShipGroup{
Index: maxIndex + 1,
OwnerID: g.Race[riAccept].ID,
TypeID: g.Race[riAccept].ShipTypes[stAcc].ID,
Number: uint(quantity),
State: g.ShipGroups[sgi].State,
CargoType: g.ShipGroups[sgi].CargoType,
Load: g.ShipGroups[sgi].Load,
Drive: g.ShipGroups[sgi].Drive,
Weapons: g.ShipGroups[sgi].Weapons,
Shields: g.ShipGroups[sgi].Shields,
Cargo: g.ShipGroups[sgi].Cargo,
Destination: g.ShipGroups[sgi].Destination,
Origin: g.ShipGroups[sgi].Origin,
Range: g.ShipGroups[sgi].Range,
})
if quantity == 0 || quantity == g.ShipGroups[sgi].Number {
g.ShipGroups = append(g.ShipGroups[:sgi], g.ShipGroups[sgi+1:]...)
} else {
g.ShipGroups[sgi].Number -= quantity
}
return nil
}
func (g *Game) breakGroupInternal(ri int, groupIndex, quantity uint) error {
sgi := -1
var maxIndex uint