refactor: game funcs moved to controller
This commit is contained in:
@@ -17,14 +17,6 @@ func (c *Controller) CreateShipType(raceName, typeName string, drive float64, am
|
||||
return c.Cache.CreateShipType(ri, typeName, drive, ammo, weapons, shileds, cargo)
|
||||
}
|
||||
|
||||
func (c *Cache) ShipClass(ri int, name string) (*game.ShipType, int, bool) {
|
||||
i := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == name })
|
||||
if i < 0 {
|
||||
return nil, -1, false
|
||||
}
|
||||
return &c.g.Race[ri].ShipTypes[i], i, true
|
||||
}
|
||||
|
||||
func (c *Cache) CreateShipType(ri int, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error {
|
||||
c.validateRaceIndex(ri)
|
||||
if err := checkShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
|
||||
@@ -35,19 +27,21 @@ func (c *Cache) CreateShipType(ri int, typeName string, drive float64, ammo int,
|
||||
return e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
if st := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == typeName }); st >= 0 {
|
||||
return e.NewEntityTypeNameDuplicateError("ship type %w", c.g.Race[ri].ShipTypes[st].Name)
|
||||
return e.NewEntityTypeNameDuplicateError("ship type %q", c.g.Race[ri].ShipTypes[st].Name)
|
||||
}
|
||||
c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes, game.ShipType{
|
||||
ID: uuid.New(),
|
||||
ShipTypeReport: game.ShipTypeReport{
|
||||
Name: n,
|
||||
Drive: drive,
|
||||
Armament: uint(ammo),
|
||||
Weapons: weapons,
|
||||
Shields: shileds,
|
||||
Cargo: cargo,
|
||||
Armament: uint(ammo),
|
||||
},
|
||||
})
|
||||
c.invalidateShipGroupCache()
|
||||
c.invalidateFleetCache()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -63,18 +57,19 @@ func (c *Cache) MergeShipType(ri int, name, targetName string) error {
|
||||
c.validateRaceIndex(ri)
|
||||
st, sti, ok := c.ShipClass(ri, name)
|
||||
|
||||
// st := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == name })
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("source ship type %w", name)
|
||||
return e.NewEntityNotExistsError("source ship type %q", name)
|
||||
}
|
||||
if name == targetName {
|
||||
tt, _, ok := c.ShipClass(ri, targetName)
|
||||
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("target ship type %q", name)
|
||||
}
|
||||
|
||||
if st.Name == tt.Name {
|
||||
return e.NewEntityTypeNameEqualityError("ship type %q", targetName)
|
||||
}
|
||||
tt, _, ok := c.ShipClass(ri, name)
|
||||
// tt := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == targetName })
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("target ship type %w", name)
|
||||
}
|
||||
|
||||
if !st.Equal(*tt) {
|
||||
return e.NewMergeShipTypeNotEqualError()
|
||||
}
|
||||
@@ -100,6 +95,9 @@ func (c *Cache) MergeShipType(ri int, name, targetName string) error {
|
||||
// remove the source type
|
||||
c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes[:sti], c.g.Race[ri].ShipTypes[sti+1:]...)
|
||||
|
||||
c.invalidateShipGroupCache()
|
||||
c.invalidateFleetCache()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -115,12 +113,8 @@ func (c *Cache) DeleteShipType(ri int, name string) error {
|
||||
c.validateRaceIndex(ri)
|
||||
st, i, ok := c.ShipClass(ri, name)
|
||||
if !ok {
|
||||
return e.NewEntityNotExistsError("ship type %w", name)
|
||||
return e.NewEntityNotExistsError("ship type %q", name)
|
||||
}
|
||||
// st := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == name })
|
||||
// if st < 0 {
|
||||
// return e.NewEntityNotExistsError("ship type %w", name)
|
||||
// }
|
||||
if pl := slices.IndexFunc(c.g.Map.Planet, func(p game.Planet) bool {
|
||||
return p.Production.Type == game.ProductionShip &&
|
||||
p.Production.SubjectID != nil &&
|
||||
@@ -135,7 +129,10 @@ func (c *Cache) DeleteShipType(ri int, name string) error {
|
||||
}
|
||||
}
|
||||
c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes[:i], c.g.Race[ri].ShipTypes[i+1:]...)
|
||||
// FIXME: update cache ON ALL FUNCs IN THIS FILE
|
||||
|
||||
c.invalidateShipGroupCache()
|
||||
c.invalidateFleetCache()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -158,13 +155,28 @@ func (c *Cache) ShipTypes(ri int) []*game.ShipType {
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *Cache) ShipType(ri int, ID uuid.UUID) *game.ShipType {
|
||||
func (c *Cache) ShipClass(ri int, name string) (*game.ShipType, int, bool) {
|
||||
i := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == name })
|
||||
if i < 0 {
|
||||
return nil, -1, false
|
||||
}
|
||||
return &c.g.Race[ri].ShipTypes[i], i, true
|
||||
}
|
||||
|
||||
func (c *Cache) ShipType(ri int, ID uuid.UUID) (*game.ShipType, bool) {
|
||||
c.validateRaceIndex(ri)
|
||||
for i := range c.g.Race[ri].ShipTypes {
|
||||
if c.g.Race[ri].ShipTypes[i].ID == ID {
|
||||
return &c.g.Race[ri].ShipTypes[i]
|
||||
return &c.g.Race[ri].ShipTypes[i], true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (c *Cache) MustShipType(ri int, ID uuid.UUID) *game.ShipType {
|
||||
if v, ok := c.ShipType(ri, ID); ok {
|
||||
return v
|
||||
}
|
||||
panic(fmt.Sprintf("ship_class not found: race_id=%d id=%v", ri, ID))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user