Browse Source

run cargo fmt

main
ThePerkinrex 4 years ago
parent
commit
22f6974288
No known key found for this signature in database GPG Key ID: FD81DE6D75E20917
  1. 12
      server/src/db.rs
  2. 27
      server/src/server/game.rs

12
server/src/db.rs

@ -79,7 +79,6 @@ impl Db {
.unwrap() .unwrap()
} }
pub fn get_uuids_in_lobby_where_user_is(&mut self, uuid: Uuid) -> Vec<Uuid> { pub fn get_uuids_in_lobby_where_user_is(&mut self, uuid: Uuid) -> Vec<Uuid> {
let mut prepared = self.conn.prepare_cached("SELECT UUID FROM Users WHERE UUID in (SELECT UserId FROM UsersInLobbies WHERE LobbyId = (SELECT LobbyId FROM UsersInLobbies WHERE UserId = ?))").unwrap(); let mut prepared = self.conn.prepare_cached("SELECT UUID FROM Users WHERE UUID in (SELECT UserId FROM UsersInLobbies WHERE LobbyId = (SELECT LobbyId FROM UsersInLobbies WHERE UserId = ?))").unwrap();
prepared prepared
@ -92,7 +91,6 @@ impl Db {
.unwrap() .unwrap()
} }
pub fn get_name_for_uuid(&mut self, uuid: Uuid) -> String { pub fn get_name_for_uuid(&mut self, uuid: Uuid) -> String {
let mut prepared = self let mut prepared = self
.conn .conn
@ -104,7 +102,6 @@ impl Db {
.unwrap() .unwrap()
} }
pub fn create_lobby(&mut self, public: bool) -> u32 { pub fn create_lobby(&mut self, public: bool) -> u32 {
let id = rand::random(); let id = rand::random();
@ -128,7 +125,6 @@ impl Db {
log::info!("{} joined the lobby {}", user, lobby); log::info!("{} joined the lobby {}", user, lobby);
} }
pub fn leave_lobby(&mut self, user: Uuid) { pub fn leave_lobby(&mut self, user: Uuid) {
log::info!("{} leaving the lobby", user); log::info!("{} leaving the lobby", user);
self.delete_vote(user); self.delete_vote(user);
@ -146,7 +142,6 @@ impl Db {
} }
} }
pub fn disconnect(&mut self, user: Uuid) { pub fn disconnect(&mut self, user: Uuid) {
self.leave_lobby(user); self.leave_lobby(user);
log::info!("{} disconnecting", user); log::info!("{} disconnecting", user);
@ -160,7 +155,6 @@ impl Db {
// log::error!("Disconnect DB result{:?}", r); // log::error!("Disconnect DB result{:?}", r);
} }
pub fn get_lobby_for_user(&mut self, user: Uuid) -> Option<u32> { pub fn get_lobby_for_user(&mut self, user: Uuid) -> Option<u32> {
match self.conn.query_row( match self.conn.query_row(
"SELECT LobbyID from UsersInLobbies WHERE UserID = ?", "SELECT LobbyID from UsersInLobbies WHERE UserID = ?",
@ -187,7 +181,6 @@ impl Db {
.unwrap() .unwrap()
} }
fn delete_vote(&mut self, user: Uuid) { fn delete_vote(&mut self, user: Uuid) {
self.conn self.conn
.execute( .execute(
@ -197,7 +190,6 @@ impl Db {
.unwrap(); .unwrap();
} }
pub fn vote(&mut self, user: Uuid, game: u32) { pub fn vote(&mut self, user: Uuid, game: u32) {
self.delete_vote(user); self.delete_vote(user);
self.conn self.conn
@ -208,7 +200,6 @@ impl Db {
.unwrap(); .unwrap();
} }
pub fn vote_ready(&mut self, user: Uuid) { pub fn vote_ready(&mut self, user: Uuid) {
// self.delete_vote(user); // self.delete_vote(user);
self.conn self.conn
@ -219,7 +210,6 @@ impl Db {
.unwrap(); .unwrap();
} }
pub fn get_votes(&mut self, lobby: u32) -> Vec<(String, u32, bool)> { pub fn get_votes(&mut self, lobby: u32) -> Vec<(String, u32, bool)> {
let mut prepared = self.conn.prepare_cached("SELECT Users.Name, Votes.GameID, Votes.Ready FROM Votes JOIN Users ON Users.UUID = Votes.UserID WHERE Votes.UserID IN (SELECT UserID FROM UsersInLobbies WHERE LobbyID = ?)").unwrap(); let mut prepared = self.conn.prepare_cached("SELECT Users.Name, Votes.GameID, Votes.Ready FROM Votes JOIN Users ON Users.UUID = Votes.UserID WHERE Votes.UserID IN (SELECT UserID FROM UsersInLobbies WHERE LobbyID = ?)").unwrap();
prepared prepared
@ -234,7 +224,6 @@ impl Db {
.unwrap() .unwrap()
} }
pub fn is_poll_finished(&mut self, lobby: u32) -> bool { pub fn is_poll_finished(&mut self, lobby: u32) -> bool {
self.conn.query_row( self.conn.query_row(
"SELECT (SELECT COUNT(*) FROM UsersInLobbies where LobbyID = ?) = (SELECT COUNT(*) FROM Votes WHERE UserID IN (SELECT UserID FROM UsersInLobbies WHERE LobbyID = ?) AND Ready = TRUE)", "SELECT (SELECT COUNT(*) FROM UsersInLobbies where LobbyID = ?) = (SELECT COUNT(*) FROM Votes WHERE UserID IN (SELECT UserID FROM UsersInLobbies WHERE LobbyID = ?) AND Ready = TRUE)",
@ -243,7 +232,6 @@ impl Db {
).unwrap() ).unwrap()
} }
pub fn delete_votes(&mut self, lobby: u32) { pub fn delete_votes(&mut self, lobby: u32) {
self.conn self.conn
.execute("DELETE FROM Votes WHERE UserId IN (SELECT UserID FROM UsersInLobbies WHERE LobbyID = ?)", params![lobby as i32]).unwrap(); .execute("DELETE FROM Votes WHERE UserId IN (SELECT UserID FROM UsersInLobbies WHERE LobbyID = ?)", params![lobby as i32]).unwrap();

27
server/src/server/game.rs

@ -3,11 +3,19 @@ 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::{self, 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, pub(super) async fn get_status(
socket_mgr: &SocketManager,) -> Result<GameStatus> { 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 {
@ -101,8 +109,17 @@ pub(super) async fn get_status(data: &mut ServiceData,
if status.has_finished { if status.has_finished {
log::info!("Game has finished: {}", name); log::info!("Game has finished: {}", name);
data.running_games.write().await.remove(&lobby); data.running_games.write().await.remove(&lobby);
let s = super::lobby::get_status(data).await.expect("Unexpected error getting lobby status"); let s = super::lobby::get_status(data)
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 "); .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)
} }

Loading…
Cancel
Save