Gameserver: lineup configuration with .env

This commit is contained in:
xeon 2024-03-29 13:55:08 +03:00
parent c132f0d2ee
commit daf4507b34
7 changed files with 52 additions and 7 deletions

1
gameserver/.env Normal file
View file

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

View file

@ -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

View file

@ -0,0 +1,16 @@
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

@ -0,0 +1 @@
pub mod globals;

View file

@ -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(())
}

View file

@ -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 {

View file

@ -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 {