From 7cb4028af835d341ea62ac68dc3c8115af881878 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 30 Dec 2023 04:21:21 -0500 Subject: [PATCH] generate empire names --- Cargo.lock | 109 ++++++++++++++++++++++++++++++-- backend/Cargo.toml | 1 + backend/src/generate.rs | 16 ++++- backend/src/state/empire.rs | 8 ++- backend/src/state/game.rs | 36 ++++++----- frontend/src/routes/MapView.tsx | 2 +- prisma/schema.prisma | 4 +- 7 files changed, 150 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd6c53c..e90b9ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,6 +185,17 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -251,7 +262,8 @@ dependencies = [ "anyhow", "axum", "chrono", - "clap", + "clap 4.4.11", + "names", "petgraph 0.6.4", "prisma-client-rust", "serde", @@ -569,6 +581,23 @@ dependencies = [ "libloading", ] +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "atty", + "bitflags 1.3.2", + "clap_derive 3.2.25", + "clap_lex 0.2.4", + "indexmap 1.9.3", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + [[package]] name = "clap" version = "4.4.11" @@ -576,7 +605,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2" dependencies = [ "clap_builder", - "clap_derive", + "clap_derive 4.4.7", ] [[package]] @@ -587,10 +616,23 @@ checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb" dependencies = [ "anstream", "anstyle", - "clap_lex", + "clap_lex 0.6.0", "strsim", ] +[[package]] +name = "clap_derive" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008" +dependencies = [ + "heck 0.4.1", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "clap_derive" version = "4.4.7" @@ -603,6 +645,15 @@ dependencies = [ "syn 2.0.43", ] +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "clap_lex" version = "0.6.0" @@ -1591,6 +1642,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.3" @@ -2599,6 +2659,16 @@ dependencies = [ "uuid", ] +[[package]] +name = "names" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bddcd3bf5144b6392de80e04c347cd7fab2508f6df16a85fc496ecd5cec39bc" +dependencies = [ + "clap 3.2.25", + "rand 0.8.5", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -2728,7 +2798,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.3", "libc", ] @@ -2856,6 +2926,12 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063" +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + [[package]] name = "overload" version = "0.1.1" @@ -3290,6 +3366,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2", "quote", + "syn 1.0.109", "version_check", ] @@ -4538,6 +4615,21 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "termcolor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" + [[package]] name = "thiserror" version = "1.0.52" @@ -5290,6 +5382,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 62f1691..0673470 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -15,3 +15,4 @@ serde = { version = "1.0.193", features = ["derive"] } tokio = { version = "1.35.1", features = ["full"] } serde_json = "1.0.108" petgraph = "0.6.4" +names = "0.14.0" diff --git a/backend/src/generate.rs b/backend/src/generate.rs index 766dc2c..642c437 100644 --- a/backend/src/generate.rs +++ b/backend/src/generate.rs @@ -2,12 +2,16 @@ use std::{collections::HashMap, f64::consts::PI}; use anyhow::Result; use chrono::Utc; +use names::Generator as NameGenerator; use petgraph::{graph::UnGraph, graphmap::UnGraphMap}; use triangle::{triangulate, Point, TrianglulateOpts}; use crate::{ prisma::PrismaClient, - state::{GameCoreState, StarSystemCoreState, StarSystemId, UniverseId}, + state::{ + EmpireCoreState, EmpireId, GameCoreState, StarSystemCoreState, + StarSystemId, UniverseId, + }, }; pub async fn generate_initial_game_state( @@ -37,7 +41,14 @@ pub async fn generate_initial_game_state( // Generate a new universe ID let universe = client.universe().create(json!({}), vec![]).exec().await?; - let empires = HashMap::new(); + // Generate some empires + let mut empires = HashMap::new(); + let name_generator = NameGenerator::default(); + for (idx, name) in name_generator.take(10).enumerate() { + let id = EmpireId(idx as i32); + let empire = EmpireCoreState { id, name }; + empires.insert(id, empire); + } // Format the star systems let mut star_systems = HashMap::new(); @@ -50,6 +61,7 @@ pub async fn generate_initial_game_state( star_systems.insert(id, star_system); } + // Format the hyperlanes into petgraph let mut hyperlanes = UnGraphMap::default(); for indexes in result.triangle_list.into_iter() { hyperlanes.add_edge(indexes[0], indexes[1], ()); diff --git a/backend/src/state/empire.rs b/backend/src/state/empire.rs index a879d72..918eddb 100644 --- a/backend/src/state/empire.rs +++ b/backend/src/state/empire.rs @@ -1,5 +1,9 @@ -pub struct EmpireId(u32); +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct EmpireId(pub i32); -pub struct EmpireCoreState {} +pub struct EmpireCoreState { + pub id: EmpireId, + pub name: String, +} pub struct EmpireDerivedState {} diff --git a/backend/src/state/game.rs b/backend/src/state/game.rs index 978773d..e225afb 100644 --- a/backend/src/state/game.rs +++ b/backend/src/state/game.rs @@ -4,7 +4,7 @@ use anyhow::Result; use chrono::{DateTime, Utc}; use petgraph::{graph::UnGraph, graphmap::UnGraphMap}; -use crate::prisma::{star_system, star_system_edges, PrismaClient}; +use crate::prisma::{empire, star_system, star_system_edges, PrismaClient}; use super::{EmpireCoreState, EmpireId, StarSystemCoreState, StarSystemId}; @@ -25,7 +25,6 @@ pub struct GameDerivedState<'a> { impl GameCoreState { pub async fn save_to_database(&self, client: &PrismaClient) -> Result<()> { - println!("Self {:?} {:?}", self.universe_id, self.star_systems); // Save star systems to database client .star_system() @@ -49,19 +48,6 @@ impl GameCoreState { .await?; // Insert edges into database - macro_rules! create_edge { - ($from:expr, $to:expr) => {{ - println!("WTF? {}, {}", $from, $to); - star_system_edges::create_unchecked( - universe.id.clone(), - star_system_map.get(&$from).unwrap().to_owned(), - $from as i32, - star_system_map.get(&$to).unwrap().to_owned(), - $to as i32, - vec![], - ) - }}; - } client .star_system_edges() .create_many( @@ -82,6 +68,26 @@ impl GameCoreState { .exec() .await?; + // Save empires to database + client + .empire() + .create_many( + self + .empires + .values() + .map(|empire| { + empire::create_unchecked( + self.universe_id.0, + empire.name.clone(), + vec![empire::id::set(empire.id.0)], + ) + }) + .collect(), + ) + .skip_duplicates() + .exec() + .await?; + Ok(()) } } diff --git a/frontend/src/routes/MapView.tsx b/frontend/src/routes/MapView.tsx index b1ec577..95a9c80 100644 --- a/frontend/src/routes/MapView.tsx +++ b/frontend/src/routes/MapView.tsx @@ -34,7 +34,7 @@ export default function MapView({}) { const x = point.coordX / 80; const y = point.coordY / 80; return ( - + ); })} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3938892..65afa2d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -42,13 +42,13 @@ model Player { } model Empire { - id String @id @default(uuid()) + id Int @id @default(autoincrement()) universeId Int universe Universe @relation(fields: [universeId], references: [id]) name String - color String + color String? playerId String? @unique player Player? @relation(fields: [playerId], references: [id])