Skip to content

Commit

Permalink
progress create game
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDavenport committed Apr 14, 2024
1 parent 0e82289 commit 4c8bee1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
16 changes: 14 additions & 2 deletions gamercade_app/src/modes/arcade_mode/creator_dashboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::app::AppDrawContext;
use gamercade_interface::game::UpdateGameRequest;

use crate::{app::AppDrawContext, task_manager::GameRequest};

#[derive(Default)]
pub struct CreatorDashboardView {}
Expand All @@ -9,7 +11,17 @@ impl CreatorDashboardView {

ui.label("Creator Dashboard");

if ui.button("Create Game").clicked() {}
if ui.button("Create Game").clicked() {
context
.task_manager
.game
.send(GameRequest::CreateGame(UpdateGameRequest {
game_id: None,
title: Some("Test Game".to_string()),
short_description: Some("A game for testing".to_string()),
long_description: Some("Some more details....".to_string()),
}))
}

if ui.button("Manage Game").clicked() {}

Expand Down
52 changes: 52 additions & 0 deletions gamercade_app/src/task_manager/game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use gamercade_interface::game::{game_service_client::GameServiceClient, UpdateGameRequest};
use tokio::sync::OnceCell;
use tonic::{transport::Channel, Request};

use crate::urls::SERVICE_IP_GRPC;

use super::{TaskManager, TaskRequest};

pub type GameManager = TaskManager<GameManagerState, GameRequest>;

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

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

#[derive(Debug)]
pub enum GameRequest {
CreateGame(UpdateGameRequest),
UpdateGame(UpdateGameRequest),
}

impl TaskRequest<GameManagerState> for GameRequest {
async fn handle_request(
self,
sender: &tokio::sync::mpsc::Sender<super::TaskNotification>,
state: &tokio::sync::Mutex<GameManagerState>,
) {
let mut lock = state.lock().await;
lock.client.get_or_init(init_game_client).await;
let client = lock.client.get_mut().unwrap();

let result = match self {
GameRequest::CreateGame(request) => client.create_game(request).await,
GameRequest::UpdateGame(request) => client.update_game(request).await,
};

match result {
Ok(game_info) => println!("Got game info: {game_info:?}"),
Err(e) => println!("{e}"),
}
}
}

impl GameManager {
pub fn send(&mut self, request: GameRequest) {
self.sender.try_send(request).unwrap()
}
}
3 changes: 3 additions & 0 deletions gamercade_app/src/task_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub use auth::*;
mod release;
pub use release::*;

mod game;
pub use game::*;

const SUPER_TASK_CHANNEL_SIZE: usize = 256;
const TASK_CHANNEL_LENGTH: usize = 8;

Expand Down
5 changes: 4 additions & 1 deletion gamercade_app/src/task_manager/super_task_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use tokio::sync::mpsc::{channel, Receiver};
use crate::local_directory::{PermissionLevel, PermissionLevelId, Tag, TagId};

use super::{
AuthManager, AuthState, AuthorManager, ReleaseManager, TagManager, SUPER_TASK_CHANNEL_SIZE,
AuthManager, AuthState, AuthorManager, GameManager, ReleaseManager, TagManager,
SUPER_TASK_CHANNEL_SIZE,
};

#[derive(Debug)]
Expand All @@ -20,6 +21,7 @@ pub struct SuperTaskManager {
pub author: AuthorManager,
pub auth: AuthManager,
pub release: ReleaseManager,
pub game: GameManager,
}

impl Default for SuperTaskManager {
Expand All @@ -31,6 +33,7 @@ impl Default for SuperTaskManager {
author: AuthorManager::new(event_tx.clone()),
auth: AuthManager::new(event_tx.clone()),
release: ReleaseManager::new(event_tx.clone()),
game: GameManager::new(event_tx.clone()),
events,
}
}
Expand Down
12 changes: 12 additions & 0 deletions gamercade_app/src/urls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use gamercade_interface::{Session, SESSION_METADATA_KEY};
use tonic::{metadata::MetadataValue, Request};

pub const SERVICE_IP_GRPC: &str = "http://127.0.0.1:50051";
pub const SERVICE_IP_HTTP: &str = "http://127.0.0.1:3000";

pub fn download_release_url(game_id: u64, release_id: u64) -> String {
format!("{SERVICE_IP_HTTP}/games/{game_id}/releases/{release_id}")
}

pub fn authorized_request<T>(request: T, session: Session) -> Request<T> {
let mut request = Request::new(request);
request.metadata_mut().insert_bin(
SESSION_METADATA_KEY,
MetadataValue::from_bytes(session.bytes()),
);
request
}
2 changes: 1 addition & 1 deletion gamercade_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const REVIEW_COMMENTS_MAX_LENGTH: usize = 1027;

pub const SESSION_METADATA_KEY: &str = "gc-session";

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Session([u8; 16]);

impl Session {
Expand Down

0 comments on commit 4c8bee1

Please sign in to comment.