refactor: race index by name
This commit is contained in:
+37
-61
@@ -2,6 +2,7 @@ package game
|
||||
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/pkg/error"
|
||||
@@ -128,67 +129,50 @@ func (fl Fleet) Speed() float64 {
|
||||
}
|
||||
|
||||
func (g Game) ShipTypes(raceName string) ([]ShipType, error) {
|
||||
raceID, err := g.hostRaceID(raceName)
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return g.shipTypesInternal(raceID)
|
||||
return g.shipTypesInternal(ri), nil
|
||||
}
|
||||
|
||||
func (g Game) shipTypesInternal(race uuid.UUID) ([]ShipType, error) {
|
||||
for r := range g.Race {
|
||||
if g.Race[r].ID == race {
|
||||
return g.Race[r].ShipTypes, nil
|
||||
}
|
||||
}
|
||||
return nil, e.NewGameStateError("ShipTypes: race %v not found", race)
|
||||
func (g Game) shipTypesInternal(ri int) []ShipType {
|
||||
return g.Race[ri].ShipTypes
|
||||
}
|
||||
|
||||
func (g Game) DeleteShipType(raceName, typeName string) error {
|
||||
raceID, err := g.hostRaceID(raceName)
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.deleteShipTypeInternal(raceID, typeName)
|
||||
return g.deleteShipTypeInternal(ri, 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.NewEntityNotExistsError("ship type %w", name)
|
||||
}
|
||||
func (g Game) deleteShipTypeInternal(ri int, name string) error {
|
||||
st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == name })
|
||||
if st < 0 {
|
||||
return e.NewEntityNotExistsError("ship type %w", name)
|
||||
}
|
||||
return e.NewGameStateError("DeleteShipType: race %v not found", race)
|
||||
if pl := slices.IndexFunc(g.Map.Planet, func(p Planet) bool {
|
||||
return p.Production.Production == ProductionShip &&
|
||||
p.Production.SubjectID != nil &&
|
||||
g.Race[ri].ShipTypes[st].ID == *p.Production.SubjectID
|
||||
}); pl >= 0 {
|
||||
return e.NewDeleteShipTypePlanetProductionError(g.Map.Planet[pl].Name)
|
||||
}
|
||||
g.Race[ri].ShipTypes = append(g.Race[ri].ShipTypes[:st], g.Race[ri].ShipTypes[st+1:]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g Game) CreateShipType(raceName, typeName string, d, w, s, c float64, a int) error {
|
||||
raceID, err := g.hostRaceID(raceName)
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.createShipTypeInternal(raceID, typeName, d, w, s, c, a)
|
||||
return g.createShipTypeInternal(ri, typeName, d, w, s, c, a)
|
||||
}
|
||||
|
||||
func (g Game) createShipTypeInternal(race uuid.UUID, name string, d, w, s, c float64, a int) error {
|
||||
func (g Game) createShipTypeInternal(ri int, name string, d, w, s, c float64, a int) error {
|
||||
if err := checkShipTypeValues(d, w, s, c, a); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -196,29 +180,21 @@ func (g Game) createShipTypeInternal(race uuid.UUID, name string, d, w, s, c flo
|
||||
if !ok {
|
||||
return e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
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 == n {
|
||||
return e.NewEntityTypeNameDuplicateError("ship type %w", g.Race[r].ShipTypes[st].Name)
|
||||
}
|
||||
}
|
||||
id := uuid.New()
|
||||
g.Race[r].ShipTypes = append(g.Race[r].ShipTypes, ShipType{
|
||||
ID: id,
|
||||
ShipTypeReport: ShipTypeReport{
|
||||
Name: n,
|
||||
Drive: d,
|
||||
Weapons: w,
|
||||
Shields: s,
|
||||
Cargo: c,
|
||||
Armament: uint(a),
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
if st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == name }); st >= 0 {
|
||||
return e.NewEntityTypeNameDuplicateError("ship type %w", g.Race[ri].ShipTypes[st].Name)
|
||||
}
|
||||
return e.NewGameStateError("CreateShipType: race %v not found", race)
|
||||
g.Race[ri].ShipTypes = append(g.Race[ri].ShipTypes, ShipType{
|
||||
ID: uuid.New(),
|
||||
ShipTypeReport: ShipTypeReport{
|
||||
Name: n,
|
||||
Drive: d,
|
||||
Weapons: w,
|
||||
Shields: s,
|
||||
Cargo: c,
|
||||
Armament: uint(a),
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkShipTypeValues(d, w, s, c float64, a int) error {
|
||||
|
||||
Reference in New Issue
Block a user