Gameserver: use globals.json instead of .env

This commit is contained in:
xeon 2024-03-29 14:50:06 +03:00
parent be3ce8369a
commit 11dad98c7b
8 changed files with 46 additions and 47 deletions

View file

@ -1 +0,0 @@
LINEUP=1309,1308,1307,1315

3
gameserver/globals.json Normal file
View file

@ -0,0 +1,3 @@
{
"lineup": [1309, 1308, 1307, 1315]
}

View file

@ -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<u32>,
}

View file

@ -1,16 +0,0 @@
use std::env;
use lazy_static::lazy_static;
lazy_static! {
pub static ref LINEUP: Vec<u32> = {
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
};
}

View file

@ -1 +1,2 @@
pub mod globals; mod global_config;
pub use global_config::INSTANCE as globals;

View file

@ -7,36 +7,10 @@ mod util;
use logging::init_tracing; use logging::init_tracing;
const DEFAULT_DOTENV: &str = include_str!("../.env");
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
init_tracing(); init_tracing();
init_config()?;
net::gateway::listen("0.0.0.0", 23301).await?; 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(()) Ok(())
} }

View file

@ -14,7 +14,8 @@ pub async fn on_start_cocoon_stage_cs_req(
stage_id: 201012311, stage_id: 201012311,
logic_random_seed: 4444, logic_random_seed: 4444,
battle_id: 1, battle_id: 1,
battle_avatar_list: globals::LINEUP battle_avatar_list: globals
.lineup
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, id)| BattleAvatar { .map(|(idx, id)| BattleAvatar {

View file

@ -15,7 +15,8 @@ pub async fn on_get_all_lineup_data_cs_req(
plane_id: 10001, plane_id: 10001,
name: String::from("Lineup 1"), name: String::from("Lineup 1"),
index: 0, index: 0,
avatar_list: globals::LINEUP avatar_list: globals
.lineup
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, id)| LineupAvatar { .map(|(idx, id)| LineupAvatar {
@ -53,7 +54,8 @@ pub async fn on_get_cur_lineup_data_cs_req(
leader_slot: 0, leader_slot: 0,
mp: 5, mp: 5,
mp_max: 5, mp_max: 5,
avatar_list: globals::LINEUP avatar_list: globals
.lineup
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, id)| LineupAvatar { .map(|(idx, id)| LineupAvatar {