From ffecb5d90cc1b5b63e54174a499b7635b84fbe7c Mon Sep 17 00:00:00 2001 From: Ilia Denisov Date: Tue, 8 Aug 2023 21:57:53 +0300 Subject: [PATCH] towards generation --- pkg/{server => game}/game.go | 77 +---------------------- pkg/game/game_data.go | 100 ++++++++++++++++++++++++++++++ pkg/game/game_init.go | 39 ++++++++++++ pkg/game/game_parameter.go | 7 +++ pkg/{server => game}/game_test.go | 18 +++--- pkg/generator/generator.go | 2 +- pkg/generator/map_parameter.go | 6 +- pkg/server/server.go | 22 +++++++ pkg/storage/storage.go | 18 +++--- 9 files changed, 193 insertions(+), 96 deletions(-) rename pkg/{server => game}/game.go (69%) create mode 100644 pkg/game/game_data.go create mode 100644 pkg/game/game_init.go create mode 100644 pkg/game/game_parameter.go rename pkg/{server => game}/game_test.go (87%) create mode 100644 pkg/server/server.go diff --git a/pkg/server/game.go b/pkg/game/game.go similarity index 69% rename from pkg/server/game.go rename to pkg/game/game.go index 47ff4c5..4228df4 100644 --- a/pkg/server/game.go +++ b/pkg/game/game.go @@ -1,30 +1,7 @@ -package server +package game import "math" -type GameIdentifier string -type RaceIdentifier string - -type Game struct { - Id GameIdentifier - Name string - Schedule string // TODO: implement somehow - Turn uint - Races []Race - Planets []Planet - warState map[uint]uint -} - -type Race struct { - Id RaceIdentifier - Name string - Drive float64 - Weapons float64 - Shields float64 - Cargo float64 - Planets []Planet -} - func (r Race) FlightDistance() float64 { return r.Drive * 40 } @@ -33,26 +10,6 @@ func (r Race) VisibilityDistance() float64 { return r.Drive * 30 } -type Coordinate struct { - X, Y float64 -} - -type Planet struct { - Number uint - Name string - Position Coordinate - Size float64 - Production string // TODO: kinda enum - Resources float64 // Сырьё - Industry float64 // Промышленность - Population float64 // Население - - Capital float64 // CAP $ - Запасы промышленности - Material float64 // MAT M - Запасы сырья - Colonists float64 // COL C - Количество колонистов - // Параметр "L" означает количество свободных производственных единиц. -} - // Производственный потенциал (I) // промышленность * 0.75 + население * 0.25 func (p Planet) ProductionCapacity() float64 { @@ -87,23 +44,6 @@ func (p *Planet) IncreasePopulation() { } } -type Science struct { - Name string - Drive float64 - Weapons float64 - Shields float64 - Cargo float64 -} - -type ShipType struct { - Name string - Drive float64 // [0], [1...] - Armament uint - Weapons float64 // [0], [1...] - Shields float64 // [0], [1...] - Cargo float64 // [0], [1...] -} - // TODO: test on real values func (st ShipType) EmptyMass() float64 { shipMass := st.DriveMass() + st.ShieldsMass() + st.CargoMass() + st.WeaponsMass() @@ -126,17 +66,6 @@ func (st ShipType) WeaponsMass() float64 { return float64(st.Armament)*(st.Weapons/2) + st.Weapons/2 } -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 -} - // Грузоподъёмность func (sg ShipGroup) CargoCapacity() float64 { return sg.Drive * (sg.Type.Cargo + (sg.Type.Cargo*sg.Type.Cargo)/20) @@ -191,10 +120,6 @@ func (sg ShipGroup) BombingPower() float64 { return toFixed3(result) } -type Fleet struct { - ShipGroups []ShipGroup -} - // TODO: test this func (fl Fleet) Speed() float64 { result := math.MaxFloat64 diff --git a/pkg/game/game_data.go b/pkg/game/game_data.go new file mode 100644 index 0000000..f479861 --- /dev/null +++ b/pkg/game/game_data.go @@ -0,0 +1,100 @@ +package game + +type GameIdentifier string +type RaceIdentifier string + +type Game struct { + Id GameIdentifier + Name string + Schedule string // TODO: implement somehow + Turn uint + Races []Race + Planets []Planet + WarState map[RaceIdentifier]RaceIdentifier +} + +type Race struct { + Id RaceIdentifier + Name string + Drive float64 + Weapons float64 + Shields float64 + Cargo float64 +} + +type PlanetProduction string + +const ( + ProductionMaterial PlanetProduction = "MAT" + ProductionCapital PlanetProduction = "CAP" + ProductionDrive PlanetProduction = "DRIVE" + ProductionWeapons PlanetProduction = "WEAPONS" + ProductionShields PlanetProduction = "SHIELDS" + ProductionCargo PlanetProduction = "CARGO" + + ProductionScience PlanetProduction = "SCIENCE" + ProductionShip PlanetProduction = "SHIP" +) + +type ProductionType struct { + Production PlanetProduction + SubjectName string +} + +type Planet struct { + Number uint + Name string + Position Coordinate + Size float64 + + Owner RaceIdentifier + Production ProductionType + Resources float64 // Сырьё + Industry float64 // Промышленность + Population float64 // Население + + Capital float64 // CAP $ - Запасы промышленности + Material float64 // MAT M - Запасы сырья + Colonists float64 // COL C - Количество колонистов + // Параметр "L" означает количество свободных производственных единиц. +} + +func (p *Planet) setOwner(id RaceIdentifier) { + p.Owner = id +} + +type Coordinate struct { + X, Y float64 +} + +type Science struct { + Name string + Drive float64 + Weapons float64 + Shields float64 + Cargo float64 +} + +type ShipType struct { + Name string + Drive float64 // [0], [1...] + Armament uint + Weapons float64 // [0], [1...] + Shields float64 // [0], [1...] + Cargo float64 // [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 +} + +type Fleet struct { + ShipGroups []ShipGroup +} diff --git a/pkg/game/game_init.go b/pkg/game/game_init.go new file mode 100644 index 0000000..4fe55ad --- /dev/null +++ b/pkg/game/game_init.go @@ -0,0 +1,39 @@ +package game + +func CreateGame(id GameIdentifier, name string, races []Race, planets []Planet) Game { + return Game{ + Id: id, + Name: name, + Turn: 0, + Races: races, + Planets: planets, + } +} + +func CreateRace(id RaceIdentifier, name string) Race { + return Race{ + Id: id, + Name: name, + Drive: 1.0, + Weapons: 1.0, + Shields: 1.0, + Cargo: 1.0, + } +} + +func CreatePlanet(number uint, name string, position Coordinate, size float64, res float64, pop float64) Planet { + return Planet{ + Number: number, + Name: name, + Position: position, + Size: size, + Production: ProductionType{ProductionDrive, ""}, + Resources: res, + Population: pop, + + Industry: 0, + Capital: 0, + Material: 0, + Colonists: 0, + } +} diff --git a/pkg/game/game_parameter.go b/pkg/game/game_parameter.go new file mode 100644 index 0000000..2688c56 --- /dev/null +++ b/pkg/game/game_parameter.go @@ -0,0 +1,7 @@ +package game + +type GameParameter struct { + Series string + Players uint + Public bool +} diff --git a/pkg/server/game_test.go b/pkg/game/game_test.go similarity index 87% rename from pkg/server/game_test.go rename to pkg/game/game_test.go index bd1a51d..18e5df1 100644 --- a/pkg/server/game_test.go +++ b/pkg/game/game_test.go @@ -1,13 +1,13 @@ -package server_test +package game_test import ( "testing" - "github.com/iliadenisov/galaxy/pkg/server" + "github.com/iliadenisov/galaxy/pkg/game" ) func Test_ShipType(t *testing.T) { - Gunship := server.ShipType{ + Gunship := game.ShipType{ Drive: 4, Armament: 2, Weapons: 2, @@ -19,7 +19,7 @@ func Test_ShipType(t *testing.T) { t.Errorf("Cruiser mass expected %.3f but %.3f given", 11., Gunship.EmptyMass()) } - Cruiser := server.ShipType{ + Cruiser := game.ShipType{ Drive: 15, Armament: 1, Weapons: 15, @@ -31,7 +31,7 @@ func Test_ShipType(t *testing.T) { t.Errorf("Cruiser mass expected %.3f but %.3f given", 45., Cruiser.EmptyMass()) } - sg := server.ShipGroup{ + sg := game.ShipGroup{ Type: Cruiser, Number: 1, State: "In_Orbit", @@ -53,14 +53,14 @@ func Test_ShipType(t *testing.T) { func Test_CargoCapacity(t *testing.T) { test := func(cargoSize float64, expectCapacity float64) { - ship := server.ShipType{ + ship := game.ShipType{ Drive: 1, Armament: 1, Weapons: 1, Shields: 1, Cargo: cargoSize, } - sg := server.ShipGroup{ + sg := game.ShipGroup{ Type: ship, Number: 1, State: "In_Orbit", @@ -81,14 +81,14 @@ func Test_CargoCapacity(t *testing.T) { } func Test_BombingPower(t *testing.T) { - Gunship := server.ShipType{ + Gunship := game.ShipType{ Drive: 60.0, Armament: 3, Weapons: 30.0, Shields: 100.0, Cargo: 0.0, } - sg := server.ShipGroup{ + sg := game.ShipGroup{ Type: Gunship, Number: 1, State: "In_Orbit", diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index 3d5778b..61704a8 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -37,7 +37,7 @@ type Plotter struct { sectors draw.Plane } -func Generate(param MapParameters) (result Map) { +func Generate(param MapParameter) (result Map) { pl := func(c Coordinate, param UninhabitedPlanetParameters) Planet { return Planet{ Position: c, diff --git a/pkg/generator/map_parameter.go b/pkg/generator/map_parameter.go index e878de1..2e6d989 100644 --- a/pkg/generator/map_parameter.go +++ b/pkg/generator/map_parameter.go @@ -1,6 +1,6 @@ package generator -type MapParameters struct { +type MapParameter struct { Players uint HW_Size uint HW_Resources uint @@ -27,8 +27,8 @@ type UninhabitedPlanetParameters struct { Probability float64 } -func DefaultMapParameters() MapParameters { - return MapParameters{ +func DefaultMapParameters() MapParameter { + return MapParameter{ Players: 25, HW_Size: 1000, HW_Resources: 10, diff --git a/pkg/server/server.go b/pkg/server/server.go new file mode 100644 index 0000000..794d3cf --- /dev/null +++ b/pkg/server/server.go @@ -0,0 +1,22 @@ +package server + +import ( + "errors" + + "github.com/iliadenisov/galaxy/pkg/game" + "github.com/iliadenisov/galaxy/pkg/generator" + "github.com/iliadenisov/galaxy/pkg/storage" +) + +type Server struct { + storage storage.Storage +} + +func New(storage storage.Storage) Server { + return Server{storage: storage} +} + +func (s Server) CreateGame(gameParam game.GameParameter, mapParam generator.MapParameter) (game.Game, error) { + _ = generator.Generate(mapParam) + return game.Game{}, errors.New("not yet implemented") +} diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 7cd6055..43e39fb 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -1,11 +1,11 @@ package storage -import "github.com/iliadenisov/galaxy/pkg/server" +import "github.com/iliadenisov/galaxy/pkg/game" // games/ // data.json - id, name, turn, schedule, status // game123/ -// racelist//data.json - account_id, name, status, war/peace(?), last_order, etc. +// race//data.json - account_id, name, status, war/peace(?), last_order, etc. // order///0.json - incoming orders // turn/12/log/ - ? // turn/12/order//0.json - processed orders @@ -16,11 +16,15 @@ import "github.com/iliadenisov/galaxy/pkg/server" // turn/12/battle//.json type Storage interface { - ListGames() ([]server.GameIdentifier, error) + GenerateRaceId() game.RaceIdentifier + GenerateGameId() game.GameIdentifier - LoadRace(game_id server.GameIdentifier, race_id server.RaceIdentifier) (server.Race, error) - SaveRace(game_id server.GameIdentifier, race server.Race) error + CreateGame(game.GameParameter) (game.Game, error) + ListGames() ([]game.GameIdentifier, error) - LoadState(game_id server.GameIdentifier) (server.Game, error) - SaveState(game server.Game) error + LoadRace(game_id game.GameIdentifier, race_id game.RaceIdentifier) (game.Race, error) + SaveRace(game_id game.GameIdentifier, race game.Race) error + + LoadState(game_id game.GameIdentifier) (game.Game, error) + SaveState(game game.Game) error }