feat: implement sending and revoking readiness from gui

This commit is contained in:
2026-08-22 15:27:11 +02:00
parent 6720e4c9bd
commit dd18a263d1
2 changed files with 67 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
use std::sync::mpsc::{self, Sender};
use log::{error, info};
use macroquad::{
color::GRAY,
@@ -18,7 +20,7 @@ use crate::{
};
pub struct GameClient {
client: Client,
cmd_sender: Sender<GuiCommands>,
update_receiver: Receiver<ServerMessage>,
identity: Option<NetworkIdentity>,
@@ -44,12 +46,55 @@ struct Lobby {
checkbox_size: Vec2,
}
enum GuiCommands {
Send(String),
Ready,
Unready,
Capture,
Move,
}
impl GameClient {
pub fn new(mut client: Client, context: &GameContext) -> Option<Self> {
let Some(update_receiver) = client.get_update_receiver() else {
return None;
};
let (cmd_sender, cmd_recv) = mpsc::channel::<GuiCommands>();
std::thread::spawn(|| {
let rt = tokio::runtime::Runtime::new().unwrap(); // i dont like this
rt.spawn(async move {
loop {
match cmd_recv.recv() {
Ok(cmd) => match cmd {
GuiCommands::Send(content) => {
client.send_text_message(content).await;
}
GuiCommands::Ready => {
client.signal_readiness().await;
}
GuiCommands::Unready => {
client.revoke_readiness().await;
}
GuiCommands::Capture => todo!(),
GuiCommands::Move => todo!(),
},
Err(e) => {
error!("Connection broken: {}", e);
return;
}
}
}
});
});
let board = BoardView::default();
let game_stage = ServerStage::default();
@@ -72,7 +117,7 @@ impl GameClient {
};
Some(Self {
client,
cmd_sender,
update_receiver,
identity: None,
@@ -122,11 +167,20 @@ impl GameClient {
GRAY,
);
let before_click: bool = self.lobby.is_ready;
Checkbox::new(hash!())
.pos(self.lobby.checkbox_pos)
.label("Ready")
.size(self.lobby.checkbox_size)
.ui(&mut root_ui(), &mut self.lobby.is_ready);
if before_click != self.lobby.is_ready {
if self.lobby.is_ready {
let _ = self.cmd_sender.send(GuiCommands::Ready);
} else {
let _ = self.cmd_sender.send(GuiCommands::Unready);
}
}
}
fn draw_game(&self) {

View File

@@ -1,4 +1,4 @@
use std::sync::mpsc::{self, Receiver};
use std::sync::mpsc::{self, Receiver, Sender};
use crate::{
network::client::Client, super_advanced_ai::BotDificulty, ui::macroquad::game::GameClient,
@@ -36,6 +36,16 @@ pub struct GameContext {
pub server_url_buffer: String,
}
enum CliCommands {
Send,
Ready,
Unready,
Capture,
Move,
}
// TODO! Move this
pub fn connect_to_server() -> Receiver<Option<Client>> {
let (tx, rc) = mpsc::channel::<Option<Client>>();