cmd: rename planet

This commit is contained in:
Ilia Denisov
2025-10-01 22:42:23 +03:00
parent 2295840efe
commit 8a7e2f57c7
12 changed files with 156 additions and 20 deletions
+30
View File
@@ -4,6 +4,7 @@ import (
"math"
"github.com/google/uuid"
e "github.com/iliadenisov/galaxy/pkg/error"
)
type UnidentifiedPlanet struct {
@@ -74,3 +75,32 @@ func (p *Planet) IncreasePopulation() {
p.Population -= extraPopulation
}
}
func (g Game) RenamePlanet(raceName string, planetNumber int, typeName string) error {
raceID, err := g.hostRaceID(raceName)
if err != nil {
return err
}
return g.renamePlanetInternal(raceID, planetNumber, typeName)
}
func (g Game) renamePlanetInternal(race uuid.UUID, number int, name string) error {
n, ok := validateTypeName(name)
if !ok {
return e.NewEntityTypeNameValidationError("%q", n)
}
if number < 0 {
return e.NewPlanetNumberError(number)
}
num := uint(number)
for pl := range g.Map.Planet {
if g.Map.Planet[pl].Number == num {
if g.Map.Planet[pl].Owner != race {
return e.NewEntityNotOwnedError("planet %#d", num)
}
g.Map.Planet[pl].Name = n
return nil
}
}
return e.NewEntityNotExistsError("planet #%d", number)
}