handle ending games

This commit is contained in:
2025-06-13 19:02:30 -06:00
parent d129911b8e
commit d08c0c0f5c
9 changed files with 89 additions and 42 deletions
+26 -20
View File
@@ -6,7 +6,10 @@ use macroquad::{
};
use serde::{Deserialize, Serialize};
use crate::{font::FontSize, model::player::Player, screen::Screen, ui_scaling::window_width};
use crate::{
font::FontSize, model::player::Player, screen::Screen, ui_scaling::window_width,
util::refresh_page,
};
#[allow(dead_code)]
#[derive(Serialize)]
@@ -15,6 +18,7 @@ enum HostClientMessages {
ResetAllBuzers,
ResetPlayerBuzzer { user_id: u64 },
UpdateScore { user_id: u64, diff: i32 },
EndGame,
}
#[derive(Deserialize)]
@@ -22,6 +26,7 @@ enum HostClientMessages {
enum HostServerMessages {
Ack,
UpdatePlayerStates { players: HashMap<String, Player> },
ExitGame,
}
#[derive(Default)]
@@ -125,27 +130,24 @@ impl Screen for HostScreen {
for user_id in &players {
self.draw_user(ctx, user_id, ui, width);
}
ui.group(
hash!("Host Control"),
Vec2::new(width, window_size.y * 0.25),
|ui| {
if ui.button(Vec2::new(width * 0.05, 0.0), "Clear All") {
let msg = HostClientMessages::ResetAllBuzers;
ctx.send_msg(&msg);
}
if ui.button(Vec2::new(width * 0.55, 0.0), "End Game") {
let msg = HostClientMessages::EndGame;
ctx.send_msg(&msg);
}
},
);
});
let button_size = screen_width() * 0.025;
let button_location = Vec2::new(screen_width() * 0.50, screen_height() * 0.90);
draw_circle(
button_location.x,
button_location.y,
button_size * 1.1,
DARKBROWN,
);
draw_circle(button_location.x, button_location.y, button_size, RED);
if is_mouse_button_pressed(MouseButton::Left) {
let loc = Vec2::from(mouse_position());
let normalize = loc - button_location;
if normalize.length() < button_size {
ctx.send_msg(&HostClientMessages::ResetAllBuzers);
}
}
root_ui().pop_skin();
}
@@ -162,6 +164,10 @@ impl Screen for HostScreen {
info!("Got new player state: {:?}", players);
self.players = players;
}
HostServerMessages::ExitGame => {
info!("Exiting Game");
refresh_page();
}
};
}
+3 -19
View File
@@ -10,6 +10,8 @@ use macroquad::{
use player_screen::PlayerScreen;
use screen::Screen;
use crate::util::get_ws_url;
mod cfg;
mod context;
mod font;
@@ -19,6 +21,7 @@ mod model;
mod player_screen;
mod screen;
mod ui_scaling;
mod util;
pub enum State {
Init,
@@ -98,25 +101,6 @@ pub fn default_skin() -> Skin {
}
}
fn get_ws_url() -> String {
let location = web_sys::window()
.unwrap()
.document()
.unwrap()
.location()
.unwrap();
let protocol = if location.protocol().unwrap() == "https" {
"wss"
} else {
"ws"
};
format!("{}://{}/ws", protocol, location.host().unwrap())
}
mod modname {}
#[macroquad::main("Play of the Game")]
async fn main() {
let cfg = Cfg::load_from_cookies().unwrap();
+6
View File
@@ -5,6 +5,7 @@ use crate::StateTransition;
use crate::context::Context;
use crate::font::FontSize;
use crate::screen::Screen;
use crate::util::refresh_page;
use macroquad::prelude::*;
#[derive(Serialize, Debug)]
@@ -19,6 +20,7 @@ pub enum PlayerServerMessages {
Ack,
UpdatePointTotal { score: i32 },
ResetBuzzer,
ExitGame,
}
#[derive(Default)]
@@ -45,6 +47,10 @@ impl Screen for PlayerScreen {
info!("Score updated to: {}", score);
self.score = score;
}
PlayerServerMessages::ExitGame => {
info!("Exiting Game");
refresh_page();
}
}
}
+27
View File
@@ -0,0 +1,27 @@
pub fn refresh_page() {
let location = web_sys::window()
.unwrap()
.document()
.unwrap()
.location()
.unwrap();
location.reload().unwrap();
}
pub fn get_ws_url() -> String {
let location = web_sys::window()
.unwrap()
.document()
.unwrap()
.location()
.unwrap();
let protocol = if location.protocol().unwrap() == "https" {
"wss"
} else {
"ws"
};
format!("{}://{}/ws", protocol, location.host().unwrap())
}