detect if a game is deleted, added, modified locally
This commit is contained in:
@@ -525,6 +525,45 @@ async fn update_game_db(games: Vec<Game>, app: AppHandle) {
|
||||
refresh_games_list(&app).await;
|
||||
}
|
||||
|
||||
async fn update_local_games_in_db(local_games: Vec<Game>, app: AppHandle) {
|
||||
let state = app.state::<LanSpreadState>();
|
||||
|
||||
// Collect local game IDs first to avoid move issues
|
||||
let local_game_ids: HashSet<String> = local_games.iter().map(|g| g.id.clone()).collect();
|
||||
|
||||
{
|
||||
let mut game_db = state.games.write().await;
|
||||
|
||||
// Update installation status for games that exist locally
|
||||
for local_game in &local_games {
|
||||
if let Some(existing_game) = game_db.get_mut_game_by_id(&local_game.id) {
|
||||
existing_game.downloaded = local_game.downloaded;
|
||||
existing_game.installed = local_game.installed;
|
||||
existing_game
|
||||
.local_version
|
||||
.clone_from(&local_game.local_version);
|
||||
log::debug!("Updated local game status for: {}", local_game.id);
|
||||
}
|
||||
}
|
||||
|
||||
// For games in the main DB that are not in the local list,
|
||||
// mark them as not downloaded/installed (they were deleted)
|
||||
for game in game_db.games.values_mut() {
|
||||
if !local_game_ids.contains(&game.id) && (game.downloaded || game.installed) {
|
||||
log::info!(
|
||||
"Game {} no longer exists locally, marking as uninstalled",
|
||||
game.id
|
||||
);
|
||||
game.downloaded = false;
|
||||
game.installed = false;
|
||||
game.local_version = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
refresh_games_list(&app).await;
|
||||
}
|
||||
|
||||
fn add_final_slash(path: &str) -> String {
|
||||
#[cfg(target_os = "windows")]
|
||||
const SLASH_CHAR: char = '\\';
|
||||
@@ -699,6 +738,10 @@ pub fn run() {
|
||||
log::info!("PeerEvent::ListGames received");
|
||||
update_game_db(games, app_handle.clone()).await;
|
||||
}
|
||||
PeerEvent::LocalGamesUpdated(local_games) => {
|
||||
log::info!("PeerEvent::LocalGamesUpdated received");
|
||||
update_local_games_in_db(local_games, app_handle.clone()).await;
|
||||
}
|
||||
PeerEvent::GotGameFiles { id, file_descriptions } => {
|
||||
log::info!("PeerEvent::GotGameFiles received");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user