DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

655bd78123ab545bed91974a69dfba956f4c273d — Jonni Liljamo 1 year, 10 months ago 65f0158
WIP(sdbclient): create game UI and API call
A sdbclient/src/api/game/mod.rs => sdbclient/src/api/game/mod.rs +35 -0
@@ 0,0 1,35 @@
/*
 * 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 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()
}

M sdbclient/src/api/mod.rs => sdbclient/src/api/mod.rs +2 -1
@@ 1,6 1,6 @@
/*
 * This file is part of sdbclient
 * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * 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)]

M sdbclient/src/plugins/menu/play/creategame.rs => sdbclient/src/plugins/menu/play/creategame.rs +44 -1
@@ 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) {
        </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!();
    }
}