From 655bd78123ab545bed91974a69dfba956f4c273d Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 3 Feb 2023 14:45:18 +0200 Subject: [PATCH] WIP(sdbclient): create game UI and API call --- sdbclient/src/api/game/mod.rs | 35 +++++++++++++++ sdbclient/src/api/mod.rs | 3 +- sdbclient/src/plugins/menu/play/creategame.rs | 45 ++++++++++++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 sdbclient/src/api/game/mod.rs diff --git a/sdbclient/src/api/game/mod.rs b/sdbclient/src/api/game/mod.rs new file mode 100644 index 0000000..f8f287c --- /dev/null +++ b/sdbclient/src/api/game/mod.rs @@ -0,0 +1,35 @@ +/* + * This file is part of sdbclient + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use reqwest; +use serde::{Deserialize, Serialize}; + +use super::APIErrorWrapper; + +#[derive(Debug, Serialize, Deserialize)] +pub struct ResultCreateGame { + pub id: String, +} + +#[derive(Debug, Serialize, Deserialize)] +pub enum ResponseCreateGame { + Error(APIErrorWrapper), + Valid(ResultCreateGame), +} + +pub fn creategame(api_address: String, token: String) -> ResponseCreateGame { + let client = reqwest::blocking::Client::new(); + + let resp = client + .get(&format!("{}/game/create", api_address)) + .header("Authorization", token) + .send() + .unwrap(); + + resp.json().unwrap() +} diff --git a/sdbclient/src/api/mod.rs b/sdbclient/src/api/mod.rs index 22f8a6b..505d6e9 100644 --- a/sdbclient/src/api/mod.rs +++ b/sdbclient/src/api/mod.rs @@ -1,6 +1,6 @@ /* * This file is part of sdbclient - * Copyright (C) 2022 Jonni Liljamo + * Copyright (C) 2023 Jonni Liljamo * * Licensed under GPL-3.0-only. * See LICENSE for licensing information. @@ -9,6 +9,7 @@ use reqwest; use serde::{Deserialize, Serialize}; +pub mod game; pub mod user; #[derive(Debug, Deserialize, Serialize)] diff --git a/sdbclient/src/plugins/menu/play/creategame.rs b/sdbclient/src/plugins/menu/play/creategame.rs index e5856c6..2765145 100644 --- a/sdbclient/src/plugins/menu/play/creategame.rs +++ b/sdbclient/src/plugins/menu/play/creategame.rs @@ -6,10 +6,20 @@ * See LICENSE for licensing information. */ -use bevy::prelude::*; +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; @@ -29,3 +39,36 @@ pub(super) fn setup(mut commands: Commands) { }); } + +struct CreateGameCallResponse { + game: ResponseCreateGame, +} + +#[derive(Component)] +struct CreateGameCall(Task); + +fn start_create_game_call(mut commands: Commands, cfg_dev: Res, cfg_user: Res) { + 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, +) { + 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!(); + } +} -- 2.44.1