saving new turn

This commit is contained in:
Ilia Denisov
2025-09-26 01:38:49 +03:00
parent 6d87ea6086
commit 282150a253
13 changed files with 170 additions and 57 deletions
+13 -5
View File
@@ -1,12 +1,16 @@
package game
import "github.com/google/uuid"
import (
"encoding/json"
"github.com/google/uuid"
)
type Game struct {
ID uuid.UUID
Age uint // Game's turn number
Map Map
Race []Race
ID uuid.UUID `json:"id"`
Age uint `json:"turn"` // Game's turn number
Map Map `json:"map"`
Race []Race `json:"races"`
}
func (g Game) Votes(raceID uuid.UUID) float64 {
@@ -19,3 +23,7 @@ func (g Game) Votes(raceID uuid.UUID) float64 {
}
return pop / 1000.
}
func (g Game) MarshalBinary() (data []byte, err error) {
return json.Marshal(&g)
}
+3 -3
View File
@@ -1,7 +1,7 @@
package game
type Map struct {
Width uint32
Height uint32
Planet []Planet
Width uint32 `json:"width"`
Height uint32 `json:"height"`
Planet []Planet `json:"planets"`
}
+13 -12
View File
@@ -7,21 +7,22 @@ import (
)
type Planet struct {
X, Y float64
Size float64
X float64 `json:"x"`
Y float64 `json:"y"`
Size float64 `json:"size"`
Name string
Number uint
Owner uuid.UUID
Name string `json:"name"`
Number uint `json:"number"`
Owner uuid.UUID `json:"owner"`
Production ProductionType
Population float64 // P - Население
Industry float64 // I - Промышленность
Resources float64 // R - Ресурсы / сырьё
Production ProductionType `json:"production"`
Population float64 `json:"population"` // P - Население
Industry float64 `json:"industry"` // I - Промышленность
Resources float64 `json:"resources"` // R - Ресурсы / сырьё
Capital float64 // CAP $ - Запасы промышленности
Material float64 // MAT M - Запасы ресурсов / сырья
Colonists float64 // COL C - Количество колонистов
Capital float64 `json:"capital"` // CAP $ - Запасы промышленности
Material float64 `json:"material"` // MAT M - Запасы ресурсов / сырья
Colonists float64 `json:"colonists"` // COL C - Количество колонистов
// Параметр "L" - Свободный производственный потенциал
}
+2 -2
View File
@@ -17,8 +17,8 @@ const (
)
type ProductionType struct {
Production PlanetProduction
SubjectName string // TODO: change to UUID
Production PlanetProduction `json:"type"`
SubjectName string `json:"subject"` // TODO: change to UUID
}
func (p PlanetProduction) AsType(subject string) ProductionType {
+20 -9
View File
@@ -3,17 +3,28 @@ package game
import "github.com/google/uuid"
type Race struct {
ID uuid.UUID
Name string
Killed bool
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Extinct bool `json:"extinct"`
// Votes float64
Ally uuid.UUID // Race's Votes receiver
Vote uuid.UUID `json:"vote"`
Relations []RaceRelation `json:"relations"`
Drive float64
Weapons float64
Shields float64
Cargo float64
Drive float64 `json:"drive"`
Weapons float64 `json:"weapons"`
Shields float64 `json:"shields"`
Cargo float64 `json:"cargo"`
Sciences []Science `json:"science,omitempty"`
ShipTypes []ShipType `json:"shipType,omitempty"`
ShipGroups []ShipGroup `json:"shipGroup,omitempty"`
Fleets []Fleet `json:"fleet,omitempty"`
}
type RaceRelation struct {
RaceID uuid.UUID `json:"raceId"`
Peace bool `json:"peace"`
}
func (r Race) FlightDistance() float64 {
+8 -5
View File
@@ -1,9 +1,12 @@
package game
import "github.com/google/uuid"
type Science struct {
Name string
Drive float64
Weapons float64
Shields float64
Cargo float64
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Drive float64 `json:"drive"`
Weapons float64 `json:"weapons"`
Shields float64 `json:"shields"`
Cargo float64 `json:"cargo"`
}
+18 -15
View File
@@ -3,31 +3,34 @@ package game
import (
"math"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/pkg/number"
)
type ShipType struct {
Name string
Drive float64 // [0], [1...]
Armament uint
Weapons float64 // [0], [1...]
Shields float64 // [0], [1...]
Cargo float64 // [0], [1...]
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Drive float64 `json:"drive"` // [0], [1...]
Armament uint `json:"armament"`
Weapons float64 `json:"weapons"` // [0], [1...]
Shields float64 `json:"shields"` // [0], [1...]
Cargo float64 `json:"cargo"` // [0], [1...]
}
type ShipGroup struct {
Type ShipType
Number uint
State string // TODO: kinda enum: In_Orbit, In_Space, Transfer_State, Upgrade
Load float64 // Cargo loaded - "Масса груза"
Drive float64
Weapons float64
Shields float64
Cargo float64
TypeID uuid.UUID `json:"id"`
Type ShipType `json:"-"` // TODO: fill upon load from store
Number uint `json:"number"`
State string `json:"state"` // TODO: kinda enum: In_Orbit, In_Space, Transfer_State, Upgrade
Load float64 `json:"load"` // Cargo loaded - "Масса груза"
Drive float64 `json:"drive"`
Weapons float64 `json:"weapons"`
Shields float64 `json:"shields"`
Cargo float64 `json:"cargo"`
}
type Fleet struct {
ShipGroups []ShipGroup
ShipGroups []ShipGroup `json:"group"`
}
// TODO: test on real values
+92
View File
@@ -0,0 +1,92 @@
package game_test
import (
"testing"
"github.com/iliadenisov/galaxy/pkg/model/game"
"github.com/stretchr/testify/assert"
)
func TestShipType(t *testing.T) {
Gunship := game.ShipType{
Drive: 4,
Armament: 2,
Weapons: 2,
Shields: 4,
Cargo: 0,
}
assert.Equal(t, Gunship.EmptyMass(), 11.)
Cruiser := game.ShipType{
Drive: 15,
Armament: 1,
Weapons: 15,
Shields: 15,
Cargo: 0,
}
assert.Equal(t, Cruiser.EmptyMass(), 45.)
sg := game.ShipGroup{
Type: Cruiser,
Number: 1,
State: "In_Orbit",
Drive: 1.0,
Weapons: 1.0,
Shields: 1.0,
Cargo: 1.0,
}
upgradeCost := sg.UpgradeDriveCost(2.0) +
sg.UpgradeWeaponsCost(2.0) +
sg.UpgradeShieldsCost(2.0) +
sg.UpgradeCargoCost(2.0)
assert.Equal(t, upgradeCost, 225.)
}
func TestCargoCapacity(t *testing.T) {
test := func(cargoSize float64, expectCapacity float64) {
ship := game.ShipType{
Drive: 1,
Armament: 1,
Weapons: 1,
Shields: 1,
Cargo: cargoSize,
}
sg := game.ShipGroup{
Type: ship,
Number: 1,
State: "In_Orbit",
Drive: 1.0,
Weapons: 1.0,
Shields: 1.0,
Cargo: 1.0,
}
assert.Equal(t, expectCapacity, sg.CargoCapacity())
}
test(1, 1.05)
test(5, 6.25)
test(10, 15)
test(50, 175)
test(100, 600)
}
func TestBombingPower(t *testing.T) {
Gunship := game.ShipType{
Drive: 60.0,
Armament: 3,
Weapons: 30.0,
Shields: 100.0,
Cargo: 0.0,
}
sg := game.ShipGroup{
Type: Gunship,
Number: 1,
State: "In_Orbit",
Drive: 1.0,
Weapons: 1.0,
Shields: 1.0,
Cargo: 1.0,
}
expectedBombingPower := 139.295
result := sg.BombingPower()
assert.Equal(t, expectedBombingPower, result)
}