diff options
| author | Jomar Milan <jomarm@jomarm.com> | 2026-06-10 15:25:11 -0700 |
|---|---|---|
| committer | Jomar Milan <jomarm@jomarm.com> | 2026-06-10 15:25:11 -0700 |
| commit | c5f26f816dbd18f2dcd65bb8c8965183c4ae853d (patch) | |
| tree | 2e6114b692f927b6b49fa4a0ef1f054c72acbc70 | |
| parent | 067e9602ba4feccf947a138c9e73462c446268bc (diff) | |
Add syncing of color list to browser
| -rw-r--r-- | src/main.rs | 14 | ||||
| -rw-r--r-- | src/play.rs | 25 | ||||
| -rw-r--r-- | src/session.rs | 12 |
3 files changed, 26 insertions, 25 deletions
diff --git a/src/main.rs b/src/main.rs index 7162402..e43a337 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,8 @@ mod play; mod session; mod template; -use crate::play::handle_play; -use crate::session::{HandObject, PlayUpdate, Seat, Session}; +use crate::play::{PlayUpdate, handle_play}; +use crate::session::{HandObject, Session}; use crate::template::{IndexTemplate, SessionTemplate}; use askama::Template; use axum::extract::{Path, Query, State, WebSocketUpgrade}; @@ -125,15 +125,9 @@ async fn update_hands( Some(session) => { let mut session = session.lock().unwrap(); - for (color, hand) in payload { - let seat = session - .seats - .entry(color.to_owned()) - .or_insert_with(|| Seat { hand: Vec::new() }); + session.seats = payload.to_owned(); + let _ = session.update_tx.send(PlayUpdate::HandUpdate(payload)); - seat.hand = hand.to_owned(); - let _ = session.update_tx.send(PlayUpdate::HandUpdate(color, hand)); - } StatusCode::NO_CONTENT } None => StatusCode::NOT_FOUND, diff --git a/src/play.rs b/src/play.rs index 82380f9..c635595 100644 --- a/src/play.rs +++ b/src/play.rs @@ -1,8 +1,9 @@ use crate::AppState; -use crate::session::{HandObject, PlayUpdate, Session}; +use crate::session::{HandObject, Session}; use axum::extract::ws::{Message, Utf8Bytes, WebSocket}; use futures_util::{SinkExt, StreamExt}; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use std::sync::{Arc, Mutex, RwLock, Weak}; use tokio::sync::broadcast::Receiver; use tokio::sync::broadcast::error::RecvError; @@ -24,6 +25,11 @@ enum OutgoingPlayMessage { Error, } +#[derive(Clone)] +pub enum PlayUpdate { + HandUpdate(HashMap<String, Vec<HandObject>>), +} + pub async fn handle_play(socket: WebSocket, app_state: Arc<AppState>) { let (mut sender, mut receiver) = socket.split(); let (sender_tx, mut sender_rx) = mpsc::channel(2); @@ -126,7 +132,7 @@ pub async fn handle_play(socket: WebSocket, app_state: Arc<AppState>) { .unwrap() .seats .get(&color) - .map(|seat| seat.hand.to_owned()); + .map(|seat| seat.to_owned()); match hand { Some(hand) => { *player_color.write().unwrap() = color; @@ -171,10 +177,19 @@ async fn handle_update( ) { loop { match update_rx.recv().await { - Ok(PlayUpdate::HandUpdate(color, hand)) => { - if *player_color.read().unwrap() == color { + Ok(PlayUpdate::HandUpdate(hands)) => { + let _ = sender_tx + .send(OutgoingPlayMessage::Initialize { + colors: hands.keys().cloned().collect(), + }) + .await; + let hand = { + let color = player_color.read().unwrap(); + hands.get(&*color).map(ToOwned::to_owned) + }; + if let Some(hand) = hand { let _ = sender_tx.send(OutgoingPlayMessage::Hand(hand)).await; - } + }; } Err(RecvError::Closed) => break, Err(RecvError::Lagged(_)) => continue, diff --git a/src/session.rs b/src/session.rs index 4797c5c..d4343ee 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,10 +1,11 @@ +use crate::play::PlayUpdate; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use tokio::sync::broadcast; pub struct Session { pub steam_name: String, - pub seats: HashMap<String, Seat>, + pub seats: HashMap<String, Vec<HandObject>>, pub update_tx: broadcast::Sender<PlayUpdate>, } @@ -39,15 +40,6 @@ pub struct CustomDeck { card_id: f64, } -pub struct Seat { - pub hand: Vec<HandObject>, -} - -#[derive(Clone)] -pub enum PlayUpdate { - HandUpdate(String, Vec<HandObject>), -} - impl Session { pub fn new(steam_name: String) -> Self { let (update_tx, _) = broadcast::channel(10); |
