score round and draw new black card
This commit is contained in:
parent
97e0a69a7b
commit
f65da8eaca
4 changed files with 70 additions and 15 deletions
|
@ -179,15 +179,29 @@ pub fn Game() -> impl IntoView {
|
|||
</span>
|
||||
<span class="absolute top-0 right-0">
|
||||
<p>Players:</p>
|
||||
<ul>
|
||||
<table class="min-w-full border border-collapse table-auto">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="border-b">Name</th>
|
||||
<th class="border-b">Score</th>
|
||||
</tr>
|
||||
</thead>
|
||||
{move || {
|
||||
game_players()
|
||||
.iter()
|
||||
.map(|player| view! { <li>{player}</li> })
|
||||
.map(|player| {
|
||||
view! {
|
||||
<tr>
|
||||
<td class="text-center border-b">{&player.name}</td>
|
||||
<td class="text-center border-b">
|
||||
{&player.score.to_string()}
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
})
|
||||
.collect_view()
|
||||
}}
|
||||
|
||||
</ul>
|
||||
</table>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -100,7 +100,7 @@ pub fn Websocket() -> impl IntoView {
|
|||
} else if let Ok(judge_update) = from_str::<JudgeRound>(message) {
|
||||
set_judge_round(Some(judge_update));
|
||||
} else {
|
||||
logging::log!("Unhandled message: {}", message);
|
||||
logging::log!("Unhandled message: {:#?}", message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -39,13 +39,20 @@ pub struct WhiteCardMeta {
|
|||
pub text: String,
|
||||
}
|
||||
|
||||
/// Game Player Meta
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct GamePlayerMeta {
|
||||
pub name: String,
|
||||
pub score: usize,
|
||||
}
|
||||
|
||||
/// Game meta
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct GameStateMeta {
|
||||
pub uuid: String,
|
||||
pub name: String,
|
||||
pub host: String,
|
||||
pub players: Vec<String>,
|
||||
pub players: Vec<GamePlayerMeta>,
|
||||
pub czar: String,
|
||||
pub black: (String, u8),
|
||||
pub white: Vec<WhiteCardMeta>,
|
||||
|
|
|
@ -105,7 +105,7 @@ impl GameHandler {
|
|||
== this_user_id
|
||||
{
|
||||
// Find user who submitted the card
|
||||
let winning_user_id = this_game
|
||||
let winning_player_id = this_game
|
||||
.read()
|
||||
.unwrap()
|
||||
.judge_pool
|
||||
|
@ -113,7 +113,13 @@ impl GameHandler {
|
|||
.unwrap()
|
||||
.clone();
|
||||
|
||||
tracing::debug!("{:#?} Won the round!", winning_user_id);
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -269,19 +275,24 @@ impl GameHandler {
|
|||
.unwrap()
|
||||
.clone();
|
||||
|
||||
let players = this_game
|
||||
.read()
|
||||
.unwrap()
|
||||
.players
|
||||
.values()
|
||||
.map(|player| GamePlayerMeta {
|
||||
name: player.user.read().unwrap().name.clone(),
|
||||
score: player.black.len(),
|
||||
})
|
||||
.collect::<Vec<GamePlayerMeta>>();
|
||||
|
||||
for player in this_game.read().unwrap().players.values() {
|
||||
// Create update for user's game view
|
||||
let meta = GameStateMeta {
|
||||
uuid: game_id.clone(),
|
||||
name: this_game.read().unwrap().name.clone(),
|
||||
host: this_game.read().unwrap().host.read().unwrap().name.clone(),
|
||||
players: this_game
|
||||
.read()
|
||||
.unwrap()
|
||||
.players
|
||||
.values()
|
||||
.map(|player| player.user.read().unwrap().name.clone())
|
||||
.collect(),
|
||||
players: players.clone(),
|
||||
czar: this_game.read().unwrap().host.read().unwrap().name.clone(),
|
||||
black: (
|
||||
this_game.read().unwrap().current_black.text.clone(),
|
||||
|
@ -525,6 +536,29 @@ impl Game {
|
|||
}
|
||||
}
|
||||
|
||||
/// Tick forward
|
||||
pub fn finish_round(&mut self, winner_id: String) {
|
||||
self.players
|
||||
.get_mut(&winner_id)
|
||||
.unwrap()
|
||||
.black
|
||||
.push(self.current_black.clone());
|
||||
|
||||
self.current_black = self.draw_one_black().unwrap();
|
||||
}
|
||||
|
||||
/// Draw one black card at random from play deck.
|
||||
fn draw_one_black(&mut self) -> Option<Arc<CardBlackWithID>> {
|
||||
let deck = &mut self.black;
|
||||
|
||||
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
|
||||
Some(deck.swap_remove(index))
|
||||
} else {
|
||||
tracing::error!("Tried to draw white card that doesn't exist!");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw one white card at random from play deck.
|
||||
fn draw_one_white(&mut self) -> Option<Arc<CardWhiteWithID>> {
|
||||
let deck = &mut self.white;
|
||||
|
|
Loading…
Add table
Reference in a new issue