2024-08-08 05:29:32 -04:00
|
|
|
use crate::user_handler::*;
|
|
|
|
use crate::AppState;
|
2024-08-09 01:21:04 -04:00
|
|
|
use crate::User;
|
2024-08-12 17:14:27 -04:00
|
|
|
use anyhow::{Context, Result};
|
2024-08-09 02:57:27 -04:00
|
|
|
use lib::*;
|
2024-08-12 15:10:48 -04:00
|
|
|
use rand::prelude::IteratorRandom;
|
|
|
|
use rand::thread_rng;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
2024-08-12 17:14:27 -04:00
|
|
|
fs::read_to_string,
|
2024-08-12 15:10:48 -04:00
|
|
|
net::SocketAddr,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
|
|
|
use uuid::Uuid;
|
2024-08-08 05:29:32 -04:00
|
|
|
|
2024-08-12 17:14:27 -04:00
|
|
|
/// 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
|
|
|
|
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."))?;
|
|
|
|
|
|
|
|
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![];
|
|
|
|
|
|
|
|
for set in jayson {
|
|
|
|
let mut num_white = 0;
|
|
|
|
let mut num_black = 0;
|
|
|
|
|
|
|
|
let mut newset = CardSet {
|
|
|
|
white: Option::None,
|
|
|
|
black: Option::None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// No safe default for this so make it an Option
|
|
|
|
let mut pack: Option<u8> = Option::None;
|
|
|
|
|
|
|
|
if let Some(ref white) = set.white {
|
|
|
|
num_white = white.len();
|
|
|
|
if num_white > 0 {
|
|
|
|
pack = Some(white[0].pack);
|
|
|
|
newset.white = Some(set.white.unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref black) = set.black {
|
|
|
|
num_black = black.len();
|
|
|
|
if num_black > 0 {
|
|
|
|
pack = Some(black[0].pack);
|
|
|
|
newset.black = Some(set.black.unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let meta = CardPackMeta {
|
|
|
|
name: set.name,
|
|
|
|
pack: pack.expect("No card pack number!"),
|
|
|
|
num_white,
|
|
|
|
num_black,
|
|
|
|
};
|
|
|
|
|
|
|
|
if set.official {
|
|
|
|
official_meta.push(meta);
|
|
|
|
official.insert(pack.unwrap(), newset);
|
|
|
|
} else {
|
|
|
|
unofficial_meta.push(meta);
|
|
|
|
unofficial.insert(pack.unwrap(), newset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
let packs = CardPacks {
|
|
|
|
official,
|
|
|
|
unofficial,
|
|
|
|
};
|
|
|
|
|
|
|
|
let packs_meta = CardPacksMeta {
|
|
|
|
official_meta,
|
|
|
|
unofficial_meta,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((packs, packs_meta))
|
|
|
|
}
|
2024-08-08 05:29:32 -04:00
|
|
|
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Card Set
|
|
|
|
#[derive(Debug)]
|
2024-08-12 17:14:27 -04:00
|
|
|
struct CardSet {
|
|
|
|
white: Option<Vec<CardWhite>>,
|
|
|
|
black: Option<Vec<CardBlack>>,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Card Packs
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CardPacks {
|
2024-08-12 17:14:27 -04:00
|
|
|
official: HashMap<u8, CardSet>,
|
|
|
|
unofficial: HashMap<u8, CardSet>,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A white card
|
2024-08-12 17:14:27 -04:00
|
|
|
// TODO: Remove this clone!
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
struct CardWhite {
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Card text
|
2024-08-12 17:14:27 -04:00
|
|
|
text: String,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// ID of the pack it came from
|
2024-08-12 17:14:27 -04:00
|
|
|
pack: u8,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A black card
|
2024-08-12 17:14:27 -04:00
|
|
|
// TODO: Remove this clone!
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
struct CardBlack {
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Card text
|
2024-08-12 17:14:27 -04:00
|
|
|
text: String,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Amount of cards to submit for judging
|
2024-08-12 17:14:27 -04:00
|
|
|
pick: u8,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// ID of the pack it came from
|
2024-08-12 17:14:27 -04:00
|
|
|
pack: u8,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A card pack
|
|
|
|
#[derive(Debug, Deserialize)]
|
2024-08-12 17:14:27 -04:00
|
|
|
struct CardPack {
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Name of the pack
|
2024-08-12 17:14:27 -04:00
|
|
|
name: String,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Whether or not this is an official card pack
|
2024-08-12 17:14:27 -04:00
|
|
|
official: bool,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// White card data
|
2024-08-12 17:14:27 -04:00
|
|
|
white: Option<Vec<CardWhite>>,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Black card data
|
2024-08-12 17:14:27 -04:00
|
|
|
black: Option<Vec<CardBlack>>,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// New game request structure
|
|
|
|
#[derive(Debug)]
|
2024-08-12 17:14:27 -04:00
|
|
|
struct NewGameManifest {
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Game name
|
2024-08-12 17:14:27 -04:00
|
|
|
name: String,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Game host
|
2024-08-12 17:14:27 -04:00
|
|
|
host: Arc<RwLock<User>>,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Selected game packs
|
2024-08-12 17:14:27 -04:00
|
|
|
packs: Vec<u8>,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A struct that represents a player
|
|
|
|
#[derive(Debug)]
|
2024-08-13 18:16:31 -04:00
|
|
|
pub struct Player {
|
2024-08-12 15:10:48 -04:00
|
|
|
/// The player's hand
|
2024-08-12 17:14:27 -04:00
|
|
|
white: Vec<CardWhite>,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// The player's wins
|
2024-08-12 17:14:27 -04:00
|
|
|
black: Vec<CardBlack>,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The game master
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Game {
|
2024-08-13 18:16:31 -04:00
|
|
|
/// Game's UUID
|
|
|
|
pub uuid: Uuid,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// The name of the game
|
|
|
|
pub name: String,
|
|
|
|
/// The host user of the game
|
|
|
|
pub host: Arc<RwLock<User>>,
|
|
|
|
/// White draw pile
|
2024-08-12 17:14:27 -04:00
|
|
|
white: Vec<CardWhite>,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Black draw pile
|
2024-08-12 17:14:27 -04:00
|
|
|
black: Vec<CardBlack>,
|
2024-08-13 18:16:31 -04:00
|
|
|
pub players: HashMap<Uuid, Player>,
|
2024-08-12 15:10:48 -04:00
|
|
|
/// Black card for the current round
|
2024-08-12 17:14:27 -04:00
|
|
|
current_black: Option<CardBlack>,
|
2024-08-13 18:16:31 -04:00
|
|
|
pub packs: Vec<u8>,
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Game {
|
2024-08-12 17:14:27 -04:00
|
|
|
fn new(state: Arc<AppState>, request: NewGameManifest) -> Result<Self> {
|
2024-08-13 18:16:31 -04:00
|
|
|
tracing::debug!("{:#?}", request.packs);
|
|
|
|
tracing::debug!("{:#?}", request.packs.len());
|
2024-08-12 15:10:48 -04:00
|
|
|
let mut game = Game {
|
2024-08-13 18:16:31 -04:00
|
|
|
uuid: Uuid::now_v7(),
|
2024-08-12 15:10:48 -04:00
|
|
|
name: request.host.read().unwrap().name.clone(),
|
|
|
|
host: request.host.clone(),
|
|
|
|
white: vec![],
|
|
|
|
black: vec![],
|
|
|
|
players: HashMap::new(),
|
|
|
|
current_black: Option::None,
|
2024-08-13 18:16:31 -04:00
|
|
|
packs: request.packs.clone(),
|
2024-08-12 15:10:48 -04:00
|
|
|
};
|
|
|
|
tracing::debug!(
|
|
|
|
"Creating game {} with {} as host",
|
|
|
|
&request.name,
|
|
|
|
request.host.read().unwrap().name
|
|
|
|
);
|
|
|
|
game.name = request.name;
|
|
|
|
game.host = request.host.clone();
|
|
|
|
|
2024-08-12 17:14:27 -04:00
|
|
|
game.build_decks(state, request.packs)?;
|
2024-08-12 15:10:48 -04:00
|
|
|
game.create_player(request.host)?;
|
|
|
|
game.deal_black()?;
|
|
|
|
|
|
|
|
Ok(game)
|
|
|
|
}
|
|
|
|
|
2024-08-12 17:14:27 -04:00
|
|
|
/// Build game decks from input data for game start.
|
|
|
|
/// This should only run once and at startup.
|
|
|
|
fn build_decks(&mut self, state: Arc<AppState>, selected_packs: Vec<u8>) -> Result<()> {
|
|
|
|
// TODO: Make this right -- remove the clones, use references to single cards
|
|
|
|
for pack_num in selected_packs {
|
|
|
|
if let Some(pack) = state.packs.official.get(&pack_num) {
|
|
|
|
if let Some(white) = &pack.white {
|
|
|
|
self.white.extend(white.clone())
|
|
|
|
}
|
|
|
|
if let Some(black) = &pack.black {
|
|
|
|
self.black.extend(black.clone())
|
|
|
|
}
|
|
|
|
} else if let Some(pack) = state.packs.unofficial.get(&pack_num) {
|
|
|
|
if let Some(white) = &pack.white {
|
|
|
|
self.white.extend(white.clone())
|
|
|
|
}
|
|
|
|
if let Some(black) = &pack.black {
|
|
|
|
self.black.extend(black.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 15:10:48 -04:00
|
|
|
|
2024-08-12 17:14:27 -04:00
|
|
|
Ok(())
|
2024-08-12 15:10:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw one white card at random from play deck.
|
|
|
|
fn draw_one_white(&mut self) -> Result<CardWhite> {
|
|
|
|
let deck = &mut self.white;
|
|
|
|
|
2024-08-12 17:14:27 -04:00
|
|
|
// TODO: this feels sloppy
|
2024-08-12 15:10:48 -04:00
|
|
|
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
|
|
|
|
Ok(deck.swap_remove(index))
|
|
|
|
} else {
|
|
|
|
Ok(CardWhite {
|
|
|
|
text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
|
|
|
|
pack: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw one black card at random from play deck.
|
|
|
|
fn draw_one_black(&mut self) -> Result<CardBlack> {
|
|
|
|
let deck = &mut self.black;
|
|
|
|
|
2024-08-12 17:14:27 -04:00
|
|
|
// TODO: this feels sloppy
|
2024-08-12 15:10:48 -04:00
|
|
|
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
|
|
|
|
Ok(deck.swap_remove(index))
|
|
|
|
} else {
|
|
|
|
Ok(CardBlack {
|
|
|
|
text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
|
|
|
|
pick: 0,
|
|
|
|
pack: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deal a black card and use it for the current round
|
|
|
|
fn deal_black(&mut self) -> Result<()> {
|
|
|
|
self.current_black = Some(self.draw_one_black()?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new player and add them to the game.
|
2024-08-14 00:16:54 -04:00
|
|
|
pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> {
|
2024-08-12 15:10:48 -04:00
|
|
|
let mut new_player = Player {
|
|
|
|
white: vec![],
|
|
|
|
black: vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_player_name = user.read().unwrap().name.clone();
|
|
|
|
tracing::debug!("Creating player for {}", &new_player_name);
|
|
|
|
|
|
|
|
let mut hand_buf = vec![];
|
|
|
|
for _ in 0..10 {
|
|
|
|
hand_buf.push(self.draw_one_white()?);
|
|
|
|
}
|
|
|
|
tracing::debug!("Dealing hand to {}", &new_player_name);
|
|
|
|
|
|
|
|
new_player.white.extend(hand_buf);
|
|
|
|
|
|
|
|
self.players
|
|
|
|
.insert(user.read().unwrap().uuid.clone(), new_player);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 01:21:04 -04:00
|
|
|
pub enum GameHandlerMessage {
|
|
|
|
NewGame {
|
|
|
|
addr: SocketAddr,
|
|
|
|
new_game: NewGameRequest,
|
|
|
|
},
|
|
|
|
JoinGame {
|
2024-08-14 00:16:54 -04:00
|
|
|
addr: SocketAddr,
|
|
|
|
id: String,
|
2024-08-09 01:21:04 -04:00
|
|
|
},
|
|
|
|
}
|
2024-08-08 05:29:32 -04:00
|
|
|
pub struct GameHandler {
|
|
|
|
state: Arc<AppState>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GameHandler {
|
|
|
|
pub fn new(state: Arc<AppState>) -> Self {
|
|
|
|
GameHandler { state }
|
|
|
|
}
|
|
|
|
|
2024-08-09 01:21:04 -04:00
|
|
|
pub async fn handle(&self, message: GameHandlerMessage) {
|
|
|
|
match message {
|
|
|
|
GameHandlerMessage::NewGame { addr, new_game } => self.new_game(addr, new_game).await,
|
2024-08-14 00:16:54 -04:00
|
|
|
GameHandlerMessage::JoinGame { addr, id } => self.join_game(addr, id).await,
|
2024-08-09 01:21:04 -04:00
|
|
|
_ => {
|
|
|
|
tracing::debug!("Unhandled at game handler");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 00:16:54 -04:00
|
|
|
async fn join_game(&self, addr: SocketAddr, id: String) {
|
|
|
|
// Create player
|
|
|
|
self.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.create_player(
|
|
|
|
self.state
|
|
|
|
.online_users
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&addr)
|
|
|
|
.unwrap()
|
|
|
|
.clone(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Create update for user's game view
|
|
|
|
let mut black_card = ("Error".to_string(), 0u8);
|
|
|
|
if let Some(ref current_black) = self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.current_black
|
|
|
|
{
|
|
|
|
black_card = (current_black.text.to_owned(), current_black.pick)
|
|
|
|
}
|
|
|
|
let meta = GameStateMeta {
|
|
|
|
uuid: id.clone(),
|
|
|
|
name: self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.name
|
|
|
|
.clone(),
|
|
|
|
host: self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.host
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.name
|
|
|
|
.clone(),
|
|
|
|
players: self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.players
|
|
|
|
.iter()
|
|
|
|
.map(|player| {
|
|
|
|
self.state
|
|
|
|
.user_uuid
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(player.0)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
czar: self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.host
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.name
|
|
|
|
.clone(),
|
|
|
|
black: black_card,
|
|
|
|
white: self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.players
|
|
|
|
.get(
|
|
|
|
&self
|
|
|
|
.state
|
|
|
|
.online_users
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&addr)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.uuid,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.white
|
|
|
|
.iter()
|
|
|
|
.map(|card| card.text.clone())
|
|
|
|
.collect(),
|
|
|
|
packs: self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.packs
|
|
|
|
.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Send updates for all players
|
|
|
|
for player in self
|
|
|
|
.state
|
|
|
|
.games
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&id)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.players
|
|
|
|
.iter()
|
|
|
|
{
|
|
|
|
// // Send user's update
|
|
|
|
// self.state
|
|
|
|
// .user_uuid
|
|
|
|
// .read()
|
|
|
|
// .unwrap()
|
|
|
|
// .get(&player.0)
|
|
|
|
// .unwrap()
|
|
|
|
// .read()
|
|
|
|
// .unwrap()
|
|
|
|
// .tx
|
|
|
|
// .send(serde_json::to_string(&meta).unwrap())
|
|
|
|
// .await
|
|
|
|
// .unwrap();
|
|
|
|
tracing::debug! {"y"};
|
|
|
|
}
|
|
|
|
// let _ = self
|
|
|
|
// .state
|
|
|
|
// .games
|
|
|
|
// .read()
|
|
|
|
// .unwrap()
|
|
|
|
// .get(&id)
|
|
|
|
// .unwrap()
|
|
|
|
// .read()
|
|
|
|
// .unwrap()
|
|
|
|
// .players
|
|
|
|
// .iter()
|
|
|
|
// .map(async |player: (&Uuid, &Player)| {
|
|
|
|
// // Send user's update
|
|
|
|
// self.state
|
|
|
|
// .user_uuid
|
|
|
|
// .read()
|
|
|
|
// .unwrap()
|
|
|
|
// .get(player.0)
|
|
|
|
// .unwrap()
|
|
|
|
// .read()
|
|
|
|
// .unwrap()
|
|
|
|
// .tx
|
|
|
|
// .send(serde_json::to_string(&meta).unwrap())
|
|
|
|
// .await
|
|
|
|
// .unwrap();
|
|
|
|
// })
|
|
|
|
// .count();
|
|
|
|
|
|
|
|
// Broadcast game browser update
|
|
|
|
self.state
|
|
|
|
.broadcast_tx
|
|
|
|
.send(meta_games_browser_update(&self.state))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Broadcast server meta update
|
|
|
|
self.state
|
|
|
|
.broadcast_tx
|
|
|
|
.send(meta_server_summary_update(&self.state))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2024-08-09 01:21:04 -04:00
|
|
|
async fn new_game(&self, addr: SocketAddr, new_game: NewGameRequest) {
|
|
|
|
if new_game.packs.is_empty() {
|
|
|
|
tracing::debug!("Cards are empty");
|
|
|
|
return;
|
|
|
|
} else if new_game.name.is_empty() {
|
|
|
|
tracing::debug!("Name are empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-08 05:29:32 -04:00
|
|
|
let manifest = NewGameManifest {
|
2024-08-09 01:21:04 -04:00
|
|
|
name: new_game.name,
|
2024-08-08 05:29:32 -04:00
|
|
|
host: self
|
|
|
|
.state
|
|
|
|
.online_users
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
2024-08-09 01:21:04 -04:00
|
|
|
.get(&addr)
|
2024-08-08 05:29:32 -04:00
|
|
|
.unwrap()
|
|
|
|
.clone(),
|
2024-08-12 15:10:48 -04:00
|
|
|
packs: new_game.packs,
|
2024-08-08 05:29:32 -04:00
|
|
|
};
|
|
|
|
|
2024-08-14 00:16:54 -04:00
|
|
|
// Create game
|
2024-08-12 17:14:27 -04:00
|
|
|
if let Ok(new_game_object) = Game::new(self.state.clone(), manifest) {
|
2024-08-09 02:57:27 -04:00
|
|
|
let tx = self
|
|
|
|
.state
|
|
|
|
.online_users
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&addr)
|
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.tx
|
|
|
|
.clone();
|
|
|
|
|
2024-08-14 00:16:54 -04:00
|
|
|
// Create update for user's game view
|
2024-08-12 17:14:27 -04:00
|
|
|
let mut black_card = ("Error".to_string(), 0u8);
|
2024-08-10 19:50:26 -04:00
|
|
|
if let Some(ref current_black) = new_game_object.current_black {
|
2024-08-12 17:14:27 -04:00
|
|
|
black_card = (current_black.text.to_owned(), current_black.pick)
|
2024-08-10 19:50:26 -04:00
|
|
|
}
|
2024-08-14 00:16:54 -04:00
|
|
|
let meta = GameStateMeta {
|
2024-08-13 18:16:31 -04:00
|
|
|
uuid: new_game_object.uuid.to_string(),
|
2024-08-10 19:50:26 -04:00
|
|
|
name: new_game_object.name.clone(),
|
|
|
|
host: new_game_object.host.read().unwrap().name.clone(),
|
|
|
|
players: new_game_object
|
|
|
|
.players
|
|
|
|
.iter()
|
2024-08-11 19:51:30 -04:00
|
|
|
.map(|player| {
|
|
|
|
self.state
|
|
|
|
.user_uuid
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
2024-08-13 18:16:31 -04:00
|
|
|
.get(player.0)
|
2024-08-11 19:51:30 -04:00
|
|
|
.unwrap()
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
})
|
2024-08-10 19:50:26 -04:00
|
|
|
.collect(),
|
|
|
|
czar: new_game_object.host.read().unwrap().name.clone(),
|
2024-08-12 17:14:27 -04:00
|
|
|
black: black_card,
|
2024-08-10 19:50:26 -04:00
|
|
|
white: new_game_object
|
2024-08-11 19:51:30 -04:00
|
|
|
.players
|
|
|
|
.get(&new_game_object.host.read().unwrap().uuid)
|
|
|
|
.unwrap()
|
2024-08-10 19:50:26 -04:00
|
|
|
.white
|
|
|
|
.iter()
|
|
|
|
.map(|card| card.text.clone())
|
|
|
|
.collect(),
|
2024-08-13 18:16:31 -04:00
|
|
|
packs: new_game_object.packs.clone(),
|
2024-08-10 19:50:26 -04:00
|
|
|
};
|
|
|
|
|
2024-08-14 00:16:54 -04:00
|
|
|
// Send user's update
|
2024-08-10 19:50:26 -04:00
|
|
|
tx.send(serde_json::to_string(&meta).unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2024-08-14 00:16:54 -04:00
|
|
|
|
|
|
|
// Add game to active list
|
2024-08-09 01:21:04 -04:00
|
|
|
self.state.games.write().unwrap().insert(
|
2024-08-14 00:16:54 -04:00
|
|
|
new_game_object.uuid.to_string(),
|
2024-08-09 01:21:04 -04:00
|
|
|
Arc::new(RwLock::new(new_game_object)),
|
|
|
|
);
|
2024-08-14 00:16:54 -04:00
|
|
|
|
|
|
|
// Broadcast game browser update
|
2024-08-08 05:29:32 -04:00
|
|
|
self.state
|
|
|
|
.broadcast_tx
|
|
|
|
.send(meta_games_browser_update(&self.state))
|
|
|
|
.unwrap();
|
2024-08-14 00:16:54 -04:00
|
|
|
|
|
|
|
// Broadcast server meta update
|
2024-08-08 05:29:32 -04:00
|
|
|
self.state
|
|
|
|
.broadcast_tx
|
|
|
|
.send(meta_server_summary_update(&self.state))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|