DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 781c44385dc343d0169b41bd9e8638054e3dbf6a deck-builder/client-old-for-ref/src/plugins/menu_old/play/startgamecall.rs -rw-r--r-- 3.1 KiB
781c4438Jonni Liljamo feat(client): hide title bar from dev details 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * 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 iyes_loopless::state::NextState;

use crate::{
    api::{
        self,
        game::{types::GameState, ResponsePatchGameState},
    },
    cfg::{CfgDev, CfgUser},
    runtime::{
        game::RTDGame,
        menu::{PlayMenuUIState, RTDMenu},
    },
};

use super::{PlayMenuState, StartGameEvent};

struct StartGameCallResponse {
    res: ResponsePatchGameState,
}

#[derive(Component)]
pub(super) struct StartGameCall(Task<StartGameCallResponse>);

pub(super) fn start(
    mut events: EventReader<StartGameEvent>,
    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.as_ref().unwrap().id.clone();

        let thread_pool = AsyncComputeTaskPool::get();
        let task = thread_pool.spawn(async move {
            let patch_game_state_response =
                api::game::patch_state(api_address, token, game_id, GameState::InProgress);

            StartGameCallResponse {
                res: patch_game_state_response,
            }
        });
        commands.spawn(StartGameCall(task));
        rtdmenu.waiting_for_start_game_call = true;
    }
}

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

    let (entity, mut task) = start_game_call_tasks.single_mut();
    if let Some(start_game_call_response) = future::block_on(future::poll_once(&mut task.0)) {
        rtdmenu.waiting_for_start_game_call = false;
        match start_game_call_response.res {
            ResponsePatchGameState::Valid(res) => {
                rtdmenu.play_menu_ui_state = PlayMenuUIState::Main;
                rtdgame.cur_game = Some(res);
                commands.insert_resource(NextState(PlayMenuState::None));
                commands.insert_resource(NextState(crate::GameState::Game));

                console.send(PrintConsoleLine::new(
                    format!(
                        "Started game with id: '{}'",
                        rtdmenu.cur_game.as_ref().unwrap().id
                    )
                    .into(),
                ));
            }
            ResponsePatchGameState::Error(error) => {
                console.send(PrintConsoleLine::new(
                    format!("Starting game failed, got error: '{}'", error).into(),
                ));
            }
        }

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