diff options
| author | Jomar Milan <jomarm@jomarm.com> | 2026-06-10 23:59:11 -0700 |
|---|---|---|
| committer | Jomar Milan <jomarm@jomarm.com> | 2026-06-10 23:59:11 -0700 |
| commit | 24c4f77ae0806153436ad18c45669967b5be219b (patch) | |
| tree | 3e21526c0bc774b9742a4d30efa63834a88c5055 | |
| parent | 4eb1832c1a5b022d57a22fdfb256a31de08b8da5 (diff) | |
Add function to update session hands
| -rw-r--r-- | src/main.rs | 5 | ||||
| -rw-r--r-- | src/session.rs | 70 |
2 files changed, 61 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index a581b2b..02427e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ mod play; mod session; mod template; -use crate::play::{PlayUpdate, handle_play}; +use crate::play::handle_play; use crate::session::{HandObject, Session}; use crate::template::{IndexTemplate, SessionTemplate}; use askama::Template; @@ -137,8 +137,7 @@ async fn update_hands( Some(session) => { let mut session = session.lock().unwrap(); - session.seats = payload.to_owned(); - let _ = session.update_tx.send(PlayUpdate::HandUpdate(payload)); + session.update_hands(payload); StatusCode::NO_CONTENT } diff --git a/src/session.rs b/src/session.rs index d4343ee..93ea838 100644 --- a/src/session.rs +++ b/src/session.rs @@ -3,41 +3,42 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use tokio::sync::broadcast; +#[derive(Debug)] pub struct Session { pub steam_name: String, pub seats: HashMap<String, Vec<HandObject>>, pub update_tx: broadcast::Sender<PlayUpdate>, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] pub enum HandObject { CustomDeck(CustomDeck), } // TODO: These fields will be used in the future. When they are, the dead_code lint should no longer // be suppressed. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] #[allow(dead_code)] pub struct CustomDeck { /// The path/URL of the face cardsheet. - face: String, + pub face: String, /// The path/URL of the back cardsheet or card back. - back: String, + pub back: String, /// If each card has a unique card back (via a cardsheet). - unique_back: bool, + pub unique_back: bool, /// The number of columns on the cardsheet. - width: f64, + pub width: f64, /// The number of rows on the cardsheet. - height: f64, + pub height: f64, /// The number of cards on the cardsheet. - number: f64, + pub number: f64, /// Whether the cards are horizontal, instead of vertical. - sideways: bool, + pub sideways: bool, /// Whether the card back should be used as the hidden image (instead of the last slot of the /// `face` image). - back_is_hidden: bool, + pub back_is_hidden: bool, /// ID of the custom card within the deck. - card_id: f64, + pub card_id: f64, } impl Session { @@ -50,4 +51,51 @@ impl Session { update_tx, } } + + pub fn update_hands(&mut self, hands: HashMap<String, Vec<HandObject>>) { + self.seats = hands.to_owned(); + // Updating the hand is a success regardless of whether there are players connected to + // receive a hand update + let _ = self.update_tx.send(PlayUpdate::HandUpdate(hands)); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn hand_update_transmit() { + let mut session = Session::new("Sir Harold".to_string()); + let mut update_rx = session.update_tx.subscribe(); + + let card = CustomDeck { + face: "https://steamusercontent-a.akamaihd.net/ugc/1663479592506990057/B6EEB9A683A57C9A41CC9782993A8BAF9DCD72A1/".to_string(), + back: "https://steamusercontent-a.akamaihd.net/ugc/1663479592507076702/D16FFBC8D87B4D4FB21C0057F2BBC9DC4D4FD379/".to_string(), + unique_back: false, + width: 5.0, + height: 7.0, + number: 6.0, + sideways: false, + back_is_hidden: false, + card_id: 0.0, + }; + + let hands = HashMap::from([( + "red".to_string(), + vec![HandObject::CustomDeck(card.to_owned())], + )]); + session.update_hands(hands); + + // TODO: This lint allow be removed when PlayUpdate has more variants + #[allow(irrefutable_let_patterns)] + let PlayUpdate::HandUpdate(hand) = update_rx.recv().await.unwrap() else { + panic!("Received update was not a HandUpdate"); + }; + + assert_eq!( + hand.get("red").unwrap().first().unwrap().to_owned(), + HandObject::CustomDeck(card) + ); + } } |
