use std::{error::Error, sync::OnceLock};

use common::log;
use gameserver_service::GameserverService;

mod ability;
mod config;
mod creature;
mod entity;
mod gadget;
mod gameserver_service;
mod handler;
mod item;
mod network;
mod player;
mod scene;

static SERVICE: OnceLock<GameserverService> = OnceLock::new();

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    log::init_tracing();
    data::load_txt_data()?;
    data::load_all_config_avatar()?;
    let config = config::load().unwrap();
    let service = GameserverService::new(config).await?;
    let service = SERVICE.get_or_init(|| service);
    service.run().await;

    Ok(())
}

fn gameserver_service() -> &'static GameserverService {
    SERVICE.get().unwrap()
}
