diff --git a/backend/src/generate/map.rs b/backend/src/generate/map.rs index c06655f..1978471 100644 --- a/backend/src/generate/map.rs +++ b/backend/src/generate/map.rs @@ -44,6 +44,7 @@ pub fn generate_map() -> Result { id, position: (point.x, point.y), owned_by_empire_id: None, + planets: HashMap::new(), }; star_systems.insert(id, star_system); } diff --git a/backend/src/generate/mod.rs b/backend/src/generate/mod.rs index 5af1331..e92a1a7 100644 --- a/backend/src/generate/mod.rs +++ b/backend/src/generate/mod.rs @@ -7,14 +7,15 @@ use chrono::Utc; use colourado::{ColorPalette, PaletteType}; use names::Generator as NameGenerator; use petgraph::algo::k_shortest_path; -use rand::{ - rngs::ThreadRng, seq::SliceRandom, thread_rng, RngCore, SeedableRng, -}; +use rand::{seq::SliceRandom, thread_rng, RngCore, SeedableRng}; use rand_chacha::ChaCha12Rng; use crate::{ - prisma::PrismaClient, - state::{EmpireCoreState, EmpireId, GameCoreState, StarSystemId, UniverseId}, + prisma::{planet, star_system, universe, PrismaClient}, + state::{ + EmpireCoreState, EmpireId, GameCoreState, PlanetCoreState, PlanetId, + StarSystemId, UniverseId, + }, }; use self::map::generate_map; @@ -74,18 +75,36 @@ pub async fn generate_initial_game_state( { let id = EmpireId(idx as i32); - // Set the star system's owner - map - .star_systems - .get_mut(&capital_system_id) - .unwrap() - .owned_by_empire_id = Some(id); + { + let star_system = map.star_systems.get_mut(&capital_system_id).unwrap(); + + // Set the star system's owner + star_system.owned_by_empire_id = Some(id); + + // Generate a planet to put inside the star system + let planet = client + .planet() + .create( + universe::UniqueWhereParam::IdEquals(universe.id), + star_system::UniqueWhereParam::UniverseIdIndexEquals( + universe.id, + star_system.id.0, + ), + vec![], + ) + .exec() + .await?; + let planet_id = PlanetId(planet.id); + let planet_state = PlanetCoreState {}; + star_system.planets.insert(planet_id, planet_state); + } let empire = EmpireCoreState { id, name, color, capital_system_id, + resources: HashMap::default(), }; empires.insert(id, empire); } diff --git a/backend/src/state/empire.rs b/backend/src/state/empire.rs index 937f9fa..0d215fd 100644 --- a/backend/src/state/empire.rs +++ b/backend/src/state/empire.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use colourado::Color; use super::StarSystemId; @@ -10,6 +12,10 @@ pub struct EmpireCoreState { pub name: String, pub color: Color, pub capital_system_id: StarSystemId, + + pub resources: HashMap, } -pub struct EmpireDerivedState {} +pub struct EmpireDerivedState { + pub resources: HashMap, +} diff --git a/backend/src/state/game.rs b/backend/src/state/game.rs index 943d3ec..30c2c31 100644 --- a/backend/src/state/game.rs +++ b/backend/src/state/game.rs @@ -89,6 +89,7 @@ impl GameCoreState { self.universe_id.0, empire.name.clone(), format_color(empire.color), + json!({}), vec![empire::id::set(empire.id.0)], ) }) @@ -98,7 +99,7 @@ impl GameCoreState { .exec() .await?; - // Update the ownership + // Update the star system ownership for (star_system_id, empire_id) in owned_systems.into_iter() { client .star_system() diff --git a/backend/src/state/mod.rs b/backend/src/state/mod.rs index 6902bec..5f633e4 100644 --- a/backend/src/state/mod.rs +++ b/backend/src/state/mod.rs @@ -4,9 +4,11 @@ mod macros; mod empire; mod fleet; mod game; +mod planet; mod starsystem; pub use self::empire::*; pub use self::fleet::*; pub use self::game::*; +pub use self::planet::*; pub use self::starsystem::*; diff --git a/backend/src/state/planet.rs b/backend/src/state/planet.rs new file mode 100644 index 0000000..ca2caf0 --- /dev/null +++ b/backend/src/state/planet.rs @@ -0,0 +1,8 @@ +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct PlanetId(pub i32); + +#[derive(Debug)] +pub struct PlanetCoreState {} + +#[derive(Debug)] +pub struct PlanetDerivedState {} diff --git a/backend/src/state/starsystem.rs b/backend/src/state/starsystem.rs index 857c5dc..a0b7764 100644 --- a/backend/src/state/starsystem.rs +++ b/backend/src/state/starsystem.rs @@ -1,6 +1,7 @@ -use super::EmpireId; +use std::collections::HashMap; + +use super::{EmpireId, PlanetCoreState, PlanetId}; -/// This is only guaranteed to be unique for a specific UniverseId #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct StarSystemId(pub i32); @@ -9,6 +10,7 @@ pub struct StarSystemCoreState { pub id: StarSystemId, pub position: (f64, f64), pub owned_by_empire_id: Option, + pub planets: HashMap, } pub struct StarSystemDerivedState {} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 99c301f..e37f436 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -13,12 +13,13 @@ model Universe { config Json - players Player[] - empires Empire[] - starSystems StarSystem[] - planets Planet[] - Fleet Fleet[] - StarSystemEdges StarSystemEdges[] + players Player[] + empires Empire[] + starSystems StarSystem[] + planets Planet[] + fleets Fleet[] + hyperlanes StarSystemEdges[] + starbases Starbase[] } model User { @@ -58,6 +59,8 @@ model Empire { starSystems StarSystem[] @relation("owner") + resources Json + @@id([universeId, id]) } @@ -75,6 +78,9 @@ model StarSystem { ownedByEmpireId Int? ownedByEmpire Empire? @relation("owner", fields: [universeId, ownedByEmpireId], references: [universeId, id]) + planets Planet[] + starbase Starbase? + @@id([universeId, index]) } @@ -91,11 +97,29 @@ model StarSystemEdges { @@id([universeId, fromSystemId, toSystemId]) } -model Planet { - id String @id @default(uuid()) +model Starbase { + id Int @default(autoincrement()) universeId Int universe Universe @relation(fields: [universeId], references: [id]) + + starSystemId Int + starSystem StarSystem @relation(fields: [universeId, starSystemId], references: [universeId, index]) + + @@id([universeId, id]) + @@unique([universeId, starSystemId]) +} + +model Planet { + id Int @default(autoincrement()) + + universeId Int + universe Universe @relation(fields: [universeId], references: [id]) + + surroundingStarSystemId Int + surroundingStarSystem StarSystem @relation(fields: [universeId, surroundingStarSystemId], references: [universeId, index]) + + @@id([universeId, id]) } model Fleet {