DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: dev deck-builder/client/src/plugins/menu/ui/browse.rs -rw-r--r-- 5.5 KiB
1904a6d3Jonni Liljamo feat(client): sorta kinda check for end conditions 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * 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 bevy::prelude::*;
use bevy_egui::egui;

use crate::{
    api::game::{Game, GameState},
    plugins::{
        menu::MenuState, BrowseState, GameCreateCallEvent, GameFormingCallEvent, GameJoinCallEvent,
        GameMyGamesCallEvent,
    },
    AppState, Global,
};

use super::{MenuData, MenuUIState};

pub fn view(
    commands: &mut Commands,
    ui: &mut egui::Ui,
    global: &mut Global,
    data: &mut MenuData,
    create_ev_w: &mut EventWriter<GameCreateCallEvent>,
    forming_ev_w: &mut EventWriter<GameFormingCallEvent>,
    mygames_ev_w: &mut EventWriter<GameMyGamesCallEvent>,
    join_ev_w: &mut EventWriter<GameJoinCallEvent>,
) {
    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 => {
                for game in &data.forming_games {
                    game_row(ui, game, |ui| {
                        if game.host_id == global.user.as_ref().unwrap().id {
                            ui.add_enabled(false, egui::Button::new("Host"));
                        } else if ui.button("Join").clicked() {
                            join_ev_w.send(GameJoinCallEvent {
                                game_id: game.id.clone(),
                            });
                        }
                    });
                }
            }
            BrowseState::InProgress => {
                let mut games = data.my_games.clone();
                games.retain(|g| g.state == GameState::InProgress);
                for game in &games {
                    game_row(ui, game, |ui| {
                        if ui.button("Enter").clicked() {
                            enter_game(commands, global, &game.id);
                        }
                    });
                }
            }
            BrowseState::Finished => {
                let mut games = data.my_games.clone();
                games.retain(|g| g.state == GameState::Finished);
                for game in &games {
                    egui::Frame::none()
                        .fill(egui::Color32::BLACK)
                        .rounding(4.)
                        .outer_margin(4.)
                        .inner_margin(4.)
                        .show(ui, |ui| {
                            game_row(ui, game, |_| {});
                        });
                }
            }
        });
    });
}

fn game_row(ui: &mut egui::Ui, game: &Game, buttons: impl FnOnce(&mut egui::Ui)) {
    egui::Frame::none()
        .fill(egui::Color32::BLACK)
        .rounding(4.)
        .outer_margin(4.)
        .inner_margin(4.)
        .show(ui, |ui| {
            ui.horizontal(|ui| {
                ui.label(format!("Host: {}", game.host.as_ref().unwrap().username));
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    buttons(ui);

                    ui.label(format!(
                        "{}",
                        chrono_humanize::HumanTime::from(game.created_at)
                    ));
                });
            });
        });
}

fn enter_game(commands: &mut Commands, global: &mut Global, game_id: &str) {
    global.cur_game_id = game_id.to_string();

    commands.insert_resource(NextState(Some(MenuState::None)));
    commands.insert_resource(NextState(Some(AppState::InGame)));
}