cmd: create ship type

This commit is contained in:
Ilia Denisov
2025-09-30 23:40:50 +03:00
parent c6ccc2df9f
commit 6f29288096
8 changed files with 280 additions and 10 deletions
+34 -5
View File
@@ -9,12 +9,23 @@ const (
ErrStorageFailure int = 1000
ErrGameStateInvalid int = 2000
ErrInputUnknownHostRace int = 3000
ErrInputUnknownOpponentRace int = 3001
)
func errorText(code int) string {
const (
ErrInputUnknownHostRace int = 3000 + iota
ErrInputUnknownOpponentRace
ErrInputEntityTypeNameInvalid
ErrInputEntityTypeNameExists
ErrInputShipTypeDriveValue
ErrInputShipTypeWeaponsValue
ErrInputShipTypeShieldsValue
ErrInputShipTypeCargoValue
ErrInputShipTypeArmamentValue
ErrInputShipTypeWeaponsAndArmamentValue
ErrInputShipTypeZeroValues
)
func GenericErrorText(code int) string {
switch code {
case ErrDummy:
return "Dummy"
@@ -26,6 +37,24 @@ func errorText(code int) string {
return "Host race name is unknown to this game"
case ErrInputUnknownOpponentRace:
return "Opponent race name is unknown to this game"
case ErrInputEntityTypeNameInvalid:
return "Name has invalid length or symbols"
case ErrInputEntityTypeNameExists:
return "Name already exists"
case ErrInputShipTypeDriveValue:
return "Invalid Drive value"
case ErrInputShipTypeWeaponsValue:
return "Invalid Weapons value"
case ErrInputShipTypeShieldsValue:
return "Invalid Shields value"
case ErrInputShipTypeCargoValue:
return "Invalid Cargo value"
case ErrInputShipTypeArmamentValue:
return "Invalid Armament value"
case ErrInputShipTypeWeaponsAndArmamentValue:
return "Invalid Armament or Weapons value"
case ErrInputShipTypeZeroValues:
return "Ship type values cannot be all zeros"
default:
return fmt.Sprintf("Undescribed error with code %d", code)
}
@@ -38,7 +67,7 @@ type GenericError struct {
}
func (ge GenericError) Error() string {
msg := errorText(ge.code)
msg := GenericErrorText(ge.code)
if ge.subject != "" {
msg += ": " + ge.subject
}
+36
View File
@@ -7,3 +7,39 @@ func NewHostRaceUnknownError(arg ...any) error {
func NewOpponentRaceUnknownError(arg ...any) error {
return newGenericError(ErrInputUnknownOpponentRace, arg...)
}
func NewEntityTypeNameValidationError(arg ...any) error {
return newGenericError(ErrInputEntityTypeNameInvalid, arg...)
}
func NewEntityTypeNameExistsError(arg ...any) error {
return newGenericError(ErrInputEntityTypeNameExists, arg...)
}
func NewShipTypeDriveValueError(arg ...any) error {
return newGenericError(ErrInputShipTypeDriveValue, arg...)
}
func NewShipTypeWeaponsValueError(arg ...any) error {
return newGenericError(ErrInputShipTypeWeaponsValue, arg...)
}
func NewShipTypeShieldsValueError(arg ...any) error {
return newGenericError(ErrInputShipTypeShieldsValue, arg...)
}
func NewShipTypeCargoValueError(arg ...any) error {
return newGenericError(ErrInputShipTypeCargoValue, arg...)
}
func NewShipTypeArmamentValueError(arg ...any) error {
return newGenericError(ErrInputShipTypeArmamentValue, arg...)
}
func NewShipTypeArmamentAndWeaponsValueError(arg ...any) error {
return newGenericError(ErrInputShipTypeWeaponsAndArmamentValue, arg...)
}
func NewShipTypeShipTypeZeroValuesError(arg ...any) error {
return newGenericError(ErrInputShipTypeZeroValues, arg...)
}
+19
View File
@@ -0,0 +1,19 @@
package game
import "github.com/iliadenisov/galaxy/pkg/model/game"
func CreateShipType(configure func(*Param), race, typeName string, drive, weapons, shields, cargo float64, armament int) (err error) {
control(configure, func(c *ctrl) {
c.execute(func(r Repo, g game.Game) {
err = createShipType(r, g, race, typeName, drive, weapons, shields, cargo, armament)
})
})
return
}
func createShipType(r Repo, g game.Game, race, typeName string, d, w, s, c float64, a int) error {
if err := g.CreateShipType(race, typeName, d, w, s, c, a); err != nil {
return err
}
return r.SaveState(g)
}
+80
View File
@@ -0,0 +1,80 @@
package game_test
import (
"strconv"
"testing"
e "github.com/iliadenisov/galaxy/pkg/error"
"github.com/iliadenisov/galaxy/pkg/game"
mg "github.com/iliadenisov/galaxy/pkg/model/game"
"github.com/stretchr/testify/assert"
)
func TestCreateShipType(t *testing.T) {
race := "race_01"
g(t, func(p func(*game.Param), g func() mg.Game) {
err := game.CreateShipType(p, race, " Drone ", 1, 0, 0, 0, 0)
assert.NoError(t, err)
st, err := g().ShipTypes(race)
assert.NoError(t, err)
assert.Len(t, st, 1)
assert.Equal(t, st[0].Name, "Drone")
assert.Equal(t, st[0].Drive, 1.)
assert.Equal(t, st[0].Weapons, 0.)
assert.Equal(t, st[0].Shields, 0.)
assert.Equal(t, st[0].Cargo, 0.)
assert.Equal(t, st[0].Armament, uint(0))
})
}
func TestCreateShipTypeValidation(t *testing.T) {
race := "race_01"
typeName := "Drone"
type tc struct {
name string
d, w, s, c float64
a int
err string
}
table := []tc{
// correct values
{typeName, 1, 0, 0, 0, 0, ""},
{typeName, 1.1, 0, 0, 0, 0, ""},
{typeName, 1, 1.2, 0, 0, 1, ""},
{typeName, 1, 1.2, 2.5, 0, 1, ""},
{typeName, 1, 0, 2.5, 7.7, 0, ""},
// incorrect values...
{"", 1, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputEntityTypeNameInvalid)},
{" ", 1, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputEntityTypeNameInvalid)},
{typeName, 0, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeZeroValues)},
// drive
{typeName, -1, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeDriveValue)},
{typeName, 0.5, 0, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeDriveValue)},
// weapons
{typeName, 0, -1, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeWeaponsValue)},
{typeName, 0, 0.5, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeWeaponsValue)},
// shields
{typeName, 0, 0, -1, 0, 0, e.GenericErrorText(e.ErrInputShipTypeShieldsValue)},
{typeName, 0, 0, 0.5, 0, 0, e.GenericErrorText(e.ErrInputShipTypeShieldsValue)},
// cargo
{typeName, 0, 0, 0, -1, 0, e.GenericErrorText(e.ErrInputShipTypeCargoValue)},
{typeName, 0, 0, 0, 0.5, 0, e.GenericErrorText(e.ErrInputShipTypeCargoValue)},
// armament (and weapons)
{typeName, 0, 0, 0, 0, -1, e.GenericErrorText(e.ErrInputShipTypeArmamentValue)},
{typeName, 0, 1, 0, 0, 0, e.GenericErrorText(e.ErrInputShipTypeWeaponsAndArmamentValue)},
{typeName, 0, 0, 0, 0, 1, e.GenericErrorText(e.ErrInputShipTypeWeaponsAndArmamentValue)},
}
g(t, func(p func(*game.Param), g func() mg.Game) {
for i, tc := range table {
if tc.err == "" {
err := game.CreateShipType(p, race, tc.name+strconv.Itoa(i), tc.d, tc.w, tc.s, tc.c, tc.a)
assert.NoError(t, err)
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))
} else {
err := game.CreateShipType(p, race, tc.name, tc.d, tc.w, tc.s, tc.c, tc.a)
assert.ErrorContains(t, err, tc.err)
}
}
})
}
+1 -1
View File
@@ -26,7 +26,7 @@ func TestComposeGame(t *testing.T) {
})
}
func g(t *testing.T, f func(func(*game.Param), func() mg.Game)) {
func g(t *testing.T, f func(p func(*game.Param), g func() mg.Game)) {
root, cleanup := util.CreateWorkDir(t)
defer cleanup()
races := make([]string, testRaceCount)
+13
View File
@@ -2,6 +2,7 @@ package game
import (
"encoding/json"
"strings"
"github.com/google/uuid"
e "github.com/iliadenisov/galaxy/pkg/error"
@@ -111,6 +112,18 @@ func (g Game) relationInternal(hostID, opponentID uuid.UUID) (RaceRelation, erro
return RaceRelation{}, e.NewGameStateError("Relation: host %v not found", hostID)
}
// -----------------------------------------------------------------------------
// validateTypeName always return v without leading and trailing spaces
func validateTypeName(v string) (string, bool) {
s := strings.TrimSpace(v)
if len(s) > 0 {
return s, true
}
// TODO: special symbols
return s, false
}
func (g Game) MarshalBinary() (data []byte, err error) {
return json.Marshal(&g)
}
+4 -4
View File
@@ -9,11 +9,11 @@ import (
type ShipTypeReport struct {
Name string `json:"name"`
Drive float64 `json:"drive"` // [0], [1...]
Drive float64 `json:"drive"`
Armament uint `json:"armament"`
Weapons float64 `json:"weapons"` // [0], [1...]
Shields float64 `json:"shields"` // [0], [1...]
Cargo float64 `json:"cargo"` // [0], [1...]
Weapons float64 `json:"weapons"`
Shields float64 `json:"shields"`
Cargo float64 `json:"cargo"`
}
type ShipType struct {
+93
View File
@@ -0,0 +1,93 @@
package game
import (
"github.com/google/uuid"
e "github.com/iliadenisov/galaxy/pkg/error"
)
func (g Game) ShipTypes(raceName string) ([]ShipType, error) {
raceID, err := g.hostRaceID(raceName)
if err != nil {
return nil, err
}
return g.shipTypesInternal(raceID)
}
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) CreateShipType(raceName, typeName string, d, w, s, c float64, a int) error {
if err := checkShipTypeValues(d, w, s, c, a); err != nil {
return err
}
raceID, err := g.hostRaceID(raceName)
if err != nil {
return err
}
tn, ok := validateTypeName(typeName)
if !ok {
return e.NewEntityTypeNameValidationError("%q", tn)
}
return g.createShipTypeInternal(raceID, tn, d, w, s, c, a)
}
func (g Game) createShipTypeInternal(race uuid.UUID, name string, d, w, s, c float64, a int) 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 {
return e.NewEntityTypeNameExistsError("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: name,
Drive: d,
Weapons: w,
Shields: s,
Cargo: c,
Armament: uint(a),
},
})
return nil
}
}
return e.NewGameStateError("CreateShipType: race %v not found", race)
}
func checkShipTypeValues(d, w, s, c float64, a int) error {
if !checkShipTypeValueDWSC(d) {
return e.NewShipTypeDriveValueError(d)
}
if !checkShipTypeValueDWSC(w) {
return e.NewShipTypeWeaponsValueError(w)
}
if !checkShipTypeValueDWSC(s) {
return e.NewShipTypeShieldsValueError(s)
}
if !checkShipTypeValueDWSC(c) {
return e.NewShipTypeCargoValueError(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
}