A sdbclient/src/api/mod.rs => sdbclient/src/api/mod.rs +24 -0
@@ 0,0 1,24 @@
+/*
+ * This file is part of sdbclient
+ * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
+ *
+ * Licensed under GPL-3.0-only.
+ * See LICENSE for licensing information.
+ */
+
+use reqwest;
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize)]
+pub struct APIInfo {
+ pub info: String,
+ pub ver: String,
+}
+
+pub fn info(api_address: String) -> APIInfo {
+ let client = reqwest::blocking::Client::new();
+
+ let resp = client.get(&format!("{}/info", api_address)).send().unwrap();
+
+ resp.json().unwrap()
+}
M sdbclient/src/main.rs => sdbclient/src/main.rs +4 -2
@@ 14,6 14,7 @@ use bevy::{
use bevy_egui::EguiPlugin;
use bevy_inspector_egui::WorldInspectorPlugin;
+mod api;
mod cfg;
mod plugins;
mod util;
@@ 78,7 79,7 @@ fn main() {
user_token: "".to_string(),
})
.insert_resource(cfg::CfgHidden {
- api_server: "http://localhost:8080".to_string(),
+ api_server: "http://localhost:8080/api".to_string(),
})
.insert_resource(InputsUserLogin("".to_string(), "".to_string()))
.insert_resource(InputsUserRegister(
@@ 90,7 91,8 @@ fn main() {
app.add_startup_system(setup).add_state(GameState::Splash);
- app.add_plugin(plugins::splash::SplashPlugin)
+ app.add_plugin(plugins::connection_check::ConnectionCheckPlugin)
+ .add_plugin(plugins::splash::SplashPlugin)
.add_plugin(plugins::menu::MenuPlugin);
app.run();
A sdbclient/src/plugins/connection_check/mod.rs => sdbclient/src/plugins/connection_check/mod.rs +58 -0
@@ 0,0 1,58 @@
+/*
+ * This file is part of sdbclient
+ * Copyright (C) 2022 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 crate::{api, cfg::CfgHidden};
+
+/// This plugin will check if we can connect to the API
+pub struct ConnectionCheckPlugin;
+
+impl Plugin for ConnectionCheckPlugin {
+ fn build(&self, app: &mut App) {
+ app
+ // Load the splash when we enter the Splash state
+ .add_startup_system(start_connection_check)
+ .add_system(handle_connection_check);
+ }
+}
+
+#[derive(Component)]
+struct ConnectionCheck(Task<api::APIInfo>);
+
+fn start_connection_check(mut commands: Commands, cfg_hidden: Res<CfgHidden>) {
+ let api_address = cfg_hidden.api_server.clone();
+ let thread_pool = AsyncComputeTaskPool::get();
+ let task = thread_pool.spawn(async move {
+ let api_info = api::info(api_address);
+
+ api_info
+ });
+ commands.spawn(ConnectionCheck(task));
+}
+
+fn handle_connection_check(
+ mut commands: Commands,
+ mut connection_check_tasks: Query<(Entity, &mut ConnectionCheck)>,
+) {
+ for (entity, mut task) in &mut connection_check_tasks {
+ if let Some(api_info) = future::block_on(future::poll_once(&mut task.0)) {
+ info!("API connection check passed");
+ info!("API version: {}", api_info.ver);
+
+ // Remove the task, since it's done now
+ commands.entity(entity).remove::<ConnectionCheck>();
+ commands.entity(entity).despawn_recursive();
+ }
+ }
+}
M sdbclient/src/plugins/mod.rs => sdbclient/src/plugins/mod.rs +1 -0
@@ 6,5 6,6 @@
* See LICENSE for licensing information.
*/
+pub mod connection_check;
pub mod menu;
pub mod splash;