let game handle more of its own logic
This commit is contained in:
parent
1da5606dd6
commit
f7ffb5bb20
3 changed files with 53 additions and 63 deletions
|
@ -1,7 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Judge decision
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct JudgeDecisionRequest {
|
||||
pub game_id: String,
|
||||
pub winning_cards: Vec<String>,
|
||||
|
|
|
@ -138,6 +138,22 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
/// Judge Game
|
||||
pub fn judge_round(&mut self, request: JudgeDecisionRequest, player_user_id: String) {
|
||||
// Check if player is czar
|
||||
if self.czar.read().unwrap().uuid.to_string() == player_user_id {
|
||||
let winner_id = self
|
||||
.judge_pile_meta
|
||||
.get(&request.winning_cards)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
self.end_round(winner_id)
|
||||
} else {
|
||||
tracing::error!("Player is not czar!");
|
||||
}
|
||||
}
|
||||
|
||||
/// Process player move
|
||||
pub fn player_move(
|
||||
&mut self,
|
||||
|
@ -179,6 +195,7 @@ impl Game {
|
|||
}
|
||||
|
||||
// Meta for convenience
|
||||
// TODO: don't do this extra work
|
||||
self.judge_pile_meta.insert(trimmed, player_user_id.clone());
|
||||
|
||||
// Start judging if this is last player to submit
|
||||
|
@ -207,13 +224,14 @@ impl Game {
|
|||
)))
|
||||
} else {
|
||||
// User submitted cards
|
||||
// TODO: player submitted move indication
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tick forward
|
||||
pub fn finish_round(&mut self, winner_id: String) {
|
||||
fn end_round(&mut self, winner_id: String) {
|
||||
// Top off player hands
|
||||
let user_ids: Vec<String> = self.judge_pile_meta.drain().map(|pair| pair.1).collect();
|
||||
|
||||
|
@ -244,16 +262,12 @@ impl Game {
|
|||
.collect::<Vec<Arc<CardWhiteWithID>>>(),
|
||||
);
|
||||
|
||||
tracing::debug!("{:#?}", self.rotation);
|
||||
tracing::debug!("{:#?}", self.rotation_index);
|
||||
// Choose new czar
|
||||
if self.rotation_index == self.rotation.len() - 1 {
|
||||
self.rotation_index = 0;
|
||||
} else {
|
||||
self.rotation_index += 1;
|
||||
}
|
||||
tracing::debug!("{:#?}", self.rotation);
|
||||
tracing::debug!("{:#?}", self.rotation_index);
|
||||
self.czar = self
|
||||
.players
|
||||
.get(&self.rotation[self.rotation_index])
|
||||
|
@ -288,26 +302,31 @@ impl Game {
|
|||
|
||||
/// Create a new player and add them to the game.
|
||||
pub fn create_player(&mut self, user: Arc<RwLock<User>>) {
|
||||
// Build hand of 10 white cards
|
||||
let mut white = vec![];
|
||||
for _ in 0..10 {
|
||||
if let Some(card) = self.draw_one_white() {
|
||||
white.push(card);
|
||||
// Check if player already exists
|
||||
if !self.players.contains_key(&user.read().unwrap().uuid) {
|
||||
// Build hand of 10 white cards
|
||||
let mut white = vec![];
|
||||
for _ in 0..10 {
|
||||
if let Some(card) = self.draw_one_white() {
|
||||
white.push(card);
|
||||
}
|
||||
}
|
||||
|
||||
// New player object
|
||||
let new_player = Player {
|
||||
user: user.clone(),
|
||||
white,
|
||||
black: vec![],
|
||||
};
|
||||
|
||||
// Add player to game
|
||||
self.players
|
||||
.insert(user.read().unwrap().uuid.clone(), new_player);
|
||||
|
||||
// Add player to rotation
|
||||
self.rotation.push(user.read().unwrap().uuid.to_string());
|
||||
} else {
|
||||
tracing::debug!("User already has a player in this game!");
|
||||
}
|
||||
|
||||
// New player object
|
||||
let new_player = Player {
|
||||
user: user.clone(),
|
||||
white,
|
||||
black: vec![],
|
||||
};
|
||||
|
||||
// Add player to game
|
||||
self.players
|
||||
.insert(user.read().unwrap().uuid.clone(), new_player);
|
||||
|
||||
// Add player to rotation
|
||||
self.rotation.push(user.read().unwrap().uuid.to_string());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ impl GameHandler {
|
|||
/// Process judging
|
||||
async fn handle_judging(&self, request: JudgeDecisionRequest, addr: SocketAddr) {
|
||||
// Get pointers
|
||||
let this_user_id = self
|
||||
let player_user_id = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
|
@ -85,35 +85,14 @@ impl GameHandler {
|
|||
.unwrap()
|
||||
.clone();
|
||||
|
||||
// Check if this player is czar
|
||||
// Check if player is currently Czar
|
||||
if this_game
|
||||
.read()
|
||||
// Send to game
|
||||
this_game
|
||||
.write()
|
||||
.unwrap()
|
||||
.czar
|
||||
.read()
|
||||
.unwrap()
|
||||
.uuid
|
||||
.to_string()
|
||||
== this_user_id
|
||||
{
|
||||
// Find user who submitted the card
|
||||
let winning_player_id = this_game
|
||||
.read()
|
||||
.unwrap()
|
||||
.judge_pile_meta
|
||||
.get(&request.winning_cards)
|
||||
.unwrap()
|
||||
.clone();
|
||||
.judge_round(request.clone(), player_user_id.clone());
|
||||
|
||||
this_game
|
||||
.write()
|
||||
.unwrap()
|
||||
.finish_round(winning_player_id.clone());
|
||||
|
||||
let this_game_id = this_game.read().unwrap().uuid.to_string();
|
||||
self.send_game_state_update(this_game_id).await
|
||||
}
|
||||
let this_game_id = this_game.read().unwrap().uuid.to_string();
|
||||
self.send_game_state_update(this_game_id).await
|
||||
}
|
||||
|
||||
/// Process player move request
|
||||
|
@ -179,16 +158,8 @@ impl GameHandler {
|
|||
.unwrap()
|
||||
.clone();
|
||||
|
||||
// Check if player already exists
|
||||
if !this_game
|
||||
.read()
|
||||
.unwrap()
|
||||
.players
|
||||
.contains_key(&this_user.read().unwrap().uuid)
|
||||
{
|
||||
// Create player
|
||||
this_game.write().unwrap().create_player(this_user);
|
||||
}
|
||||
// Create player
|
||||
this_game.write().unwrap().create_player(this_user);
|
||||
|
||||
// Send updates for all players
|
||||
self.send_game_state_update(id).await;
|
||||
|
|
Loading…
Add table
Reference in a new issue