finish game logic
This commit is contained in:
parent
ff0d70376a
commit
e473114bec
2 changed files with 68 additions and 16 deletions
|
@ -64,15 +64,20 @@ pub struct Game {
|
||||||
pub czar: Arc<RwLock<User>>,
|
pub czar: Arc<RwLock<User>>,
|
||||||
/// Packs selected for this game
|
/// Packs selected for this game
|
||||||
pub packs: Vec<u8>,
|
pub packs: Vec<u8>,
|
||||||
/// White draw pile
|
/// List of players in the game
|
||||||
pub white: Vec<Arc<CardWhiteWithID>>,
|
|
||||||
/// Black draw pile
|
|
||||||
pub black: Vec<Arc<CardBlackWithID>>,
|
|
||||||
pub players: HashMap<String, Player>,
|
pub players: HashMap<String, Player>,
|
||||||
/// Black card for the current round
|
/// Black card for the current round
|
||||||
pub current_black: Arc<CardBlackWithID>,
|
pub current_black: Arc<CardBlackWithID>,
|
||||||
/// Judge pool
|
/// Judge pool
|
||||||
pub judge_pool: HashMap<Vec<String>, String>,
|
pub judge_pile_meta: HashMap<Vec<String>, String>,
|
||||||
|
/// Judge pile that contains cards
|
||||||
|
judge_pile: HashMap<String, Arc<CardWhiteWithID>>,
|
||||||
|
/// White draw pile
|
||||||
|
white: Vec<Arc<CardWhiteWithID>>,
|
||||||
|
/// White discard pile
|
||||||
|
white_discard: Vec<Arc<CardWhiteWithID>>,
|
||||||
|
/// Black draw pile
|
||||||
|
black: Vec<Arc<CardBlackWithID>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
|
@ -121,7 +126,9 @@ impl Game {
|
||||||
players: HashMap::new(),
|
players: HashMap::new(),
|
||||||
current_black,
|
current_black,
|
||||||
packs: request.packs.clone(),
|
packs: request.packs.clone(),
|
||||||
judge_pool: HashMap::new(),
|
judge_pile_meta: HashMap::new(),
|
||||||
|
judge_pile: HashMap::new(),
|
||||||
|
white_discard: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,23 +146,48 @@ impl Game {
|
||||||
let trimmed = request.card_ids[..self.current_black.pick.into()].to_vec();
|
let trimmed = request.card_ids[..self.current_black.pick.into()].to_vec();
|
||||||
// TODO: handle not enough cards
|
// TODO: handle not enough cards
|
||||||
tracing::debug!("Trimmed: {:#?}", trimmed);
|
tracing::debug!("Trimmed: {:#?}", trimmed);
|
||||||
// Put cards into judge pool
|
|
||||||
self.judge_pool.insert(trimmed, player_user_id.clone());
|
// Move card from player's hand to judge pile
|
||||||
|
for id in &trimmed {
|
||||||
|
let index = self
|
||||||
|
.players
|
||||||
|
.get(&player_user_id)
|
||||||
|
.unwrap()
|
||||||
|
.white
|
||||||
|
.iter()
|
||||||
|
.position(|card| card.uuid.to_string() == *id)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let card = self
|
||||||
|
.players
|
||||||
|
.get_mut(&player_user_id)
|
||||||
|
.unwrap()
|
||||||
|
.white
|
||||||
|
.remove(index);
|
||||||
|
|
||||||
|
self.judge_pile.insert(card.uuid.to_string(), card);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Meta for convenience
|
||||||
|
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
|
||||||
if self.judge_pool.len() == self.players.len() - 1 {
|
if self.judge_pile_meta.len() == self.players.len() - 1 {
|
||||||
Ok(Some((
|
Ok(Some((
|
||||||
JudgeRound {
|
JudgeRound {
|
||||||
cards_to_judge: self
|
cards_to_judge: self
|
||||||
.judge_pool
|
.judge_pile_meta
|
||||||
.keys()
|
.keys()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|group| {
|
.map(|group| {
|
||||||
group
|
group
|
||||||
.iter()
|
.iter()
|
||||||
.map(|id| WhiteCardMeta {
|
.map(|id| {
|
||||||
uuid: id.to_string(),
|
let card = self.judge_pile.get(id).unwrap().clone();
|
||||||
text: "test".to_string(),
|
WhiteCardMeta {
|
||||||
|
uuid: card.uuid.to_string(),
|
||||||
|
text: card.text.clone(),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
|
@ -172,14 +204,35 @@ impl Game {
|
||||||
|
|
||||||
/// Tick forward
|
/// Tick forward
|
||||||
pub fn finish_round(&mut self, winner_id: String) {
|
pub fn finish_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();
|
||||||
|
|
||||||
|
for id in user_ids {
|
||||||
|
for _ in 0..self.current_black.pick.into() {
|
||||||
|
let card = self.draw_one_white().unwrap();
|
||||||
|
let player = self.players.get_mut(&id).unwrap();
|
||||||
|
|
||||||
|
player.white.push(card);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Award card/point to winning player
|
||||||
self.players
|
self.players
|
||||||
.get_mut(&winner_id)
|
.get_mut(&winner_id)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.black
|
.black
|
||||||
.push(self.current_black.clone());
|
.push(self.current_black.clone());
|
||||||
|
|
||||||
|
// Draw new black card
|
||||||
self.current_black = self.draw_one_black().unwrap();
|
self.current_black = self.draw_one_black().unwrap();
|
||||||
self.judge_pool.clear();
|
|
||||||
|
// End of round clean-up
|
||||||
|
self.white_discard.extend(
|
||||||
|
self.judge_pile
|
||||||
|
.drain()
|
||||||
|
.map(|pair| pair.1)
|
||||||
|
.collect::<Vec<Arc<CardWhiteWithID>>>(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draw one black card at random from play deck.
|
/// Draw one black card at random from play deck.
|
||||||
|
|
|
@ -101,7 +101,7 @@ impl GameHandler {
|
||||||
let winning_player_id = this_game
|
let winning_player_id = this_game
|
||||||
.read()
|
.read()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.judge_pool
|
.judge_pile_meta
|
||||||
.get(&request.winning_cards)
|
.get(&request.winning_cards)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clone();
|
.clone();
|
||||||
|
@ -141,7 +141,6 @@ impl GameHandler {
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
// Do the stuff
|
// Do the stuff
|
||||||
|
|
||||||
match this_game
|
match this_game
|
||||||
.write()
|
.write()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
Loading…
Add table
Reference in a new issue