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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#[allow(unused_imports)]
use std::ops::Deref;

#[cfg(not(target_arch = "wasm32"))]
use bones_framework::networking::{socket::Socket, NetworkSocket, SocketTarget, SyncingInfo};

use crate::prelude::*;

use super::player_image::player_image;

pub fn session_plugin(session: &mut Session) {
    session.add_system_to_stage(Update, scoring_menu_system);
}

pub fn game_plugin(game: &mut Game) {
    game.insert_shared_resource(ScoringMenuState::default());
}

#[derive(HasSchema, Debug, Default, Clone)]
pub struct ScoringMenuState {
    pub active: bool,
    pub ready_players: HashSet<PlayerIdx>,
    pub match_score: MatchScore,
    pub next_maps: Option<MapPool>,
}

impl ScoringMenuState {
    /// Reset menu state
    pub fn reset(&mut self) {
        *self = default();
    }
}

struct PlayerScoreInfo {
    pub entity: Entity,
    pub player_idx: PlayerIdx,
    pub score: u32,
}

const SCORING_MESSAGE_MAGIC: u8 = 183;

#[derive(Serialize, Deserialize)]
struct ScoringMessage {
    pub data: ScoringMessageEnum,
    pub magic: u8,
}

#[derive(Serialize, Deserialize)]
enum ScoringMessageEnum {
    PlayerReady(u32),
}

impl From<ScoringMessageEnum> for ScoringMessage {
    fn from(value: ScoringMessageEnum) -> Self {
        Self {
            data: value,
            magic: SCORING_MESSAGE_MAGIC,
        }
    }
}

/// The width of each player panel
const PLAYER_PANEL_WIDTH: f32 = 200.0;

/// If Player won the match or is tied for win
enum PlayerWon {
    Tied,
    SoleWinner,
}

