From f562571c0c947f21efdfad86f2ef39c3b569346d Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 3 May 2023 18:01:29 +0300 Subject: [PATCH] feat(client): parsing event --- client/src/plugins/async_tasks/mod.rs | 13 +++- .../plugins/async_tasks/parse_game_status.rs | 59 +++++++++++++++++++ client/src/plugins/game/mod.rs | 4 ++ client/src/plugins/game/ui/mod.rs | 14 ++++- 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 client/src/plugins/async_tasks/parse_game_status.rs diff --git a/client/src/plugins/async_tasks/mod.rs b/client/src/plugins/async_tasks/mod.rs index c5ab83b..70b3bc2 100644 --- a/client/src/plugins/async_tasks/mod.rs +++ b/client/src/plugins/async_tasks/mod.rs @@ -29,6 +29,9 @@ pub use req_game_mygames::GameMyGamesCallEvent; mod req_game_details; pub use req_game_details::GameDetailsCallEvent; +mod parse_game_status; +pub use parse_game_status::ParseGameStatusEvent; + pub struct AsyncTasksPlugin; impl Plugin for AsyncTasksPlugin { @@ -41,6 +44,7 @@ impl Plugin for AsyncTasksPlugin { .add_event::() .add_event::() .add_event::() + .add_event::() .add_systems( ( req_login::start_call, @@ -57,7 +61,14 @@ impl Plugin for AsyncTasksPlugin { req_game_mygames::handle_call, req_game_details::start_call, req_game_details::handle_call, + ) + .chain() + ) + .add_systems( + ( + parse_game_status::start_call, + parse_game_status::handle_call, ).chain() - ); + ); } } diff --git a/client/src/plugins/async_tasks/parse_game_status.rs b/client/src/plugins/async_tasks/parse_game_status.rs new file mode 100644 index 0000000..ca59406 --- /dev/null +++ b/client/src/plugins/async_tasks/parse_game_status.rs @@ -0,0 +1,59 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use crate::{ + plugins::game::GameData, game_status::GameStatus, api::game::Game, +}; + +use bevy::{prelude::*, tasks::{Task, AsyncComputeTaskPool}}; +use futures_lite::future; + +#[derive(Component)] +pub struct ParseGameStatus(Task); + +#[derive(Clone)] +pub struct ParseGameStatusEvent { + pub game: Game, +} + +pub fn start_call( + mut commands: Commands, + mut start_ev_r: EventReader, + ) { + for ev in start_ev_r.iter() { + let thread_pool = AsyncComputeTaskPool::get(); + let ev = ev.clone(); + let task = thread_pool.spawn(async move { + let res = GameStatus::new(&ev.game.actions.unwrap()); + + res + }); + commands.spawn(ParseGameStatus(task)); + } +} + +pub fn handle_call( + mut commands: Commands, + mut tasks: Query<(Entity, &mut ParseGameStatus)>, + mut game_data: ResMut, + ) { + match tasks.get_single_mut() { + Ok((entity, mut task)) => { + if let Some(response) = future::block_on(future::poll_once(&mut task.0)) { + game_data.game_status = Some(response); + game_data.parsing_data = false; + + // remove the task + commands.entity(entity).remove::(); + commands.entity(entity).despawn_recursive(); + } + } + // NOTE: don't do anything if the wanted thingy doesn't exist + _ => {} + } +} diff --git a/client/src/plugins/game/mod.rs b/client/src/plugins/game/mod.rs index c8c93d3..aab4d6e 100644 --- a/client/src/plugins/game/mod.rs +++ b/client/src/plugins/game/mod.rs @@ -28,6 +28,8 @@ impl Plugin for GamePlugin { pub struct GameData { pub game: Option, pub game_status: Option, + + pub parsing_data: bool, } impl Default for GameData { @@ -35,6 +37,8 @@ impl Default for GameData { Self { game: None, game_status: None, + + parsing_data: false, } } } diff --git a/client/src/plugins/game/ui/mod.rs b/client/src/plugins/game/ui/mod.rs index 7a28221..abd4977 100644 --- a/client/src/plugins/game/ui/mod.rs +++ b/client/src/plugins/game/ui/mod.rs @@ -9,7 +9,7 @@ use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; -use crate::{plugins::GameDetailsCallEvent, Global}; +use crate::{plugins::{GameDetailsCallEvent, ParseGameStatusEvent}, Global}; use super::GameData; @@ -19,6 +19,7 @@ pub fn ui( global: Res, mut game_data: ResMut, mut details_ev_w: EventWriter, + mut parse_ev_w: EventWriter, ) { egui::Window::new("Game Details") .show(contexts.ctx_mut(), |ui| { @@ -27,7 +28,16 @@ pub fn ui( return; }; let Some(status) = &game_data.game_status else { - // early return if game_status is None + if game_data.parsing_data { + // early return if game_status is None, and we're parsing it + return; + } + + // since we're not already parsing the data, we start it + game_data.parsing_data = true; + parse_ev_w.send(ParseGameStatusEvent { + game: game_data.game.as_ref().unwrap().clone(), + }); return; }; -- 2.44.1