/*
* This file is part of laurelin/server
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy_ecs::prelude::*;
use bevy_quinnet::server::Server;
use laurelin_shared::server::protocol::ClientMessage;
use crate::Global;
pub fn handle_client_messages(mut global: ResMut<Global>, mut server: ResMut<Server>) {
let endpoint = server.endpoint_mut();
for client_id in endpoint.clients() {
while let Some(message) = endpoint.try_receive_message_from::<ClientMessage>(client_id) {
match message {
ClientMessage::Disconnect {} => {
// TODO: handle
endpoint.disconnect_client(client_id).unwrap();
}
}
}
}
}
/*
use bevy_ecs::{
event::EventReader,
system::{Res, ResMut},
};
use laurelin_shared::{
api::{
self,
game::{
all_forming, create, my_games, ResponseAllForming, ResponseCreateGame,
ResponseGameInfo, ResponseMyGames,
},
user::{self, ResponseInfo},
},
server::{
channels::{CookieRefreshChannel, DataRequestChannel},
messages::{CookieRefresh, DataRequest, DataRequestResponse, DataRequestType},
},
types::game::GamePub,
};
use naia_bevy_server::{events::MessageEvents, Server};
use crate::{Config, Global};
pub fn message_events(
mut ev: EventReader<MessageEvents>,
mut server: Server,
config: Res<Config>,
mut global: ResMut<Global>,
) {
for events in ev.iter() {
for (user_key, request) in events.read::<DataRequestChannel, DataRequest>() {
match DataRequestType::from_u8(&request.r#type) {
DataRequestType::GameCreate => {
// TODO: handle
let cookie = global.user_to_session_map.get(&user_key).unwrap();
let wrapped = create(&config.api_address, cookie);
let game = match wrapped.response {
ResponseCreateGame::Error(_err) => None, // TODO: handle
ResponseCreateGame::Valid(result) => Some(result),
};
let game_pub_json = match game {
None => {
panic!("I can't be bothered to handle this right now.")
}
Some(game) => serde_json::to_string(&GamePub::from_game(&game)).unwrap(),
};
server.send_message::<DataRequestChannel, DataRequestResponse>(
&user_key,
&DataRequestResponse::new(request.r#type, Some(vec![game_pub_json])),
);
// update cookie
global
.user_to_session_map
.insert(user_key, wrapped.cookie.clone());
server.send_message::<CookieRefreshChannel, CookieRefresh>(
&user_key,
&CookieRefresh::new(&wrapped.cookie),
);
}
DataRequestType::GameAllForming => {
// TODO: handle
let cookie = global.user_to_session_map.get(&user_key).unwrap();
let wrapped = all_forming(&config.api_address, cookie);
let game_ids = match wrapped.response {
ResponseAllForming::Error(_err) => vec![], // TODO: handle
ResponseAllForming::Valid(result) => result,
};
server.send_message::<DataRequestChannel, DataRequestResponse>(
&user_key,
&DataRequestResponse::new(request.r#type, Some(game_ids)),
);
// update cookie
global
.user_to_session_map
.insert(user_key, wrapped.cookie.clone());
server.send_message::<CookieRefreshChannel, CookieRefresh>(
&user_key,
&CookieRefresh::new(&wrapped.cookie),
);
}
DataRequestType::GameMyGames => {
// TODO: handle
let cookie = global.user_to_session_map.get(&user_key).unwrap();
let wrapped = my_games(&config.api_address, cookie);
let game_ids = match wrapped.response {
ResponseMyGames::Error(_err) => vec![], // TODO: handle
ResponseMyGames::Valid(result) => result,
};
server.send_message::<DataRequestChannel, DataRequestResponse>(
&user_key,
&DataRequestResponse::new(request.r#type, Some(game_ids)),
);
// update cookie
global
.user_to_session_map
.insert(user_key, wrapped.cookie.clone());
server.send_message::<CookieRefreshChannel, CookieRefresh>(
&user_key,
&CookieRefresh::new(&wrapped.cookie),
);
}
DataRequestType::GameInfo => {
// TODO: handle
let cookie = global.user_to_session_map.get(&user_key).unwrap();
let wrapped = api::game::info(
&config.api_address,
cookie,
&request.data.unwrap_or("".to_string()),
);
let game = match wrapped.response {
ResponseGameInfo::Error(_err) => None, // TODO: handle
ResponseGameInfo::Valid(result) => Some(result),
};
let game_pub_json = match game {
None => {
panic!("I can't be bothered to handle this right now.")
}
Some(game) => serde_json::to_string(&GamePub::from_game(&game)).unwrap(),
};
server.send_message::<DataRequestChannel, DataRequestResponse>(
&user_key,
&DataRequestResponse::new(request.r#type, Some(vec![game_pub_json])),
);
// update cookie
global
.user_to_session_map
.insert(user_key, wrapped.cookie.clone());
server.send_message::<CookieRefreshChannel, CookieRefresh>(
&user_key,
&CookieRefresh::new(&wrapped.cookie),
);
}
DataRequestType::PubUserDetails => {
// TODO: handle
let cookie = global.user_to_session_map.get(&user_key).unwrap();
let wrapped = user::info(
&config.api_address,
&request.data.unwrap_or("".to_string()),
cookie,
);
let user_details = match wrapped.response {
ResponseInfo::Error(_err) => {
panic!("I can't be bothered to handle this right now.")
}
ResponseInfo::Ok(result) => result,
};
// TODO: handle
let user_details_json = serde_json::to_string(&user_details).unwrap();
server.send_message::<DataRequestChannel, DataRequestResponse>(
&user_key,
&DataRequestResponse::new(request.r#type, Some(vec![user_details_json])),
);
// update cookie
global
.user_to_session_map
.insert(user_key, wrapped.cookie.clone());
server.send_message::<CookieRefreshChannel, CookieRefresh>(
&user_key,
&CookieRefresh::new(&wrapped.cookie),
);
}
}
}
}
}
*/