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>>,
|
||||
/// Packs selected for this game
|
||||
pub packs: Vec<u8>,
|
||||
/// White draw pile
|
||||
pub white: Vec<Arc<CardWhiteWithID>>,
|
||||
/// Black draw pile
|
||||
pub black: Vec<Arc<CardBlackWithID>>,
|
||||
/// List of players in the game
|
||||
pub players: HashMap<String, Player>,
|
||||
/// Black card for the current round
|
||||
pub current_black: Arc<CardBlackWithID>,
|
||||
/// 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 {
|
||||
|
@ -121,7 +126,9 @@ impl Game {
|
|||
players: HashMap::new(),
|
||||
current_black,
|
||||
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();
|
||||
// TODO: handle not enough cards
|
||||
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
|
||||
if self.judge_pool.len() == self.players.len() - 1 {
|
||||
if self.judge_pile_meta.len() == self.players.len() - 1 {
|
||||
Ok(Some((
|
||||
JudgeRound {
|
||||
cards_to_judge: self
|
||||
.judge_pool
|
||||
.judge_pile_meta
|
||||
.keys()
|
||||
.into_iter()
|
||||
.map(|group| {
|
||||
group
|
||||
.iter()
|
||||
.map(|id| WhiteCardMeta {
|
||||
uuid: id.to_string(),
|
||||
text: "test".to_string(),
|
||||
.map(|id| {
|
||||
let card = self.judge_pile.get(id).unwrap().clone();
|
||||
WhiteCardMeta {
|
||||
uuid: card.uuid.to_string(),
|
||||
text: card.text.clone(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
|
@ -172,14 +204,35 @@ impl Game {
|
|||
|
||||
/// Tick forward
|
||||
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
|
||||
.get_mut(&winner_id)
|
||||
.unwrap()
|
||||
.black
|
||||
.push(self.current_black.clone());
|
||||
|
||||
// Draw new black card
|
||||
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.
|
||||
|
|
|
@ -101,7 +101,7 @@ impl GameHandler {
|
|||
let winning_player_id = this_game
|
||||
.read()
|
||||
.unwrap()
|
||||
.judge_pool
|
||||
.judge_pile_meta
|
||||
.get(&request.winning_cards)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
@ -141,7 +141,6 @@ impl GameHandler {
|
|||
.clone();
|
||||
|
||||
// Do the stuff
|
||||
|
||||
match this_game
|
||||
.write()
|
||||
.unwrap()
|
||||
|
|
Loading…
Add table
Reference in a new issue