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