From daf4507b34f8ca01e33317888245781b5ebddd9f Mon Sep 17 00:00:00 2001 From: xeon Date: Fri, 29 Mar 2024 13:55:08 +0300 Subject: [PATCH] Gameserver: lineup configuration with .env --- gameserver/.env | 1 + gameserver/Cargo.toml | 2 ++ gameserver/src/game/globals.rs | 16 ++++++++++++++++ gameserver/src/game/mod.rs | 1 + gameserver/src/main.rs | 27 +++++++++++++++++++++++++++ gameserver/src/net/handlers/battle.rs | 5 ++--- gameserver/src/net/handlers/lineup.rs | 7 +++---- 7 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 gameserver/.env create mode 100644 gameserver/src/game/globals.rs create mode 100644 gameserver/src/game/mod.rs diff --git a/gameserver/.env b/gameserver/.env new file mode 100644 index 0000000..bf40ee2 --- /dev/null +++ b/gameserver/.env @@ -0,0 +1 @@ +LINEUP=1309,1308,1307,1315 \ No newline at end of file diff --git a/gameserver/Cargo.toml b/gameserver/Cargo.toml index ab61073..df9b42d 100644 --- a/gameserver/Cargo.toml +++ b/gameserver/Cargo.toml @@ -7,6 +7,8 @@ version.workspace = true ansi_term.workspace = true anyhow.workspace = true atomic_refcell.workspace = true +dirs.workspace = true +dotenv.workspace = true env_logger.workspace = true hex.workspace = true lazy_static.workspace = true diff --git a/gameserver/src/game/globals.rs b/gameserver/src/game/globals.rs new file mode 100644 index 0000000..bf642a0 --- /dev/null +++ b/gameserver/src/game/globals.rs @@ -0,0 +1,16 @@ +use std::env; + +use lazy_static::lazy_static; + +lazy_static! { + pub static ref LINEUP: Vec = { + let lineup_str = env::var("LINEUP").expect("Missing .env parameter: LINEUP"); + let mut lineup = Vec::new(); + + for s in lineup_str.trim().replace(" ", "").split(",") { + lineup.push(s.parse().unwrap()); + } + + lineup + }; +} diff --git a/gameserver/src/game/mod.rs b/gameserver/src/game/mod.rs new file mode 100644 index 0000000..068bedc --- /dev/null +++ b/gameserver/src/game/mod.rs @@ -0,0 +1 @@ +pub mod globals; diff --git a/gameserver/src/main.rs b/gameserver/src/main.rs index 4e7e4c3..f361915 100644 --- a/gameserver/src/main.rs +++ b/gameserver/src/main.rs @@ -1,15 +1,42 @@ use anyhow::Result; +mod game; mod logging; mod net; mod util; use logging::init_tracing; +const DEFAULT_DOTENV: &str = include_str!("../.env"); + #[tokio::main] async fn main() -> Result<()> { init_tracing(); + init_config()?; + net::gateway::listen("0.0.0.0", 23301).await?; + Ok(()) +} + +fn init_config() -> Result<()> { + let local_dotenv = std::path::Path::new(".env"); + if local_dotenv.exists() { + dotenv::dotenv()?; + } else { + let config = dirs::config_dir() + .ok_or_else(|| anyhow::anyhow!("No config directory found"))? + .join("hkrpg-gameserver"); + + std::fs::create_dir_all(&config)?; + + let env = config.join(".env"); + + if !env.exists() { + std::fs::write(&env, DEFAULT_DOTENV)?; + } + + dotenv::from_path(&env)?; + } Ok(()) } diff --git a/gameserver/src/net/handlers/battle.rs b/gameserver/src/net/handlers/battle.rs index 82bd8cb..4729265 100644 --- a/gameserver/src/net/handlers/battle.rs +++ b/gameserver/src/net/handlers/battle.rs @@ -1,6 +1,5 @@ use super::*; - -static BATTLE_LINEUP: [u32; 4] = [1309, 1308, 1307, 1315]; +use crate::game::globals; pub async fn on_start_cocoon_stage_cs_req( session: &mut PlayerSession, @@ -15,7 +14,7 @@ pub async fn on_start_cocoon_stage_cs_req( stage_id: 201012311, logic_random_seed: 4444, battle_id: 1, - battle_avatar_list: BATTLE_LINEUP + battle_avatar_list: globals::LINEUP .iter() .enumerate() .map(|(idx, id)| BattleAvatar { diff --git a/gameserver/src/net/handlers/lineup.rs b/gameserver/src/net/handlers/lineup.rs index 78cd99f..bc9d66f 100644 --- a/gameserver/src/net/handlers/lineup.rs +++ b/gameserver/src/net/handlers/lineup.rs @@ -1,6 +1,5 @@ use super::*; - -static STARTING_LINEUP: [u32; 4] = [1309, 1308, 1307, 1315]; +use crate::game::globals; pub async fn on_get_all_lineup_data_cs_req( session: &mut PlayerSession, @@ -16,7 +15,7 @@ pub async fn on_get_all_lineup_data_cs_req( plane_id: 10001, name: String::from("Lineup 1"), index: 0, - avatar_list: STARTING_LINEUP + avatar_list: globals::LINEUP .iter() .enumerate() .map(|(idx, id)| LineupAvatar { @@ -51,7 +50,7 @@ pub async fn on_get_cur_lineup_data_cs_req( plane_id: 10001, name: String::from("Lineup 1"), index: 0, - avatar_list: STARTING_LINEUP + avatar_list: globals::LINEUP .iter() .enumerate() .map(|(idx, id)| LineupAvatar {