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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use bevy::prelude::*;
use de_gui::{
    ButtonCommands, ButtonOps, GuiCommands, LabelCommands, OuterStyle, TextBoxCommands,
    TextBoxQuery, ToastEvent,
};
use de_lobby_model::{GameConfig, GameMap, Validatable};
use de_map::hash::MapHash;

use super::{setup::SetupGameEvent, MultiplayerState};
use crate::{
    mapselection::{MapSelectedEvent, SelectMapEvent},
    menu::Menu,
};

pub(super) struct CreateGamePlugin;

impl Plugin for CreateGamePlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<CreateGameEvent>()
            .add_systems(OnEnter(MultiplayerState::GameCreation), setup)
            .add_systems(OnExit(MultiplayerState::GameCreation), cleanup)
            .add_systems(
                Update,
                (
                    button_system.in_set(CreateSet::Buttons),
                    map_selected_system.in_set(CreateSet::MapSelected),
                    create_game_system
                        .run_if(on_event::<CreateGameEvent>())
                        .after(CreateSet::Buttons)
                        .after(CreateSet::MapSelected),
                )
                    .run_if(in_state(MultiplayerState::GameCreation)),
            );
    }
}

#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, SystemSet)]
enum CreateSet {
    Buttons,
    MapSelected,
}

#[derive(Component, Clone, Copy)]
enum ButtonAction {
    SelectMap,
    Create,
}

#[derive(Resource)]
struct Inputs {
    name: Entity,
    max_players: Entity,
    map: Entity,
}

#[derive(Resource)]
struct SelectedMap(GameMap);

#[derive(Event)]
struct CreateGameEvent;

fn setup(mut commands: GuiCommands, menu: Res<Menu>) {
    let column_id = column(&mut commands, menu.root_node());

    let name_row_id = row(&mut commands, column_id);

    let name_id = text_input(&mut commands, name_row_id, "Name");

    let max_players_row_id = row(&mut commands, column_id);
    let max_players_id = text_input(&mut commands, max_players_row_id, "Max Players");

    let map_row_id = row(&mut commands, column_id);
    let map_id = map_button(&mut commands, map_row_id);

    commands.insert_resource(Inputs {
        name: name_id,
        max_players: max_players_id,
        map: map_id,
    });

    let buttons_row_id = row(&mut commands, column_id);
    let create_id = commands
        .spawn_button(
            OuterStyle {
                width: Val::Percent(100.),
                height: Val::Percent(100.),
                ..default()
            },
            "Create Game",
        )
        .insert(ButtonAction::Create)
        .id();
    commands.entity(buttons_row_id).add_child(create_id);
}

fn column(commands: &mut GuiCommands, parent_id: Entity) -> Entity {
    let column_id = commands
        .spawn(NodeBundle {
            style: Style {
                flex_direction: FlexDirection::Column,
                width: Val::Percent(50.),
                height: Val::Percent(100.),
                margin: UiRect::all(Val::Auto),
                align_items: AlignItems::Center,
                justify_content: JustifyContent::Center,
                ..default()
            },
            ..default()
        })
        .id();
    commands.entity(parent_id).add_child(column_id);
    column_id
}

fn row(commands: &mut GuiCommands, parent_id: Entity) -> Entity {
    let row_id = commands
        .spawn(NodeBundle {
            style: Style {
                flex_direction: FlexDirection::Row,
                width: Val::Percent(100.),
                height: Val::Percent(8.),
                margin: UiRect::new(
                    Val::Percent(0.),
                    Val::Percent(0.),
                    Val::Percent(2.),
                    Val::Percent(2.),
                ),
                justify_content: JustifyContent::SpaceBetween,
                align_items: AlignItems::Center,
                ..default()
            },
            ..default()
        })
        .id();
    commands.entity(parent_id).add_child(row_id);
    row_id
}

fn text_input(commands: &mut GuiCommands, parent_id: Entity, caption: &str) -> Entity {
    spawn_caption(commands, parent_id, caption);

    let input_id = commands
        .spawn_text_box(
            OuterStyle {
                width: Val::Percent(65.),
                height: Val::Percent(100.),
                ..default()
            },
            false,
        )
        .id();
    commands.entity(parent_id).add_child(input_id);
    input_id
}

fn map_button(commands: &mut GuiCommands, parent_id: Entity) -> Entity {
    spawn_caption(commands, parent_id, "Map");

    let input_id = commands
        .spawn_button(
            OuterStyle {
                width: Val::Percent(65.),
                height: Val::Percent(100.),
                ..default()
            },
            "-",
        )
        .insert(ButtonAction::SelectMap)
        .id();
    commands.entity(parent_id).add_child(input_id);
    input_id
}

fn spawn_caption(commands: &mut GuiCommands, parent_id: Entity, caption: &str) {
    let caption_id = commands
        .spawn_label(
            OuterStyle {
                width: Val::Percent(35.),
                height: Val::Percent(100.),
                ..default()
            },
            caption,
        )
        .id();
    commands.entity(parent_id).add_child(caption_id);
}

fn cleanup(mut commands: GuiCommands) {
    commands.remove_resource::<Inputs>();
    commands.remove_resource::<SelectedMap>();
}

fn button_system(
    interactions: Query<(&Interaction, &ButtonAction), Changed<Interaction>>,
    mut map_events: EventWriter<SelectMapEvent>,
    mut create_events: EventWriter<CreateGameEvent>,
) {
    for (&interaction, &action) in interactions.iter() {
        if let Interaction::Pressed = interaction {
            match action {
                ButtonAction::SelectMap => {
                    map_events.send(SelectMapEvent);
                }
                ButtonAction::Create => {
                    create_events.send(CreateGameEvent);
                }
            }
        }
    }
}

fn map_selected_system(
    mut commands: Commands,
    mut map_selected_events: EventReader<MapSelectedEvent>,
    intpus: Res<Inputs>,
    mut buttons: ButtonOps,
    mut toasts: EventWriter<ToastEvent>,
) {
    let Some(event) = map_selected_events.read().last() else {
        return;
    };
    let hash = match MapHash::try_from(event.path()) {
        Ok(hash) => hash,
        Err(error) => {
            toasts.send(ToastEvent::new(format!("Map error: {error}")));
            return;
        }
    };

    buttons
        .set_text(intpus.map, event.metadata().name().to_owned())
        .unwrap();
    commands.insert_resource(SelectedMap(GameMap::new(
        hash.to_hex(),
        event.metadata().name().to_owned(),
    )));
}

fn create_game_system(
    inputs: Res<Inputs>,
    texts: TextBoxQuery,
    selected_map: Option<Res<SelectedMap>>,
    mut toasts: EventWriter<ToastEvent>,
    mut setup_events: EventWriter<SetupGameEvent>,
) {
    let Some(selected_map) = selected_map else {
        toasts.send(ToastEvent::new("No map selected."));
        return;
    };

    let name = texts.text(inputs.name).unwrap().to_string();
    let max_players: u8 = match texts.text(inputs.max_players).unwrap().parse() {
        Ok(value) => value,
        Err(error) => {
            toasts.send(ToastEvent::new(format!("Invalid max players: {error}")));
            return;
        }
    };

    let game_config = GameConfig::new(name, max_players, selected_map.0.clone());
    if let Err(error) = game_config.validate() {
        toasts.send(ToastEvent::new(format!("{error}")));
        return;
    }
    setup_events.send(SetupGameEvent::new(game_config));
}