From 11dad98c7bdd0c0d9da4f57ee8be1c636ccbe2f2 Mon Sep 17 00:00:00 2001 From: xeon Date: Fri, 29 Mar 2024 14:50:06 +0300 Subject: [PATCH] Gameserver: use globals.json instead of .env --- gameserver/.env | 1 - gameserver/globals.json | 3 +++ gameserver/src/game/global_config.rs | 35 +++++++++++++++++++++++++++ gameserver/src/game/globals.rs | 16 ------------ gameserver/src/game/mod.rs | 3 ++- gameserver/src/main.rs | 26 -------------------- gameserver/src/net/handlers/battle.rs | 3 ++- gameserver/src/net/handlers/lineup.rs | 6 +++-- 8 files changed, 46 insertions(+), 47 deletions(-) delete mode 100644 gameserver/.env create mode 100644 gameserver/globals.json create mode 100644 gameserver/src/game/global_config.rs delete mode 100644 gameserver/src/game/globals.rs diff --git a/gameserver/.env b/gameserver/.env deleted file mode 100644 index bf40ee2..0000000 --- a/gameserver/.env +++ /dev/null @@ -1 +0,0 @@ -LINEUP=1309,1308,1307,1315 \ No newline at end of file diff --git a/gameserver/globals.json b/gameserver/globals.json new file mode 100644 index 0000000..ac97cea --- /dev/null +++ b/gameserver/globals.json @@ -0,0 +1,3 @@ +{ + "lineup": [1309, 1308, 1307, 1315] +} diff --git a/gameserver/src/game/global_config.rs b/gameserver/src/game/global_config.rs new file mode 100644 index 0000000..50b8713 --- /dev/null +++ b/gameserver/src/game/global_config.rs @@ -0,0 +1,35 @@ +use lazy_static::lazy_static; +use serde::Deserialize; +use serde_json::from_str; + +const DEFAULT_GLOBALS: &str = include_str!("../../globals.json"); + +lazy_static! { + pub static ref INSTANCE: Globals = { + let local_config = std::path::Path::new("globals.json"); + let data = if local_config.exists() { + std::fs::read_to_string("globals.json").unwrap() + } else { + let config = dirs::config_dir() + .expect("No config directory found") + .join("hkrpg-gameserver"); + + std::fs::create_dir_all(&config).unwrap(); + + let env = config.join("globals.json"); + + if !env.exists() { + std::fs::write(&env, DEFAULT_GLOBALS).unwrap(); + } + + DEFAULT_GLOBALS.to_string() + }; + + from_str(&data).unwrap() + }; +} + +#[derive(Deserialize)] +pub struct Globals { + pub lineup: Vec, +} diff --git a/gameserver/src/game/globals.rs b/gameserver/src/game/globals.rs deleted file mode 100644 index bf642a0..0000000 --- a/gameserver/src/game/globals.rs +++ /dev/null @@ -1,16 +0,0 @@ -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 index 068bedc..685bf55 100644 --- a/gameserver/src/game/mod.rs +++ b/gameserver/src/game/mod.rs @@ -1 +1,2 @@ -pub mod globals; +mod global_config; +pub use global_config::INSTANCE as globals; diff --git a/gameserver/src/main.rs b/gameserver/src/main.rs index f361915..b8fb9fc 100644 --- a/gameserver/src/main.rs +++ b/gameserver/src/main.rs @@ -7,36 +7,10 @@ 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 4729265..8bc8fab 100644 --- a/gameserver/src/net/handlers/battle.rs +++ b/gameserver/src/net/handlers/battle.rs @@ -14,7 +14,8 @@ pub async fn on_start_cocoon_stage_cs_req( stage_id: 201012311, logic_random_seed: 4444, battle_id: 1, - battle_avatar_list: globals::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 e2c8251..c397089 100644 --- a/gameserver/src/net/handlers/lineup.rs +++ b/gameserver/src/net/handlers/lineup.rs @@ -15,7 +15,8 @@ pub async fn on_get_all_lineup_data_cs_req( plane_id: 10001, name: String::from("Lineup 1"), index: 0, - avatar_list: globals::LINEUP + avatar_list: globals + .lineup .iter() .enumerate() .map(|(idx, id)| LineupAvatar { @@ -53,7 +54,8 @@ pub async fn on_get_cur_lineup_data_cs_req( leader_slot: 0, mp: 5, mp_max: 5, - avatar_list: globals::LINEUP + avatar_list: globals + .lineup .iter() .enumerate() .map(|(idx, id)| LineupAvatar {