M client/src/macros/async_task.rs => client/src/macros/async_task.rs +23 -0
@@ 133,4 133,27 @@ macro_rules! async_task_handle_call {
}
}
};
+
+ ($call_type:ty, |$unused:ident, $ihatethis:ident, $response:ident, $game_data:ident, $parse_ev_w:ident| $handler_func:expr) => {
+ pub fn handle_call(
+ mut commands: Commands,
+ mut tasks: Query<(Entity, &mut $call_type)>,
+ mut $game_data: ResMut<GameData>,
+ mut $parse_ev_w: EventWriter<ParseGameStatusEvent>,
+ ) {
+ match tasks.get_single_mut() {
+ Ok((entity, mut task)) => {
+ if let Some($response) = future::block_on(future::poll_once(&mut task.0)) {
+ $handler_func;
+
+ // remove the task
+ commands.entity(entity).remove::<$call_type>();
+ commands.entity(entity).despawn_recursive();
+ }
+ }
+ // NOTE: don't do anything if the wanted thingy doesn't exist
+ _ => {}
+ }
+ }
+ };
}
M client/src/plugins/async_tasks/req_game_details.rs => client/src/plugins/async_tasks/req_game_details.rs +8 -2
@@ 9,7 9,7 @@
use crate::{
async_task_start_call, async_task_handle_call,
api::{self, game::DetailsResponse}, NetworkingOptions,
- plugins::game::GameData,
+ plugins::{game::GameData, ParseGameStatusEvent},
};
use bevy::{prelude::*, tasks::{Task, AsyncComputeTaskPool}};
@@ 29,12 29,18 @@ async_task_start_call!(GameDetailsCallEvent, GameDetailsCall, |ev, no| {
res
});
-async_task_handle_call!(GameDetailsCall, |response, _global, game_data| {
+async_task_handle_call!(GameDetailsCall, |_unused, _ihatethis, response, game_data, parse_ev_w| {
match response {
Err(_err) => panic!("game details failed, handle me"),
Ok(resp) => {
info!("game details for {}", resp.id);
game_data.game = Some(resp);
+
+ // start data parsing
+ game_data.parsing_data = true;
+ parse_ev_w.send(ParseGameStatusEvent {
+ game: game_data.game.as_ref().unwrap().clone(),
+ });
}
}
});
M client/src/plugins/game/ui/mod.rs => client/src/plugins/game/ui/mod.rs +3 -10
@@ 9,7 9,7 @@
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts};
-use crate::{plugins::{GameDetailsCallEvent, ParseGameStatusEvent}, Global};
+use crate::{plugins::GameDetailsCallEvent, Global};
use super::GameData;
@@ 19,7 19,6 @@ pub fn ui(
global: Res<Global>,
mut game_data: ResMut<GameData>,
mut details_ev_w: EventWriter<GameDetailsCallEvent>,
- mut parse_ev_w: EventWriter<ParseGameStatusEvent>,
) {
egui::Window::new("Game Details")
.show(contexts.ctx_mut(), |ui| {
@@ 32,12 31,8 @@ pub fn ui(
// 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(),
- });
+ // game data is None, but we're not parsing it...
+ // should be unreachable, I think?
return;
};
@@ 45,8 40,6 @@ pub fn ui(
details_ev_w.send(GameDetailsCallEvent {
game_id: game_data.game.as_ref().unwrap().id.clone(),
});
-
- // somehow trigger ParseGameStatusEvent after fetched.
}
ui.separator();