fn scoring_menu_system(
    meta: Root<GameMeta>,
    mut sessions: ResMut<Sessions>,
    ctx: Res<EguiCtx>,
    mut state: ResMut<ScoringMenuState>,
    controls: Res<GlobalPlayerControls>,
    world: &World,
) {
    if !state.active {
        // Make sure state is cleared if menu is closed
        state.reset();
        return;
    }

    let mut continue_game = false;
    let mut game_won = false;
    if let Some(session) = sessions.get_mut(SessionNames::GAME) {
        let player_indices = session.world.components.get::<PlayerIdx>();
        let player_indices_ref = player_indices.borrow();
        let game_entities = session.world.get_resource::<Entities>().unwrap();
        let match_inputs = session.world.get_resource::<MatchInputs>().unwrap();

        #[cfg(not(target_arch = "wasm32"))]
        let network_socket: Option<Socket> = session
            .world
            .get_resource::<SyncingInfo>()
            .and_then(|x| x.socket().cloned());

        // Build Vec<PlayerScoreInfo> sorted by player indices
        let mut player_entities: Vec<(Entity, &PlayerIdx)> =
            game_entities.iter_with(&player_indices_ref).collect();
        player_entities.sort_by_key(|x| x.1 .0);
        let player_score_info: Vec<PlayerScoreInfo> = player_entities
            .iter()
            .map(|x| PlayerScoreInfo {
                entity: x.0,
                player_idx: *x.1,
                score: state.match_score.score(*x.1),
            })
            .collect();

        // Contains any players who have broken win threshold. If multiple players have winning scores,
        // contains player with highest score. If players are tied, tied players are all included.
        let mut winning_players = Vec::<PlayerIdx>::default();

        let win_threshold = meta.core.config.winning_score_threshold;
        let mut highest_score = 0;

        for score_info in player_score_info.iter() {
            let score = score_info.score;
            if score >= win_threshold && score >= highest_score {
                if score > highest_score {
                    // New winning player, clear prev winners
                    winning_players.clear();
                    highest_score = score;
                }

                winning_players.push(score_info.player_idx);
            }
        }
        if winning_players.len() == 1 {
            game_won = true;
        }

        // Check for inputs from local players toggling ready state
        for (_, player_idx) in player_entities.iter() {
            if let Some(source) = match_inputs.get_control_source(player_idx.0 as usize) {
                if let Some(control) = controls.get(&source) {
                    if control.menu_confirm_just_pressed
                        && !state.ready_players.contains(*player_idx)
                    {
                        #[cfg(not(target_arch = "wasm32"))]
                        {
                            if let Some(socket) = network_socket.as_ref() {
                                socket.send_reliable(
                                    SocketTarget::All,
                                    &postcard::to_allocvec(&ScoringMessage::from(
                                        ScoringMessageEnum::PlayerReady(player_idx.0),
                                    ))
                                    .unwrap(),
                                );

                                debug!("Send message local player {} ready", player_idx.0);
                            }
                        }

                        state.ready_players.insert(**player_idx);
                    }
                }
            }
        }

        #[cfg(not(target_arch = "wasm32"))]
        if let Some(socket) = network_socket.as_ref() {
            handle_scoring_messages(socket, &mut state);
        }

        // Check if all non-ai players are ready
        let mut all_players_ready = true;
        for (_, player_idx) in player_entities.iter() {
            let is_ai = match_inputs
                .players
                .get(player_idx.0 as usize)
                .unwrap()
                .is_ai;
            if !is_ai && !state.ready_players.contains(*player_idx) {
                all_players_ready = false;
            }
        }
        if all_players_ready {
            continue_game = true;
        }

        egui::CentralPanel::default()
            .frame(egui::Frame::none())
            .show(&ctx, |ui| {
                let screen_rect = ui.max_rect();

                // Determine x margin based on size of player panels, some extra spacing, and screen space available.
                // This way window does not cover whole screen if < 4 players, and has consistent layout.
                let player_panel_spacing = 20.0;
                let max_width =
                    (PLAYER_PANEL_WIDTH + player_panel_spacing) * player_score_info.len() as f32;
                let x_margin = (screen_rect.width() - max_width) / 2.0;

                // x margin is dynamic, y margin percentage of screen
                let outer_margin =
                    egui::style::Margin::symmetric(x_margin, screen_rect.height() * 0.2);

                ui.vertical_centered(|ui| {
                    BorderedFrame::new(&meta.theme.panel.border)
                        .margin(outer_margin)
                        .padding(meta.theme.panel.padding)
                        .show(ui, |ui| {
                            world.run_system(
                                scoring_menu,
                                (
                                    ui,
                                    &player_score_info,
                                    &*match_inputs,
                                    &state,
                                    &winning_players,
                                ),
                            );
                        });
                });
            });
    } else {
        error!("Scoring menu failed to load score from existing game session. Closing scoring UI.");
        state.reset();
    }

    if continue_game {
        state.reset();
        let next_maps = state.next_maps.clone();
        let reset_score = game_won;
        sessions.add_command(Box::new(move |sessions: &mut Sessions| {
            sessions.restart_game(next_maps, reset_score);
        }));
    }
}

fn scoring_menu(
    mut param: In<(
        &mut egui::Ui,
        &Vec<PlayerScoreInfo>,
        &MatchInputs,
        &ScoringMenuState,
        &Vec<PlayerIdx>,
    )>,
    meta: Root<GameMeta>,
    localization: Localization<GameMeta>,
    world: &World,
) {
    let (ui, player_score_info, match_inputs, menu_state, winning_players) = &mut *param;

    // Scoring heading label
    ui.vertical_centered(|ui| {
        let (text, color) = match winning_players.len() {
            0 => ("intermission", meta.theme.panel.font_color),
            1 => ("match-complete", meta.theme.colors.positive),
            _ => ("tied-for-win", meta.theme.panel.font_color),
        };

        ui.label(
            meta.theme
                .font_styles
                .heading
                .rich(localization.get(text))
                .color(color),
        );
    });

    ui.vertical_centered(|ui| {
        ui.horizontal_centered(|ui| {
            let player_count = player_score_info.len();
            let available_spacing = ui.available_width() - PLAYER_PANEL_WIDTH * player_count as f32;

            // Compute how much space to use in gaps between panels
            let spacing = available_spacing / (player_count + 1) as f32;
            ui.add_space(spacing);
            for (i, _) in player_score_info.iter().enumerate() {
                let player_input = match_inputs.players.get(i).unwrap();
                let player_score_info = player_score_info.get(i).unwrap();
                let player_idx = player_score_info.player_idx;

                let ready = menu_state.ready_players.contains(&player_idx);
                let player_won =
                    if winning_players.contains(&player_idx) && winning_players.len() == 1 {
                        Some(PlayerWon::SoleWinner)
                    } else if winning_players.contains(&player_idx) {
                        Some(PlayerWon::Tied)
                    } else {
                        None
                    };

                world.run_system(
                    player_score_panel,
                    (ui, player_input, player_score_info, ready, player_won),
                );
                ui.add_space(spacing);
            }
        });
    });

    let match_complete = winning_players.len() == 1;
    ui.horizontal(|ui| {
        if match_complete {
            ui.label(
                meta.theme
                    .font_styles
                    .normal
                    .rich(localization.get("press-confirm-to-play-again"))
                    .color(meta.theme.panel.font_color),
            );
        } else {
            ui.label(
                meta.theme
                    .font_styles
                    .normal
                    .rich(localization.get("press-confirm-to-ready-up"))
                    .color(meta.theme.panel.font_color),
            );
        }
    });
}

