Handle the initial incoming cards list on game join in gm_view
This commit is contained in:
parent
c5c3cea79f
commit
f454f04ac1
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@ result
|
||||
*.sqlite-shm
|
||||
*.sqlite-wal
|
||||
file-service/var
|
||||
.obsidian
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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())
|
||||
})
|
||||
|
||||
@ -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)]
|
||||
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user