Skip to content

Commit

Permalink
task manager minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDavenport committed Apr 19, 2024
1 parent e68b82d commit 7d8b5fe
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 43 deletions.
12 changes: 12 additions & 0 deletions gamercade_app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use eframe::egui::{self, Ui};
use gamercade_interface::platform::FrontPageRequest;

use crate::{
local_directory::LocalDirectory,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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:?}");
}
}
}
}
Expand Down
27 changes: 3 additions & 24 deletions gamercade_app/src/local_directory/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,9 @@ pub struct Game {
pub id: i64,
pub title: String,
pub short_description: String,
pub long_description: Option<String>,
pub releases: Vec<GameRelease>,
pub long_description: String,
pub tags: Vec<TagId>,
pub rating: Option<f32>,
pub images: Vec<GameImage>,
}

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 = "
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions gamercade_app/src/task_manager/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Debug;

use gamercade_interface::{
auth::{
auth_service_client::AuthServiceClient, login_request::Provider, LoginRequest,
Expand Down Expand Up @@ -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<AuthManagerState> for AuthRequest {
async fn handle_request(
self,
Expand Down
1 change: 1 addition & 0 deletions gamercade_app/src/task_manager/authors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl TaskRequest<AuthorManagerState> for AuthorRequest {
}
}

#[derive(Debug)]
pub enum AuthorRequest {
Initialize,
}
6 changes: 0 additions & 6 deletions gamercade_app/src/task_manager/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,3 @@ impl TaskRequest<GameManagerState> for GameRequest {
}
}
}

impl GameManager {
pub fn send(&mut self, request: GameRequest) {
self.sender.try_send(request).unwrap()
}
}
11 changes: 9 additions & 2 deletions gamercade_app/src/task_manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{fmt::Debug, sync::Arc};

use tokio::sync::{
mpsc::{channel, Sender},
Expand All @@ -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;

Expand All @@ -34,7 +37,7 @@ pub struct TaskManager<STATE, REQUEST> {
impl<STATE, REQUEST> TaskManager<STATE, REQUEST>
where
STATE: Default + Send + 'static,
REQUEST: Send + 'static + TaskRequest<STATE>,
REQUEST: Send + 'static + TaskRequest<STATE> + Debug,
{
pub fn new(notification_tx: Sender<TaskNotification>) -> Self {
let state = Arc::new(Mutex::new(STATE::default()));
Expand All @@ -59,6 +62,10 @@ where

client_sender
}

pub fn send(&self, message: REQUEST) {
self.sender.try_send(message).unwrap()
}
}

pub trait TaskRequest<STATE: Send> {
Expand Down
53 changes: 53 additions & 0 deletions gamercade_app/src/task_manager/platform.rs
Original file line number Diff line number Diff line change
@@ -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<PlatformManagerState, PlatformRequest>;

async fn init_platform_client() -> PlatformServiceClient<Channel> {
PlatformServiceClient::connect(SERVICE_IP_GRPC)
.await
.unwrap()
}

#[derive(Default)]
pub struct PlatformManagerState {
client: OnceCell<PlatformServiceClient<Channel>>,
}

#[derive(Debug)]
pub enum PlatformRequest {
FrontPage(FrontPageRequest),
Search(GameSearchRequest),
}

impl TaskRequest<PlatformManagerState> for PlatformRequest {
async fn handle_request(
self,
sender: &tokio::sync::mpsc::Sender<super::TaskNotification>,
state: &tokio::sync::Mutex<PlatformManagerState>,
) {
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!(),
}
}
}
8 changes: 7 additions & 1 deletion gamercade_app/src/task_manager/super_task_manager.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand All @@ -14,6 +15,9 @@ pub enum TaskNotification {
AuthStateChanged(AuthState),
LoginFailed,
DownloadRomComplete(DownloadRomComplete),

// Platform
FrontPageResponse(FrontPageResponse),
}

#[derive(Debug)]
Expand All @@ -29,6 +33,7 @@ pub struct SuperTaskManager {
pub auth: AuthManager,
pub rom: RomManager,
pub game: GameManager,
pub platform: PlatformManager,
}

impl Default for SuperTaskManager {
Expand All @@ -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,
}
}
Expand Down
1 change: 1 addition & 0 deletions gamercade_app/src/task_manager/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl TaskRequest<TagManagerState> for TagRequest {
}
}

#[derive(Debug)]
pub enum TagRequest {
Initialize,
}
9 changes: 4 additions & 5 deletions gamercade_interface/proto/platform.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ message GameSearchRequest {
}

message FrontPageResponse {
map<sfixed64, game.GameInfoBasic> 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;
}
10 changes: 5 additions & 5 deletions gamercade_interface/src/output/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64, super::game::GameInfoBasic>,
#[prost(message, repeated, tag = "1")]
pub games: ::prost::alloc::vec::Vec<super::game::GameInfoBasic>,
#[prost(sfixed64, repeated, tag = "2")]
pub popular_games: ::prost::alloc::vec::Vec<i64>,
pub popular_games_ids: ::prost::alloc::vec::Vec<i64>,
#[prost(sfixed64, repeated, tag = "3")]
pub top_rated_games: ::prost::alloc::vec::Vec<i64>,
pub top_rated_games_ids: ::prost::alloc::vec::Vec<i64>,
#[prost(sfixed64, repeated, tag = "4")]
pub new_games: ::prost::alloc::vec::Vec<i64>,
pub new_games_ids: ::prost::alloc::vec::Vec<i64>,
}
/// Generated client implementations.
pub mod platform_service_client {
Expand Down

0 comments on commit 7d8b5fe

Please sign in to comment.