Impl "too_many_players" response from back-end

Previous implementation did not respond with the proper JSON response if
there were too many players.
This commit is contained in:
Stephen Seo 2022-04-08 11:01:33 +09:00
parent bc3de10d4e
commit c9ba80e6a4
3 changed files with 18 additions and 11 deletions

View file

@ -19,7 +19,9 @@ use std::{fmt, thread};
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use rusqlite::{params, Connection, Error as RusqliteError}; use rusqlite::{params, Connection, Error as RusqliteError};
pub type GetIDSenderType = (u32, Option<bool>); /// first value is ID, None if too many players
/// second value is true if player is cyan_player, None if not paired yet
pub type GetIDSenderType = (Option<u32>, Option<bool>);
/// first bool is player exists, /// first bool is player exists,
/// second bool is if paired, /// second bool is if paired,
/// third bool is if cyan player /// third bool is if cyan player
@ -172,6 +174,7 @@ impl DBHandler {
let create_player_result = self.create_new_player(Some(&conn)); let create_player_result = self.create_new_player(Some(&conn));
if let Err(e) = create_player_result { if let Err(e) = create_player_result {
println!("{}", e); println!("{}", e);
player_tx.send((None, None)).ok();
// don't stop server because player limit may have been reached // don't stop server because player limit may have been reached
return false; return false;
} }
@ -193,11 +196,11 @@ impl DBHandler {
if paired { if paired {
// don't stop server on send fail, may have timed // don't stop server on send fail, may have timed
// out and dropped the receiver // out and dropped the receiver
player_tx.send((player_id, Some(is_cyan))).ok(); player_tx.send((Some(player_id), Some(is_cyan))).ok();
} else { } else {
// don't stop server on send fail, may have timed // don't stop server on send fail, may have timed
// out and dropped the receiver // out and dropped the receiver
player_tx.send((player_id, None)).ok(); player_tx.send((Some(player_id), None)).ok();
} }
} else { } else {
println!("Internal error, created player doesn't exist"); println!("Internal error, created player doesn't exist");

View file

@ -41,7 +41,11 @@ fn handle_pairing_request(tx: SyncSender<DBHandlerRequest>) -> Result<String, St
if tx.send(DBHandlerRequest::GetID(player_tx)).is_err() { if tx.send(DBHandlerRequest::GetID(player_tx)).is_err() {
return Err("{\"type\":\"pairing_response\", \"status\":\"internal_error\"}".into()); return Err("{\"type\":\"pairing_response\", \"status\":\"internal_error\"}".into());
} }
if let Ok((pid, is_cyan_opt)) = player_rx.recv_timeout(DB_REQUEST_TIMEOUT) { if let Ok((pid_opt, is_cyan_opt)) = player_rx.recv_timeout(DB_REQUEST_TIMEOUT) {
if pid_opt.is_none() {
return Ok("{\"type\":\"pairing_response\", \"status\":\"too_many_players\"}".into());
}
let pid = pid_opt.unwrap();
if let Some(is_cyan) = is_cyan_opt { if let Some(is_cyan) = is_cyan_opt {
Ok(format!( Ok(format!(
"{{\"type\":\"pairing_response\", \"id\": {}, \"status\": \"paired\", \"color\": \"{}\"}}", "{{\"type\":\"pairing_response\", \"id\": {}, \"status\": \"paired\", \"color\": \"{}\"}}",

View file

@ -35,9 +35,9 @@ impl GameState {
matches!( matches!(
self, self,
GameState::NetworkedMultiplayer { GameState::NetworkedMultiplayer {
paired, paired: _,
current_side, current_side: _,
current_turn current_turn: _
} }
) )
} }
@ -55,9 +55,9 @@ impl GameState {
pub fn get_networked_current_side(&self) -> Option<Turn> { pub fn get_networked_current_side(&self) -> Option<Turn> {
if let GameState::NetworkedMultiplayer { if let GameState::NetworkedMultiplayer {
paired, paired: _,
current_side, current_side,
current_turn, current_turn: _,
} = *self } = *self
{ {
current_side current_side
@ -68,9 +68,9 @@ impl GameState {
pub fn set_networked_current_side(&mut self, side: Option<Turn>) { pub fn set_networked_current_side(&mut self, side: Option<Turn>) {
if let GameState::NetworkedMultiplayer { if let GameState::NetworkedMultiplayer {
paired, paired: _,
ref mut current_side, ref mut current_side,
current_turn, current_turn: _,
} = self } = self
{ {
*current_side = side; *current_side = side;