added resign event and deleting finished matches
This commit is contained in:
1080
server/Cargo.lock
generated
Normal file
1080
server/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,11 @@
|
||||
use crate::connection::ClientEvent::*;
|
||||
use crate::matchmaking;
|
||||
use engine::chessmove::ChessMove;
|
||||
use engine::gameend::GameEnd::{self, *};
|
||||
use engine::{get_available_moves, is_game_over};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::char::from_u32_unchecked;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::sync::Arc;
|
||||
use tokio::net::TcpStream;
|
||||
@@ -17,6 +19,10 @@ pub type ConnectionMap = Arc<Mutex<HashMap<Uuid, PlayerConnection>>>;
|
||||
pub type MatchMap = Arc<Mutex<HashMap<Uuid, GameMatch>>>;
|
||||
pub type WaitingQueue = Arc<Mutex<VecDeque<Uuid>>>;
|
||||
|
||||
pub async fn clean_up_match(matches: &MatchMap, match_id: &Uuid) {
|
||||
matches.lock().await.remove(&match_id);
|
||||
}
|
||||
|
||||
// Helper functions to create new instances
|
||||
pub fn new_connection_map() -> ConnectionMap {
|
||||
Arc::new(Mutex::new(HashMap::new()))
|
||||
@@ -61,6 +67,9 @@ pub enum ServerMessage2 {
|
||||
color: String,
|
||||
opponent_name: String,
|
||||
},
|
||||
Ok {
|
||||
response: Result<(), String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -74,11 +83,6 @@ enum ClientEvent {
|
||||
RequestLegalMoves { fen: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct EventResponse {
|
||||
pub response: Result<(), String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PlayerConnection {
|
||||
pub id: Uuid,
|
||||
@@ -191,8 +195,6 @@ pub async fn handle_connection(
|
||||
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 } => {
|
||||
{
|
||||
@@ -202,11 +204,7 @@ pub async fn handle_connection(
|
||||
}
|
||||
|
||||
//respone to client
|
||||
let response: EventResponse = EventResponse {
|
||||
response: core::result::Result::Ok(()),
|
||||
};
|
||||
|
||||
println!("response: {:?}", response);
|
||||
let response = ServerMessage2::Ok { response: Ok(()) };
|
||||
|
||||
let mut conn_map = connections.lock().await;
|
||||
let _ = send_message_to_player_connection(
|
||||
@@ -257,9 +255,11 @@ pub async fn handle_connection(
|
||||
.await;
|
||||
|
||||
{
|
||||
match engine::is_game_over(
|
||||
let is_game_end = engine::is_game_over(
|
||||
&matches.lock().await.get(&match_id).unwrap().board_state,
|
||||
) {
|
||||
);
|
||||
|
||||
match is_game_end {
|
||||
Some(res) => {
|
||||
let message = ServerMessage2::GameEnd { winner: res };
|
||||
let _ = broadcast_to_match(
|
||||
@@ -269,6 +269,7 @@ pub async fn handle_connection(
|
||||
&serde_json::to_string(&message).unwrap(),
|
||||
)
|
||||
.await;
|
||||
clean_up_match(&matches, &match_id);
|
||||
}
|
||||
None => {
|
||||
println!("No winner match continues.")
|
||||
@@ -286,8 +287,64 @@ pub async fn handle_connection(
|
||||
println!("Sent moves to player: {}", player_id);
|
||||
}
|
||||
Resign => {
|
||||
// TODO: set game over and turn on game end ui, then delete the match
|
||||
println!("Resigned!");
|
||||
let (fuck, fuck_id): (ServerMessage2, &Uuid) = {
|
||||
let matches = matches.lock().await;
|
||||
|
||||
let curr_match = matches
|
||||
.get(
|
||||
&connections
|
||||
.lock()
|
||||
.await
|
||||
.get(&player_id)
|
||||
.unwrap()
|
||||
.current_match
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
if player_id == curr_match.player_white {
|
||||
(
|
||||
ServerMessage2::GameEnd {
|
||||
winner: GameEnd::BlackWon("Resigned".to_string()),
|
||||
},
|
||||
&connections
|
||||
.lock()
|
||||
.await
|
||||
.get(&player_id)
|
||||
.unwrap()
|
||||
.current_match
|
||||
.unwrap(),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
ServerMessage2::GameEnd {
|
||||
winner: GameEnd::WhiteWon("Resigned".to_string()),
|
||||
},
|
||||
&connections
|
||||
.lock()
|
||||
.await
|
||||
.get(&player_id)
|
||||
.unwrap()
|
||||
.current_match
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
broadcast_to_match(
|
||||
&connections,
|
||||
&matches,
|
||||
connections
|
||||
.lock()
|
||||
.await
|
||||
.get(&player_id)
|
||||
.unwrap()
|
||||
.current_match
|
||||
.unwrap(),
|
||||
&serde_json::to_string(&fuck).unwrap(),
|
||||
)
|
||||
.await;
|
||||
clean_up_match(&matches, fuck_id);
|
||||
}
|
||||
_ => {
|
||||
println!("Not known client event");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::connection::ServerMessage2;
|
||||
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue, broadcast_to_match};
|
||||
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue};
|
||||
use rand::random;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -25,6 +25,10 @@ impl MatchmakingSystem {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn clean_up(&self, match_id: Uuid) {
|
||||
self.matches.lock().await.remove(&match_id);
|
||||
}
|
||||
|
||||
async fn try_create_match(&self) {
|
||||
let mut queue = self.waiting_queue.lock().await;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user