refactor: group uuid instead of index

This commit is contained in:
IliaDenisov
2026-02-10 20:54:43 +03:00
parent 6c8384ce7a
commit 56998d4c2d
26 changed files with 333 additions and 363 deletions
+25 -34
View File
@@ -1,17 +1,18 @@
package controller
import (
"github.com/google/uuid"
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/iliadenisov/galaxy/internal/model/game"
"github.com/iliadenisov/galaxy/internal/util"
)
func (c *Cache) shipGroupSend(ri int, groupIndex, planetNumber, quantity uint) error {
func (c *Cache) shipGroupSend(ri int, groupID uuid.UUID, planetNumber, quantity uint) error {
c.validateRaceIndex(ri)
sgi, ok := c.raceShipGroupIndex(ri, groupIndex)
sgi, ok := c.raceShipGroupIndex(ri, groupID)
if !ok {
return e.NewEntityNotExistsError("group #%d", groupIndex)
return e.NewEntityNotExistsError("group %s", groupID)
}
st := c.ShipGroupShipClass(sgi)
@@ -42,7 +43,7 @@ func (c *Cache) shipGroupSend(ri int, groupIndex, planetNumber, quantity uint) e
}
if quantity > 0 && quantity < c.ShipGroup(sgi).Number {
nsgi, err := c.breakGroupSafe(ri, groupIndex, quantity)
nsgi, err := c.breakGroupSafe(ri, groupID, quantity)
if err != nil {
return err
}
@@ -50,48 +51,38 @@ func (c *Cache) shipGroupSend(ri int, groupIndex, planetNumber, quantity uint) e
}
if p1.Number == p2.Number {
c.UnsendShips(c.ShipGroup(sgi))
c.UnsendShips(sgi)
c.shipGroupMerge(ri)
return nil
}
c.LaunchShips(c.ShipGroup(sgi), planetNumber)
c.LaunchShips(sgi, planetNumber)
return nil
}
func (c *Cache) LaunchShips(sg *game.ShipGroup, destination uint) *game.ShipGroup {
for i := range c.ShipGroupsIndex() {
if c.ShipGroup(i).OwnerID == sg.OwnerID && c.ShipGroup(i).Index == sg.Index {
state := c.ShipGroup(i).State()
var p *game.Planet
switch state {
case game.StateInOrbit:
p = c.MustPlanet(sg.Destination)
case game.StateLaunched:
p = c.MustPlanet(sg.StateInSpace.Origin)
default:
panic("state invalid")
}
c.g.ShipGroups[i] = LaunchShips(*sg, destination, p.X.F(), p.Y.F())
return &c.g.ShipGroups[i]
}
func (c *Cache) LaunchShips(sgi int, destination uint) *game.ShipGroup {
sg := c.ShipGroup(sgi)
var p *game.Planet
switch sg.State() {
case game.StateInOrbit:
p = c.MustPlanet(sg.Destination)
case game.StateLaunched:
p = c.MustPlanet(sg.StateInSpace.Origin)
default:
panic("state invalid")
}
panic("ship group not found")
c.g.ShipGroups[sgi] = LaunchShips(*sg, destination, p.X.F(), p.Y.F())
return &c.g.ShipGroups[sgi]
}
func (c *Cache) UnsendShips(sg *game.ShipGroup) *game.ShipGroup {
for i := range c.ShipGroupsIndex() {
if c.ShipGroup(i).OwnerID == sg.OwnerID && c.ShipGroup(i).Index == sg.Index {
state := c.ShipGroup(i).State()
if state != game.StateLaunched {
panic("state invalid")
}
c.g.ShipGroups[i] = UnsendShips(*sg)
return &c.g.ShipGroups[i]
}
func (c *Cache) UnsendShips(sgi int) *game.ShipGroup {
sg := c.ShipGroup(sgi)
if sg.State() != game.StateLaunched {
panic("state invalid")
}
panic("ship group not found")
c.g.ShipGroups[sgi] = UnsendShips(*sg)
return &c.g.ShipGroups[sgi]
}
func LaunchShips(sg game.ShipGroup, destination uint, originX, originY float64) game.ShipGroup {