Skip to content

Commit

Permalink
refresh game if sync happens
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed May 16, 2022
1 parent 457eb5b commit b198042
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
7 changes: 5 additions & 2 deletions ui/src/game_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BitMove>) -> &mut Self {
self.board.set_board_state(fen);
if let Some(mv) = mv {
self.board.set_last_move(mv);
}
self
}

Expand Down
25 changes: 22 additions & 3 deletions ui/src/screen_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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()?;
}
}
Expand Down Expand Up @@ -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<P: AsRef<Path>>(path: P) -> anyhow::Result<GameCollection> {
let mut games = GameCollection::default();
if path.as_ref().exists() {
Expand All @@ -282,3 +297,7 @@ fn load_games<P: AsRef<Path>>(path: P) -> anyhow::Result<GameCollection> {
}
Ok(games)
}

fn current_timestamp() -> u64 {
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
}

0 comments on commit b198042

Please sign in to comment.