From e74a2cbda1637443df8fbf8c5d583dd494be3b66 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 17 Mar 2023 15:10:33 +0200 Subject: [PATCH] feat(server, shared): handle currently existing datarequests --- server/src/main.rs | 1 + server/src/systems/event/message/mod.rs | 80 +++++++++++++++++++++++ server/src/systems/event/mod.rs | 3 + shared/src/server/messages/datarequest.rs | 18 +++++ shared/src/server/messages/mod.rs | 1 + 5 files changed, 103 insertions(+) create mode 100644 server/src/systems/event/message/mod.rs diff --git a/server/src/main.rs b/server/src/main.rs index 2ea3aa1..9834d62 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -73,6 +73,7 @@ fn main() { systems::event::disconnect_events, systems::event::error_events, systems::event::tick_events, + systems::event::message_events, ) .chain() .in_set(ReceiveEvents), diff --git a/server/src/systems/event/message/mod.rs b/server/src/systems/event/message/mod.rs new file mode 100644 index 0000000..7fd35d9 --- /dev/null +++ b/server/src/systems/event/message/mod.rs @@ -0,0 +1,80 @@ +/* + * This file is part of laurelin/server + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use bevy_ecs::{event::EventReader, system::Res}; +use laurelin_shared::{ + api::game::{ + all_forming, create, my_games, ResponseAllForming, ResponseCreateGame, ResponseMyGames, + }, + server::{ + channels::DataRequestChannel, + messages::{DataRequest, DataRequestResponse, DataRequestType}, + }, +}; +use naia_bevy_server::{events::MessageEvents, Server}; + +use crate::{Config, Global}; + +pub(crate) fn message_events( + mut ev: EventReader, + mut server: Server, + config: Res, + global: Res, +) { + for events in ev.iter() { + for (user_key, request) in events.read::() { + match DataRequestType::from_u8(&request.r#type) { + DataRequestType::GameCreate => { + // TODO: handle + let cookie = global.user_to_session_map.get(&user_key).unwrap(); + let response = create(&config.api_address, &cookie); + let json = match response { + ResponseCreateGame::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle + ResponseCreateGame::Valid(result) => { + serde_json::to_string(&result).unwrap() // TODO: handle + } + }; + server.send_message::( + &user_key, + &DataRequestResponse::new(request.r#type, &json), + ); + } + DataRequestType::GameAllForming => { + // TODO: handle + let cookie = global.user_to_session_map.get(&user_key).unwrap(); + let response = all_forming(&config.api_address, &cookie); + let json = match response { + ResponseAllForming::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle + ResponseAllForming::Valid(result) => { + serde_json::to_string(&result).unwrap() // TODO: handle + } + }; + server.send_message::( + &user_key, + &DataRequestResponse::new(request.r#type, &json), + ); + } + DataRequestType::GameMyGames => { + // TODO: handle + let cookie = global.user_to_session_map.get(&user_key).unwrap(); + let response = my_games(&config.api_address, &cookie); + let json = match response { + ResponseMyGames::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle + ResponseMyGames::Valid(result) => { + serde_json::to_string(&result).unwrap() // TODO: handle + } + }; + server.send_message::( + &user_key, + &DataRequestResponse::new(request.r#type, &json), + ); + } + } + } + } +} diff --git a/server/src/systems/event/mod.rs b/server/src/systems/event/mod.rs index 419c658..e36baa4 100644 --- a/server/src/systems/event/mod.rs +++ b/server/src/systems/event/mod.rs @@ -25,6 +25,9 @@ use naia_bevy_server::{ use crate::{Config, Global, RuntimeTemp}; +mod message; +pub(crate) use message::message_events; + pub(crate) fn auth_events( mut ev: EventReader, mut server: Server, diff --git a/shared/src/server/messages/datarequest.rs b/shared/src/server/messages/datarequest.rs index c62ec4a..59eedc3 100644 --- a/shared/src/server/messages/datarequest.rs +++ b/shared/src/server/messages/datarequest.rs @@ -38,4 +38,22 @@ impl DataRequestResponse { pub enum DataRequestType { GameAllForming = 100, GameMyGames = 101, + GameCreate = 102, +} + +impl DataRequestType { + // TODO: should prolly do this, like, not by hand. + pub fn from_u8(value: &u8) -> Self { + match value { + 100 => Self::GameAllForming, + 101 => Self::GameMyGames, + 102 => Self::GameCreate, + _ => { + // NOTE/TODO/FIXME: bad, veeeery bad. + // just... I'm not sure how to handle this best, + // so right now it just returns something, lol. + Self::GameCreate + } + } + } } diff --git a/shared/src/server/messages/mod.rs b/shared/src/server/messages/mod.rs index a727cc7..ae7e7bb 100644 --- a/shared/src/server/messages/mod.rs +++ b/shared/src/server/messages/mod.rs @@ -12,6 +12,7 @@ mod auth; pub use auth::{AfterAuth, Auth}; mod datarequest; +pub use datarequest::{DataRequest, DataRequestResponse, DataRequestType}; pub struct MessagesPlugin; -- 2.44.1