DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

482b3e330398f9c80dbc71021227a12550e1b547 — Jonni Liljamo 1 year, 7 months ago 1d41589
WIP(client): browsing and related call events
M client/src/plugins/async_tasks/mod.rs => client/src/plugins/async_tasks/mod.rs +18 -0
@@ 14,6 14,15 @@ pub use req_login::LoginCallEvent;
mod req_register;
pub use req_register::RegisterCallEvent;

mod req_game_create;
pub use req_game_create::GameCreateCallEvent;

mod req_game_forming;
pub use req_game_forming::GameFormingCallEvent;

mod req_game_mygames;
pub use req_game_mygames::GameMyGamesCallEvent;

pub struct AsyncTasksPlugin;

impl Plugin for AsyncTasksPlugin {


@@ 21,12 30,21 @@ impl Plugin for AsyncTasksPlugin {
        app
            .add_event::<LoginCallEvent>()
            .add_event::<RegisterCallEvent>()
            .add_event::<GameCreateCallEvent>()
            .add_event::<GameFormingCallEvent>()
            .add_event::<GameMyGamesCallEvent>()
            .add_systems(
            (
                req_login::start_call,
                req_login::handle_call,
                req_register::start_call,
                req_register::handle_call,
                req_game_create::start_call,
                req_game_create::handle_call,
                req_game_forming::start_call,
                req_game_forming::handle_call,
                req_game_mygames::start_call,
                req_game_mygames::handle_call,
            ).chain()
        );
    }

A client/src/plugins/async_tasks/req_game_create.rs => client/src/plugins/async_tasks/req_game_create.rs +37 -0
@@ 0,0 1,37 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use crate::{
    async_task_start_call, async_task_handle_call,
    api::{self, game::CreateResponse}, NetworkingOptions,
    plugins::menu::MenuData,
};

use bevy::{prelude::*, tasks::{Task, AsyncComputeTaskPool}};
use futures_lite::future;

#[derive(Component)]
pub struct GameCreateCall(Task<CreateResponse>);

#[derive(Clone)]
pub struct GameCreateCallEvent;

async_task_start_call!(GameCreateCallEvent, GameCreateCall, |ev, no| {
    let res = api::game::create(&mut no);

    res
});

async_task_handle_call!(GameCreateCall, |response, menu_data| {
    match response {
        Err(_err) => panic!("game create failed, handle me"),
        Ok(resp) => {
            info!("created game {}", resp.id);
        }
    }
});

A client/src/plugins/async_tasks/req_game_forming.rs => client/src/plugins/async_tasks/req_game_forming.rs +37 -0
@@ 0,0 1,37 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use crate::{
    async_task_start_call, async_task_handle_call,
    api::{self, game::FormingResponse}, NetworkingOptions,
    plugins::menu::MenuData,
};

use bevy::{prelude::*, tasks::{Task, AsyncComputeTaskPool}};
use futures_lite::future;

#[derive(Component)]
pub struct GameFormingCall(Task<FormingResponse>);

#[derive(Clone)]
pub struct GameFormingCallEvent;

async_task_start_call!(GameFormingCallEvent, GameFormingCall, |ev, no| {
    let res = api::game::forming(&mut no);

    res
});

async_task_handle_call!(GameFormingCall, |response, menu_data| {
    match response {
        Err(_err) => panic!("forming games failed, handle me"),
        Ok(resp) => {
            // save to a menu data field
        }
    }
});

A client/src/plugins/async_tasks/req_game_mygames.rs => client/src/plugins/async_tasks/req_game_mygames.rs +37 -0
@@ 0,0 1,37 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use crate::{
    async_task_start_call, async_task_handle_call,
    api::{self, game::MyGamesResponse}, NetworkingOptions,
    plugins::menu::MenuData,
};

use bevy::{prelude::*, tasks::{Task, AsyncComputeTaskPool}};
use futures_lite::future;

#[derive(Component)]
pub struct GameMyGamesCall(Task<MyGamesResponse>);

#[derive(Clone)]
pub struct GameMyGamesCallEvent;

async_task_start_call!(GameMyGamesCallEvent, GameMyGamesCall, |ev, no| {
    let res = api::game::my_games(&mut no);

    res
});

async_task_handle_call!(GameMyGamesCall, |response, menu_data| {
    match response {
        Err(_err) => panic!("my games failed, handle me"),
        Ok(resp) => {
            // save to a menu data field
        }
    }
});

M client/src/plugins/menu/mod.rs => client/src/plugins/menu/mod.rs +14 -0
@@ 33,6 33,10 @@ enum MenuState {
#[derive(Resource)]
pub struct MenuData {
    pub ui_state: MenuUIState,
    pub browse_state: BrowseState,

    /// true if waiting on some request
    pub waiting: bool,

    error: String,



@@ 49,6 53,9 @@ impl Default for MenuData {
    fn default() -> Self {
        Self {
            ui_state: MenuUIState::Login,
            browse_state: BrowseState::Forming,

            waiting: false,

            error: String::from(""),



@@ 73,6 80,13 @@ pub enum MenuUIState {

}

#[derive(Debug, PartialEq)]
pub enum BrowseState {
    Forming,
    InProgress,
    Finished,
}

fn menu_setup(mut commands: Commands) {
    commands.insert_resource(NextState(Some(MenuState::Visible)));
}

M client/src/plugins/menu/ui/browse.rs => client/src/plugins/menu/ui/browse.rs +63 -2
@@ 9,13 9,74 @@
use bevy::prelude::*;
use bevy_egui::egui;

use crate::{util::egui::password, plugins::LoginCallEvent};
use crate::plugins::{
    GameCreateCallEvent, BrowseState, 
    GameFormingCallEvent, GameMyGamesCallEvent
};

use super::{MenuData, MenuUIState};

pub fn view(
    ui: &mut egui::Ui,
    data: &mut MenuData,
    create_ev_w: &mut EventWriter<GameCreateCallEvent>,
    forming_ev_w: &mut EventWriter<GameFormingCallEvent>,
    mygames_ev_w: &mut EventWriter<GameMyGamesCallEvent>,
) {
    ui.label("browse");
    egui::SidePanel::left("browse_side_panel")
        .resizable(false)
        .show_inside(ui, |ui| {
            ui.vertical_centered(|ui| {
                ui.add_enabled_ui(!data.waiting, |ui| {
                    if ui.button("Create").clicked() {
                        create_ev_w.send(GameCreateCallEvent);
                    }
                    if ui.button("Back").clicked() {
                        data.ui_state = MenuUIState::Main;
                    }
                });
            });
        });
    egui::TopBottomPanel::top("browse_top_panel")
        .resizable(false)
        .show_inside(ui, |ui| {
            ui.horizontal(|ui| {
                ui.selectable_value(&mut data.browse_state, BrowseState::Forming, "Forming");
                ui.selectable_value(&mut data.browse_state, BrowseState::InProgress, "In Progress");
                ui.selectable_value(&mut data.browse_state, BrowseState::Finished, "Finished");

                ui.with_layout(
                    egui::Layout::right_to_left(egui::Align::Center),
                    |ui| {
                        if ui.button("Refresh").clicked() {
                            match data.browse_state {
                                BrowseState::Forming => {
                                    if !data.waiting {
                                        forming_ev_w.send(GameFormingCallEvent);
                                    }
                                }
                                BrowseState::InProgress => {
                                    if !data.waiting {
                                        mygames_ev_w.send(GameMyGamesCallEvent);
                                    }
                                }
                                BrowseState::Finished => {
                                    if !data.waiting {
                                        mygames_ev_w.send(GameMyGamesCallEvent);
                                    }
                                }
                            }
                        }
                    },
                );
            });
        });
    ui.vertical_centered(|ui| {
        egui::ScrollArea::vertical().show(ui, |ui| match data.browse_state {
            BrowseState::Forming => {}
            BrowseState::InProgress => {}
            BrowseState::Finished => {}
        }
        );
    });
}

M client/src/plugins/menu/ui/mod.rs => client/src/plugins/menu/ui/mod.rs +12 -3
@@ 9,7 9,7 @@
use bevy::prelude::*;
use bevy_egui::{EguiContexts, egui};

use crate::plugins::{LoginCallEvent, RegisterCallEvent};
use crate::plugins::{LoginCallEvent, RegisterCallEvent, GameCreateCallEvent, GameFormingCallEvent, GameMyGamesCallEvent};

pub use super::{MenuData, MenuUIState};



@@ 23,13 23,16 @@ pub fn ui(
    mut data: ResMut<MenuData>,
    mut login_ev_w: EventWriter<LoginCallEvent>,
    mut register_ev_w: EventWriter<RegisterCallEvent>,
    mut create_ev_w: EventWriter<GameCreateCallEvent>,
    mut forming_ev_w: EventWriter<GameFormingCallEvent>,
    mut mygames_ev_w: EventWriter<GameMyGamesCallEvent>,
) {
    egui::Window::new(format!("{:?}", data.ui_state))
        .collapsible(false)
        .resizable(false)
        .anchor(egui::Align2::CENTER_CENTER, egui::Vec2::ZERO)
        .show(contexts.ctx_mut(), |ui| {
        match data.ui_state {
        match &data.ui_state {
            MenuUIState::Loading => {
                ui.horizontal(|ui| {
                    ui.spinner();


@@ 43,7 46,13 @@ pub fn ui(
                    data.ui_state = MenuUIState::Browse;
                }
            }
            MenuUIState::Browse => browse::view(ui, &mut data),
            MenuUIState::Browse => browse::view(
                ui,
                &mut data,
                &mut create_ev_w,
                &mut forming_ev_w,
                &mut mygames_ev_w,
            ),
        }
    });
}