Browse Source

Send lobby status when game finishes

main
ThePerkinrex 4 years ago
parent
commit
4bad9c0b04
No known key found for this signature in database GPG Key ID: FD81DE6D75E20917
  1. 3
      server/src/games/run.rs
  2. 27
      server/src/server/game.rs
  3. 4
      server/src/server/lobby.rs

3
server/src/games/run.rs

@ -16,8 +16,7 @@ pub use types::{CardId, CardIdx, PileKind, RunningPile};
use types::{Data, Player, RhaiResult}; use types::{Data, Player, RhaiResult};
pub struct RunningGame { pub struct RunningGame {
#[allow(unused)] // TODO Remove pub name: String,
name: String,
pub piles: HashMap<String, RunningPile>, pub piles: HashMap<String, RunningPile>,
pub player_piles: Vec<HashMap<String, RunningPile>>, pub player_piles: Vec<HashMap<String, RunningPile>>,
functions: Functions, functions: Functions,

27
server/src/server/game.rs

@ -3,16 +3,18 @@ use super::{
socket_manager::SocketManager, socket_manager::SocketManager,
ServiceData, ServiceData,
}; };
use crate::{games::{CardId, CardIdx, PileKind, RunningPile}, server::protos::{game::game_status::CustomInfoMessage, protocol::server_client_packet::Data}}; use crate::{games::{CardId, CardIdx, PileKind, RunningPile}, server::protos::{game::game_status::CustomInfoMessage, protocol::server_client_packet::{self, Data}}};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
pub(super) async fn get_status(data: &mut ServiceData) -> Result<GameStatus> { pub(super) async fn get_status(data: &mut ServiceData,
socket_mgr: &SocketManager,) -> Result<GameStatus> {
let uuid = data.user_id.get()?; let uuid = data.user_id.get()?;
log::info!("Creating a new status for {}", uuid); log::info!("Creating a new status for {}", uuid);
let lobby: u32 = match data.db.get_lobby_for_user(uuid).await { let lobby: u32 = match data.db.get_lobby_for_user(uuid).await {
Some(l) => l, Some(l) => l,
None => return Err(anyhow!("User isn't in a lobby")), None => return Err(anyhow!("User isn't in a lobby")),
}; };
let (status, name) = {
let games_lock = data.running_games.read().await; // This encounters a deadlock let games_lock = data.running_games.read().await; // This encounters a deadlock
// log::info!("Locked games"); // log::info!("Locked games");
let game_lock = match games_lock.get(&lobby) { let game_lock = match games_lock.get(&lobby) {
@ -30,7 +32,8 @@ pub(super) async fn get_status(data: &mut ServiceData) -> Result<GameStatus> {
.into_iter() .into_iter()
.map(|(x, _)| super::protos::common::Name { name: x }) .map(|(x, _)| super::protos::common::Name { name: x })
.collect(); .collect();
let status = GameStatus { (
GameStatus {
current_turn: game.get_current_player(), current_turn: game.get_current_player(),
common_piles: Some(Piles { common_piles: Some(Piles {
piles: game piles: game
@ -90,13 +93,22 @@ pub(super) async fn get_status(data: &mut ServiceData) -> Result<GameStatus> {
names, names,
has_finished: game.has_finished, has_finished: game.has_finished,
info: None, info: None,
},
game.name.clone(),
)
}; };
log::info!("Created a new status for {}", uuid); log::info!("Created a new status for {}", uuid);
if status.has_finished {
log::info!("Game has finished: {}", name);
data.running_games.write().await.remove(&lobby);
let s = super::lobby::get_status(data).await.expect("Unexpected error getting lobby status");
socket_mgr.broadcast_to_lobby(&mut data.db, data.user_id.0.unwrap(), server_client_packet::Data::LobbyStatus(s)).await.expect("Error sending finished game ");
}
Ok(status) Ok(status)
} }
pub(super) async fn query_status(data: &mut ServiceData, socket_mgr: &SocketManager) -> Result<()> { pub(super) async fn query_status(data: &mut ServiceData, socket_mgr: &SocketManager) -> Result<()> {
let status = get_status(data).await?; let status = get_status(data, socket_mgr).await?;
socket_mgr socket_mgr
.write(data.user_id.get_ref()?, Data::GameStatus(status)) .write(data.user_id.get_ref()?, Data::GameStatus(status))
.await .await
@ -177,10 +189,13 @@ pub(super) async fn on_click(
pile_name: card.pile_name, pile_name: card.pile_name,
}; };
if let Err(e) = game.on_click(card, game.get_player_for_uuid(&uuid).unwrap()) { if let Err(e) = game.on_click(card, game.get_player_for_uuid(&uuid).unwrap()) {
message = Some(CustomInfoMessage { title: "Error in game execution".to_string(), m: e.to_string() }); message = Some(CustomInfoMessage {
title: "Error in game execution".to_string(),
m: e.to_string(),
});
} }
} // drop the connection so that the lock is released } // drop the connection so that the lock is released
let mut status = get_status(data).await?; let mut status = get_status(data, socket_mgr).await?;
status.info = message; status.info = message;
socket_mgr socket_mgr

4
server/src/server/lobby.rs

@ -24,7 +24,7 @@ pub(super) async fn vote(
Ok(()) Ok(())
} }
async fn get_status(data: &mut ServiceData) -> Result<LobbyStatus> { pub(super) async fn get_status(data: &mut ServiceData) -> Result<LobbyStatus> {
let uuid = data.user_id.get()?; let uuid = data.user_id.get()?;
if let Some(l) = data.db.get_lobby_for_user(uuid).await { if let Some(l) = data.db.get_lobby_for_user(uuid).await {
let (votes, is_starting) = data.voting.status(&l).await.unwrap_or_default(); let (votes, is_starting) = data.voting.status(&l).await.unwrap_or_default();
@ -67,7 +67,7 @@ pub(super) async fn ready(data: &mut ServiceData, socket_mgr: &SocketManager) ->
.await?; .await?;
log::info!("Player {} is ready", uuid); log::info!("Player {} is ready", uuid);
if is_starting { if is_starting {
let game_status = super::game::get_status(data).await?; let game_status = super::game::get_status(data, socket_mgr).await?;
socket_mgr socket_mgr
.broadcast_to_lobby( .broadcast_to_lobby(
&mut data.db, &mut data.db,

Loading…
Cancel
Save