fn player_score_panel(
    mut params: In<(
        &mut egui::Ui,
        &PlayerInput,
        &PlayerScoreInfo,
        bool,
        Option<PlayerWon>,
    )>,
    meta: Root<GameMeta>,
    assets: Res<AssetServer>,
    localization: Localization<GameMeta>,
    world: &World,
) {
    let (ui, player_input, player_score_info, ready, won) = &mut *params;
    let panel = &meta.theme.panel;

    BorderedFrame::new(&panel.border)
        .padding(panel.padding)
        .show(ui, |ui| {
            // PLAYER_PANEL_WIDTH is total space for entire bordered frame, remove space lost to padding
            // and use this for inner contents.
            ui.set_width(PLAYER_PANEL_WIDTH - panel.padding.left - panel.padding.right);

            ui.vertical_centered(|ui| {
                ui.label(
                    meta.theme
                        .font_styles
                        .bigger
                        .rich(format!(
                            "{} {}",
                            localization.get("player"),
                            player_score_info.player_idx.0
                        ))
                        .color(meta.theme.panel.font_color),
                );

                match won {
                    Some(won) => {
                        let text = match won {
                            PlayerWon::Tied => localization.get("tied"),
                            PlayerWon::SoleWinner => localization.get("won"),
                        };

                        ui.label(
                            meta.theme
                                .font_styles
                                .bigger
                                .rich(text)
                                .color(meta.theme.colors.positive),
                        );
                    }
                    None => {
                        ui.add_space(meta.theme.font_styles.bigger.size);
                    }
                }

                let player_meta = assets.get(player_input.selected_player);
                let hat_meta = player_input.selected_hat.map(|x| assets.get(x));
                world.run_system(player_image, (ui, &player_meta, hat_meta.as_deref()));

                ui.label(
                    meta.theme
                        .font_styles
                        .bigger
                        .rich(format!(
                            "{}: {}",
                            localization.get("score"),
                            player_score_info.score,
                        ))
                        .color(meta.theme.panel.font_color),
                );

                if !player_input.is_ai {
                    let (ready_str, color) = match *ready {
                        true => ("ready", meta.theme.colors.positive),
                        false => ("not-ready", meta.theme.colors.negative),
                    };
                    let ready_localized = localization.get(ready_str);
                    ui.label(
                        meta.theme
                            .font_styles
                            .normal
                            .rich(ready_localized)
                            .color(color),
                    );
                } else {
                    ui.label(
                        meta.theme
                            .font_styles
                            .normal
                            .rich(localization.get("ai"))
                            .color(meta.theme.panel.font_color),
                    );
                }
            });
        });
}

#[cfg(not(target_arch = "wasm32"))]
fn handle_scoring_messages(
    network_socket: &(impl NetworkSocket + ?Sized),
    state: &mut ScoringMenuState,
) {
    // TODO handle disconnects
    let datas: Vec<(u32, Vec<u8>)> = network_socket.recv_reliable();
    let local_player_idx = network_socket.player_idx();
    for (_, data) in datas {
        match postcard::from_bytes::<ScoringMessage>(&data) {
            Ok(message) => match message.data {
                ScoringMessageEnum::PlayerReady(player) => {
                    if message.magic == SCORING_MESSAGE_MAGIC && player != local_player_idx {
                        state.ready_players.insert(PlayerIdx(player));
                        debug!("Received message player {} ready", player);
                    }
                }
            },
            Err(e) => warn!("Ignoring network message that was not understood: {e}"),
        }
    }
}