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