DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

e170beff6e390259d0dda63069cdd52637e63e72 — Jonni Liljamo 1 year, 10 months ago 68248ed
feat(sdbclient): join games
A sdbclient/src/plugins/menu/play/joingamecall.rs => sdbclient/src/plugins/menu/play/joingamecall.rs +89 -0
@@ 0,0 1,89 @@
/*
 * 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 bevy_console::PrintConsoleLine;

use futures_lite::future;

use crate::{
    api::{self, game::ResponseJoinGame},
    cfg::{CfgDev, CfgUser},
    runtime::menu::{PlayMenuUIState, RTDMenu},
};

use super::JoinGameEvent;

struct JoinGameCallResponse {
    res: ResponseJoinGame,
}

#[derive(Component)]
pub(super) struct JoinGameCall(Task<JoinGameCallResponse>);

pub(super) fn start(
    mut events: EventReader<JoinGameEvent>,
    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_id.clone();

        let thread_pool = AsyncComputeTaskPool::get();
        let task = thread_pool.spawn(async move {
            let join_game_response = api::game::join(api_address, token, game_id);

            JoinGameCallResponse {
                res: join_game_response,
            }
        });
        commands.spawn(JoinGameCall(task));
        rtdmenu.waiting_for_join_game_call = true;
    }
}

pub(super) fn handle(
    mut commands: Commands,
    mut join_game_call_tasks: Query<(Entity, &mut JoinGameCall)>,
    mut rtdmenu: ResMut<RTDMenu>,
    mut console: EventWriter<PrintConsoleLine>,
) {
    if join_game_call_tasks.is_empty() {
        return;
    }

    let (entity, mut task) = join_game_call_tasks.single_mut();
    if let Some(join_game_call_response) = future::block_on(future::poll_once(&mut task.0)) {
        rtdmenu.waiting_for_join_game_call = false;
        match join_game_call_response.res {
            ResponseJoinGame::Valid(_res) => {
                rtdmenu.play_menu_ui_state = PlayMenuUIState::InLobbyGuest;

                console.send(PrintConsoleLine::new(
                    format!("Joined game with id: '{}'", rtdmenu.cur_game_id).into(),
                ));
            }
            ResponseJoinGame::Error(error) => {
                console.send(PrintConsoleLine::new(
                    format!("Join game failed, got error: '{}'", error).into(),
                ));
            }
        }

        // Remove the task, since it's done now
        commands.entity(entity).remove::<JoinGameCall>();
        commands.entity(entity).despawn_recursive();
    }
}

M sdbclient/src/plugins/menu/play/mod.rs => sdbclient/src/plugins/menu/play/mod.rs +9 -0
@@ 17,9 17,11 @@ mod ui;

mod allformingcall;
mod creategamecall;
mod joingamecall;

struct CreateGameEvent;
struct AllFormingEvent;
struct JoinGameEvent;

pub(super) struct PlayMenuPlugin;



@@ 28,6 30,7 @@ impl Plugin for PlayMenuPlugin {
        app.add_loopless_state(PlayMenuState::None)
            .add_event::<CreateGameEvent>()
            .add_event::<AllFormingEvent>()
            .add_event::<JoinGameEvent>()
            .add_enter_system(MenuState::Play, play_menu_setup)
            .add_system_set(
                ConditionSet::new()


@@ 37,6 40,8 @@ impl Plugin for PlayMenuPlugin {
                    .with_system(creategamecall::handle.run_if(waiting_for_create_game_call))
                    .with_system(allformingcall::start.run_on_event::<AllFormingEvent>())
                    .with_system(allformingcall::handle.run_if(waiting_for_all_forming_call))
                    .with_system(joingamecall::start.run_on_event::<JoinGameEvent>())
                    .with_system(joingamecall::handle.run_if(waiting_for_join_game_call))
                    .into(),
            );
    }


@@ 60,3 65,7 @@ fn waiting_for_create_game_call(rtdmenu: Res<RTDMenu>) -> bool {
fn waiting_for_all_forming_call(rtdmenu: Res<RTDMenu>) -> bool {
    rtdmenu.waiting_for_all_forming_call
}

fn waiting_for_join_game_call(rtdmenu: Res<RTDMenu>) -> bool {
    rtdmenu.waiting_for_join_game_call
}

M sdbclient/src/plugins/menu/play/ui.rs => sdbclient/src/plugins/menu/play/ui.rs +21 -5
@@ 12,17 12,19 @@ use iyes_loopless::prelude::*;

use crate::{
    plugins::menu::MenuState,
    runtime::menu::{PlayMenuUIState, RTDMenu},
    runtime::menu::{PlayMenuUIState, RTDMenu}, cfg::CfgUser,
};

use super::{AllFormingEvent, CreateGameEvent, PlayMenuState};
use super::{AllFormingEvent, CreateGameEvent, JoinGameEvent, PlayMenuState};

pub(super) fn show(
    mut commands: Commands,
    mut egui_context: ResMut<EguiContext>,
    mut rtdmenu: ResMut<RTDMenu>,
    cfg_user: Res<CfgUser>,
    mut creategame_ev_w: EventWriter<CreateGameEvent>,
    mut allforming_ev_w: EventWriter<AllFormingEvent>,
    mut joingame_ev_w: EventWriter<JoinGameEvent>,
) {
    egui::Window::new(egui::RichText::new(rtdmenu.play_menu_ui_state.display()).size(32.))
        .resizable(false)


@@ 93,9 95,10 @@ pub(super) fn show(
                                            ui.label("No games found.");
                                        }
                                        false => {
                                            for game in &rtdmenu.all_forming_games {
                                            for game in rtdmenu.all_forming_games.clone() {
                                                egui::Frame::none()
                                                    .fill(egui::Color32::BLACK)
                                                    .rounding(4.)
                                                    .outer_margin(4.)
                                                    .inner_margin(4.)
                                                    .show(ui, |ui| {


@@ 109,8 112,21 @@ pub(super) fn show(
                                                                    egui::Align::Center,
                                                                ),
                                                                |ui| {
                                                                    if ui.button("Join").clicked() {
                                                                        todo!();
                                                                    if game.guest_id == cfg_user.id {
                                                                        ui.add_enabled(false, egui::Button::new("Joined"));
                                                                        if ui.button("Inspect").clicked() {
                                                                            rtdmenu.play_menu_ui_state = PlayMenuUIState::InLobbyGuest;
                                                                        }
                                                                    } else if game.host_id == cfg_user.id {
                                                                        ui.add_enabled(false, egui::Button::new("Host"));
                                                                        if ui.button("Inspect").clicked() {
                                                                            rtdmenu.play_menu_ui_state = PlayMenuUIState::InLobbyHost;
                                                                        }
                                                                    } else {
                                                                        if ui.button("Join").clicked() {
                                                                            rtdmenu.cur_game_id = game.id.clone();
                                                                            joingame_ev_w.send(JoinGameEvent);
                                                                        }
                                                                    }
                                                                },
                                                            );

M sdbclient/src/runtime/menu/mod.rs => sdbclient/src/runtime/menu/mod.rs +2 -0
@@ 20,6 20,7 @@ pub(crate) struct RTDMenu {
    /// List of all forming games
    pub all_forming_games: Vec<Game>,
    pub waiting_for_all_forming_call: bool,
    pub waiting_for_join_game_call: bool,
}

impl Default for RTDMenu {


@@ 30,6 31,7 @@ impl Default for RTDMenu {
            cur_game_id: String::from(""),
            all_forming_games: vec![],
            waiting_for_all_forming_call: false,
            waiting_for_join_game_call: false,
        }
    }
}