Multiplayer #1

Merged
joel merged 19 commits from multiplayer into main 2024-12-26 18:38:24 +00:00
3 changed files with 26 additions and 9 deletions
Showing only changes of commit 02e24c0803 - Show all commits

4
server/config.json Normal file
View file

@ -0,0 +1,4 @@
{
"dictionary_path": "../resources/dictionary.csv",
"static_path": "../ui/dist"
}

View file

@ -2,16 +2,12 @@ use rocket::serde::{Deserialize, Serialize};
use rocket::tokio::sync::broadcast::Sender; use rocket::tokio::sync::broadcast::Sender;
use rocket::tokio::sync::RwLock; use rocket::tokio::sync::RwLock;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{LazyLock, Weak}; use std::sync::Weak;
use word_grid::api::{APIGame, ApiState, Update}; use word_grid::api::{APIGame, ApiState, Update};
use word_grid::dictionary::{Dictionary, DictionaryImpl};
use word_grid::game::{Error, PlayedTile}; use word_grid::game::{Error, PlayedTile};
use word_grid::player_interaction::ai::Difficulty; use word_grid::player_interaction::ai::Difficulty;
use ws::frame::{CloseCode, CloseFrame}; use ws::frame::{CloseCode, CloseFrame};
pub static DICTIONARY: LazyLock<DictionaryImpl> =
LazyLock::new(|| DictionaryImpl::create_from_path("../resources/dictionary.csv"));
pub fn escape_characters(x: &str) -> String { pub fn escape_characters(x: &str) -> String {
let x = x let x = x
.replace("&", "&amp;") .replace("&", "&amp;")

View file

@ -6,18 +6,21 @@ use rand::prelude::SmallRng;
use rand::SeedableRng; use rand::SeedableRng;
use rocket::fs::FileServer; use rocket::fs::FileServer;
use rocket::futures::{SinkExt, StreamExt}; use rocket::futures::{SinkExt, StreamExt};
use rocket::serde::Deserialize;
use rocket::tokio::sync::broadcast::Sender; use rocket::tokio::sync::broadcast::Sender;
use rocket::tokio::sync::{Mutex, RwLock}; use rocket::tokio::sync::{Mutex, RwLock};
use rocket::tokio::time::interval; use rocket::tokio::time::interval;
use rocket::{tokio, State}; use rocket::{tokio, State};
use server::{ use server::{
escape_characters, reject_websocket_with_reason, ClientToServerMessage, GameEvent, GameMove, 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 std::time::Duration;
use tokio::select; use tokio::select;
use word_grid::api::{APIGame, Update}; use word_grid::api::{APIGame, Update};
use word_grid::dictionary::{Dictionary, DictionaryImpl};
use word_grid::game::Game; use word_grid::game::Game;
use ws::frame::CloseCode; use ws::frame::CloseCode;
use ws::stream::DuplexStream; use ws::stream::DuplexStream;
@ -88,7 +91,7 @@ async fn incoming_message_handler<E: std::fmt::Display>(
); );
} else { } else {
let rng = SmallRng::from_entropy(); let rng = SmallRng::from_entropy();
let dictionary = DICTIONARY.clone(); let dictionary = DICTIONARY.get().unwrap().clone();
let player_names = room let player_names = room
.party_info .party_info
.players .players
@ -393,10 +396,24 @@ async fn room(
}) })
} }
#[derive(Debug, Deserialize)]
struct Config {
dictionary_path: String,
static_path: String,
}
pub static DICTIONARY: OnceLock<DictionaryImpl> = OnceLock::new();
#[launch] #[launch]
fn rocket() -> _ { 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() rocket::build()
.manage(Mutex::new(RoomMap::new())) .manage(Mutex::new(RoomMap::new()))
.mount("/", FileServer::from("../ui/dist")) .mount("/", FileServer::from(config.static_path))
.mount("/", routes![room]) .mount("/", routes![room])
} }