feat: validate user input for entity names
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
package controller
|
||||
|
||||
import "strings"
|
||||
|
||||
// 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 AND include error check in all user-input test
|
||||
return s, false
|
||||
}
|
||||
|
||||
func maxUint(a, b uint) uint {
|
||||
if b > a {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/internal/error"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/util"
|
||||
)
|
||||
|
||||
var fleetStateNil = game.ShipGroupState("-")
|
||||
@@ -110,7 +111,7 @@ func (c *Controller) JoinShipGroupToFleet(raceName, fleetName string, group, cou
|
||||
|
||||
func (c *Cache) JoinShipGroupToFleet(ri int, fleetName string, groupIndex, quantity uint) (err error) {
|
||||
c.validateRaceIndex(ri)
|
||||
name, ok := validateTypeName(fleetName)
|
||||
name, ok := util.ValidateTypeName(fleetName)
|
||||
if !ok {
|
||||
return e.NewEntityTypeNameValidationError("%q", name)
|
||||
}
|
||||
@@ -211,7 +212,7 @@ func (c *Cache) JoinFleets(ri int, fleetSourceName, fleetTargetName string) (err
|
||||
|
||||
func (c *Cache) createFleet(ri int, name string) (int, error) {
|
||||
c.validateRaceIndex(ri)
|
||||
n, ok := validateTypeName(name)
|
||||
n, ok := util.ValidateTypeName(name)
|
||||
if !ok {
|
||||
return 0, e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/internal/error"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/util"
|
||||
)
|
||||
|
||||
func (c *Controller) RenamePlanet(raceName string, planetNumber int, typeName string) error {
|
||||
@@ -20,7 +21,7 @@ func (c *Controller) RenamePlanet(raceName string, planetNumber int, typeName st
|
||||
}
|
||||
|
||||
func (c *Cache) RenamePlanet(ri int, number int, name string) error {
|
||||
n, ok := validateTypeName(name)
|
||||
n, ok := util.ValidateTypeName(name)
|
||||
if !ok {
|
||||
return e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/internal/error"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/util"
|
||||
)
|
||||
|
||||
func (c *Controller) CreateScience(raceName, typeName string, drive, weapons, shields, cargo float64) error {
|
||||
@@ -19,7 +20,7 @@ func (c *Controller) CreateScience(raceName, typeName string, drive, weapons, sh
|
||||
|
||||
func (c *Cache) CreateScience(ri int, name string, drive, weapons, shileds, cargo float64) error {
|
||||
c.validateRaceIndex(ri)
|
||||
n, ok := validateTypeName(name)
|
||||
n, ok := util.ValidateTypeName(name)
|
||||
if !ok {
|
||||
return e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/internal/error"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/util"
|
||||
)
|
||||
|
||||
func (c *Controller) CreateShipType(raceName, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error {
|
||||
@@ -23,7 +24,7 @@ func (c *Cache) CreateShipType(ri int, typeName string, drive float64, ammo int,
|
||||
if err := checkShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
|
||||
return err
|
||||
}
|
||||
n, ok := validateTypeName(typeName)
|
||||
n, ok := util.ValidateTypeName(typeName)
|
||||
if !ok {
|
||||
return e.NewEntityTypeNameValidationError("%q", n)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
e "github.com/iliadenisov/galaxy/internal/error"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/number"
|
||||
)
|
||||
|
||||
func (c *Cache) CreateShips(ri int, shipTypeName string, planetNumber uint, quantity int) error {
|
||||
@@ -154,7 +155,7 @@ func (c *Cache) JoinEqualGroups(ri int) {
|
||||
for i := 0; i < len(raceGroups)-1; i++ {
|
||||
for j := len(raceGroups) - 1; j > i; j-- {
|
||||
if raceGroups[i].Equal(raceGroups[j]) {
|
||||
raceGroups[i].Index = maxUint(raceGroups[i].Index, raceGroups[j].Index)
|
||||
raceGroups[i].Index = number.Max(raceGroups[i].Index, raceGroups[j].Index)
|
||||
raceGroups[i].Number += raceGroups[j].Number
|
||||
raceGroups = append(raceGroups[:j], raceGroups[j+1:]...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user