wip: refactor controller
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -8,6 +9,14 @@ import (
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
)
|
||||
|
||||
func (c *Controller) CreateShipType(raceName, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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 {
|
||||
@@ -16,25 +25,17 @@ func (c *Cache) ShipClass(ri int, name string) (*game.ShipType, int, bool) {
|
||||
return &c.g.Race[ri].ShipTypes[i], i, true
|
||||
}
|
||||
|
||||
func (c *Cache) CreateShipType(raceName, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error {
|
||||
ri, err := c.raceIndex(raceName)
|
||||
if err != nil {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
_, err = c.createShipTypeInternal(ri, typeName, drive, ammo, weapons, shileds, cargo)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Cache) createShipTypeInternal(ri int, name string, drive float64, ammo int, weapons, shileds, cargo float64) (int, error) {
|
||||
if err := checkShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
n, ok := validateTypeName(name)
|
||||
n, ok := validateTypeName(typeName)
|
||||
if !ok {
|
||||
return -1, e.NewEntityTypeNameValidationError("%q", n)
|
||||
return e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
if st := slices.IndexFunc(c.g.Race[ri].ShipTypes, func(st game.ShipType) bool { return st.Name == name }); st >= 0 {
|
||||
return -1, e.NewEntityTypeNameDuplicateError("ship type %w", c.g.Race[ri].ShipTypes[st].Name)
|
||||
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)
|
||||
}
|
||||
c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes, game.ShipType{
|
||||
ID: uuid.New(),
|
||||
@@ -47,7 +48,124 @@ func (c *Cache) createShipTypeInternal(ri int, name string, drive float64, ammo
|
||||
Armament: uint(ammo),
|
||||
},
|
||||
})
|
||||
return len(c.g.Race[ri].ShipTypes) - 1, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) MergeShipType(race, name, targetName string) error {
|
||||
ri, err := c.Cache.raceIndex(race)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Cache.MergeShipType(ri, name, targetName)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
if name == targetName {
|
||||
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()
|
||||
}
|
||||
|
||||
// switch planet productions to the new type
|
||||
for pl := range c.g.Map.Planet {
|
||||
if c.g.Map.Planet[pl].Owner == c.g.Race[ri].ID &&
|
||||
c.g.Map.Planet[pl].Production.Type == game.ProductionShip &&
|
||||
c.g.Map.Planet[pl].Production.SubjectID != nil &&
|
||||
*c.g.Map.Planet[pl].Production.SubjectID == st.ID {
|
||||
|
||||
c.g.Map.Planet[pl].Production.SubjectID = &tt.ID
|
||||
}
|
||||
}
|
||||
|
||||
// switch ship groups to the new type
|
||||
for sg := range c.g.ShipGroups {
|
||||
if c.g.ShipGroups[sg].OwnerID == c.g.Race[ri].ID && c.g.ShipGroups[sg].TypeID == st.ID {
|
||||
c.g.ShipGroups[sg].TypeID = tt.ID
|
||||
}
|
||||
}
|
||||
|
||||
// remove the source type
|
||||
c.g.Race[ri].ShipTypes = append(c.g.Race[ri].ShipTypes[:sti], c.g.Race[ri].ShipTypes[sti+1:]...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) DeleteShipType(raceName, typeName string) error {
|
||||
ri, err := c.Cache.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Cache.DeleteShipType(ri, typeName)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
// 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 &&
|
||||
st.ID == *p.Production.SubjectID
|
||||
}); pl >= 0 {
|
||||
return e.NewDeleteShipTypePlanetProductionError(c.g.Map.Planet[pl].Name)
|
||||
}
|
||||
|
||||
for sg := range c.listShipGroups(ri) {
|
||||
if sg.TypeID == st.ID {
|
||||
return e.NewDeleteShipTypeExistingGroupError("group: %v", sg.Index)
|
||||
}
|
||||
}
|
||||
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
|
||||
return nil
|
||||
}
|
||||
|
||||
// ShipTypes used for tests only
|
||||
func (c *Controller) ShipTypes(race string) ([]*game.ShipType, error) {
|
||||
ri, err := c.Cache.raceIndex(race)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.Cache.ShipTypes(ri), nil
|
||||
}
|
||||
|
||||
// ShipTypes used for tests only
|
||||
func (c *Cache) ShipTypes(ri int) []*game.ShipType {
|
||||
c.validateRaceIndex(ri)
|
||||
result := make([]*game.ShipType, len(c.g.Race[ri].ShipTypes))
|
||||
for i := range c.g.Race[ri].ShipTypes {
|
||||
result[i] = &c.g.Race[ri].ShipTypes[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *Cache) ShipType(ri int, ID uuid.UUID) *game.ShipType {
|
||||
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]
|
||||
}
|
||||
}
|
||||
panic(fmt.Sprintf("ship_class not found: race_id=%d id=%v", ri, ID))
|
||||
}
|
||||
|
||||
func checkShipTypeValues(d float64, a int, w, s, c float64) error {
|
||||
|
||||
Reference in New Issue
Block a user