[backup] on the way

This commit is contained in:
2024-11-15 08:59:53 +01:00
parent 2b64d1e4ba
commit f9cd8471b4
3 changed files with 142 additions and 22 deletions

View File

@ -1,4 +1,5 @@
use std::{
collections::HashSet,
net::SocketAddr,
path::{Path, PathBuf},
sync::Arc,
@ -16,6 +17,7 @@ struct LanSpreadState {
server_addr: Mutex<Option<SocketAddr>>,
client_ctrl: UnboundedSender<ClientCommand>,
games: Arc<Mutex<GameDB>>,
games_in_download: Arc<Mutex<HashSet<String>>>,
}
#[tauri::command]
@ -28,22 +30,26 @@ fn request_games(state: tauri::State<LanSpreadState>) {
}
#[tauri::command]
fn install_game(id: String, state: tauri::State<LanSpreadState>) -> String {
fn install_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
log::error!("Running game with id {id}");
// let result = Command::new(r#"C:\Users\ddidderr\scoop\apps\mpv\0.39.0\mpv.exe"#).spawn();
let already_in_download = tauri::async_runtime::block_on(async {
if state.inner().games_in_download.lock().await.contains(&id) {
log::error!("Game is already downloading: {id}");
return true;
}
false
});
if already_in_download {
return false;
}
if let Err(e) = state.inner().client_ctrl.send(ClientCommand::GetGame(id)) {
log::error!("Failed to send message to client: {e:?}");
}
"TODO".to_string()
// if result.is_ok() {
// "Ok".to_string()
// } else {
// "Failed to run game".to_string()
// }
true
}
fn set_game_install_state_from_path(game_db: &mut GameDB, path: &Path, installed: bool) {
@ -183,6 +189,7 @@ pub fn run() {
server_addr: Mutex::new(None),
client_ctrl: tx_client_control,
games: Arc::new(Mutex::new(GameDB::empty())),
games_in_download: Arc::new(Mutex::new(HashSet::new())),
};
tauri::Builder::default()
@ -209,21 +216,50 @@ pub fn run() {
while let Some(event) = rx_client_event.recv().await {
match event {
ClientEvent::ListGames(games) => {
log::debug!("Received client event: ListGames");
log::info!("ClientEvent::ListGames received");
update_game_db(games, app_handle.clone()).await;
}
ClientEvent::GotGameFiles(game_file_descs) => {
log::debug!("Received client event: GotGameFiles");
if let Err(e) = app_handle.emit("game-download-in-progress", Some(())) {
log::error!("Failed to emit game-files event: {e}");
log::info!("ClientEvent::GotGameFiles received");
if let Some(first_desc_file) = game_file_descs.first() {
if let Err(e) = app_handle.emit(
"game-download-pre",
Some(first_desc_file.game_id.clone()),
) {
log::error!("ClientEvent::GotGameFiles: Failed to emit game-download-pre event: {e}");
}
app_handle
.state::<LanSpreadState>()
.inner()
.client_ctrl
.send(ClientCommand::DownloadGameFiles(game_file_descs))
.unwrap();
} else {
log::error!("ClientEvent::GotGameFiles: Got empty game files list");
}
}
ClientEvent::DownloadGameFilesBegin { id } => {
log::info!("ClientEvent::DownloadGameFilesBegin received");
app_handle
.state::<LanSpreadState>()
.inner()
.client_ctrl
.send(ClientCommand::DownloadGameFiles(game_file_descs))
.unwrap();
.games_in_download
.lock()
.await
.insert(id.clone());
if let Err(e) = app_handle.emit("game-download-begin", Some(id)) {
log::error!("ClientEvent::DownloadGameFilesBegin: Failed to emit game-download-begin event: {e}");
}
}
ClientEvent::DownloadGameFilesFinished { id } => {
log::info!("ClientEvent::DownloadGameFilesFinished received");
if let Err(e) = app_handle.emit("game-download-finished", Some(id)) {
log::error!("ClientEvent::DownloadGameFilesFinished: Failed to emit game-download-finished event: {e}");
}
}
}
}