reduce potential explosions and add error messages
This commit is contained in:
parent
13ba4944ca
commit
1550772995
2 changed files with 194 additions and 260 deletions
|
@ -46,78 +46,51 @@ impl GameHandler {
|
|||
/// Delete game
|
||||
async fn delete_game(&self, request: GameDeleteRequest) {
|
||||
// TODO: add auth lol
|
||||
let _ = self
|
||||
if self
|
||||
.state
|
||||
.games
|
||||
.write()
|
||||
.unwrap()
|
||||
.remove(&request.delete_game_id)
|
||||
.unwrap();
|
||||
|
||||
.is_some()
|
||||
{
|
||||
// Broadcast game browser update
|
||||
self.state
|
||||
.broadcast_tx
|
||||
.send(meta_games_browser_update(&self.state))
|
||||
.unwrap();
|
||||
} else {
|
||||
tracing::error!("User tried to delete a nonexistent game!");
|
||||
}
|
||||
}
|
||||
|
||||
/// Process judging
|
||||
async fn handle_judging(&self, request: JudgeDecisionRequest, addr: SocketAddr) {
|
||||
// Get pointers
|
||||
let player_user_id = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.uuid
|
||||
.to_string();
|
||||
|
||||
let this_game = self
|
||||
.state
|
||||
.games
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&request.game_id)
|
||||
.unwrap()
|
||||
.clone();
|
||||
if let Some(this_game) = self.state.games.read().unwrap().get(&request.game_id) {
|
||||
if let Some(player_user) = self.state.online_users.read().unwrap().get(&addr) {
|
||||
let player_user_id = player_user.read().unwrap().uuid.to_string();
|
||||
let game_id = request.game_id.to_string();
|
||||
|
||||
// Send to game
|
||||
this_game
|
||||
.write()
|
||||
.unwrap()
|
||||
.judge_round(request.clone(), player_user_id.clone());
|
||||
.judge_round(request, player_user_id);
|
||||
|
||||
let this_game_id = this_game.read().unwrap().uuid.to_string();
|
||||
self.send_game_state_update(this_game_id).await
|
||||
self.send_game_state_update(game_id);
|
||||
} else {
|
||||
tracing::error!("Received judge request for nonexistent judge player!");
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Received judge request for nonexistent game!");
|
||||
}
|
||||
}
|
||||
|
||||
/// Process player move request
|
||||
async fn handle_player_move(&self, request: PlayerMoveRequest, addr: SocketAddr) {
|
||||
// Get pointers
|
||||
let player_user_id = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.uuid
|
||||
.to_string();
|
||||
|
||||
let this_game = self
|
||||
.state
|
||||
.games
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&request.game_id)
|
||||
.unwrap()
|
||||
.clone();
|
||||
if let Some(this_game) = self.state.games.read().unwrap().get(&request.game_id) {
|
||||
if let Some(player_user) = self.state.online_users.read().unwrap().get(&addr) {
|
||||
let player_user_id = player_user.read().unwrap().uuid.to_string();
|
||||
|
||||
// Do the stuff
|
||||
match this_game
|
||||
|
@ -143,26 +116,31 @@ impl GameHandler {
|
|||
})
|
||||
}
|
||||
};
|
||||
} else {
|
||||
tracing::error!("Nonexistent player tried to submit move for game!");
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Player tried to submit move for nonexistent game!");
|
||||
}
|
||||
}
|
||||
|
||||
/// Puts a user in a game
|
||||
async fn join_game(&self, id: String, addr: SocketAddr) {
|
||||
// Get pointers
|
||||
let this_game = self.state.games.read().unwrap().get(&id).unwrap().clone();
|
||||
let this_user = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
if let Some(this_game) = self.state.games.read().unwrap().get(&id) {
|
||||
if let Some(this_user) = self.state.online_users.read().unwrap().get(&addr) {
|
||||
// Create player
|
||||
this_game.write().unwrap().create_player(this_user);
|
||||
this_game.write().unwrap().create_player(this_user.clone());
|
||||
} else {
|
||||
tracing::error!("Tried to add a nonexistent user to game!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
tracing::error!("User tried to join a nonexistent game!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Send updates for all players
|
||||
self.send_game_state_update(id).await;
|
||||
self.send_game_state_update(id);
|
||||
|
||||
// Broadcast game browser update
|
||||
self.state
|
||||
|
@ -178,16 +156,8 @@ impl GameHandler {
|
|||
}
|
||||
|
||||
/// Send game state update for all players of a game
|
||||
async fn send_game_state_update(&self, game_id: String) {
|
||||
let this_game = self
|
||||
.state
|
||||
.games
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&game_id)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
fn send_game_state_update(&self, game_id: String) {
|
||||
if let Some(this_game) = self.state.games.read().unwrap().get(&game_id) {
|
||||
let players = this_game
|
||||
.read()
|
||||
.unwrap()
|
||||
|
@ -230,6 +200,9 @@ impl GameHandler {
|
|||
let user_tx = player.user.read().unwrap().tx.clone();
|
||||
tokio::spawn(async move { user_tx.send(msg).await });
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Attempted to create game state update for nonexistent game!");
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new game
|
||||
|
@ -242,16 +215,7 @@ impl GameHandler {
|
|||
return;
|
||||
}
|
||||
|
||||
// Get host pointer
|
||||
let host = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
if let Some(host) = self.state.online_users.read().unwrap().get(&addr) {
|
||||
// Create manifest
|
||||
let manifest = NewGameManifest {
|
||||
name: new_game.name,
|
||||
|
@ -273,7 +237,7 @@ impl GameHandler {
|
|||
Arc::new(RwLock::new(new_game_object)),
|
||||
);
|
||||
|
||||
self.send_game_state_update(game_id).await;
|
||||
self.send_game_state_update(game_id);
|
||||
|
||||
// Broadcast game browser update
|
||||
self.state
|
||||
|
@ -286,5 +250,8 @@ impl GameHandler {
|
|||
.broadcast_tx
|
||||
.send(meta_server_summary_update(&self.state))
|
||||
.unwrap();
|
||||
} else {
|
||||
tracing::error!("Attempted to create game for nonexistent player!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,6 +63,8 @@ impl UserHandler {
|
|||
tx = user.read().unwrap().tx.clone();
|
||||
} else {
|
||||
tracing::error!("Attempted to send message to invalid user id!");
|
||||
// TODO: User can still be offline causing send to fail. This is suppressed
|
||||
// below
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -77,6 +79,7 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
// Serialize and send message
|
||||
// TODO: Send failures are suppressed
|
||||
match message {
|
||||
SendUserUpdate(message) => {
|
||||
let msg = to_string::<UserUpdate>(&message).unwrap();
|
||||
|
@ -99,7 +102,7 @@ impl UserHandler {
|
|||
let new_user = Arc::new(RwLock::new(user));
|
||||
|
||||
// Notify client of new username
|
||||
tx.send(user_client_self_update(&new_user)).await.unwrap();
|
||||
let _ = tx.send(user_client_self_update(&new_user)).await;
|
||||
|
||||
// Register uuid
|
||||
self.state
|
||||
|
@ -116,12 +119,8 @@ impl UserHandler {
|
|||
.insert(addr, new_user.clone());
|
||||
|
||||
// Hydrate client
|
||||
tx.send(meta_games_browser_update(&self.state))
|
||||
.await
|
||||
.unwrap();
|
||||
tx.send(meta_new_game_card_packs(&self.state))
|
||||
.await
|
||||
.unwrap();
|
||||
let _ = tx.send(meta_games_browser_update(&self.state)).await;
|
||||
let _ = tx.send(meta_new_game_card_packs(&self.state)).await;
|
||||
|
||||
// Broadcast new user's existence
|
||||
// TODO: this should probably be combined and sent as one
|
||||
|
@ -136,24 +135,21 @@ impl UserHandler {
|
|||
let _ = &self.state.broadcast_tx.send(meta_chat_update(&self.state));
|
||||
|
||||
// TODO: this races the broadcasts but if it's done last it'll probably show up last...
|
||||
tx.send(meta_motd()).await.unwrap();
|
||||
let _ = tx.send(meta_motd()).await;
|
||||
}
|
||||
|
||||
/// Handle user login
|
||||
async fn login(&self, request: UserLogInRequest, addr: SocketAddr) {
|
||||
let broadcast_tx = self.state.broadcast_tx.clone();
|
||||
let new_name = request.username.clone();
|
||||
let old_name = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.name
|
||||
.clone();
|
||||
let old_name;
|
||||
|
||||
if let Some(user) = self.state.online_users.read().unwrap().get(&addr) {
|
||||
old_name = user.read().unwrap().name.clone();
|
||||
} else {
|
||||
tracing::error!("Nonexistent user attempting login");
|
||||
return;
|
||||
}
|
||||
|
||||
// Resume user's old session if they exist as offline
|
||||
if self
|
||||
|
@ -163,42 +159,25 @@ impl UserHandler {
|
|||
.unwrap()
|
||||
.contains_key(&new_name)
|
||||
{
|
||||
let buf;
|
||||
// Copy over new tx
|
||||
self.state
|
||||
.offline_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&new_name)
|
||||
.unwrap()
|
||||
.write()
|
||||
.unwrap()
|
||||
.tx = self
|
||||
.state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.tx
|
||||
.clone();
|
||||
if let Some(online_user) = self.state.online_users.write().unwrap().remove(&addr) {
|
||||
if let Some(offline_user) =
|
||||
self.state.offline_users.write().unwrap().remove(&new_name)
|
||||
{
|
||||
offline_user.write().unwrap().tx = online_user.write().unwrap().tx.clone();
|
||||
buf = offline_user;
|
||||
} else {
|
||||
tracing::error!("Error copying tx to new user: Can't find offline user!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
tracing::error!("Error copying tx to new user: Can't find online user!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Move offline user object to online
|
||||
self.state
|
||||
.online_users
|
||||
.write()
|
||||
.unwrap()
|
||||
.insert(
|
||||
addr,
|
||||
self.state
|
||||
.offline_users
|
||||
.write()
|
||||
.unwrap()
|
||||
.remove(&new_name)
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
self.state.online_users.write().unwrap().insert(addr, buf);
|
||||
|
||||
// Send welcome back messages
|
||||
let msg = format! {
|
||||
|
@ -241,15 +220,12 @@ impl UserHandler {
|
|||
.insert(new_name.clone());
|
||||
|
||||
// Change user's name
|
||||
self.state
|
||||
.online_users
|
||||
.write()
|
||||
.unwrap()
|
||||
.get_mut(&addr)
|
||||
.unwrap()
|
||||
.write()
|
||||
.unwrap()
|
||||
.change_name(request.username);
|
||||
if let Some(user) = self.state.online_users.write().unwrap().get_mut(&addr) {
|
||||
user.write().unwrap().change_name(request.username);
|
||||
} else {
|
||||
tracing::error!("Error updating username: Can't find user!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Send chat updates
|
||||
let msg = format! {
|
||||
|
@ -257,9 +233,8 @@ impl UserHandler {
|
|||
old_name,
|
||||
new_name
|
||||
};
|
||||
broadcast_tx
|
||||
.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap())
|
||||
.unwrap();
|
||||
let _ =
|
||||
broadcast_tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap());
|
||||
}
|
||||
|
||||
// Send the user their new name
|
||||
|
@ -270,12 +245,9 @@ impl UserHandler {
|
|||
Addr(addr),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Send client updates
|
||||
broadcast_tx
|
||||
.send(meta_games_browser_update(&self.state))
|
||||
.unwrap();
|
||||
broadcast_tx.send(meta_chat_update(&self.state)).unwrap();
|
||||
let _ = broadcast_tx.send(meta_games_browser_update(&self.state));
|
||||
let _ = broadcast_tx.send(meta_chat_update(&self.state));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -347,18 +319,13 @@ pub fn meta_games_browser_update(state: &Arc<AppState>) -> String {
|
|||
|
||||
/// Generate chatroom join announcement
|
||||
pub fn meta_announce_user_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
|
||||
let msg = format!(
|
||||
"{} joined.",
|
||||
state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.name
|
||||
);
|
||||
let msg = format!("{} joined.", {
|
||||
if let Some(user) = state.online_users.read().unwrap().get(addr) {
|
||||
user.read().unwrap().name.clone()
|
||||
} else {
|
||||
return Default::default();
|
||||
}
|
||||
});
|
||||
|
||||
tracing::debug!("{}", &msg);
|
||||
to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap()
|
||||
|
|
Loading…
Add table
Reference in a new issue