refactor: planet owner

This commit is contained in:
Ilia Denisov
2026-02-04 19:26:17 +02:00
parent 6a603ea9ad
commit c1d397c993
17 changed files with 170 additions and 183 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ type BattleMeta struct {
func (g Game) RaceVotes(raceID uuid.UUID) float64 {
var result float64
for i := range g.Map.Planet {
if g.Map.Planet[i].Owner == raceID {
if g.Map.Planet[i].OwnedBy(raceID) {
result += g.Map.Planet[i].Votes()
}
}
+39 -30
View File
@@ -6,39 +6,48 @@ import (
"github.com/google/uuid"
)
type UnidentifiedPlanet struct {
X Float `json:"x"`
Y Float `json:"y"`
Number uint `json:"number"`
}
type UninhabitedPlanet struct {
UnidentifiedPlanet
Size Float `json:"size"`
Name string `json:"name"`
Resources Float `json:"resources"` // R - Ресурсы
Capital Float `json:"capital"` // CAP $ - Запасы промышленности
Material Float `json:"material"` // MAT M - Запасы ресурсов / сырьё
}
type PlanetReport struct {
UninhabitedPlanet
Industry Float `json:"industry"` // I - Промышленность
Population Float `json:"population"` // P - Население
Colonists Float `json:"colonists"` // COL C - Количество колонистов
Production Production `json:"production"` // TODO: internal/report format
}
// TODO: unwrap in one struct
type Planet struct {
Owner uuid.UUID `json:"owner"` // FIXME: nil value when no owner
Route map[RouteType]uint `json:"route"`
PlanetReport
X Float `json:"x"`
Y Float `json:"y"`
Number uint `json:"number"`
Size Float `json:"size"`
Name string `json:"name"`
Owner *uuid.UUID `json:"owner,omitempty"`
Resources Float `json:"resources"` // R - Ресурсы
Capital Float `json:"capital"` // CAP $ - Запасы промышленности
Material Float `json:"material"` // MAT M - Запасы ресурсов / сырьё
Industry Float `json:"industry"` // I - Промышленность
Population Float `json:"population"` // P - Население
Colonists Float `json:"colonists"` // COL C - Количество колонистов
Production Production `json:"production"`
Route map[RouteType]uint `json:"route"`
}
type PlanetReportForeign struct {
RaceName string
PlanetReport
func (p *Planet) Own(v uuid.UUID) {
if v == uuid.Nil {
p.Free()
return
}
if p.Owner == nil || *p.Owner != v {
p.Production = ProductionCapital.AsType(uuid.Nil)
}
p.Owner = &v
}
func (p *Planet) Free() {
p.Owner = nil
p.Production = ProductionNone.AsType(uuid.Nil)
}
func (p Planet) Owned() bool {
return p.Owner != nil && *p.Owner != uuid.Nil
}
func (p Planet) OwnedBy(v uuid.UUID) bool {
if !p.Owned() {
return false
}
return *p.Owner == v
}
func (p *Planet) Mat(v float64) {
+24 -48
View File
@@ -16,24 +16,16 @@ func TestPlanetProduction(t *testing.T) {
func TestProduceIndustry(t *testing.T) {
HW := &game.Planet{
PlanetReport: game.PlanetReport{
UninhabitedPlanet: game.UninhabitedPlanet{
Size: 1000,
Resources: 10,
},
Population: 1000,
Industry: 1000,
},
Size: 1000,
Resources: 10,
Population: 1000,
Industry: 1000,
}
DW := &game.Planet{
PlanetReport: game.PlanetReport{
UninhabitedPlanet: game.UninhabitedPlanet{
Size: 500,
Resources: 10,
},
Population: 500,
Industry: 500,
},
Size: 500,
Resources: 10,
Population: 500,
Industry: 500,
}
HW.ProduceIndustry()
assert.InDelta(t, 196.078, HW.Capital.F(), 0.0005)
@@ -58,14 +50,10 @@ func TestProduceIndustry(t *testing.T) {
func TestProduceMaterial(t *testing.T) {
HW := &game.Planet{
PlanetReport: game.PlanetReport{
UninhabitedPlanet: game.UninhabitedPlanet{
Size: 1000,
Resources: 10,
},
Population: 1000,
Industry: 1000,
},
Size: 1000,
Resources: 10,
Population: 1000,
Industry: 1000,
}
assert.Equal(t, 0., HW.Material.F())
@@ -84,14 +72,10 @@ func TestProduceMaterial(t *testing.T) {
func TestUnpackCapital(t *testing.T) {
HW := &game.Planet{
PlanetReport: game.PlanetReport{
UninhabitedPlanet: game.UninhabitedPlanet{
Size: 1000,
Resources: 10,
},
Population: 1000,
Industry: 1000,
},
Size: 1000,
Resources: 10,
Population: 1000,
Industry: 1000,
}
assert.Equal(t, 0., HW.Capital.F())
@@ -119,14 +103,10 @@ func TestUnpackCapital(t *testing.T) {
func TestUnpackColonists(t *testing.T) {
HW := &game.Planet{
PlanetReport: game.PlanetReport{
UninhabitedPlanet: game.UninhabitedPlanet{
Size: 1000,
Resources: 10,
},
Population: 1000,
Industry: 1000,
},
Size: 1000,
Resources: 10,
Population: 1000,
Industry: 1000,
}
assert.Equal(t, 0., HW.Colonists.F())
@@ -152,14 +132,10 @@ func TestUnpackColonists(t *testing.T) {
func TestProducePopulation(t *testing.T) {
HW := &game.Planet{
PlanetReport: game.PlanetReport{
UninhabitedPlanet: game.UninhabitedPlanet{
Size: 1000,
Resources: 10,
},
Population: 500,
Industry: 1000,
},
Size: 1000,
Resources: 10,
Population: 500,
Industry: 1000,
}
assert.Equal(t, 500., HW.Population.F())
assert.Equal(t, 0., HW.Colonists.F())