stop being a dumbass

This commit is contained in:
Adam 2024-08-17 15:57:18 -04:00
parent 3f4ceb3fa0
commit 788bf5b00e
3 changed files with 69 additions and 50 deletions

4
Cargo.lock generated
View file

@ -2098,9 +2098,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.39.2"
version = "1.39.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1"
checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5"
dependencies = [
"backtrace",
"bytes",

View file

@ -45,7 +45,7 @@ pub fn Websocket() -> impl IntoView {
open,
close,
..
} = use_websocket::<String, FromToStringCodec>("ws://0.0.0.0:3030/websocket");
} = use_websocket::<String, FromToStringCodec>("ws://10.0.0.107:3030/websocket");
provide_context(WebSocketContext::new(
ready_state,

View file

@ -131,9 +131,13 @@ impl GameHandler {
host: host.clone(),
packs: new_game.packs,
};
tracing::debug!("{:#?}", host.clone().read().unwrap().uuid);
// Create game using manifest
let new_game_object = Game::new(self.state.clone(), manifest);
let mut new_game_object = Game::new(self.state.clone(), manifest);
// Don't forget to create the host player!!!
new_game_object.create_player(host.clone());
// Create update for user's game view
let meta = GameStateMeta {
@ -201,8 +205,8 @@ impl GameHandler {
/// Card Set
#[derive(Debug)]
struct CardSet {
white: Option<Vec<Arc<CardWhite>>>,
black: Option<Vec<Arc<CardBlack>>>,
white: Option<Vec<Arc<CardWhiteWithID>>>,
black: Option<Vec<Arc<CardBlackWithID>>>,
}
/// Card Packs
@ -212,18 +216,18 @@ pub struct CardPacks {
unofficial: HashMap<u8, CardSet>,
}
/// A white card
/// A raw white card as it exists in the source json
#[derive(Debug, Deserialize)]
struct CardWhite {
struct CardWhiteFromJSON {
/// Card text
text: String,
/// ID of the pack it came from
pack: u8,
}
/// A black card
/// A raw black card as it exists in the source json
#[derive(Debug, Deserialize)]
struct CardBlack {
struct CardBlackFromJSON {
/// Card text
text: String,
/// Amount of cards to submit for judging
@ -232,6 +236,26 @@ struct CardBlack {
pack: u8,
}
/// A processed white card for use server-side
#[derive(Debug)]
struct CardWhiteWithID {
/// Unique identifier
uuid: Uuid,
/// Card text
text: String,
}
/// A processed black card for use server-side
#[derive(Debug)]
struct CardBlackWithID {
/// Unique identifier
uuid: Uuid,
/// Card text
text: String,
/// Amount of cards to submit for judging
pick: u8,
}
/// A card pack
#[derive(Debug, Deserialize)]
struct CardPack {
@ -240,9 +264,9 @@ struct CardPack {
/// Whether or not this is an official card pack
official: bool,
/// White card data
white: Option<Vec<CardWhite>>,
white: Option<Vec<CardWhiteFromJSON>>,
/// Black card data
black: Option<Vec<CardBlack>>,
black: Option<Vec<CardBlackFromJSON>>,
}
/// Internal manifest for making a new game
@ -262,9 +286,9 @@ pub struct Player {
/// Pointer to user
user: Arc<RwLock<User>>,
/// The player's hand
white: Vec<Arc<CardWhite>>,
white: Vec<Arc<CardWhiteWithID>>,
/// The player's wins
black: Vec<Arc<CardBlack>>,
black: Vec<Arc<CardBlackWithID>>,
}
/// The game object
@ -277,12 +301,12 @@ pub struct Game {
/// The host user of the game
pub host: Arc<RwLock<User>>,
/// White draw pile
white: Vec<Arc<CardWhite>>,
pub white: Vec<Arc<CardWhiteWithID>>,
/// Black draw pile
black: Vec<Arc<CardBlack>>,
black: Vec<Arc<CardBlackWithID>>,
pub players: HashMap<Uuid, Player>,
/// Black card for the current round
current_black: Arc<CardBlack>,
current_black: Arc<CardBlackWithID>,
pub packs: Vec<u8>,
}
@ -311,7 +335,11 @@ 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())
.expect("No black cards to draw from!"),
);
// These are at the largest size they should ever be
white.shrink_to_fit();
@ -331,7 +359,7 @@ impl Game {
}
/// Draw one white card at random from play deck.
fn draw_one_white(&mut self) -> Option<Arc<CardWhite>> {
fn draw_one_white(&mut self) -> Option<Arc<CardWhiteWithID>> {
let deck = &mut self.white;
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
@ -380,7 +408,7 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
let mut unofficial_meta: Vec<CardPackMeta> = vec![];
// Unpack the json
for set in jayson {
for sets in jayson {
let mut num_white = 0;
let mut num_black = 0;
@ -393,55 +421,47 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
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) = sets.white {
num_white = white.len();
if num_white > 0 {
pack = Some(white[0].pack);
newset.white = Some(
set.white
.unwrap()
.iter()
.map(|card| {
Arc::new(CardWhite {
text: card.text.clone(),
pack: card.pack.clone(),
})
})
.collect(),
);
let mut white_buf = vec![];
for card in sets.white.unwrap() {
white_buf.push(Arc::new(CardWhiteWithID {
uuid: Uuid::now_v7(),
text: card.text,
}));
}
newset.white = Some(white_buf);
}
}
// Process black cards if there are any
if let Some(ref black) = set.black {
if let Some(ref black) = sets.black {
num_black = black.len();
if num_black > 0 {
pack = Some(black[0].pack);
newset.black = Some(
set.black
.unwrap()
.iter()
.map(|card| {
Arc::new(CardBlack {
text: card.text.clone(),
pick: card.pick.clone(),
pack: card.pack.clone(),
})
})
.collect(),
);
let mut black_buf = vec![];
for card in sets.black.unwrap() {
black_buf.push(Arc::new(CardBlackWithID {
uuid: Uuid::now_v7(),
text: card.text,
pick: card.pick,
}));
}
newset.black = Some(black_buf);
}
}
// Start repackaging
let meta = CardPackMeta {
name: set.name,
name: sets.name,
pack: pack.expect("No card pack number!"),
num_white,
num_black,
};
if set.official {
if sets.official {
official_meta.push(meta);
official.insert(pack.unwrap(), newset);
} else {
@ -456,7 +476,6 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
official_meta.shrink_to_fit();
unofficial_meta.shrink_to_fit();
// Package for export
let packs = CardPacks {
official,