cmd: delete ship type
This commit is contained in:
+12
-2
@@ -9,13 +9,17 @@ const (
|
|||||||
|
|
||||||
ErrStorageFailure int = 1000
|
ErrStorageFailure int = 1000
|
||||||
ErrGameStateInvalid int = 2000
|
ErrGameStateInvalid int = 2000
|
||||||
|
|
||||||
|
ErrDeleteShipTypeExistingGroup = 5000
|
||||||
|
ErrDeleteShipTypePlanetProduction = 5001
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ErrInputUnknownHostRace int = 3000 + iota
|
ErrInputUnknownHostRace int = 3000 + iota
|
||||||
ErrInputUnknownOpponentRace
|
ErrInputUnknownOpponentRace
|
||||||
ErrInputEntityTypeNameInvalid
|
ErrInputEntityTypeNameInvalid
|
||||||
ErrInputEntityTypeNameExists
|
ErrInputEntityTypeNameDuplicate
|
||||||
|
ErrInputEntityTypeNameNotExists
|
||||||
ErrInputShipTypeDriveValue
|
ErrInputShipTypeDriveValue
|
||||||
ErrInputShipTypeWeaponsValue
|
ErrInputShipTypeWeaponsValue
|
||||||
ErrInputShipTypeShieldsValue
|
ErrInputShipTypeShieldsValue
|
||||||
@@ -39,8 +43,10 @@ func GenericErrorText(code int) string {
|
|||||||
return "Opponent race name is unknown to this game"
|
return "Opponent race name is unknown to this game"
|
||||||
case ErrInputEntityTypeNameInvalid:
|
case ErrInputEntityTypeNameInvalid:
|
||||||
return "Name has invalid length or symbols"
|
return "Name has invalid length or symbols"
|
||||||
case ErrInputEntityTypeNameExists:
|
case ErrInputEntityTypeNameDuplicate:
|
||||||
return "Name already exists"
|
return "Name already exists"
|
||||||
|
case ErrInputEntityTypeNameNotExists:
|
||||||
|
return "Name not exists"
|
||||||
case ErrInputShipTypeDriveValue:
|
case ErrInputShipTypeDriveValue:
|
||||||
return "Invalid Drive value"
|
return "Invalid Drive value"
|
||||||
case ErrInputShipTypeWeaponsValue:
|
case ErrInputShipTypeWeaponsValue:
|
||||||
@@ -55,6 +61,10 @@ func GenericErrorText(code int) string {
|
|||||||
return "Invalid Armament or Weapons value"
|
return "Invalid Armament or Weapons value"
|
||||||
case ErrInputShipTypeZeroValues:
|
case ErrInputShipTypeZeroValues:
|
||||||
return "Ship type values cannot be all zeros"
|
return "Ship type values cannot be all zeros"
|
||||||
|
case ErrDeleteShipTypeExistingGroup:
|
||||||
|
return "Ship type exists in a Group"
|
||||||
|
case ErrDeleteShipTypePlanetProduction:
|
||||||
|
return "Ship type in production on the Planet"
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("Undescribed error with code %d", code)
|
return fmt.Sprintf("Undescribed error with code %d", code)
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-2
@@ -12,8 +12,12 @@ func NewEntityTypeNameValidationError(arg ...any) error {
|
|||||||
return newGenericError(ErrInputEntityTypeNameInvalid, arg...)
|
return newGenericError(ErrInputEntityTypeNameInvalid, arg...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEntityTypeNameExistsError(arg ...any) error {
|
func NewEntityTypeNameDuplicateError(arg ...any) error {
|
||||||
return newGenericError(ErrInputEntityTypeNameExists, arg...)
|
return newGenericError(ErrInputEntityTypeNameDuplicate, arg...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEntityTypeNameNotExistsError(arg ...any) error {
|
||||||
|
return newGenericError(ErrInputEntityTypeNameNotExists, arg...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShipTypeDriveValueError(arg ...any) error {
|
func NewShipTypeDriveValueError(arg ...any) error {
|
||||||
|
|||||||
@@ -3,3 +3,11 @@ package error
|
|||||||
func NewGameStateError(arg ...any) error {
|
func NewGameStateError(arg ...any) error {
|
||||||
return newGenericError(ErrGameStateInvalid, arg...)
|
return newGenericError(ErrGameStateInvalid, arg...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewDeleteShipTypeExistingGroupError(arg ...any) error {
|
||||||
|
return newGenericError(ErrDeleteShipTypeExistingGroup, arg...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDeleteShipTypePlanetProductionError(arg ...any) error {
|
||||||
|
return newGenericError(ErrDeleteShipTypePlanetProduction, arg...)
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,3 +17,19 @@ func createShipType(r Repo, g game.Game, race, typeName string, d, w, s, c float
|
|||||||
}
|
}
|
||||||
return r.SaveState(g)
|
return r.SaveState(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteShipType(configure func(*Param), race, typeName string) (err error) {
|
||||||
|
control(configure, func(c *ctrl) {
|
||||||
|
c.execute(func(r Repo, g game.Game) {
|
||||||
|
err = deleteShipType(r, g, race, typeName)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteShipType(r Repo, g game.Game, race, typeName string) error {
|
||||||
|
if err := g.DeleteShipType(race, typeName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return r.SaveState(g)
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,8 +12,11 @@ import (
|
|||||||
|
|
||||||
func TestCreateShipType(t *testing.T) {
|
func TestCreateShipType(t *testing.T) {
|
||||||
race := "race_01"
|
race := "race_01"
|
||||||
|
typeName := "Drone"
|
||||||
g(t, func(p func(*game.Param), g func() mg.Game) {
|
g(t, func(p func(*game.Param), g func() mg.Game) {
|
||||||
err := game.CreateShipType(p, race, " Drone ", 1, 0, 0, 0, 0)
|
err := game.DeleteShipType(p, race, typeName)
|
||||||
|
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityTypeNameNotExists))
|
||||||
|
err = game.CreateShipType(p, race, " "+typeName+" ", 1, 0, 0, 0, 0)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
st, err := g().ShipTypes(race)
|
st, err := g().ShipTypes(race)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@@ -24,6 +27,12 @@ func TestCreateShipType(t *testing.T) {
|
|||||||
assert.Equal(t, st[0].Shields, 0.)
|
assert.Equal(t, st[0].Shields, 0.)
|
||||||
assert.Equal(t, st[0].Cargo, 0.)
|
assert.Equal(t, st[0].Cargo, 0.)
|
||||||
assert.Equal(t, st[0].Armament, uint(0))
|
assert.Equal(t, st[0].Armament, uint(0))
|
||||||
|
// TODO: test with existing ship group
|
||||||
|
err = game.DeleteShipType(p, race, typeName)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
st, err = g().ShipTypes(race)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, st, 0)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +79,7 @@ func TestCreateShipTypeValidation(t *testing.T) {
|
|||||||
err := game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
|
err := game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
err = game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
|
err = game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
|
||||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityTypeNameExists))
|
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityTypeNameDuplicate))
|
||||||
} else {
|
} else {
|
||||||
err := game.CreateShipType(p, race, tc.name, tc.d, tc.w, tc.s, tc.c, tc.a)
|
err := game.CreateShipType(p, race, tc.name, tc.d, tc.w, tc.s, tc.c, tc.a)
|
||||||
assert.ErrorContains(t, err, tc.err)
|
assert.ErrorContains(t, err, tc.err)
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func buildGameOnMap(races []string, m generator.Map) (*game.Game, error) {
|
|||||||
m.HomePlanets[i].HW.Size, // HW's pop & ind = size
|
m.HomePlanets[i].HW.Size, // HW's pop & ind = size
|
||||||
m.HomePlanets[i].HW.Size,
|
m.HomePlanets[i].HW.Size,
|
||||||
m.HomePlanets[i].HW.Resources,
|
m.HomePlanets[i].HW.Resources,
|
||||||
game.ResearchDrive.AsType(""),
|
game.ResearchDrive.AsType(uuid.Nil),
|
||||||
))
|
))
|
||||||
planetCount++
|
planetCount++
|
||||||
for dw := range m.HomePlanets[i].DW {
|
for dw := range m.HomePlanets[i].DW {
|
||||||
@@ -89,7 +89,7 @@ func buildGameOnMap(races []string, m generator.Map) (*game.Game, error) {
|
|||||||
m.HomePlanets[i].DW[dw].Size, // DW's pop & ind = size
|
m.HomePlanets[i].DW[dw].Size, // DW's pop & ind = size
|
||||||
m.HomePlanets[i].DW[dw].Size,
|
m.HomePlanets[i].DW[dw].Size,
|
||||||
m.HomePlanets[i].DW[dw].Resources,
|
m.HomePlanets[i].DW[dw].Resources,
|
||||||
game.ResearchDrive.AsType(""),
|
game.ResearchDrive.AsType(uuid.Nil),
|
||||||
))
|
))
|
||||||
planetCount++
|
planetCount++
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ func buildGameOnMap(races []string, m generator.Map) (*game.Game, error) {
|
|||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
m.FreePlanets[i].Resources,
|
m.FreePlanets[i].Resources,
|
||||||
game.ProductionNone.AsType(""),
|
game.ProductionNone.AsType(uuid.Nil),
|
||||||
))
|
))
|
||||||
planetCount++
|
planetCount++
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
type PlanetProduction string
|
type PlanetProduction string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -17,15 +19,15 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ProductionType struct {
|
type ProductionType struct {
|
||||||
Production PlanetProduction `json:"type"`
|
Production PlanetProduction `json:"type"`
|
||||||
SubjectName string `json:"subject"` // TODO: change to UUID
|
SubjectID *uuid.UUID `json:"subjectId"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PlanetProduction) AsType(subject string) ProductionType {
|
func (p PlanetProduction) AsType(subject uuid.UUID) ProductionType {
|
||||||
switch p {
|
switch p {
|
||||||
case ResearchScience, ProductionShip:
|
case ResearchScience, ProductionShip:
|
||||||
return ProductionType{Production: p, SubjectName: subject}
|
return ProductionType{Production: p, SubjectID: &subject}
|
||||||
default:
|
default:
|
||||||
return ProductionType{Production: p}
|
return ProductionType{Production: p, SubjectID: nil}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,42 @@ func (g Game) shipTypesInternal(race uuid.UUID) ([]ShipType, error) {
|
|||||||
return nil, e.NewGameStateError("ShipTypes: race %v not found", race)
|
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 {
|
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 {
|
if err := checkShipTypeValues(d, w, s, c, a); err != nil {
|
||||||
return err
|
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 {
|
if g.Race[r].ID == race {
|
||||||
for st := range g.Race[r].ShipTypes {
|
for st := range g.Race[r].ShipTypes {
|
||||||
if g.Race[r].ShipTypes[st].Name == name {
|
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()
|
id := uuid.New()
|
||||||
|
|||||||
Reference in New Issue
Block a user