Handle the initial incoming cards list on game join in gm_view

This commit is contained in:
Savanni D'Gerinel 2025-11-07 12:39:45 -05:00
parent c5c3cea79f
commit f454f04ac1
5 changed files with 19 additions and 55 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ result
*.sqlite-shm
*.sqlite-wal
file-service/var
.obsidian

View File

@ -87,6 +87,11 @@ impl Dispatcher {
))
.await
.unwrap();
socket
.sender
.send(GameMessage::Cards(state.cards(&game_id).cloned().collect()))
.await
.unwrap();
} else {
let mut character_ids = state.characters_for_user(&game_id, &user.id);
if let Some(id) = character_ids.next() {

View File

@ -35,9 +35,9 @@ pub async fn create_card(
clone!((state, request, dispatcher), async move {
let Json(CreateCardRequest(card)) = request;
let mut state = state.write().await;
let card_id = state.create_card(card);
let card_id = state.create_card(card.clone());
dispatcher
.send_to_user(&user.id, GameMessage::NewCard(card_id.clone()))
.send_to_user(&user.id, GameMessage::Card(card))
.await;
(StatusCode::OK, card_id.as_str().to_owned())
})
@ -55,9 +55,9 @@ pub async fn update_card(
clone!((state, request, dispatcher), async move {
let Json(UpdateCardRequest(card)) = request;
let mut state = state.write().await;
let card_id = state.update_card(card);
let card_id = state.update_card(card.clone());
dispatcher
.send_to_user(&user.id, GameMessage::UpdateCard(card_id.clone()))
.send_to_user(&user.id, GameMessage::Card(card))
.await;
(StatusCode::OK, card_id.as_str().to_owned())
})

View File

@ -7,9 +7,8 @@ pub enum GameRequest {
JoinGame(SessionId, GameId),
SetScene(GameId, SceneId),
SetBackground(String),
PlaceCard(CardId, ScreenPosition),
MoveCard(CardId, ScreenPosition),
TakeCard(CardId),
CardNew(Card),
CardUpdate(Card),
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
@ -18,15 +17,12 @@ pub enum GameMessage {
AvailableImages(Vec<String>),
AvailableScenes(Vec<(SceneId, String)>),
Background(String),
CardSidebar,
Card(Card),
Cards(Vec<Card>),
CharacterSheets(Vec<Charsheet>),
Charsheet(Charsheet),
NewCard(CardId),
MoveCard(CardId, ScreenPosition),
PlaceCard(Card, ScreenPosition),
Tabletop(String),
Title(String),
UpdateCard(CardId),
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]

View File

@ -370,26 +370,6 @@ fn GmView_<C: Client + Clone + 'static>(
}),
);
use_effect_with(
(game.id.clone(), client.clone()),
clone!(state, move |(game_id, client)| {
clone!(
(game_id, client, state),
wasm_bindgen_futures::spawn_local(async move {
match client.get_user_cards(game_id).await {
Ok(cards) => {
state.dispatch(ViewStateAction::SetCards(cards));
}
Err(_err) => {
error!("error retrieving cards");
}
}
})
);
|| ()
}),
);
html! {
<View game_id={game.id.clone()}
system={game.system.clone()}
@ -437,14 +417,11 @@ pub fn GmView<C: Client + Clone + 'static>(GmViewProps { client, game }: &GmView
GameMessage::Title(title) => {
view_state.dispatch(ViewStateAction::SetTitle(title));
}
GameMessage::NewCard(card_id) | GameMessage::UpdateCard(card_id) => {
clone!(
(client, view_state),
wasm_bindgen_futures::spawn_local(async move {
let _ =
update_cards(client.clone(), view_state.clone(), &card_id).await;
})
);
GameMessage::Card(card) => {
view_state.dispatch(ViewStateAction::UpdateCard(card));
}
GameMessage::Cards(cards) => {
view_state.dispatch(ViewStateAction::SetCards(cards));
}
_ => unimplemented!(),
}
@ -464,18 +441,3 @@ pub fn GmView<C: Client + Clone + 'static>(GmViewProps { client, game }: &GmView
}
}
async fn update_cards<C: Client>(
client: C,
view_state: UseReducerHandle<ViewState>,
card_id: &CardId,
) {
match client.get_card(&card_id).await {
Ok(card) => {
view_state.dispatch(ViewStateAction::UpdateCard(card));
}
Err(_err) => {
log!("failed to get a card");
}
}
}