Working on event system, join implemented
This commit is contained in:
@@ -67,22 +67,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Welcome! Your player ID: {}", player_id);
|
||||
}
|
||||
}
|
||||
"match_found" => {
|
||||
if let (Some(opponent), Some(color), Some(match_id)) =
|
||||
(parsed.opponent, parsed.color, parsed.match_id)
|
||||
{
|
||||
println!(
|
||||
"Match found! Opponent: {}, Color: {}, Match ID: {}",
|
||||
opponent, color, match_id
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
println!("cucc: {:?}", parsed);
|
||||
}
|
||||
"error" => {
|
||||
if let Some(reason) = parsed.reason {
|
||||
println!("Error: {}", reason);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use crate::events::ClientEvent::*;
|
||||
use crate::events::{ClientEvent, EventResponse};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
@@ -141,8 +143,35 @@ pub async fn handle_connection(
|
||||
let text = message.to_text()?;
|
||||
println!("Received from {}: {}", player_id, text);
|
||||
|
||||
// TODO: Parse and handle message with event system
|
||||
// This will be implemented when we integrate the event system
|
||||
let client_data: ClientEvent = serde_json::from_str(text)
|
||||
.expect("Failed to convert data into json at handle_connection");
|
||||
|
||||
println!("client: {:?}", client_data);
|
||||
|
||||
match client_data {
|
||||
Join { username } => {
|
||||
{
|
||||
let mut conn_map = connections.lock().await;
|
||||
let mut player = conn_map.get_mut(&player_id).unwrap();
|
||||
player.username = Some(username);
|
||||
}
|
||||
|
||||
//respone to client
|
||||
let response: EventResponse = EventResponse {
|
||||
response: core::result::Result::Ok(true),
|
||||
};
|
||||
|
||||
println!("response: {:?}", response);
|
||||
|
||||
send_message_to_player(
|
||||
&connections,
|
||||
player_id,
|
||||
&serde_json::to_string(&response).unwrap(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::mpsc;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::connection::ConnectionMap;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Step {
|
||||
pub from: String,
|
||||
@@ -23,7 +26,7 @@ pub enum ClientEvent {
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct EventResponse {
|
||||
response: Result<bool, String>,
|
||||
pub response: Result<bool, String>,
|
||||
}
|
||||
|
||||
pub struct EventSystem {
|
||||
@@ -52,9 +55,17 @@ impl EventSystem {
|
||||
pub async fn send_event(
|
||||
&self,
|
||||
player_id: Uuid,
|
||||
event: EventResponse,
|
||||
event: &EventResponse,
|
||||
connections: &ConnectionMap,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
self.sender.send((player_id, event))?;
|
||||
//self.sender.send((player_id, event))?;
|
||||
|
||||
crate::connection::send_message_to_player(
|
||||
&connections,
|
||||
player_id,
|
||||
&serde_json::to_string(&event).unwrap(),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user