wip: refactor controller
This commit is contained in:
+100
-100
@@ -102,116 +102,116 @@ func (g Game) shipTypesInternal(ri int) []ShipType {
|
||||
return g.Race[ri].ShipTypes
|
||||
}
|
||||
|
||||
func (g Game) DeleteShipType(raceName, typeName string) error {
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.deleteShipTypeInternal(ri, typeName)
|
||||
}
|
||||
// func (g Game) DeleteShipType(raceName, typeName string) error {
|
||||
// ri, err := g.raceIndex(raceName)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return g.deleteShipTypeInternal(ri, typeName)
|
||||
// }
|
||||
|
||||
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)
|
||||
}
|
||||
if pl := slices.IndexFunc(g.Map.Planet, func(p Planet) bool {
|
||||
return p.Production.Type == ProductionShip &&
|
||||
p.Production.SubjectID != nil &&
|
||||
g.Race[ri].ShipTypes[st].ID == *p.Production.SubjectID
|
||||
}); pl >= 0 {
|
||||
return e.NewDeleteShipTypePlanetProductionError(g.Map.Planet[pl].Name)
|
||||
}
|
||||
for sg := range g.listShipGroups(ri) {
|
||||
if sg.TypeID == g.Race[ri].ShipTypes[st].ID {
|
||||
return e.NewDeleteShipTypeExistingGroupError("group: %v", sg.Index)
|
||||
}
|
||||
}
|
||||
g.Race[ri].ShipTypes = append(g.Race[ri].ShipTypes[:st], g.Race[ri].ShipTypes[st+1:]...)
|
||||
return nil
|
||||
}
|
||||
// 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)
|
||||
// }
|
||||
// if pl := slices.IndexFunc(g.Map.Planet, func(p Planet) bool {
|
||||
// return p.Production.Type == ProductionShip &&
|
||||
// p.Production.SubjectID != nil &&
|
||||
// g.Race[ri].ShipTypes[st].ID == *p.Production.SubjectID
|
||||
// }); pl >= 0 {
|
||||
// return e.NewDeleteShipTypePlanetProductionError(g.Map.Planet[pl].Name)
|
||||
// }
|
||||
// for sg := range g.listShipGroups(ri) {
|
||||
// if sg.TypeID == g.Race[ri].ShipTypes[st].ID {
|
||||
// return e.NewDeleteShipTypeExistingGroupError("group: %v", sg.Index)
|
||||
// }
|
||||
// }
|
||||
// g.Race[ri].ShipTypes = append(g.Race[ri].ShipTypes[:st], g.Race[ri].ShipTypes[st+1:]...)
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// TODO: D A W S C
|
||||
func (g Game) CreateShipType(raceName, typeName string, d, w, s, c float64, a int) error {
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = g.createShipTypeInternal(ri, typeName, d, w, s, c, a)
|
||||
return err
|
||||
}
|
||||
// func (g Game) CreateShipType(raceName, typeName string, d, w, s, c float64, a int) error {
|
||||
// ri, err := g.raceIndex(raceName)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// _, err = g.createShipTypeInternal(ri, typeName, d, w, s, c, a)
|
||||
// return err
|
||||
// }
|
||||
|
||||
func (g Game) createShipTypeInternal(ri int, name string, d, w, s, c float64, a int) (int, error) {
|
||||
if err := checkShipTypeValues(d, w, s, c, a); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
n, ok := validateTypeName(name)
|
||||
if !ok {
|
||||
return -1, e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
if st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == name }); st >= 0 {
|
||||
return -1, e.NewEntityTypeNameDuplicateError("ship type %w", g.Race[ri].ShipTypes[st].Name)
|
||||
}
|
||||
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 len(g.Race[ri].ShipTypes) - 1, nil
|
||||
}
|
||||
// func (g Game) createShipTypeInternal(ri int, name string, d, w, s, c float64, a int) (int, error) {
|
||||
// if err := checkShipTypeValues(d, w, s, c, a); err != nil {
|
||||
// return -1, err
|
||||
// }
|
||||
// n, ok := validateTypeName(name)
|
||||
// if !ok {
|
||||
// return -1, e.NewEntityTypeNameValidationError("%q", n)
|
||||
// }
|
||||
// if st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == name }); st >= 0 {
|
||||
// return -1, e.NewEntityTypeNameDuplicateError("ship type %w", g.Race[ri].ShipTypes[st].Name)
|
||||
// }
|
||||
// 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 len(g.Race[ri].ShipTypes) - 1, nil
|
||||
// }
|
||||
|
||||
func (g Game) MergeShipType(race, name, targetName string) error {
|
||||
ri, err := g.raceIndex(race)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.mergeShipTypeInternal(ri, name, targetName)
|
||||
}
|
||||
// func (g Game) MergeShipType(race, name, targetName string) error {
|
||||
// ri, err := g.raceIndex(race)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return g.mergeShipTypeInternal(ri, name, targetName)
|
||||
// }
|
||||
|
||||
func (g Game) mergeShipTypeInternal(ri int, name, targetName string) error {
|
||||
st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == name })
|
||||
if st < 0 {
|
||||
return e.NewEntityNotExistsError("source ship type %w", name)
|
||||
}
|
||||
if name == targetName {
|
||||
return e.NewEntityTypeNameEqualityError("ship type %q", targetName)
|
||||
}
|
||||
tt := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == targetName })
|
||||
if tt < 0 {
|
||||
return e.NewEntityNotExistsError("target ship type %w", name)
|
||||
}
|
||||
if !g.Race[ri].ShipTypes[st].Equal(g.Race[ri].ShipTypes[tt]) {
|
||||
return e.NewMergeShipTypeNotEqualError()
|
||||
}
|
||||
// func (g Game) mergeShipTypeInternal(ri int, name, targetName string) error {
|
||||
// st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == name })
|
||||
// if st < 0 {
|
||||
// return e.NewEntityNotExistsError("source ship type %w", name)
|
||||
// }
|
||||
// if name == targetName {
|
||||
// return e.NewEntityTypeNameEqualityError("ship type %q", targetName)
|
||||
// }
|
||||
// tt := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == targetName })
|
||||
// if tt < 0 {
|
||||
// return e.NewEntityNotExistsError("target ship type %w", name)
|
||||
// }
|
||||
// if !g.Race[ri].ShipTypes[st].Equal(g.Race[ri].ShipTypes[tt]) {
|
||||
// return e.NewMergeShipTypeNotEqualError()
|
||||
// }
|
||||
|
||||
// switch planet productions to the new type
|
||||
for pl := range g.Map.Planet {
|
||||
if g.Map.Planet[pl].Owner == g.Race[ri].ID &&
|
||||
g.Map.Planet[pl].Production.Type == ProductionShip &&
|
||||
g.Map.Planet[pl].Production.SubjectID != nil &&
|
||||
*g.Map.Planet[pl].Production.SubjectID == g.Race[ri].ShipTypes[st].ID {
|
||||
g.Map.Planet[pl].Production.SubjectID = &g.Race[ri].ShipTypes[tt].ID
|
||||
}
|
||||
}
|
||||
// // switch planet productions to the new type
|
||||
// for pl := range g.Map.Planet {
|
||||
// if g.Map.Planet[pl].Owner == g.Race[ri].ID &&
|
||||
// g.Map.Planet[pl].Production.Type == ProductionShip &&
|
||||
// g.Map.Planet[pl].Production.SubjectID != nil &&
|
||||
// *g.Map.Planet[pl].Production.SubjectID == g.Race[ri].ShipTypes[st].ID {
|
||||
// g.Map.Planet[pl].Production.SubjectID = &g.Race[ri].ShipTypes[tt].ID
|
||||
// }
|
||||
// }
|
||||
|
||||
// switch ship groups to the new type
|
||||
for sg := range g.ShipGroups {
|
||||
if g.ShipGroups[sg].OwnerID == g.Race[ri].ID && g.ShipGroups[sg].TypeID == g.Race[ri].ShipTypes[st].ID {
|
||||
g.ShipGroups[sg].TypeID = g.Race[ri].ShipTypes[tt].ID
|
||||
}
|
||||
}
|
||||
// // switch ship groups to the new type
|
||||
// for sg := range g.ShipGroups {
|
||||
// if g.ShipGroups[sg].OwnerID == g.Race[ri].ID && g.ShipGroups[sg].TypeID == g.Race[ri].ShipTypes[st].ID {
|
||||
// g.ShipGroups[sg].TypeID = g.Race[ri].ShipTypes[tt].ID
|
||||
// }
|
||||
// }
|
||||
|
||||
// remove the source type
|
||||
g.Race[ri].ShipTypes = append(g.Race[ri].ShipTypes[:st], g.Race[ri].ShipTypes[st+1:]...)
|
||||
// // remove the source type
|
||||
// g.Race[ri].ShipTypes = append(g.Race[ri].ShipTypes[:st], g.Race[ri].ShipTypes[st+1:]...)
|
||||
|
||||
return nil
|
||||
}
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func checkShipTypeValues(d, w, s, c float64, a int) error {
|
||||
if !checkShipTypeValueDWSC(d) {
|
||||
|
||||
Reference in New Issue
Block a user