/*
* This file is part of sdbclient
* 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 futures_lite::future;
use iyes_loopless::prelude::*;
use belly::prelude::*;
use bevy_console::PrintConsoleLine;
use crate::{
api::{self, game::ResponseCreateGame},
cfg::{CfgDev, CfgUser},
};
use super::PlayMenuState;
pub(super) fn setup(mut commands: Commands) {
commands.add(eml! {
<body>
<div c:menu>
<span c:menutitle>
"Create"
</span>
<button c:menubutton on:press=connect!(|ctx| {
ctx.commands().insert_resource(NextState(PlayMenuState::Main))
})>
"Back"
</button>
</div>
</body>
});
}
struct CreateGameCallResponse {
game: ResponseCreateGame,
}
#[derive(Component)]
struct CreateGameCall(Task<CreateGameCallResponse>);
fn start_create_game_call(mut commands: Commands, cfg_dev: Res<CfgDev>, cfg_user: Res<CfgUser>) {
let api_address = cfg_dev.api_server.clone();
let token = cfg_user.user_token.clone();
let thread_pool = AsyncComputeTaskPool::get();
let task = thread_pool.spawn(async move {
let create_game_response = api::game::creategame(api_address.clone(), token.clone());
CreateGameCallResponse {
game: create_game_response,
}
});
commands.spawn(CreateGameCall(task));
}
fn handle_create_game_call(
mut commands: Commands,
mut create_game_tasks: Query<(Entity, &mut CreateGameCall)>,
mut console: EventWriter<PrintConsoleLine>,
) {
let (entity, mut task) = create_game_tasks.single_mut();
if let Some(create_game_call_response) = future::block_on(future::poll_once(&mut task.0)) {
todo!();
}
}