ChatGPT Codex 5.5 xhigh refactored even more

This commit is contained in:
2026-05-02 15:31:37 +02:00
parent 86d0f93ede
commit b4585b663a
24 changed files with 2160 additions and 1972 deletions
+13 -42
View File
@@ -2,19 +2,24 @@
use std::{net::SocketAddr, sync::Arc};
use lanspread_db::db::{Game, GameFileDescription};
use lanspread_db::db::GameFileDescription;
use tokio::sync::{RwLock, mpsc::UnboundedSender};
use crate::{
PeerEvent,
context::Ctx,
download::download_game_files,
events,
identity::FEATURE_LIBRARY_DELTA,
local_games::{
LocalLibraryScan, get_game_file_descriptions, local_download_available, scan_local_library,
LocalLibraryScan,
get_game_file_descriptions,
local_download_available,
scan_local_library,
},
network::{announce_games_to_peer, request_game_details_from_peer, send_library_delta},
peer_db::{PeerGameDB, PeerId},
peer_db::PeerGameDB,
remote_peer::ensure_peer_id_for_addr,
};
// =============================================================================
@@ -24,32 +29,7 @@ use crate::{
/// Handles the `ListGames` command.
pub async fn handle_list_games_command(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) {
log::info!("ListGames command received");
emit_peer_game_list(&ctx.peer_game_db, tx_notify_ui).await;
}
/// Emits the aggregated game list to the UI.
pub async fn emit_peer_game_list(
peer_game_db: &Arc<RwLock<PeerGameDB>>,
tx_notify_ui: &UnboundedSender<PeerEvent>,
) {
let all_games = { peer_game_db.read().await.get_all_games() };
if let Err(e) = tx_notify_ui.send(PeerEvent::ListGames(all_games)) {
log::error!("Failed to send ListGames event: {e}");
}
}
async fn ensure_peer_id_for_addr(
peer_game_db: &Arc<RwLock<PeerGameDB>>,
peer_addr: SocketAddr,
) -> PeerId {
let mut db = peer_game_db.write().await;
if let Some(peer_id) = db.peer_id_for_addr(&peer_addr).cloned() {
return peer_id;
}
let legacy_id = format!("legacy-{peer_addr}");
db.upsert_peer(legacy_id.clone(), peer_addr);
legacy_id
events::emit_peer_game_list(&ctx.peer_game_db, tx_notify_ui).await;
}
/// Tries to serve a game from local files.
@@ -165,12 +145,10 @@ pub async fn handle_download_game_files_command(
) {
log::info!("Got PeerCommand::DownloadGameFiles");
let games_folder = { ctx.game_dir.read().await.clone() };
if games_folder.is_none() {
let Some(games_folder) = games_folder else {
log::error!("Cannot handle game file descriptions: games_folder is not set");
return;
}
let games_folder = games_folder.expect("checked above");
};
// Use majority validation to get trusted file descriptions and peer whitelist
let (validated_descriptions, peer_whitelist, file_peer_map) = {
@@ -312,10 +290,7 @@ pub async fn handle_set_game_dir_command(
/// Handles the `GetPeerCount` command.
pub async fn handle_get_peer_count_command(ctx: &Ctx, tx_notify_ui: &UnboundedSender<PeerEvent>) {
log::info!("GetPeerCount command received");
let peer_count = { ctx.peer_game_db.read().await.get_peer_addresses().len() };
if let Err(e) = tx_notify_ui.send(PeerEvent::PeerCountUpdated(peer_count)) {
log::error!("Failed to send PeerCountUpdated event: {e}");
}
events::emit_peer_count(&ctx.peer_game_db, tx_notify_ui).await;
}
// =============================================================================
@@ -348,11 +323,7 @@ pub async fn update_and_announce_games(
*db_guard = Some(game_db.clone());
}
let all_games = game_db
.all_games()
.into_iter()
.cloned()
.collect::<Vec<Game>>();
let all_games = game_db.all_games().into_iter().cloned().collect::<Vec<_>>();
if let Err(e) = tx_notify_ui.send(PeerEvent::LocalGamesUpdated(all_games.clone())) {
log::error!("Failed to send LocalGamesUpdated event: {e}");