package controller import ( "fmt" "slices" "github.com/google/uuid" e "github.com/iliadenisov/galaxy/internal/error" "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) 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 } n, ok := validateTypeName(typeName) if !ok { 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 %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, }, }) c.invalidateShipGroupCache() c.invalidateFleetCache() 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) if !ok { return e.NewEntityNotExistsError("source ship type %q", name) } 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) } 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:]...) c.invalidateShipGroupCache() c.invalidateFleetCache() 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 %q", 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:]...) c.invalidateShipGroupCache() c.invalidateFleetCache() 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) 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], 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)) } func checkShipTypeValues(d float64, a int, w, s, c float64) error { if !checkShipTypeValueDWSC(d) { return e.NewDriveValueError(d) } if !checkShipTypeValueDWSC(w) { return e.NewWeaponsValueError(w) } if !checkShipTypeValueDWSC(s) { return e.NewShieldsValueError(s) } if !checkShipTypeValueDWSC(c) { return e.NewCargoValueError(s) } if a < 0 { return e.NewShipTypeArmamentValueError(a) } if (w == 0 && a > 0) || (a == 0 && w > 0) { return e.NewShipTypeArmamentAndWeaponsValueError("A=%d W=%.0f", a, w) } if d == 0 && w == 0 && s == 0 && c == 0 && a == 0 { return e.NewShipTypeShipTypeZeroValuesError() } return nil } func checkShipTypeValueDWSC(v float64) bool { return v == 0 || v >= 1 }