Skip to content

Commit

Permalink
Rc all the Vecs!
Browse files Browse the repository at this point in the history
  • Loading branch information
BakerNet committed Aug 28, 2024
1 parent 4c2d2b7 commit 9c9f151
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
20 changes: 10 additions & 10 deletions web/src/app/minesweeper/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct PlayersContext {
pub is_owner: bool,
pub has_owner: bool,
pub player_id: ReadSignal<Option<usize>>,
pub players: Vec<ReadSignal<Option<ClientPlayer>>>,
pub players: Rc<Vec<ReadSignal<Option<ClientPlayer>>>>,
pub players_loaded: ReadSignal<bool>,
pub join_trigger: Trigger,
pub started: ReadSignal<bool>,
Expand All @@ -32,7 +32,7 @@ impl PlayersContext {
is_owner: frontend_game.is_owner,
has_owner: frontend_game.has_owner,
player_id: frontend_game.player_id,
players: frontend_game.players.clone(),
players: Rc::clone(&frontend_game.players),
players_loaded: frontend_game.players_loaded,
join_trigger: frontend_game.join_trigger,
started: frontend_game.started,
Expand All @@ -46,18 +46,18 @@ pub struct FrontendGame {
pub is_owner: bool,
pub has_owner: bool,
pub player_id: ReadSignal<Option<usize>>,
pub players: Vec<ReadSignal<Option<ClientPlayer>>>,
pub players: Rc<Vec<ReadSignal<Option<ClientPlayer>>>>,
pub players_loaded: ReadSignal<bool>,
pub err_signal: WriteSignal<Option<String>>,
pub join_trigger: Trigger,
pub started: ReadSignal<bool>,
pub completed: ReadSignal<bool>,
pub sync_time: ReadSignal<Option<usize>>,
pub flag_count: ReadSignal<usize>,
pub cells: Vec<Vec<ReadSignal<PlayerCell>>>,
cell_signals: Vec<Vec<WriteSignal<PlayerCell>>>,
pub cells: Rc<Vec<Vec<ReadSignal<PlayerCell>>>>,
cell_signals: Rc<Vec<Vec<WriteSignal<PlayerCell>>>>,
set_player_id: WriteSignal<Option<usize>>,
player_signals: Vec<WriteSignal<Option<ClientPlayer>>>,
player_signals: Rc<Vec<WriteSignal<Option<ClientPlayer>>>>,
set_players_loaded: WriteSignal<bool>,
set_started: WriteSignal<bool>,
set_completed: WriteSignal<bool>,
Expand Down Expand Up @@ -94,12 +94,12 @@ impl FrontendGame {
game_id: Rc::new(game_info.game_id.to_owned()),
is_owner: game_info.is_owner,
has_owner: game_info.has_owner,
cells: read_signals,
cell_signals: write_signals,
cells: read_signals.into(),
cell_signals: write_signals.into(),
player_id,
set_player_id,
players,
player_signals,
players: players.into(),
player_signals: player_signals.into(),
players_loaded,
set_players_loaded,
err_signal,
Expand Down
25 changes: 14 additions & 11 deletions web/src/app/minesweeper/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,15 @@ where
&game_info.game_id
));

let game = FrontendGame::new(&game_info, set_error, Rc::new(send.clone()));
let (game_signal, _) = create_signal(game.clone());

let game = FrontendGame::new(&game_info, set_error, Rc::new(send));
let players_context = PlayersContext::from(&game);
let flag_count = game.flag_count;
let completed = game.completed;
let sync_time = game.sync_time;
let cells = Rc::clone(&game.cells);
let players = Rc::clone(&game.players);

let (game_signal, _) = create_signal(game);

let game_id = game_info.game_id.clone();
create_effect(move |_| {
Expand Down Expand Up @@ -399,20 +404,17 @@ where
}
};

let players = players_context.players.clone();

view! {
<ActivePlayers players title="Players">
<PlayerButtons players_context />
</ActivePlayers>
<GameWidgets>
<ActiveMines num_mines=game_info.num_mines flag_count=game.flag_count />
<ActiveMines num_mines=game_info.num_mines flag_count />
<CopyGameLink game_id=game_info.game_id />
<ActiveTimer sync_time=game.sync_time completed=game.completed />
<ActiveTimer sync_time completed />
</GameWidgets>
<GameBorder set_active=set_game_is_active>
{game
.cells
{cells
.iter()
.enumerate()
.map(move |(row, vec)| {
Expand Down Expand Up @@ -513,6 +515,7 @@ fn ReplayGame(replay_data: GameInfoWithLog) -> impl IntoView {
.collect::<(Vec<_>, Vec<_>)>();

let (cell_read_signals, cell_write_signals) = signals_from_board(&game_info.final_board);
let cell_read_signals = Rc::new(cell_read_signals);

let completed_minesweeper = CompletedMinesweeper::from_log(
Board::from_vec(game_info.final_board),
Expand All @@ -523,10 +526,10 @@ fn ReplayGame(replay_data: GameInfoWithLog) -> impl IntoView {
.replay(replay_data.player_num.map(|p| p.into()))
.expect("We are guaranteed log is not None");

let cells = cell_read_signals.clone();
let cells = Rc::clone(&cell_read_signals);

view! {
<ActivePlayers players=player_read_signals title="Replay">
<ActivePlayers players=player_read_signals.into() title="Replay">
{}
</ActivePlayers>
<GameWidgets>
Expand Down
7 changes: 4 additions & 3 deletions web/src/app/minesweeper/players.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use leptos::*;
use leptos_router::*;
use std::rc::Rc;

use minesweeper_lib::client::ClientPlayer;

Expand Down Expand Up @@ -38,7 +39,7 @@ fn Scoreboard(children: Children) -> impl IntoView {

#[component]
pub fn ActivePlayers(
players: Vec<ReadSignal<Option<ClientPlayer>>>,
players: Rc<Vec<ReadSignal<Option<ClientPlayer>>>>,
title: &'static str,
children: Children,
) -> impl IntoView {
Expand All @@ -47,10 +48,10 @@ pub fn ActivePlayers(
<h4 class="text-2xl my-4 text-gray-900 dark:text-gray-200">{title}</h4>
<Scoreboard>
{players
.into_iter()
.iter()
.enumerate()
.map(move |(n, player)| {
view! { <ActivePlayer player_num=n player=player /> }
view! { <ActivePlayer player_num=n player=*player /> }
})
.collect_view()}
</Scoreboard>
Expand Down
12 changes: 6 additions & 6 deletions web/src/app/minesweeper/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use minesweeper_lib::{
#[allow(dead_code)]
struct ReplayStore {
replay: Rc<RefCell<MinesweeperReplay>>,
cell_read_signals: Vec<Vec<ReadSignal<PlayerCell>>>,
cell_write_signals: Vec<Vec<WriteSignal<PlayerCell>>>,
player_write_signals: Vec<WriteSignal<Option<ClientPlayer>>>,
cell_read_signals: Rc<Vec<Vec<ReadSignal<PlayerCell>>>>,
cell_write_signals: Rc<Vec<Vec<WriteSignal<PlayerCell>>>>,
player_write_signals: Rc<Vec<WriteSignal<Option<ClientPlayer>>>>,
}

impl ReplayStore {
Expand Down Expand Up @@ -61,7 +61,7 @@ impl ReplayStore {
#[component]
pub fn ReplayControls(
replay: MinesweeperReplay,
cell_read_signals: Vec<Vec<ReadSignal<PlayerCell>>>,
cell_read_signals: Rc<Vec<Vec<ReadSignal<PlayerCell>>>>,
cell_write_signals: Vec<Vec<WriteSignal<PlayerCell>>>,
set_flag_count: WriteSignal<usize>,
player_write_signals: Vec<WriteSignal<Option<ClientPlayer>>>,
Expand All @@ -80,8 +80,8 @@ pub fn ReplayControls(
let replay = ReplayStore {
replay: Rc::new(RefCell::new(replay)),
cell_read_signals,
cell_write_signals,
player_write_signals,
cell_write_signals: cell_write_signals.into(),
player_write_signals: player_write_signals.into(),
};
let (replay, _) = create_signal(replay);

Expand Down

0 comments on commit 9c9f151

Please sign in to comment.