beginnings of game state management

This commit is contained in:
DecDuck
2024-10-17 21:05:25 +11:00
parent 01b092c5fe
commit bf46dec359
6 changed files with 84 additions and 32 deletions

View File

@ -33,7 +33,7 @@
</NuxtLink> </NuxtLink>
</ul> </ul>
</div> </div>
<div class="grow"> <div class="grow overflow-y-scroll">
<NuxtPage /> <NuxtPage />
</div> </div>
</div> </div>

View File

@ -5,12 +5,22 @@
<!-- banner image --> <!-- banner image -->
<div class="absolute flex top-0 h-fit inset-x-0 -z-[20]"> <div class="absolute flex top-0 h-fit inset-x-0 -z-[20]">
<img :src="bannerUrl" class="w-full h-auto object-cover" /> <img :src="bannerUrl" class="w-full h-auto object-cover" />
<h1
class="absolute inset-x-0 w-full text-center top-32 -translate-y-[50%] text-4xl text-zinc-100 font-bold font-display z-50"
>
{{ game.mName }}
</h1>
<div <div
class="absolute inset-0 bg-gradient-to-b from-transparent to-50% to-zinc-900" class="absolute inset-0 bg-gradient-to-b from-transparent to-50% to-zinc-900"
/> />
</div> </div>
<!-- main page --> <!-- main page -->
<div class="w-full min-h-screen mx-auto bg-zinc-900 px-16 py-12"></div> <div class="w-full min-h-screen mx-auto bg-zinc-900 px-5 py-6">
<!-- game toolbar -->
<div>
<GameButton v-model="status" />
</div>
</div>
</div> </div>
</template> </template>
@ -21,8 +31,11 @@ import { invoke } from "@tauri-apps/api/core";
const route = useRoute(); const route = useRoute();
const id = route.params.id; const id = route.params.id;
const rawGame = await invoke<string>("fetch_game", { id: id }); const raw: { game: Game; status: any } = JSON.parse(
const game: Game = JSON.parse(rawGame); await invoke<string>("fetch_game", { id: id })
);
const game = ref(raw.game);
const status = ref(raw.status);
const bannerUrl = await useObject(game.mBannerId); const bannerUrl = await useObject(game.value.mBannerId);
</script> </script>

View File

@ -1,4 +1,5 @@
use std::{ use std::{
collections::HashMap,
fs::{self, create_dir_all}, fs::{self, create_dir_all},
path::PathBuf, path::PathBuf,
sync::LazyLock, sync::LazyLock,
@ -10,7 +11,7 @@ use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
#[derive(serde::Serialize, Clone, Deserialize)] #[derive(serde::Serialize, Clone, Deserialize)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
pub struct DatabaseAuth { pub struct DatabaseAuth {
pub private: String, pub private: String,
pub cert: String, pub cert: String,
@ -18,17 +19,27 @@ pub struct DatabaseAuth {
} }
#[derive(Serialize, Clone, Deserialize)] #[derive(Serialize, Clone, Deserialize)]
#[serde(rename_all="camelCase")] pub enum DatabaseGameStatus {
pub struct DatabaseApps { Remote,
pub apps_base_dir: String, Downloading,
Installed,
Updating,
Uninstalling,
} }
#[derive(Serialize, Clone, Deserialize)] #[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 struct Database {
pub auth: Option<DatabaseAuth>, pub auth: Option<DatabaseAuth>,
pub base_url: String, pub base_url: String,
pub downloads: DatabaseApps, pub games: DatabaseGames,
} }
pub type DatabaseInterface = pub type DatabaseInterface =
@ -41,16 +52,17 @@ pub trait DatabaseImpls {
impl DatabaseImpls for DatabaseInterface { impl DatabaseImpls for DatabaseInterface {
fn set_up_database() -> DatabaseInterface { fn set_up_database() -> DatabaseInterface {
let db_path = DATA_ROOT_DIR.join("drop.db"); 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(DATA_ROOT_DIR.clone()).unwrap();
create_dir_all(apps_base_dir.clone()).unwrap(); create_dir_all(games_base_dir.clone()).unwrap();
let default = Database { let default = Database {
auth: None, auth: None,
base_url: "".to_string(), base_url: "".to_string(),
downloads: DatabaseApps { games: DatabaseGames {
apps_base_dir: apps_base_dir.to_str().unwrap().to_string(), games_base_dir: games_base_dir.to_str().unwrap().to_string(),
games_statuses: HashMap::new(),
}, },
}; };
#[allow(clippy::let_and_return)] #[allow(clippy::let_and_return)]

View File

@ -147,8 +147,6 @@ pub fn run() {
.join(object_id) .join(object_id)
.unwrap(); .unwrap();
info!["{}", object_url.to_string()];
let header = generate_authorization_header(); let header = generate_authorization_header();
let client: reqwest::blocking::Client = reqwest::blocking::Client::new(); let client: reqwest::blocking::Client = reqwest::blocking::Client::new();
let response = client let response = client

View File

@ -6,9 +6,16 @@ use tauri::{AppHandle, Manager};
use crate::{auth::generate_authorization_header, AppState, DB}; use crate::{auth::generate_authorization_header, AppState, DB};
use crate::db::DatabaseImpls; use crate::db::DatabaseImpls;
use crate::db::DatabaseGameStatus;
#[derive(serde::Serialize)]
struct FetchGameStruct {
game: Game,
status: DatabaseGameStatus,
}
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all="camelCase")] #[serde(rename_all = "camelCase")]
pub struct Game { pub struct Game {
id: String, id: String,
m_name: String, m_name: String,
@ -16,7 +23,6 @@ pub struct Game {
m_description: String, m_description: String,
// mDevelopers // mDevelopers
// mPublishers // mPublishers
m_icon_id: String, m_icon_id: String,
m_banner_id: String, m_banner_id: String,
m_cover_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 state = app.state::<Mutex<AppState>>();
let mut handle = state.lock().unwrap(); let mut handle = state.lock().unwrap();
let mut db_handle = DB.borrow_data_mut().unwrap();
for game in games.iter() { for game in games.iter() {
handle.games.insert(game.id.clone(), game.clone()); 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); 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 state = app.state::<Mutex<AppState>>();
let handle = state.lock().unwrap(); let handle = state.lock().unwrap();
let game = handle.games.get(&id); let game = handle.games.get(&id);
if game.is_some() { if let Some(game) = game {
return Ok(json!(game.unwrap()).to_string()); 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())
} }

28
types.d.ts vendored
View File

@ -1,17 +1,17 @@
import type { User } from "@prisma/client"; import type { User } from "@prisma/client";
import type { Component } from "vue" import type { Component } from "vue";
export type NavigationItem = { export type NavigationItem = {
prefix: string, prefix: string;
route: string, route: string;
label: string, label: string;
} };
export type QuickActionNav = { export type QuickActionNav = {
icon: Component, icon: Component;
notifications?: number, notifications?: number;
action: () => Promise<void>, action: () => Promise<void>;
} };
export type AppState = { export type AppState = {
status: AppStatus; status: AppStatus;
user?: User; user?: User;
@ -22,4 +22,12 @@ export enum AppStatus {
SignedOut = "SignedOut", SignedOut = "SignedOut",
SignedIn = "SignedIn", SignedIn = "SignedIn",
SignedInNeedsReauth = "SignedInNeedsReauth", SignedInNeedsReauth = "SignedInNeedsReauth",
} }
export enum GameStatus {
Remote = "Remote",
Downloading = "Downloading",
Installed = "Installed",
Updating = "Updating",
Uninstalling = "Uninstalling",
}