From 7d8b5fe778e7e710996ef40f55fb6cd145c83565 Mon Sep 17 00:00:00 2001 From: RobDavenport Date: Fri, 19 Apr 2024 18:15:22 +0900 Subject: [PATCH] task manager minor refactor --- gamercade_app/src/app.rs | 12 +++++ gamercade_app/src/local_directory/game.rs | 27 ++-------- gamercade_app/src/task_manager/auth.rs | 11 ++++ gamercade_app/src/task_manager/authors.rs | 1 + gamercade_app/src/task_manager/game.rs | 6 --- gamercade_app/src/task_manager/mod.rs | 11 +++- gamercade_app/src/task_manager/platform.rs | 53 +++++++++++++++++++ .../src/task_manager/super_task_manager.rs | 8 ++- gamercade_app/src/task_manager/tags.rs | 1 + gamercade_interface/proto/platform.proto | 9 ++-- gamercade_interface/src/output/platform.rs | 10 ++-- 11 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 gamercade_app/src/task_manager/platform.rs diff --git a/gamercade_app/src/app.rs b/gamercade_app/src/app.rs index 7944b76..90cfb44 100644 --- a/gamercade_app/src/app.rs +++ b/gamercade_app/src/app.rs @@ -1,4 +1,5 @@ use eframe::egui::{self, Ui}; +use gamercade_interface::platform::FrontPageRequest; use crate::{ local_directory::LocalDirectory, @@ -41,6 +42,14 @@ impl eframe::App for App { // self.tasks.tags.send_request(TagRequest::Initialize) // } + if ui.button("Front Page").clicked() { + self.tasks + .platform + .send(crate::task_manager::PlatformRequest::FrontPage( + FrontPageRequest {}, + )) + } + ui.horizontal(|ui| { ui.selectable_value(&mut self.active_mode, AppMode::Arcade, "Arcade"); ui.selectable_value(&mut self.active_mode, AppMode::Library, "Library"); @@ -88,6 +97,9 @@ impl App { TaskNotification::DownloadRomComplete(complete) => { println!("TODO: Release download complete") } + TaskNotification::FrontPageResponse(front_page_response) => { + println!("TODO: Got Response: {front_page_response:?}"); + } } } } diff --git a/gamercade_app/src/local_directory/game.rs b/gamercade_app/src/local_directory/game.rs index bb03192..69a2fb6 100644 --- a/gamercade_app/src/local_directory/game.rs +++ b/gamercade_app/src/local_directory/game.rs @@ -6,21 +6,9 @@ pub struct Game { pub id: i64, pub title: String, pub short_description: String, - pub long_description: Option, - pub releases: Vec, + pub long_description: String, pub tags: Vec, - pub rating: Option, - pub images: Vec, -} - -pub struct GameRelease { - pub id: i64, - pub checksum: i64, - pub name: String, -} - -pub struct GameImage { - pub path: String, + pub rating: f32, } const UPSERT_GAMES_QUERIES: &str = " @@ -31,19 +19,10 @@ CREATE TABLE IF NOT EXISTS games ( short_description TEXT NOT NULL, long_description TEXT NOT NULL, rating REAL, + file_checksum INTEGER, UNIQUE(title) ) STRICT; -CREATE TABLE IF NOT EXISTS releases( - id INTEGER PRIMARY KEY, - file_checksum BLOB, - game_id INTEGER NOT NULL, - release_name TEXT NOT NULL, - FOREIGN KEY (game_id) REFERENCES games (id), - UNIQUE(game_id, file_checksum), - UNIQUE(game_id, release_name) -) STRICT; - CREATE TABLE IF NOT EXISTS game_tags( id INTEGER PRIMARY KEY, game_id INTEGER NOT NULL, diff --git a/gamercade_app/src/task_manager/auth.rs b/gamercade_app/src/task_manager/auth.rs index 8b3d536..316e542 100644 --- a/gamercade_app/src/task_manager/auth.rs +++ b/gamercade_app/src/task_manager/auth.rs @@ -1,3 +1,5 @@ +use std::fmt::Debug; + use gamercade_interface::{ auth::{ auth_service_client::AuthServiceClient, login_request::Provider, LoginRequest, @@ -47,6 +49,15 @@ pub enum AuthRequest { SignUp(SignUpRequest), } +impl Debug for AuthRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Login(_) => f.write_str("Login(Fields Hidden)"), + Self::SignUp(_) => f.write_str("SignUp(Fields Hidden)"), + } + } +} + impl TaskRequest for AuthRequest { async fn handle_request( self, diff --git a/gamercade_app/src/task_manager/authors.rs b/gamercade_app/src/task_manager/authors.rs index 9d7cd92..70da73b 100644 --- a/gamercade_app/src/task_manager/authors.rs +++ b/gamercade_app/src/task_manager/authors.rs @@ -58,6 +58,7 @@ impl TaskRequest for AuthorRequest { } } +#[derive(Debug)] pub enum AuthorRequest { Initialize, } diff --git a/gamercade_app/src/task_manager/game.rs b/gamercade_app/src/task_manager/game.rs index a6a5b2a..37beb4d 100644 --- a/gamercade_app/src/task_manager/game.rs +++ b/gamercade_app/src/task_manager/game.rs @@ -51,9 +51,3 @@ impl TaskRequest for GameRequest { } } } - -impl GameManager { - pub fn send(&mut self, request: GameRequest) { - self.sender.try_send(request).unwrap() - } -} diff --git a/gamercade_app/src/task_manager/mod.rs b/gamercade_app/src/task_manager/mod.rs index a94482e..ad1253d 100644 --- a/gamercade_app/src/task_manager/mod.rs +++ b/gamercade_app/src/task_manager/mod.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{fmt::Debug, sync::Arc}; use tokio::sync::{ mpsc::{channel, Sender}, @@ -23,6 +23,9 @@ pub use rom::*; mod game; pub use game::*; +mod platform; +pub use platform::*; + const SUPER_TASK_CHANNEL_SIZE: usize = 256; const TASK_CHANNEL_LENGTH: usize = 8; @@ -34,7 +37,7 @@ pub struct TaskManager { impl TaskManager where STATE: Default + Send + 'static, - REQUEST: Send + 'static + TaskRequest, + REQUEST: Send + 'static + TaskRequest + Debug, { pub fn new(notification_tx: Sender) -> Self { let state = Arc::new(Mutex::new(STATE::default())); @@ -59,6 +62,10 @@ where client_sender } + + pub fn send(&self, message: REQUEST) { + self.sender.try_send(message).unwrap() + } } pub trait TaskRequest { diff --git a/gamercade_app/src/task_manager/platform.rs b/gamercade_app/src/task_manager/platform.rs new file mode 100644 index 0000000..f3a26b6 --- /dev/null +++ b/gamercade_app/src/task_manager/platform.rs @@ -0,0 +1,53 @@ +use gamercade_interface::{ + platform::{ + platform_service_client::PlatformServiceClient, FrontPageRequest, GameSearchRequest, + }, + Session, +}; +use tokio::sync::OnceCell; +use tonic::{transport::Channel, Request}; + +use crate::urls::{WithSession, SERVICE_IP_GRPC}; + +use super::{TaskManager, TaskNotification, TaskRequest}; + +pub type PlatformManager = TaskManager; + +async fn init_platform_client() -> PlatformServiceClient { + PlatformServiceClient::connect(SERVICE_IP_GRPC) + .await + .unwrap() +} + +#[derive(Default)] +pub struct PlatformManagerState { + client: OnceCell>, +} + +#[derive(Debug)] +pub enum PlatformRequest { + FrontPage(FrontPageRequest), + Search(GameSearchRequest), +} + +impl TaskRequest for PlatformRequest { + async fn handle_request( + self, + sender: &tokio::sync::mpsc::Sender, + state: &tokio::sync::Mutex, + ) { + let mut lock = state.lock().await; + lock.client.get_or_init(init_platform_client).await; + let client = lock.client.get_mut().unwrap(); + + match self { + PlatformRequest::FrontPage(request) => match client.front_page(request).await { + Ok(response) => sender + .try_send(TaskNotification::FrontPageResponse(response.into_inner())) + .unwrap(), + Err(err) => println!("front page response err: {err}"), + }, + PlatformRequest::Search(request) => todo!(), + } + } +} diff --git a/gamercade_app/src/task_manager/super_task_manager.rs b/gamercade_app/src/task_manager/super_task_manager.rs index b4d986a..ab3a9c5 100644 --- a/gamercade_app/src/task_manager/super_task_manager.rs +++ b/gamercade_app/src/task_manager/super_task_manager.rs @@ -1,9 +1,10 @@ +use gamercade_interface::platform::FrontPageResponse; use tokio::sync::mpsc::{channel, Receiver}; use crate::local_directory::{PermissionLevel, PermissionLevelId, Tag, TagId}; use super::{ - AuthManager, AuthState, AuthorManager, GameManager, RomManager, TagManager, + AuthManager, AuthState, AuthorManager, GameManager, PlatformManager, RomManager, TagManager, SUPER_TASK_CHANNEL_SIZE, }; @@ -14,6 +15,9 @@ pub enum TaskNotification { AuthStateChanged(AuthState), LoginFailed, DownloadRomComplete(DownloadRomComplete), + + // Platform + FrontPageResponse(FrontPageResponse), } #[derive(Debug)] @@ -29,6 +33,7 @@ pub struct SuperTaskManager { pub auth: AuthManager, pub rom: RomManager, pub game: GameManager, + pub platform: PlatformManager, } impl Default for SuperTaskManager { @@ -41,6 +46,7 @@ impl Default for SuperTaskManager { auth: AuthManager::new(event_tx.clone()), rom: RomManager::new(event_tx.clone()), game: GameManager::new(event_tx.clone()), + platform: PlatformManager::new(event_tx.clone()), events, } } diff --git a/gamercade_app/src/task_manager/tags.rs b/gamercade_app/src/task_manager/tags.rs index f81edf7..f4243bd 100644 --- a/gamercade_app/src/task_manager/tags.rs +++ b/gamercade_app/src/task_manager/tags.rs @@ -51,6 +51,7 @@ impl TaskRequest for TagRequest { } } +#[derive(Debug)] pub enum TagRequest { Initialize, } diff --git a/gamercade_interface/proto/platform.proto b/gamercade_interface/proto/platform.proto index 6de3dfc..b455a87 100644 --- a/gamercade_interface/proto/platform.proto +++ b/gamercade_interface/proto/platform.proto @@ -20,9 +20,8 @@ message GameSearchRequest { } message FrontPageResponse { - map data = 1; - - repeated sfixed64 popular_games = 2; - repeated sfixed64 top_rated_games = 3; - repeated sfixed64 new_games = 4; + repeated game.GameInfoBasic games = 1; + repeated sfixed64 popular_games_ids = 2; + repeated sfixed64 top_rated_games_ids = 3; + repeated sfixed64 new_games_ids = 4; } \ No newline at end of file diff --git a/gamercade_interface/src/output/platform.rs b/gamercade_interface/src/output/platform.rs index 23f33e3..2445e90 100644 --- a/gamercade_interface/src/output/platform.rs +++ b/gamercade_interface/src/output/platform.rs @@ -13,14 +13,14 @@ pub struct GameSearchRequest { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct FrontPageResponse { - #[prost(map = "sfixed64, message", tag = "1")] - pub data: ::std::collections::HashMap, + #[prost(message, repeated, tag = "1")] + pub games: ::prost::alloc::vec::Vec, #[prost(sfixed64, repeated, tag = "2")] - pub popular_games: ::prost::alloc::vec::Vec, + pub popular_games_ids: ::prost::alloc::vec::Vec, #[prost(sfixed64, repeated, tag = "3")] - pub top_rated_games: ::prost::alloc::vec::Vec, + pub top_rated_games_ids: ::prost::alloc::vec::Vec, #[prost(sfixed64, repeated, tag = "4")] - pub new_games: ::prost::alloc::vec::Vec, + pub new_games_ids: ::prost::alloc::vec::Vec, } /// Generated client implementations. pub mod platform_service_client {