From b198042db30358a4f90241cd9f7ea2040490530d Mon Sep 17 00:00:00 2001 From: Stan Date: Mon, 16 May 2022 11:53:21 +0400 Subject: [PATCH] refresh game if sync happens --- ui/src/game_screen.rs | 7 +++++-- ui/src/screen_manager.rs | 25 ++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/ui/src/game_screen.rs b/ui/src/game_screen.rs index 5eb6671..3f206a0 100644 --- a/ui/src/game_screen.rs +++ b/ui/src/game_screen.rs @@ -81,8 +81,11 @@ impl GameScreen { self.seq } - pub fn set_board_state(&mut self, fen: &str, mv: BitMove) -> &mut Self { - self.board.set_board_state(fen).set_last_move(mv); + pub fn set_board_state(&mut self, fen: &str, mv: Option) -> &mut Self { + self.board.set_board_state(fen); + if let Some(mv) = mv { + self.board.set_last_move(mv); + } self } diff --git a/ui/src/screen_manager.rs b/ui/src/screen_manager.rs index 5ada956..7ea994b 100644 --- a/ui/src/screen_manager.rs +++ b/ui/src/screen_manager.rs @@ -70,7 +70,7 @@ impl ScreenManager { seq: 0, player: Player::White, result: GameResult::None, - last_activity: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(), + last_activity: current_timestamp(), }); self.save_games().unwrap(); self.send_message(ChessOperation { @@ -137,7 +137,7 @@ impl ScreenManager { self.channel.try_send(msg).unwrap(); if let Some(game_mut) = self.games.get_mut(game.game_id()) { game_mut.board_fen = game.to_board_fen(); - game_mut.last_activity = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); + game_mut.last_activity = current_timestamp(); } } @@ -218,7 +218,7 @@ impl ScreenManager { if screen.game_id() == op.game_id { let mv = BitMove::new(*mv); log::info!("Move played for active game {}", mv); - screen.set_board_state(&board, mv).set_seq(op.seq); + screen.set_board_state(&board, Some(mv)).set_seq(op.seq); } } } @@ -246,6 +246,8 @@ impl ScreenManager { // Update our game state game_mut.board_fen = board.clone(); game_mut.seq = op.seq; + game_mut.last_activity = current_timestamp(); + self.active_screen.refresh_game(&*game_mut); self.save_games()?; } } @@ -274,6 +276,19 @@ enum Screen { Game(GameScreen), } +impl Screen { + pub fn refresh_game(&mut self, game: &Game) { + match self { + Screen::Start(_) => {}, + Screen::Game(g) => { + if g.game_id() == game.id { + g.set_board_state(&game.board_fen, None); + } + }, + } + } +} + fn load_games>(path: P) -> anyhow::Result { let mut games = GameCollection::default(); if path.as_ref().exists() { @@ -282,3 +297,7 @@ fn load_games>(path: P) -> anyhow::Result { } Ok(games) } + +fn current_timestamp() -> u64 { + SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() +}