Skip to content

Commit 6f1a198

Browse files
fix: lichess use proper streaming (#189)
* feat: use streams instead of polling also fix a bug of game seek without the app running Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com> * chore: improve the piece style Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com> * docs: update the doc and new blog post Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com> * chore: clean remove useless code Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com> * fix: stop seeking game when we go back to the menu or join a game Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com> --------- Signed-off-by: Thomas Mauran <thomasmauran@yahoo.com>
1 parent c455543 commit 6f1a198

File tree

9 files changed

+517
-658
lines changed

9 files changed

+517
-658
lines changed

src/app.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,9 @@ impl App {
271271
self.current_popup = Some(Popups::SeekingLichessGame);
272272

273273
std::thread::spawn(move || {
274-
// Fetch user profile to know our ID
275-
let my_id = match client.get_my_profile() {
276-
Ok(id) => id,
277-
Err(e) => {
278-
let _ = tx.send(Err(format!("Failed to fetch profile: {}", e)));
279-
return;
280-
}
281-
};
282-
283274
// Seek a correspondence game (no timer) since timer isn't implemented yet
284275
// Using 0,0 which will trigger the days parameter in seek_game
285-
match client.seek_game(0, 0, cancellation_token, my_id) {
276+
match client.seek_game(0, 0, cancellation_token) {
286277
Ok((game_id, color)) => {
287278
let _ = tx.send(Ok((game_id, color)));
288279
}
@@ -349,6 +340,13 @@ impl App {
349340
self.lichess_seek_receiver = None;
350341
self.current_popup = None;
351342

343+
// Cancel the seek since we found a game (or got an error)
344+
if let Some(cancellation_token) = &self.lichess_cancellation_token {
345+
cancellation_token.store(true, std::sync::atomic::Ordering::Relaxed);
346+
log::info!("Cancelling Lichess seek - game found or error occurred");
347+
}
348+
self.lichess_cancellation_token = None;
349+
352350
match result {
353351
Ok((game_id, color)) => {
354352
log::info!("Found Lichess game: {} with color {:?}", game_id, color);
@@ -483,7 +481,7 @@ impl App {
483481
let client = LichessClient::new(token.clone());
484482
let (lichess_to_app_tx, lichess_to_app_rx) = channel::<String>();
485483
let (app_to_lichess_tx, app_to_lichess_rx) = channel::<String>();
486-
let (player_move_tx, player_move_rx) = channel::<()>();
484+
let (player_move_tx, _player_move_rx) = channel::<()>();
487485

488486
// Send last move immediately if we have it (before starting stream)
489487
// This ensures it shows in green right away instead of waiting for first poll
@@ -498,12 +496,9 @@ impl App {
498496

499497
// Start streaming game events (clone the sender since it's moved)
500498
let lichess_to_app_tx_clone = lichess_to_app_tx.clone();
501-
if let Err(e) = client.stream_game(
502-
game_id.clone(),
503-
lichess_to_app_tx_clone,
504-
Some(color),
505-
Some(player_move_rx),
506-
) {
499+
if let Err(e) =
500+
client.stream_game(game_id.clone(), lichess_to_app_tx_clone, Some(color))
501+
{
507502
log::error!("Failed to stream Lichess game: {}", e);
508503
self.error_message = Some(format!("Failed to stream game: {}", e));
509504
self.current_popup = Some(Popups::Error);
@@ -1064,6 +1059,11 @@ impl App {
10641059

10651060
/// Set running to false to quit the application.
10661061
pub fn quit(&mut self) {
1062+
// Cancel any active Lichess seek before quitting
1063+
if let Some(cancellation_token) = &self.lichess_cancellation_token {
1064+
cancellation_token.store(true, std::sync::atomic::Ordering::Relaxed);
1065+
log::info!("Cancelling Lichess seek before quit");
1066+
}
10671067
self.running = false;
10681068
}
10691069

@@ -1564,6 +1564,14 @@ impl App {
15641564
let current_skin = self.game.ui.skin.clone();
15651565
self.end_screen_dismissed = false;
15661566

1567+
// Cancel any active Lichess seek before resetting
1568+
if let Some(cancellation_token) = &self.lichess_cancellation_token {
1569+
cancellation_token.store(true, std::sync::atomic::Ordering::Relaxed);
1570+
log::info!("Cancelling Lichess seek before returning to home");
1571+
}
1572+
self.lichess_cancellation_token = None;
1573+
self.lichess_seek_receiver = None;
1574+
15671575
// Reset game-related state
15681576
self.selected_color = None;
15691577
self.game.logic.bot = None;

0 commit comments

Comments
 (0)