diff --git a/server/config.json b/server/config.json new file mode 100644 index 0000000..393d76b --- /dev/null +++ b/server/config.json @@ -0,0 +1,4 @@ +{ + "dictionary_path": "../resources/dictionary.csv", + "static_path": "../ui/dist" +} diff --git a/server/src/lib.rs b/server/src/lib.rs index 08c855f..1baa1d3 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -2,16 +2,12 @@ use rocket::serde::{Deserialize, Serialize}; use rocket::tokio::sync::broadcast::Sender; use rocket::tokio::sync::RwLock; use std::collections::HashMap; -use std::sync::{LazyLock, Weak}; +use std::sync::Weak; use word_grid::api::{APIGame, ApiState, Update}; -use word_grid::dictionary::{Dictionary, DictionaryImpl}; use word_grid::game::{Error, PlayedTile}; use word_grid::player_interaction::ai::Difficulty; use ws::frame::{CloseCode, CloseFrame}; -pub static DICTIONARY: LazyLock = - LazyLock::new(|| DictionaryImpl::create_from_path("../resources/dictionary.csv")); - pub fn escape_characters(x: &str) -> String { let x = x .replace("&", "&") diff --git a/server/src/main.rs b/server/src/main.rs index 9c8654c..64636e8 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -6,18 +6,21 @@ use rand::prelude::SmallRng; use rand::SeedableRng; use rocket::fs::FileServer; use rocket::futures::{SinkExt, StreamExt}; +use rocket::serde::Deserialize; use rocket::tokio::sync::broadcast::Sender; use rocket::tokio::sync::{Mutex, RwLock}; use rocket::tokio::time::interval; use rocket::{tokio, State}; use server::{ escape_characters, reject_websocket_with_reason, ClientToServerMessage, GameEvent, GameMove, - InnerRoomMessage, Player, Room, RoomEvent, RoomMap, ServerToClientMessage, DICTIONARY, + InnerRoomMessage, Player, Room, RoomEvent, RoomMap, ServerToClientMessage, }; -use std::sync::Arc; +use std::fs; +use std::sync::{Arc, OnceLock}; use std::time::Duration; use tokio::select; use word_grid::api::{APIGame, Update}; +use word_grid::dictionary::{Dictionary, DictionaryImpl}; use word_grid::game::Game; use ws::frame::CloseCode; use ws::stream::DuplexStream; @@ -88,7 +91,7 @@ async fn incoming_message_handler( ); } else { let rng = SmallRng::from_entropy(); - let dictionary = DICTIONARY.clone(); + let dictionary = DICTIONARY.get().unwrap().clone(); let player_names = room .party_info .players @@ -393,10 +396,24 @@ async fn room( }) } +#[derive(Debug, Deserialize)] +struct Config { + dictionary_path: String, + static_path: String, +} + +pub static DICTIONARY: OnceLock = OnceLock::new(); + #[launch] fn rocket() -> _ { + let config_str = fs::read_to_string("config.json").unwrap(); + let config: Config = serde_json::from_str(&config_str).unwrap(); + + let dictionary = DictionaryImpl::create_from_path(&config.dictionary_path); + DICTIONARY.set(dictionary).unwrap(); + rocket::build() .manage(Mutex::new(RoomMap::new())) - .mount("/", FileServer::from("../ui/dist")) + .mount("/", FileServer::from(config.static_path)) .mount("/", routes![room]) }