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;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
/// For interacting with the game handler
|
||||||
pub enum GameHandlerMessage {
|
pub enum GameHandlerMessage {
|
||||||
NewGame {
|
NewGame {
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
|
@ -25,15 +26,19 @@ pub enum GameHandlerMessage {
|
||||||
id: String,
|
id: String,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles game stuff
|
||||||
pub struct GameHandler {
|
pub struct GameHandler {
|
||||||
state: Arc<AppState>,
|
state: Arc<AppState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameHandler {
|
impl GameHandler {
|
||||||
|
/// Returns a new game handler
|
||||||
pub fn new(state: Arc<AppState>) -> Self {
|
pub fn new(state: Arc<AppState>) -> Self {
|
||||||
GameHandler { state }
|
GameHandler { state }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles incoming messages
|
||||||
pub async fn handle(&self, message: GameHandlerMessage) {
|
pub async fn handle(&self, message: GameHandlerMessage) {
|
||||||
match message {
|
match message {
|
||||||
NewGame { addr, new_game } => self.new_game(addr, new_game).await,
|
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) {
|
async fn join_game(&self, addr: SocketAddr, id: String) {
|
||||||
// Get pointers
|
// Get pointers
|
||||||
let this_game = self.state.games.read().unwrap().get(&id).unwrap().clone();
|
let this_game = self.state.games.read().unwrap().get(&id).unwrap().clone();
|
||||||
|
@ -98,6 +104,7 @@ impl GameHandler {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new game
|
||||||
async fn new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
|
async fn new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
|
||||||
if new_game.packs.is_empty() {
|
if new_game.packs.is_empty() {
|
||||||
tracing::error!("New game cards are empty!");
|
tracing::error!("New game cards are empty!");
|
||||||
|
@ -205,8 +212,7 @@ pub struct CardPacks {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A white card
|
/// A white card
|
||||||
// TODO: Remove this clone!
|
#[derive(Debug, Deserialize)]
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
|
||||||
struct CardWhite {
|
struct CardWhite {
|
||||||
/// Card text
|
/// Card text
|
||||||
text: String,
|
text: String,
|
||||||
|
@ -215,8 +221,7 @@ struct CardWhite {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A black card
|
/// A black card
|
||||||
// TODO: Remove this clone!
|
#[derive(Debug, Deserialize)]
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
|
||||||
struct CardBlack {
|
struct CardBlack {
|
||||||
/// Card text
|
/// Card text
|
||||||
text: String,
|
text: String,
|
||||||
|
@ -239,7 +244,7 @@ struct CardPack {
|
||||||
black: Option<Vec<CardBlack>>,
|
black: Option<Vec<CardBlack>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// New game request structure
|
/// Internal manifest for making a new game
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct NewGameManifest {
|
struct NewGameManifest {
|
||||||
/// Game name
|
/// Game name
|
||||||
|
@ -250,7 +255,7 @@ struct NewGameManifest {
|
||||||
packs: Vec<u8>,
|
packs: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A struct that represents a player
|
/// A player
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
/// Pointer to user
|
/// Pointer to user
|
||||||
|
@ -261,7 +266,7 @@ pub struct Player {
|
||||||
black: Vec<Arc<CardBlack>>,
|
black: Vec<Arc<CardBlack>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The game master
|
/// The game object
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
/// Game's UUID
|
/// Game's UUID
|
||||||
|
@ -281,13 +286,8 @@ pub struct Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
|
/// Returns a new game object
|
||||||
fn new(state: Arc<AppState>, request: NewGameManifest) -> Self {
|
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
|
// Build the decks
|
||||||
let mut white = vec![];
|
let mut white = vec![];
|
||||||
let mut black = 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());
|
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();
|
white.shrink_to_fit();
|
||||||
black.shrink_to_fit();
|
black.shrink_to_fit();
|
||||||
|
|
||||||
|
// Return game object
|
||||||
Game {
|
Game {
|
||||||
uuid: Uuid::now_v7(),
|
uuid: Uuid::now_v7(),
|
||||||
name: request.name,
|
name: request.name,
|
||||||
|
@ -362,19 +366,19 @@ impl Game {
|
||||||
|
|
||||||
/// Parse json for card data
|
/// Parse json for card data
|
||||||
pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
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
|
// Load in json
|
||||||
// cloning stuff
|
|
||||||
let data: String =
|
let data: String =
|
||||||
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
|
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
|
||||||
let jayson: Vec<CardPack> = serde_json::from_str(&data)
|
let jayson: Vec<CardPack> = serde_json::from_str(&data)
|
||||||
.with_context(|| format!("The contents of \"{path}\" is not valid JSON."))?;
|
.with_context(|| format!("The contents of \"{path}\" is not valid JSON."))?;
|
||||||
|
|
||||||
|
// For global state
|
||||||
let mut official: HashMap<u8, CardSet> = HashMap::new();
|
let mut official: HashMap<u8, CardSet> = HashMap::new();
|
||||||
let mut unofficial: HashMap<u8, CardSet> = HashMap::new();
|
let mut unofficial: HashMap<u8, CardSet> = HashMap::new();
|
||||||
|
|
||||||
let mut official_meta: Vec<CardPackMeta> = vec![];
|
let mut official_meta: Vec<CardPackMeta> = vec![];
|
||||||
let mut unofficial_meta: Vec<CardPackMeta> = vec![];
|
let mut unofficial_meta: Vec<CardPackMeta> = vec![];
|
||||||
|
|
||||||
|
// Unpack the json
|
||||||
for set in jayson {
|
for set in jayson {
|
||||||
let mut num_white = 0;
|
let mut num_white = 0;
|
||||||
let mut num_black = 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
|
// No safe default for this so make it an Option
|
||||||
let mut pack: Option<u8> = Option::None;
|
let mut pack: Option<u8> = Option::None;
|
||||||
|
|
||||||
|
// Process white cards if there are any
|
||||||
if let Some(ref white) = set.white {
|
if let Some(ref white) = set.white {
|
||||||
num_white = white.len();
|
num_white = white.len();
|
||||||
if num_white > 0 {
|
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 {
|
if let Some(ref black) = set.black {
|
||||||
num_black = black.len();
|
num_black = black.len();
|
||||||
if num_black > 0 {
|
if num_black > 0 {
|
||||||
|
@ -426,6 +432,7 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start repackaging
|
||||||
let meta = CardPackMeta {
|
let meta = CardPackMeta {
|
||||||
name: set.name,
|
name: set.name,
|
||||||
pack: pack.expect("No card pack number!"),
|
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();
|
official.shrink_to_fit();
|
||||||
unofficial.shrink_to_fit();
|
unofficial.shrink_to_fit();
|
||||||
|
|
||||||
official_meta.shrink_to_fit();
|
official_meta.shrink_to_fit();
|
||||||
unofficial_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 {
|
let packs = CardPacks {
|
||||||
official,
|
official,
|
||||||
unofficial,
|
unofficial,
|
||||||
};
|
};
|
||||||
|
|
||||||
let packs_meta = CardPacksMeta {
|
let packs_meta = CardPacksMeta {
|
||||||
official_meta,
|
official_meta,
|
||||||
unofficial_meta,
|
unofficial_meta,
|
||||||
|
|
Loading…
Add table
Reference in a new issue