Skip to content

Commit

Permalink
Fix broken tests, remove unnecessary clones, and cleanup some unused
Browse files Browse the repository at this point in the history
code
  • Loading branch information
BakerNet committed Aug 25, 2024
1 parent fa9aeef commit f41cc81
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 267 deletions.
177 changes: 89 additions & 88 deletions minesweeper-lib/src/game.rs

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions minesweeper-lib/src/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl MinesweeperReplay {
self.current_play
}

pub fn current_board(&self) -> Board<PlayerCell> {
self.current_board.clone()
pub fn current_board(&self) -> &Board<PlayerCell> {
&self.current_board
}

pub fn advance(&mut self) -> Result<()> {
Expand Down Expand Up @@ -287,37 +287,37 @@ mod test {
// test advance
assert_eq!(replay.len(), 5);
assert!(matches!(replay.advance(), Ok(())));
assert_eq!(replay.current_board(), expected_board_1);
assert_eq!(replay.current_board(), &expected_board_1);
assert!(matches!(replay.advance(), Ok(())));
assert_eq!(replay.current_board(), expected_board_2);
assert_eq!(replay.current_board(), &expected_board_2);
assert!(matches!(replay.advance(), Ok(())));
assert_eq!(replay.current_board(), expected_board_3);
assert_eq!(replay.current_board(), &expected_board_3);
assert!(matches!(replay.advance(), Ok(())));
assert_eq!(replay.current_board(), expected_final_board);
assert_eq!(replay.current_board(), &expected_final_board);

// should error on advance at end
assert!(replay.advance().is_err());

// test rewind
assert!(matches!(replay.rewind(), Ok(())));
assert_eq!(replay.current_board(), expected_board_3);
assert_eq!(replay.current_board(), &expected_board_3);
assert!(matches!(replay.rewind(), Ok(())));
assert_eq!(replay.current_board(), expected_board_2);
assert_eq!(replay.current_board(), &expected_board_2);
assert!(matches!(replay.rewind(), Ok(())));
assert_eq!(replay.current_board(), expected_board_1);
assert_eq!(replay.current_board(), &expected_board_1);
assert!(matches!(replay.rewind(), Ok(())));
assert_eq!(replay.current_board(), expected_starting_board);
assert_eq!(replay.current_board(), &expected_starting_board);

// should error on rewind at beginning
assert!(replay.rewind().is_err());

// try to_pos (auto advance/rewind)
assert!(matches!(replay.to_pos(2), Ok(())));
assert_eq!(replay.current_board(), expected_board_2);
assert_eq!(replay.current_board(), &expected_board_2);
assert!(matches!(replay.to_pos(4), Ok(())));
assert_eq!(replay.current_board(), expected_final_board);
assert_eq!(replay.current_board(), &expected_final_board);
assert!(matches!(replay.to_pos(1), Ok(())));
assert_eq!(replay.current_board(), expected_board_1);
assert_eq!(replay.current_board(), &expected_board_1);

assert!(replay.to_pos(5).is_err());
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/app/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct FrontendUser {
}

impl FrontendUser {
pub fn display_name_or_anon(display_name: &Option<String>, is_user: bool) -> String {
pub fn display_name_or_anon(display_name: Option<&String>, is_user: bool) -> String {
if let Some(name) = display_name {
name.to_owned()
} else if is_user {
Expand Down
18 changes: 9 additions & 9 deletions web/src/app/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ fn logo() -> impl IntoView {
let cell_class_flag = cell_class("", "bg-neutral-500 hover:bg-neutral-600/90");
view! {
<span class="whitespace-nowrap">
<span class=cell_class_4.clone()>M</span>
<span class=cell_class_2.clone()>i</span>
<span class=cell_class_3.clone()>n</span>
<span class=&cell_class_4>M</span>
<span class=&cell_class_2>i</span>
<span class=&cell_class_3>n</span>
<span class=cell_class_3>e</span>
<span class=cell_class_4>s</span>
<span class=cell_class_2.clone()>w</span>
<span class=&cell_class_2>w</span>
<span class=cell_class_2>e</span>
<span class=cell_class_1.clone()>e</span>
<span class=&cell_class_1>e</span>
<span class=cell_class_flag>
<Flag />
</span>
<span class=cell_class_1.clone()>e</span>
<span class=&cell_class_1>e</span>
<span class=cell_class_1>r</span>
</span>
}
Expand All @@ -42,18 +42,18 @@ where
None => view! {
"Guest ("
<A href="/auth/login" class=aclass>
Log in
"Log in"
</A>
")"
}
.into_view(),
Some(user) => {
let name = FrontendUser::display_name_or_anon(&user.display_name, true);
let name = FrontendUser::display_name_or_anon(user.display_name.as_ref(), true);
view! {
{name}
" ("
<A href="/profile" class=aclass>
Profile
"Profile"
</A>
")"
}
Expand Down
5 changes: 4 additions & 1 deletion web/src/app/minesweeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ impl From<&PlayerUser> for ClientPlayer {
fn from(value: &PlayerUser) -> Self {
ClientPlayer {
player_id: value.player as usize,
username: FrontendUser::display_name_or_anon(&value.display_name, value.user.is_some()),
username: FrontendUser::display_name_or_anon(
value.display_name.as_ref(),
value.user.is_some(),
),
dead: value.dead,
victory_click: value.victory_click,
top_score: value.top_score,
Expand Down
4 changes: 2 additions & 2 deletions web/src/app/minesweeper/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct PlayersContext {
impl PlayersContext {
pub fn from(frontend_game: &FrontendGame) -> Self {
PlayersContext {
game_id: frontend_game.game_id.clone(),
game_id: Rc::clone(&frontend_game.game_id),
is_owner: frontend_game.is_owner,
has_owner: frontend_game.has_owner,
player_id: frontend_game.player_id,
Expand Down Expand Up @@ -103,7 +103,7 @@ impl FrontendGame {
let rows = game_info.rows;
let cols = game_info.cols;
FrontendGame {
game_id: Rc::new(game_info.game_id.clone()),
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,
Expand Down
41 changes: 21 additions & 20 deletions web/src/app/minesweeper/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,7 @@ pub async fn get_game(game_id: String) -> Result<GameInfo, ServerFnError> {
} else {
None
};
let players_simple = players
.into_iter()
.map(ClientPlayer::from)
.collect::<Vec<_>>();
let players_frontend =
players_simple
.iter()
.fold(vec![None; game.max_players as usize], |mut acc, p| {
acc[p.player_id] = Some(p.clone());
acc
});
let players_simple = players.iter().map(ClientPlayer::from).collect::<Vec<_>>();
let final_board = match (game.final_board, game_log) {
(Some(board), Some(game_log)) => {
let completed_minesweeper = CompletedMinesweeper::from_log(
Expand All @@ -90,6 +80,14 @@ pub async fn get_game(game_id: String) -> Result<GameInfo, ServerFnError> {
game.rows as usize
]),
};
let players_frontend =
players
.into_iter()
.fold(vec![None; game.max_players as usize], |mut acc, p| {
let index = p.player as usize;
acc[index] = Some(ClientPlayer::from(p));
acc
});
Ok(GameInfo {
game_id: game.game_id,
has_owner: game.owner.is_some(),
Expand Down Expand Up @@ -132,7 +130,8 @@ pub fn Game() -> impl IntoView {
view! {
<ErrorBoundary fallback=|_| {
view! { <div class="text-red-600">"Game not found"</div> }
}>{move || { game_info.clone().map(game_view) }}</ErrorBoundary>
}>{game_info.map(game_view)
}</ErrorBoundary>
}
})
}}
Expand Down Expand Up @@ -168,19 +167,22 @@ where
{
let (error, set_error) = create_signal::<Option<String>>(None);

let game_id = game_info.game_id.clone();
let UseWebSocketReturn {
ready_state,
message,
send,
..
} = use_websocket::<String, FromToStringCodec>(&format!("/api/websocket/game/{}", &game_id));
} = use_websocket::<String, FromToStringCodec>(&format!(
"/api/websocket/game/{}",
&game_info.game_id
));

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

provide_context::<PlayersContext>(PlayersContext::from(&game));
let players_context = PlayersContext::from(&game);

let game_id = game_info.game_id.clone();
create_effect(move |_| {
log::debug!("before ready_state");
let state = ready_state();
Expand Down Expand Up @@ -276,9 +278,9 @@ where
view! {
<div class="text-center">
<h3 class="text-4xl my-4 text-gray-900 dark:text-gray-200">
Game: {{ game_info.game_id.clone() }}
"Game: "{ &game_info.game_id }
</h3>
<ActivePlayers />
<ActivePlayers players_context />
<GameWidgets>
<ActiveMines num_mines=game_info.num_mines flag_count=game.flag_count />
<CopyGameLink game_id=game_info.game_id />
Expand Down Expand Up @@ -322,7 +324,6 @@ where

#[component]
pub fn InactiveGame(game_info: GameInfo) -> impl IntoView {
let players = game_info.players.clone();
let game_settings = GameSettings::from(&game_info);
let game_time = match (game_info.start_time, game_info.end_time) {
(Some(st), Some(et)) => et.signed_duration_since(st).num_seconds(),
Expand All @@ -338,9 +339,9 @@ pub fn InactiveGame(game_info: GameInfo) -> impl IntoView {
view! {
<div class="text-center">
<h3 class="text-4xl my-4 text-gray-900 dark:text-gray-200">
Game: {{ game_info.game_id.clone() }}
"Game: "{ &game_info.game_id }
</h3>
<InactivePlayers players />
<InactivePlayers players=game_info.players />
<GameWidgets>
<InactiveMines num_mines=num_mines />
<CopyGameLink game_id=game_info.game_id />
Expand Down
Loading

0 comments on commit f41cc81

Please sign in to comment.