start letting game handle its own logic
This commit is contained in:
parent
2e8411de32
commit
ff0d70376a
2 changed files with 68 additions and 71 deletions
|
@ -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.
|
||||||
|
|
|
@ -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
|
.write()
|
||||||
.read()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.czar
|
.player_move(request, player_user_id)
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.uuid
|
|
||||||
.to_string()
|
|
||||||
== this_user_id
|
|
||||||
{
|
{
|
||||||
// Tell player no
|
Err(err) => {
|
||||||
let message = ChatMessage {
|
let message = ChatMessage { text: err };
|
||||||
text: "You can't submit cards to judge, you ARE the judge!".to_string(),
|
let tx = self.state.users_tx.clone();
|
||||||
};
|
tokio::spawn(async move {
|
||||||
let _ = self
|
let _ = tx.send(DmUser(SendChatMessage(message), Addr(addr))).await;
|
||||||
.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()
|
|
||||||
.unwrap()
|
|
||||||
.judge_pool
|
|
||||||
.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 {
|
|
||||||
cards_to_judge: this_game
|
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.judge_pool
|
|
||||||
.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()
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
});
|
|
||||||
|
|
||||||
tracing::debug!("send for judging");
|
|
||||||
let czar_id = this_game.read().unwrap().czar.read().unwrap().uuid.clone();
|
|
||||||
let _ = self.state.users_tx.send(DmUser(message, Id(czar_id))).await;
|
|
||||||
}
|
}
|
||||||
}
|
Ok(None) => tokio::spawn(async move { tracing::debug!("None") }),
|
||||||
|
Ok(Some((judge_round, czar_id))) => {
|
||||||
|
let tx = self.state.users_tx.clone();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let _ = tx
|
||||||
|
.send(DmUser(SendJudgeRound(judge_round), Id(czar_id)))
|
||||||
|
.await;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Puts a user in a game
|
/// Puts a user in a game
|
||||||
|
|
Loading…
Add table
Reference in a new issue