game hopping

This commit is contained in:
Adam 2024-08-18 19:51:45 -04:00
parent fc33f0fef8
commit 11a694bbac
2 changed files with 32 additions and 56 deletions

View file

@ -17,11 +17,10 @@ pub fn Game() -> impl IntoView {
let (game_czar, set_game_czar) = create_signal("".to_string());
let (game_black, set_game_black) = create_signal(("".to_string(), 0u8));
let (game_white, set_game_white) = create_signal(vec![]);
let (selected_cards, set_selected_cards) =
let (player_white, set_player_white) =
create_signal::<HashMap<String, WhiteCardMeta>>(HashMap::new());
let (selected_cards_ordered, set_selected_cards_ordered) = create_signal::<Vec<String>>(vec![]);
let (player_hand, set_player_hand) =
create_signal::<HashMap<String, WhiteCardMeta>>(HashMap::new());
let (player_hand, set_player_hand) = create_signal::<Vec<String>>(vec![]);
let (card_clicked, set_card_clicked) = create_signal::<String>(String::new());
provide_context::<WriteSignal<String>>(set_card_clicked);
@ -36,55 +35,46 @@ pub fn Game() -> impl IntoView {
set_game_white(game.white.clone());
// Primitive reset when joining new game
set_player_hand(HashMap::new());
set_player_hand(vec![]);
set_selected_cards_ordered(vec![]);
// Load hand
for card in game.white {
set_player_hand.update(|hand| {
set_player_white.update(|hand| {
hand.insert(card.uuid.clone(), card.clone());
})
});
set_player_hand.update(|hand| {
hand.push(card.uuid.clone());
});
}
}
});
// Move cards back and forth from hand to selected when clicked
create_effect(move |_| {
// Move the card
if player_hand().contains_key(&card_clicked()) {
set_selected_cards.update(|cards| {
cards.insert(
card_clicked(),
set_player_hand
.try_update(|cards| cards.remove(&card_clicked()))
.unwrap()
.unwrap(),
);
if let Some(card_index) = player_hand()
.iter()
.position(|card| *card == card_clicked())
{
set_player_hand.update(|list| {
list.remove(card_index);
});
// Track ordering
set_selected_cards_ordered.update(|list| {
list.push(card_clicked());
});
} else if selected_cards().contains_key(&card_clicked()) {
// Update tracking before moving the card or else the view tries to display a card that
// doesn't exist
set_selected_cards_ordered.update(|list| {
list.remove(
list.iter()
})
} else if let Some(card_index) = selected_cards_ordered()
.iter()
.position(|card| *card == card_clicked())
.unwrap(),
);
{
set_selected_cards_ordered.update(|list| {
list.remove(card_index);
});
set_player_hand.update(|cards| {
cards.insert(
card_clicked(),
set_selected_cards
.try_update(|cards| cards.remove(&card_clicked()))
.unwrap()
.unwrap(),
);
});
set_player_hand.update(|list| {
list.push(card_clicked());
})
}
});
@ -127,20 +117,18 @@ pub fn Game() -> impl IntoView {
// Card selection
<For
each=move || selected_cards_ordered()
key=move |card| selected_cards().get(card).unwrap().uuid.clone()
key=move |card| player_white().get(card).unwrap().uuid.clone()
children=move |card| {
view! {
<WhiteCard card_data=selected_cards().get(&card).unwrap().clone() />
}
view! { <WhiteCard card_data=player_white().get(&card).unwrap().clone() /> }
}
/>
</span>
<div class="inline-flex flex-wrap justify-center">
<For
each=move || player_hand()
key=|card| card.0.clone()
key=move |card| player_white().get(card).unwrap().uuid.clone()
children=move |card| {
view! { <WhiteCard card_data=card.1 /> }
view! { <WhiteCard card_data=player_white().get(&card).unwrap().clone() /> }
}
/>

View file

@ -110,27 +110,15 @@ impl GameHandler {
.unwrap()
.clone();
if this_game
if !this_game
.read()
.unwrap()
.players
.contains_key(&this_user.read().unwrap().uuid)
{
self.state
.users_tx
.send(DmUserAddr {
addr,
message: SendChatMessage(ChatMessage {
text: "You're already in this game!".to_owned(),
}),
})
.await
.unwrap();
return;
}
// Create player
this_game.write().unwrap().create_player(this_user);
}
// Send updates for all players
for player in this_game.read().unwrap().players.values() {