cmd: create ship type
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user