mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: implement sending and revoking readiness from gui
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::sync::mpsc::{self, Sender};
|
||||||
|
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use macroquad::{
|
use macroquad::{
|
||||||
color::GRAY,
|
color::GRAY,
|
||||||
@@ -18,7 +20,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct GameClient {
|
pub struct GameClient {
|
||||||
client: Client,
|
cmd_sender: Sender<GuiCommands>,
|
||||||
update_receiver: Receiver<ServerMessage>,
|
update_receiver: Receiver<ServerMessage>,
|
||||||
|
|
||||||
identity: Option<NetworkIdentity>,
|
identity: Option<NetworkIdentity>,
|
||||||
@@ -44,12 +46,55 @@ struct Lobby {
|
|||||||
checkbox_size: Vec2,
|
checkbox_size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum GuiCommands {
|
||||||
|
Send(String),
|
||||||
|
|
||||||
|
Ready,
|
||||||
|
Unready,
|
||||||
|
|
||||||
|
Capture,
|
||||||
|
Move,
|
||||||
|
}
|
||||||
|
|
||||||
impl GameClient {
|
impl GameClient {
|
||||||
pub fn new(mut client: Client, context: &GameContext) -> Option<Self> {
|
pub fn new(mut client: Client, context: &GameContext) -> Option<Self> {
|
||||||
let Some(update_receiver) = client.get_update_receiver() else {
|
let Some(update_receiver) = client.get_update_receiver() else {
|
||||||
return None;
|
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 board = BoardView::default();
|
||||||
|
|
||||||
let game_stage = ServerStage::default();
|
let game_stage = ServerStage::default();
|
||||||
@@ -72,7 +117,7 @@ impl GameClient {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
client,
|
cmd_sender,
|
||||||
update_receiver,
|
update_receiver,
|
||||||
identity: None,
|
identity: None,
|
||||||
|
|
||||||
@@ -122,11 +167,20 @@ impl GameClient {
|
|||||||
GRAY,
|
GRAY,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let before_click: bool = self.lobby.is_ready;
|
||||||
Checkbox::new(hash!())
|
Checkbox::new(hash!())
|
||||||
.pos(self.lobby.checkbox_pos)
|
.pos(self.lobby.checkbox_pos)
|
||||||
.label("Ready")
|
.label("Ready")
|
||||||
.size(self.lobby.checkbox_size)
|
.size(self.lobby.checkbox_size)
|
||||||
.ui(&mut root_ui(), &mut self.lobby.is_ready);
|
.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) {
|
fn draw_game(&self) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use std::sync::mpsc::{self, Receiver};
|
use std::sync::mpsc::{self, Receiver, Sender};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
network::client::Client, super_advanced_ai::BotDificulty, ui::macroquad::game::GameClient,
|
network::client::Client, super_advanced_ai::BotDificulty, ui::macroquad::game::GameClient,
|
||||||
@@ -36,6 +36,16 @@ pub struct GameContext {
|
|||||||
pub server_url_buffer: String,
|
pub server_url_buffer: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum CliCommands {
|
||||||
|
Send,
|
||||||
|
|
||||||
|
Ready,
|
||||||
|
Unready,
|
||||||
|
|
||||||
|
Capture,
|
||||||
|
Move,
|
||||||
|
}
|
||||||
|
|
||||||
// TODO! Move this
|
// TODO! Move this
|
||||||
pub fn connect_to_server() -> Receiver<Option<Client>> {
|
pub fn connect_to_server() -> Receiver<Option<Client>> {
|
||||||
let (tx, rc) = mpsc::channel::<Option<Client>>();
|
let (tx, rc) = mpsc::channel::<Option<Client>>();
|
||||||
|
|||||||
Reference in New Issue
Block a user