/*
* This file is part of laurelin/client
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::prelude::*;
use laurelin_shared::{
server::{
channels::{AfterAuthChannel, CookieRefreshChannel, DataRequestChannel},
messages::{AfterAuth, CookieRefresh, DataRequestResponse, DataRequestType},
},
types::{game::GamePub, user::UserPub},
};
use naia_bevy_client::{
events::{ClientTickEvent, ConnectEvent, DisconnectEvent, MessageEvents, RejectEvent},
Client,
};
use crate::{
cfg::CfgUser,
plugins::menu::{
ui::{
connect::{ConnectScreenData, ConnectState},
play::{PlayScreenData, PlayScreenState},
},
MenuState,
},
Global,
};
use super::send::{game::GameInfoEvent, user::PubUserDetailsEvent};
pub fn connect_events(mut ev: EventReader<ConnectEvent>, client: Client) {
for _ in ev.iter() {
if let Ok(server_address) = client.server_address() {
info!("Laurelin client connected to: '{}'", server_address);
}
}
}
pub fn reject_events(mut ev: EventReader<RejectEvent>, mut data: ResMut<ConnectScreenData>) {
for _ in ev.iter() {
warn!("Laurelin client rejected from connecting to server!");
data.error = "rejected from connecting".to_string();
data.state = if data.state == ConnectState::LoggingIn {
ConnectState::Login
} else {
ConnectState::Register
}
}
}
pub fn disconnect_events(mut ev: EventReader<DisconnectEvent>) {
for _ in ev.iter() {
info!("Laurelin client disconnected from server.");
}
}
pub fn message_events(
mut commands: Commands,
mut ev: EventReader<MessageEvents>,
mut pud_ev_w: EventWriter<PubUserDetailsEvent>,
mut gi_ev_w: EventWriter<GameInfoEvent>,
mut global: ResMut<Global>,
mut cfg_user: ResMut<CfgUser>,
mut connect_data: ResMut<ConnectScreenData>,
mut play_data: ResMut<PlayScreenData>,
) {
for events in ev.iter() {
for aa_message in events.read::<AfterAuthChannel, AfterAuth>() {
// TODO: move handling to a separate function?
// save details from AfterAuth
cfg_user.id = aa_message.id;
cfg_user.username = aa_message.username;
cfg_user.cookie = aa_message.cookie;
// reset the connection screen to login
connect_data.state = ConnectState::Login;
// take us to the main menu
commands.insert_resource(NextState(Some(MenuState::Menu)));
}
for cr_message in events.read::<CookieRefreshChannel, CookieRefresh>() {
cfg_user.cookie = cr_message.cookie;
}
for response in events.read::<DataRequestChannel, DataRequestResponse>() {
match DataRequestType::from_u8(&response.r#type) {
DataRequestType::GameCreate => {
// TODO: handle possible errors (unwrap,
// and if the response data is an error)
let game =
serde_json::from_str(response.data.unwrap().get(0).unwrap()).unwrap();
global.cur_game = Some(game);
play_data.state = PlayScreenState::InLobbyHost;
play_data.waiting_for_create_game = false;
}
DataRequestType::GameAllForming => {
// TODO: handle
let all_forming_ids = response.data.unwrap();
for game_id in &all_forming_ids {
gi_ev_w.send(GameInfoEvent {
id: game_id.clone(),
});
}
play_data.waiting_for_all_forming = false;
}
DataRequestType::GameMyGames => {
// TODO: handle
let my_games_ids = response.data.unwrap();
for game_id in &my_games_ids {
gi_ev_w.send(GameInfoEvent {
id: game_id.clone(),
});
}
play_data.waiting_for_my_games = false;
}
DataRequestType::GameInfo => {
// TODO: handle
let game: GamePub =
serde_json::from_str(response.data.unwrap().get(0).unwrap()).unwrap();
// cache players of the game
pud_ev_w.send(PubUserDetailsEvent {
id: game.host_id.clone(),
});
if let Some(guest_id) = &game.guest_id {
pud_ev_w.send(PubUserDetailsEvent {
id: guest_id.clone(),
});
}
global.games_cache_queue.retain(|id| id != &game.id);
global.games_cache.insert(game.id.clone(), game);
}
DataRequestType::PubUserDetails => {
// TODO: handle possible error
let user: UserPub =
serde_json::from_str(response.data.unwrap().get(0).unwrap()).unwrap();
global.users_cache_queue.retain(|id| id != &user.id);
global.users_cache.insert(user.id.clone(), user);
}
}
}
}
}
pub fn tick_events(mut ev: EventReader<ClientTickEvent>, client: Client) {
if !client.is_connected() {
return;
}
for ClientTickEvent(_tick) in ev.iter() {}
}