/*
* 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::*,
tasks::{AsyncComputeTaskPool, Task},
};
use bevy_console::PrintConsoleLine;
use futures_lite::future;
use iyes_loopless::state::NextState;
use crate::{
api::{
self,
game::{types::GameState, ResponsePatchGameState},
},
cfg::{CfgDev, CfgUser},
runtime::{
game::RTDGame,
menu::{PlayMenuUIState, RTDMenu},
},
};
use super::{PlayMenuState, StartGameEvent};
struct StartGameCallResponse {
res: ResponsePatchGameState,
}
#[derive(Component)]
pub(super) struct StartGameCall(Task<StartGameCallResponse>);
pub(super) fn start(
mut events: EventReader<StartGameEvent>,
mut commands: Commands,
cfg_dev: Res<CfgDev>,
cfg_user: Res<CfgUser>,
mut rtdmenu: ResMut<RTDMenu>,
) {
for _event in events.iter() {
let api_address = cfg_dev.api_server.clone();
let token = cfg_user.user_token.clone();
let game_id = rtdmenu.cur_game.as_ref().unwrap().id.clone();
let thread_pool = AsyncComputeTaskPool::get();
let task = thread_pool.spawn(async move {
let patch_game_state_response =
api::game::patch_state(api_address, token, game_id, GameState::InProgress);
StartGameCallResponse {
res: patch_game_state_response,
}
});
commands.spawn(StartGameCall(task));
rtdmenu.waiting_for_start_game_call = true;
}
}
pub(super) fn handle(
mut commands: Commands,
mut start_game_call_tasks: Query<(Entity, &mut StartGameCall)>,
mut rtdmenu: ResMut<RTDMenu>,
mut rtdgame: ResMut<RTDGame>,
mut console: EventWriter<PrintConsoleLine>,
) {
if start_game_call_tasks.is_empty() {
return;
}
let (entity, mut task) = start_game_call_tasks.single_mut();
if let Some(start_game_call_response) = future::block_on(future::poll_once(&mut task.0)) {
rtdmenu.waiting_for_start_game_call = false;
match start_game_call_response.res {
ResponsePatchGameState::Valid(res) => {
rtdmenu.play_menu_ui_state = PlayMenuUIState::Main;
rtdgame.cur_game = Some(res);
commands.insert_resource(NextState(PlayMenuState::None));
commands.insert_resource(NextState(crate::GameState::Game));
console.send(PrintConsoleLine::new(
format!(
"Started game with id: '{}'",
rtdmenu.cur_game.as_ref().unwrap().id
)
.into(),
));
}
ResponsePatchGameState::Error(error) => {
console.send(PrintConsoleLine::new(
format!("Starting game failed, got error: '{}'", error).into(),
));
}
}
// Remove the task, since it's done now
commands.entity(entity).remove::<StartGameCall>();
commands.entity(entity).despawn_recursive();
}
}