cleanup
This commit is contained in:
parent
87f814b7d8
commit
1e5ef3b801
1 changed files with 25 additions and 24 deletions
|
@ -15,6 +15,7 @@ use std::{
|
|||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// For interacting with the game handler
|
||||
pub enum GameHandlerMessage {
|
||||
NewGame {
|
||||
addr: SocketAddr,
|
||||
|
@ -25,15 +26,19 @@ pub enum GameHandlerMessage {
|
|||
id: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Handles game stuff
|
||||
pub struct GameHandler {
|
||||
state: Arc<AppState>,
|
||||
}
|
||||
|
||||
impl GameHandler {
|
||||
/// Returns a new game handler
|
||||
pub fn new(state: Arc<AppState>) -> Self {
|
||||
GameHandler { state }
|
||||
}
|
||||
|
||||
/// Handles incoming messages
|
||||
pub async fn handle(&self, message: GameHandlerMessage) {
|
||||
match message {
|
||||
NewGame { addr, new_game } => self.new_game(addr, new_game).await,
|
||||
|
@ -41,6 +46,7 @@ impl GameHandler {
|
|||
}
|
||||
}
|
||||
|
||||
/// Puts a user in a game
|
||||
async fn join_game(&self, addr: SocketAddr, id: String) {
|
||||
// Get pointers
|
||||
let this_game = self.state.games.read().unwrap().get(&id).unwrap().clone();
|
||||
|
@ -98,6 +104,7 @@ impl GameHandler {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
/// Creates a new game
|
||||
async fn new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
|
||||
if new_game.packs.is_empty() {
|
||||
tracing::error!("New game cards are empty!");
|
||||
|
@ -205,8 +212,7 @@ pub struct CardPacks {
|
|||
}
|
||||
|
||||
/// A white card
|
||||
// TODO: Remove this clone!
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CardWhite {
|
||||
/// Card text
|
||||
text: String,
|
||||
|
@ -215,8 +221,7 @@ struct CardWhite {
|
|||
}
|
||||
|
||||
/// A black card
|
||||
// TODO: Remove this clone!
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CardBlack {
|
||||
/// Card text
|
||||
text: String,
|
||||
|
@ -239,7 +244,7 @@ struct CardPack {
|
|||
black: Option<Vec<CardBlack>>,
|
||||
}
|
||||
|
||||
/// New game request structure
|
||||
/// Internal manifest for making a new game
|
||||
#[derive(Debug)]
|
||||
struct NewGameManifest {
|
||||
/// Game name
|
||||
|
@ -250,7 +255,7 @@ struct NewGameManifest {
|
|||
packs: Vec<u8>,
|
||||
}
|
||||
|
||||
/// A struct that represents a player
|
||||
/// A player
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
/// Pointer to user
|
||||
|
@ -261,7 +266,7 @@ pub struct Player {
|
|||
black: Vec<Arc<CardBlack>>,
|
||||
}
|
||||
|
||||
/// The game master
|
||||
/// The game object
|
||||
#[derive(Debug)]
|
||||
pub struct Game {
|
||||
/// Game's UUID
|
||||
|
@ -281,13 +286,8 @@ pub struct Game {
|
|||
}
|
||||
|
||||
impl Game {
|
||||
/// Returns a new game object
|
||||
fn new(state: Arc<AppState>, request: NewGameManifest) -> Self {
|
||||
tracing::debug!(
|
||||
"Creating game {} with {} as host",
|
||||
&request.name,
|
||||
&request.host.read().unwrap().name
|
||||
);
|
||||
|
||||
// Build the decks
|
||||
let mut white = vec![];
|
||||
let mut black = vec![];
|
||||
|
@ -309,10 +309,14 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
// Draw first black card
|
||||
let current_black = black.swap_remove((0..black.len()).choose(&mut thread_rng()).unwrap());
|
||||
|
||||
// These are at the largest size they should ever be
|
||||
white.shrink_to_fit();
|
||||
black.shrink_to_fit();
|
||||
|
||||
// Return game object
|
||||
Game {
|
||||
uuid: Uuid::now_v7(),
|
||||
name: request.name,
|
||||
|
@ -362,19 +366,19 @@ impl Game {
|
|||
|
||||
/// Parse json for card data
|
||||
pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
||||
// TODO: Repack these cards so every card is stored once and pointers are passed around instead of
|
||||
// cloning stuff
|
||||
// Load in json
|
||||
let data: String =
|
||||
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
|
||||
let jayson: Vec<CardPack> = serde_json::from_str(&data)
|
||||
.with_context(|| format!("The contents of \"{path}\" is not valid JSON."))?;
|
||||
|
||||
// For global state
|
||||
let mut official: HashMap<u8, CardSet> = HashMap::new();
|
||||
let mut unofficial: HashMap<u8, CardSet> = HashMap::new();
|
||||
|
||||
let mut official_meta: Vec<CardPackMeta> = vec![];
|
||||
let mut unofficial_meta: Vec<CardPackMeta> = vec![];
|
||||
|
||||
// Unpack the json
|
||||
for set in jayson {
|
||||
let mut num_white = 0;
|
||||
let mut num_black = 0;
|
||||
|
@ -387,6 +391,7 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
|||
// No safe default for this so make it an Option
|
||||
let mut pack: Option<u8> = Option::None;
|
||||
|
||||
// Process white cards if there are any
|
||||
if let Some(ref white) = set.white {
|
||||
num_white = white.len();
|
||||
if num_white > 0 {
|
||||
|
@ -406,6 +411,7 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
|||
}
|
||||
}
|
||||
|
||||
// Process black cards if there are any
|
||||
if let Some(ref black) = set.black {
|
||||
num_black = black.len();
|
||||
if num_black > 0 {
|
||||
|
@ -426,6 +432,7 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
|||
}
|
||||
}
|
||||
|
||||
// Start repackaging
|
||||
let meta = CardPackMeta {
|
||||
name: set.name,
|
||||
pack: pack.expect("No card pack number!"),
|
||||
|
@ -442,24 +449,18 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
|||
}
|
||||
}
|
||||
|
||||
// These are now the largest size they should ever be
|
||||
official.shrink_to_fit();
|
||||
unofficial.shrink_to_fit();
|
||||
|
||||
official_meta.shrink_to_fit();
|
||||
unofficial_meta.shrink_to_fit();
|
||||
|
||||
tracing::debug!("{} official", official.len());
|
||||
tracing::debug!("{} official meta", official_meta.len());
|
||||
tracing::debug!("{} unofficial", unofficial.len());
|
||||
tracing::debug!("{} unofficial meta", unofficial_meta.len());
|
||||
tracing::debug!("{:#?}", official_meta[0]);
|
||||
tracing::debug!("{:#?}", unofficial_meta[0]);
|
||||
|
||||
// Package for export
|
||||
let packs = CardPacks {
|
||||
official,
|
||||
unofficial,
|
||||
};
|
||||
|
||||
let packs_meta = CardPacksMeta {
|
||||
official_meta,
|
||||
unofficial_meta,
|
||||
|
|
Loading…
Add table
Reference in a new issue