Skip to content

Commit

Permalink
chore(lint): applied further clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Jun 1, 2023
1 parent f0bb132 commit 0ab65b8
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 131 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
},
"rust-analyzer.linkedProjects": [
"./protocol/protocol_types/Cargo.toml",
"./server/Cargo.toml"
"./server/Cargo.toml",
"./client/Cargo.toml",
"./protocol/Cargo.toml",
"./protocol/protocol_data_types/Cargo.toml"
]
}
}
31 changes: 0 additions & 31 deletions server/src/game/data_manager.rs

This file was deleted.

10 changes: 5 additions & 5 deletions server/src/game/game_instance_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl GameInstancePlayer {
};

let cost = shop_character.cost as u16;
if self.money < cost as u16 {
if self.money < cost {
// Not enough money
return Err(());
}
Expand Down Expand Up @@ -115,11 +115,11 @@ impl GameInstancePlayer {
self.board[free_index] = Some(board_character.clone());
self.board[board_idx] = Some(shop_character);

self.money -= cost as u16;
self.money -= cost;
*self.shop.characters.get_mut(shop_idx).unwrap() = None;
} else {
// Board is empty at index
self.money -= cost as u16;
self.money -= cost;
*board_character = Some(shop_character);
*self.shop.characters.get_mut(shop_idx).unwrap() = None;
}
Expand All @@ -133,13 +133,13 @@ impl GameInstancePlayer {
return Err(());
}

if let Some(_) = self.board.get(character_idx).unwrap() {
if self.board.get(character_idx).unwrap().is_some() {
self.money += 1;
self.board[character_idx] = None;
Ok(())
} else {
// Board is empty at index
return Err(());
Err(())
}
}

Expand Down
1 change: 0 additions & 1 deletion server/src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub(crate) mod data_manager;
pub(crate) mod game_instance;
pub(crate) mod game_instance_player;
pub(crate) mod shop;
Expand Down
2 changes: 1 addition & 1 deletion server/src/game/shop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Shop {

pub fn get_new_characters(count: usize) -> Vec<Option<CharacterInstance>> {
get_characters()
.choose_multiple(&mut rand::thread_rng(), count as usize)
.choose_multiple(&mut rand::thread_rng(), count)
.cloned()
.map(|c| Some(CharacterInstance::from(&c, false)))
.collect::<Vec<_>>()
Expand Down
6 changes: 3 additions & 3 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
use std::{collections::HashMap, env, error::Error, sync::Arc};

use dotenv::dotenv;
use model::model::get_api;
use model::routes::get_api;
use rocket::{
fairing::AdHoc,
figment::{
Expand Down Expand Up @@ -53,11 +53,11 @@ async fn main() -> Result<(), rocket::Error> {
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL is missing");
let pool_size: u32 = env::var("POOL_SIZE").map_or(10, |size| {
size.parse()
.expect(format!("POOL_SIZE {} can not be cast to u32", size).as_str())
.unwrap_or_else(|_| panic!("POOL_SIZE {} can not be cast to u32", size))
});
let port: u32 = env::var("PORT").map_or(8000, |port| {
port.parse()
.expect(format!("PORT {} can not be cast to u32", port).as_str())
.unwrap_or_else(|_| panic!("PORT {} can not be cast to u32", port))
});

let db: Map<_, Value> = map! {
Expand Down
17 changes: 7 additions & 10 deletions server/src/model/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,19 @@ impl NewGame {
}
}

#[derive(AsChangeset, Debug)]
impl Default for NewGame {
fn default() -> Self {
Self::new()
}
}

#[derive(AsChangeset, Debug, Default)]
#[diesel(table_name = games)]
pub struct GameUpdate {
pub next_battle: Option<NaiveDateTime>,
pub current_round: Option<i32>,
}

impl GameUpdate {
pub fn new() -> Self {
Self {
next_battle: None,
current_round: None,
}
}
}

#[derive(Debug)]
pub enum GameError {
Internal,
Expand Down
13 changes: 2 additions & 11 deletions server/src/model/game_user_characters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct NewGameUserCharacter {
pub defense_bonus: i32,
}

#[derive(AsChangeset)]
#[derive(AsChangeset, Default)]
#[diesel(table_name = game_user_characters)]
pub struct GameUserCharacterUpdate {
pub position: Option<i32>,
Expand All @@ -66,15 +66,6 @@ pub struct GameUserCharacterUpdate {
}

impl GameUserCharacterUpdate {
pub fn new() -> Self {
Self {
position: None,
upgraded: None,
attack_bonus: None,
defense_bonus: None,
}
}

pub fn with_position(mut self, position: i32) -> Self {
self.position = Some(position);
self
Expand Down Expand Up @@ -160,7 +151,7 @@ pub async fn sell_character(user: &User, game: GameGuard, character_idx: usize)
health: game_user.health,
money: game_user.money,
name: user.username.clone(),
avatar: game_user.god.clone().and_then(|g| Some(g.id)),
avatar: game_user.god.clone().map(|g| g.id),
},
game_user.board.to_vec(),
))
Expand Down
2 changes: 1 addition & 1 deletion server/src/model/game_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub async fn get_own_user(user: &User, game: GameGuard) -> Json<Protocol> {
health: game_user.health,
money: game_user.money,
name: user.username.clone(),
avatar: game_user.god.clone().and_then(|g| Some(g.id)),
avatar: game_user.god.clone().map(|g| g.id),
}))
}

Expand Down
14 changes: 7 additions & 7 deletions server/src/model/lobbies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Lobby {
}

impl Lobby {
pub fn into_lobby_info(&self, users: &Vec<LobbyUser>) -> LobbyInfo {
pub fn into_lobby_info(&self, users: &[LobbyUser]) -> LobbyInfo {
LobbyInfo {
name: self.name.clone(),
master: users
Expand All @@ -48,9 +48,9 @@ pub struct LobbyWithUsers {
pub users: Vec<LobbyUser>,
}

impl Into<LobbyInfo> for LobbyWithUsers {
fn into(self) -> LobbyInfo {
self.lobby.into_lobby_info(&self.users)
impl From<LobbyWithUsers> for LobbyInfo {
fn from(val: LobbyWithUsers) -> Self {
val.lobby.into_lobby_info(&val.users)
}
}

Expand Down Expand Up @@ -100,10 +100,10 @@ impl<'r> FromRequest<'r> for LobbyWithUsers {
let users = LobbyUser::belonging_to(&lobby)
.load(con)
.unwrap_or_default();
return Outcome::Success(LobbyWithUsers { lobby, users });
Outcome::Success(LobbyWithUsers { lobby, users })
} else {
return Outcome::Forward(());
};
Outcome::Forward(())
}
})
.await;
}
Expand Down
10 changes: 5 additions & 5 deletions server/src/model/lobby_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub struct LobbyUser {
updated_at: NaiveDateTime,
}

impl Into<protocol::LobbyUser> for LobbyUser {
fn into(self) -> protocol::LobbyUser {
impl From<LobbyUser> for protocol::LobbyUser {
fn from(val: LobbyUser) -> Self {
protocol::LobbyUser {
id: self.id,
name: self.username,
ready: self.ready,
id: val.id,
name: val.username,
ready: val.ready,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions server/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod polling;
pub(crate) mod shop;
pub mod users;

pub mod model {
pub mod routes {
use protocol::protocol::{Protocol, Status};
use rocket::{serde::json::Json, Route};

Expand Down Expand Up @@ -47,7 +47,6 @@ pub mod model {
game_user_characters::move_character,
game_user_characters::sell_character,
polling::poll,
polling::notify,
]
}
}
5 changes: 0 additions & 5 deletions server/src/model/polling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,3 @@ pub async fn poll(user: &User) -> Json<Protocol> {
Err(_) => Json(Protocol::PollingTimeout),
}
}

#[get("/notify")]
pub async fn notify(user: &User) {
ActivePolls::notify(user.id, Protocol::EMPTY(String::new())).await;
}
14 changes: 10 additions & 4 deletions server/src/model/shop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ impl ShopUpdate {
}
}

impl Default for ShopUpdate {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug)]
pub enum ShopError {
Internal,
Expand All @@ -80,7 +86,7 @@ pub async fn get_shop(game: GameGuard, user: &User) -> Json<Protocol> {
health: game_user.health,
money: game_user.money,
name: game_user.display_name.to_string(),
avatar: game_user.god.clone().and_then(|g| Some(g.id)),
avatar: game_user.god.clone().map(|g| g.id),
},
game_user.shop.locked,
game_user.shop.characters.clone(),
Expand All @@ -105,7 +111,7 @@ pub async fn reroll_shop(game: GameGuard, user: &User) -> Json<Protocol> {
health: game_user.health,
money: game_user.money,
name: game_user.display_name.to_string(),
avatar: game_user.god.clone().and_then(|g| Some(g.id)),
avatar: game_user.god.clone().map(|g| g.id),
},
false,
game_user.shop.characters.clone(),
Expand Down Expand Up @@ -138,7 +144,7 @@ pub async fn toggle_lock_shop(game: GameGuard, user: &User) -> Json<Protocol> {
health: user.health,
money: user.money,
name: user.display_name.to_string(),
avatar: user.god.clone().and_then(|g| Some(g.id)),
avatar: user.god.clone().map(|g| g.id),
},
user.shop.locked,
user.shop.characters.clone(),
Expand Down Expand Up @@ -172,7 +178,7 @@ pub async fn buy_character(
health: game_user.health,
money: game_user.money,
name: game_user.display_name.to_string(),
avatar: game_user.god.clone().and_then(|g| Some(g.id)),
avatar: game_user.god.clone().map(|g| g.id),
},
game_user.shop.characters.clone(),
game_user.board.to_vec(),
Expand Down
19 changes: 9 additions & 10 deletions server/src/model/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use protocol::protocol::{Credentials, Error, LoginResponse, Protocol, UserData};
use rand_core::OsRng;
use rocket::{
http::Status,
log::private::debug,
request::{self, FromRequest, Outcome},
serde::json::Json,
Request, State,
Expand Down Expand Up @@ -95,7 +96,7 @@ impl<'r> FromRequest<'r> for &'r User {
})
.await;
match user {
Ok(user) => Outcome::Success(&user),
Ok(user) => Outcome::Success(user),
Err(e) => Outcome::Failure((Status::Unauthorized, e.clone())),
}
}
Expand Down Expand Up @@ -191,8 +192,7 @@ pub async fn login(
&creds,
&SaltString::from_b64(user.salt.as_str()).expect("User salt currupted")
)
.expect("Failed to verify login")
.to_string(),
.expect("Failed to verify login"),
user.password
);

Expand All @@ -215,6 +215,7 @@ pub async fn login(

for (id, g) in games.games.lock().await.iter() {
if g.lock().await.has_user(user.id) {
debug!("User {:?} is in game {:?}", user.id, id);
game = Some(Channel::Game(*id));
break;
}
Expand All @@ -226,17 +227,15 @@ pub async fn login(
.filter(lobby_users::user_id.eq(user.id))
.select(lobby_users::lobby_id)
.first::<i32>(con)
.map(|id| Channel::Lobby(id))
.map(Channel::Lobby)
.ok()
})
.await,
game,
];

for channel in channels {
if let Some(channel) = channel {
ActivePolls::join_user(channel, user.id);
}
for channel in channels.into_iter().flatten() {
ActivePolls::join_user(channel, user.id);
}

Json(Protocol::LoginResponse(LoginResponse {
Expand Down Expand Up @@ -281,9 +280,9 @@ pub async fn set_display_name(
{
Json(Protocol::DisplaynameResponse(display_name.0))
} else {
return Json(Error::new_protocol(
Json(Error::new_protocol(
Status::InternalServerError.code,
"Failed to update display name".to_string(),
));
))
}
}
Loading

0 comments on commit 0ab65b8

Please sign in to comment.