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 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,
/// second bool is if paired,
/// third bool is if cyan player
@ -172,6 +174,7 @@ impl DBHandler {
let create_player_result = self.create_new_player(Some(&conn));
if let Err(e) = create_player_result {
println!("{}", e);
player_tx.send((None, None)).ok();
// don't stop server because player limit may have been reached
return false;
}
@ -193,11 +196,11 @@ impl DBHandler {
if paired {
// don't stop server on send fail, may have timed
// 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 {
// don't stop server on send fail, may have timed
// out and dropped the receiver
player_tx.send((player_id, None)).ok();
player_tx.send((Some(player_id), None)).ok();
}
} else {
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() {
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 {
Ok(format!(
"{{\"type\":\"pairing_response\", \"id\": {}, \"status\": \"paired\", \"color\": \"{}\"}}",

View file

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