start letting game handle its own logic

This commit is contained in:
Adam 2024-08-24 18:39:11 -04:00
parent 2e8411de32
commit ff0d70376a
2 changed files with 68 additions and 71 deletions

View file

@ -1,5 +1,6 @@
use crate::AppState; use crate::AppState;
use crate::User; use crate::User;
use lib::*;
use rand::prelude::IteratorRandom; use rand::prelude::IteratorRandom;
use rand::thread_rng; use rand::thread_rng;
use std::{ use std::{
@ -124,6 +125,51 @@ impl Game {
} }
} }
/// Process player move
pub fn player_move(
&mut self,
request: PlayerMoveRequest,
player_user_id: String,
) -> Result<Option<(JudgeRound, String)>, String> {
if self.czar.read().unwrap().uuid == player_user_id {
tracing::debug!("Player is czar");
Err("You can't submit cards to judge, you ARE the judge!".to_string())
} else {
// Ignore extra cards
let trimmed = request.card_ids[..self.current_black.pick.into()].to_vec();
// TODO: handle not enough cards
tracing::debug!("Trimmed: {:#?}", trimmed);
// Put cards into judge pool
self.judge_pool.insert(trimmed, player_user_id.clone());
// Start judging if this is last player to submit
if self.judge_pool.len() == self.players.len() - 1 {
Ok(Some((
JudgeRound {
cards_to_judge: self
.judge_pool
.keys()
.into_iter()
.map(|group| {
group
.iter()
.map(|id| WhiteCardMeta {
uuid: id.to_string(),
text: "test".to_string(),
})
.collect()
})
.collect(),
},
self.czar.read().unwrap().uuid.clone(),
)))
} else {
// User submitted cards
Ok(None)
}
}
}
/// Tick forward /// Tick forward
pub fn finish_round(&mut self, winner_id: String) { pub fn finish_round(&mut self, winner_id: String) {
self.players self.players
@ -133,6 +179,7 @@ impl Game {
.push(self.current_black.clone()); .push(self.current_black.clone());
self.current_black = self.draw_one_black().unwrap(); self.current_black = self.draw_one_black().unwrap();
self.judge_pool.clear();
} }
/// Draw one black card at random from play deck. /// Draw one black card at random from play deck.

View file

@ -119,7 +119,7 @@ impl GameHandler {
/// Process player move request /// Process player move request
async fn handle_player_move(&self, request: PlayerMoveRequest, addr: SocketAddr) { async fn handle_player_move(&self, request: PlayerMoveRequest, addr: SocketAddr) {
// Get pointers // Get pointers
let this_user_id = self let player_user_id = self
.state .state
.online_users .online_users
.read() .read()
@ -142,79 +142,29 @@ impl GameHandler {
// Do the stuff // Do the stuff
// Check if player is currently Czar match this_game
if this_game
.read()
.unwrap()
.czar
.read()
.unwrap()
.uuid
.to_string()
== this_user_id
{
// Tell player no
let message = ChatMessage {
text: "You can't submit cards to judge, you ARE the judge!".to_string(),
};
let _ = self
.state
.users_tx
.send(DmUser(SendChatMessage(message), Addr(addr)))
.await;
} else {
// Ignore extra cards
let current_round_pick: usize = this_game.read().unwrap().current_black.pick.into();
// TODO: handle not enough cards submitted
let trimmed = &request.card_ids[..current_round_pick];
// Put cards into game judge pool
this_game
.write() .write()
.unwrap() .unwrap()
.judge_pool .player_move(request, player_user_id)
.insert(trimmed.to_vec(), this_user_id.clone());
// Check if this is the last player to submit
if this_game.read().unwrap().judge_pool.len()
== this_game.read().unwrap().players.len() - 1
{ {
let message = SendJudgeRound(JudgeRound { Err(err) => {
cards_to_judge: this_game let message = ChatMessage { text: err };
.read() let tx = self.state.users_tx.clone();
.unwrap() tokio::spawn(async move {
.judge_pool let _ = tx.send(DmUser(SendChatMessage(message), Addr(addr))).await;
.keys()
.into_iter()
.map(|group| {
group
.iter()
.map(|id| WhiteCardMeta {
uuid: self
.state
.white_cards_by_id
.get(id)
.unwrap()
.uuid
.to_string(),
text: self
.state
.white_cards_by_id
.get(id)
.unwrap()
.text
.clone(),
}) })
.collect() }
}) Ok(None) => tokio::spawn(async move { tracing::debug!("None") }),
.collect(), Ok(Some((judge_round, czar_id))) => {
}); let tx = self.state.users_tx.clone();
tracing::debug!("send for judging"); tokio::spawn(async move {
let czar_id = this_game.read().unwrap().czar.read().unwrap().uuid.clone(); let _ = tx
let _ = self.state.users_tx.send(DmUser(message, Id(czar_id))).await; .send(DmUser(SendJudgeRound(judge_round), Id(czar_id)))
} .await;
})
} }
};
} }
/// Puts a user in a game /// Puts a user in a game