cmd: delete ship type

This commit is contained in:
Ilia Denisov
2025-10-01 00:29:25 +03:00
parent 6f29288096
commit bcab29a47f
8 changed files with 100 additions and 15 deletions
+7 -5
View File
@@ -1,5 +1,7 @@
package game
import "github.com/google/uuid"
type PlanetProduction string
const (
@@ -17,15 +19,15 @@ const (
)
type ProductionType struct {
Production PlanetProduction `json:"type"`
SubjectName string `json:"subject"` // TODO: change to UUID
Production PlanetProduction `json:"type"`
SubjectID *uuid.UUID `json:"subjectId"`
}
func (p PlanetProduction) AsType(subject string) ProductionType {
func (p PlanetProduction) AsType(subject uuid.UUID) ProductionType {
switch p {
case ResearchScience, ProductionShip:
return ProductionType{Production: p, SubjectName: subject}
return ProductionType{Production: p, SubjectID: &subject}
default:
return ProductionType{Production: p}
return ProductionType{Production: p, SubjectID: nil}
}
}
+37 -1
View File
@@ -22,6 +22,42 @@ func (g Game) shipTypesInternal(race uuid.UUID) ([]ShipType, error) {
return nil, e.NewGameStateError("ShipTypes: race %v not found", race)
}
func (g Game) DeleteShipType(raceName, typeName string) error {
raceID, err := g.hostRaceID(raceName)
if err != nil {
return err
}
return g.deleteShipTypeInternal(raceID, typeName)
}
func (g Game) deleteShipTypeInternal(race uuid.UUID, name string) error {
for r := range g.Race {
if g.Race[r].ID == race {
for st := range g.Race[r].ShipTypes {
if g.Race[r].ShipTypes[st].Name == name {
for sg := range g.Race[r].ShipGroups {
if g.Race[r].ShipGroups[sg].TypeID == g.Race[r].ShipTypes[st].ID {
return e.NewDeleteShipTypeExistingGroupError(g.Race[r].ShipGroups[sg].Number)
}
}
for pl := range g.Map.Planet {
if g.Map.Planet[pl].Owner == race &&
g.Map.Planet[pl].Production.Production == ProductionShip &&
g.Map.Planet[pl].Production.SubjectID != nil &&
g.Race[r].ShipTypes[st].ID == *g.Map.Planet[pl].Production.SubjectID {
return e.NewDeleteShipTypePlanetProductionError(g.Map.Planet[pl].Name)
}
}
g.Race[r].ShipTypes = append(g.Race[r].ShipTypes[:st], g.Race[r].ShipTypes[st+1:]...)
return nil
}
}
return e.NewEntityTypeNameNotExistsError("ship type %w", name)
}
}
return e.NewGameStateError("DeleteShipType: race %v not found", race)
}
func (g Game) CreateShipType(raceName, typeName string, d, w, s, c float64, a int) error {
if err := checkShipTypeValues(d, w, s, c, a); err != nil {
return err
@@ -42,7 +78,7 @@ func (g Game) createShipTypeInternal(race uuid.UUID, name string, d, w, s, c flo
if g.Race[r].ID == race {
for st := range g.Race[r].ShipTypes {
if g.Race[r].ShipTypes[st].Name == name {
return e.NewEntityTypeNameExistsError("ship type %w", g.Race[r].ShipTypes[st].Name)
return e.NewEntityTypeNameDuplicateError("ship type %w", g.Race[r].ShipTypes[st].Name)
}
}
id := uuid.New()