mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 08:41:21 +10:00
beginnings of game state management
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{self, create_dir_all},
|
||||
path::PathBuf,
|
||||
sync::LazyLock,
|
||||
@ -10,7 +11,7 @@ use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
#[derive(serde::Serialize, Clone, Deserialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DatabaseAuth {
|
||||
pub private: String,
|
||||
pub cert: String,
|
||||
@ -18,17 +19,27 @@ pub struct DatabaseAuth {
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
pub struct DatabaseApps {
|
||||
pub apps_base_dir: String,
|
||||
pub enum DatabaseGameStatus {
|
||||
Remote,
|
||||
Downloading,
|
||||
Installed,
|
||||
Updating,
|
||||
Uninstalling,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DatabaseGames {
|
||||
pub games_base_dir: String,
|
||||
pub games_statuses: HashMap<String, DatabaseGameStatus>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Database {
|
||||
pub auth: Option<DatabaseAuth>,
|
||||
pub base_url: String,
|
||||
pub downloads: DatabaseApps,
|
||||
pub games: DatabaseGames,
|
||||
}
|
||||
|
||||
pub type DatabaseInterface =
|
||||
@ -41,16 +52,17 @@ pub trait DatabaseImpls {
|
||||
impl DatabaseImpls for DatabaseInterface {
|
||||
fn set_up_database() -> DatabaseInterface {
|
||||
let db_path = DATA_ROOT_DIR.join("drop.db");
|
||||
let apps_base_dir = DATA_ROOT_DIR.join("apps");
|
||||
let games_base_dir = DATA_ROOT_DIR.join("games");
|
||||
|
||||
create_dir_all(DATA_ROOT_DIR.clone()).unwrap();
|
||||
create_dir_all(apps_base_dir.clone()).unwrap();
|
||||
create_dir_all(games_base_dir.clone()).unwrap();
|
||||
|
||||
let default = Database {
|
||||
auth: None,
|
||||
base_url: "".to_string(),
|
||||
downloads: DatabaseApps {
|
||||
apps_base_dir: apps_base_dir.to_str().unwrap().to_string(),
|
||||
games: DatabaseGames {
|
||||
games_base_dir: games_base_dir.to_str().unwrap().to_string(),
|
||||
games_statuses: HashMap::new(),
|
||||
},
|
||||
};
|
||||
#[allow(clippy::let_and_return)]
|
||||
|
||||
@ -147,8 +147,6 @@ pub fn run() {
|
||||
.join(object_id)
|
||||
.unwrap();
|
||||
|
||||
info!["{}", object_url.to_string()];
|
||||
|
||||
let header = generate_authorization_header();
|
||||
let client: reqwest::blocking::Client = reqwest::blocking::Client::new();
|
||||
let response = client
|
||||
|
||||
@ -6,9 +6,16 @@ use tauri::{AppHandle, Manager};
|
||||
|
||||
use crate::{auth::generate_authorization_header, AppState, DB};
|
||||
use crate::db::DatabaseImpls;
|
||||
use crate::db::DatabaseGameStatus;
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct FetchGameStruct {
|
||||
game: Game,
|
||||
status: DatabaseGameStatus,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all="camelCase")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Game {
|
||||
id: String,
|
||||
m_name: String,
|
||||
@ -16,7 +23,6 @@ pub struct Game {
|
||||
m_description: String,
|
||||
// mDevelopers
|
||||
// mPublishers
|
||||
|
||||
m_icon_id: String,
|
||||
m_banner_id: String,
|
||||
m_cover_id: String,
|
||||
@ -50,8 +56,16 @@ pub fn fetch_library(app: AppHandle) -> Result<String, String> {
|
||||
let state = app.state::<Mutex<AppState>>();
|
||||
let mut handle = state.lock().unwrap();
|
||||
|
||||
let mut db_handle = DB.borrow_data_mut().unwrap();
|
||||
|
||||
for game in games.iter() {
|
||||
handle.games.insert(game.id.clone(), game.clone());
|
||||
if !db_handle.games.games_statuses.contains_key(&game.id) {
|
||||
db_handle
|
||||
.games
|
||||
.games_statuses
|
||||
.insert(game.id.clone(), DatabaseGameStatus::Remote);
|
||||
}
|
||||
}
|
||||
|
||||
drop(handle);
|
||||
@ -64,9 +78,16 @@ pub fn fetch_game(id: String, app: tauri::AppHandle) -> Result<String, String> {
|
||||
let state = app.state::<Mutex<AppState>>();
|
||||
let handle = state.lock().unwrap();
|
||||
let game = handle.games.get(&id);
|
||||
if game.is_some() {
|
||||
return Ok(json!(game.unwrap()).to_string());
|
||||
if let Some(game) = game {
|
||||
let db_handle = DB.borrow_data().unwrap();
|
||||
|
||||
let data = FetchGameStruct {
|
||||
game: game.clone(),
|
||||
status: db_handle.games.games_statuses.get(&game.id).unwrap().clone(),
|
||||
};
|
||||
|
||||
return Ok(json!(data).to_string());
|
||||
}
|
||||
|
||||
Ok("".to_string())
|
||||
Err("".to_string())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user