From c34bc3c11e2fafedd1a7efc4c16ab491c9217e51 Mon Sep 17 00:00:00 2001 From: Lionarius Date: Sat, 22 Apr 2023 19:19:42 +0300 Subject: [PATCH 01/11] rename DPS to Dps --- src/bot/commands/lobby.rs | 4 ++-- src/bot/commands/settings.rs | 4 ++-- src/database/models/role.rs | 8 ++++---- src/database/queries/player.rs | 2 +- src/mixer/mixer.rs | 4 ++-- src/mixer/player.rs | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/bot/commands/lobby.rs b/src/bot/commands/lobby.rs index d324459..3762a8f 100644 --- a/src/bot/commands/lobby.rs +++ b/src/bot/commands/lobby.rs @@ -267,8 +267,8 @@ impl LobbyCommand { .collect::>(); let slots = vec![ Role::Tank, - Role::DPS, - Role::DPS, + Role::Dps, + Role::Dps, Role::Support, Role::Support, ]; diff --git a/src/bot/commands/settings.rs b/src/bot/commands/settings.rs index e860594..e484b0e 100644 --- a/src/bot/commands/settings.rs +++ b/src/bot/commands/settings.rs @@ -79,8 +79,8 @@ impl SettingsCommand { let roles: HashMap<_, _, RandomState> = HashMap::from_iter( [ ("support", Role::Support), - ("damage", Role::DPS), - ("dps", Role::DPS), + ("damage", Role::Dps), + ("dps", Role::Dps), ("tank", Role::Tank), ] .into_iter(), diff --git a/src/database/models/role.rs b/src/database/models/role.rs index 9cf6552..ba2c043 100644 --- a/src/database/models/role.rs +++ b/src/database/models/role.rs @@ -6,7 +6,7 @@ pub enum Role { #[sea_orm(string_value = "tank")] Tank, #[sea_orm(string_value = "dps")] - DPS, + Dps, #[sea_orm(string_value = "support")] Support, } @@ -15,7 +15,7 @@ impl From for String { fn from(role: Role) -> Self { match role { Role::Tank => "tank".to_string(), - Role::DPS => "dps".to_string(), + Role::Dps => "dps".to_string(), Role::Support => "support".to_string(), } } @@ -27,7 +27,7 @@ impl TryFrom<&str> for Role { fn try_from(role: &str) -> Result { match role { "tank" => Ok(Role::Tank), - "dps" => Ok(Role::DPS), + "dps" => Ok(Role::Dps), "support" => Ok(Role::Support), _ => Err(()), } @@ -38,7 +38,7 @@ impl From for i32 { fn from(role: Role) -> Self { match role { Role::Tank => 0, - Role::DPS => 1, + Role::Dps => 1, Role::Support => 2, } } diff --git a/src/database/queries/player.rs b/src/database/queries/player.rs index 79a9cb9..7460779 100644 --- a/src/database/queries/player.rs +++ b/src/database/queries/player.rs @@ -73,7 +73,7 @@ impl Query { player.tank_rd = Set(rating.rd); player.tank_volatility = Set(rating.volatility); } - Role::DPS => { + Role::Dps => { player.dps_rating = Set(rating.value); player.dps_rd = Set(rating.rd); player.dps_volatility = Set(rating.volatility); diff --git a/src/mixer/mixer.rs b/src/mixer/mixer.rs index 75b13a9..77cbc28 100644 --- a/src/mixer/mixer.rs +++ b/src/mixer/mixer.rs @@ -30,7 +30,7 @@ fn get_role_entries( let (tanks, rest) = entries .into_iter() .partition::, _>(|e| e.role == Role::Tank); - let (dps, supports) = rest.into_iter().partition(|e| e.role == Role::DPS); + let (dps, supports) = rest.into_iter().partition(|e| e.role == Role::Dps); (tanks, dps, supports) } @@ -55,7 +55,7 @@ pub fn mix_players(players: &[Player], slots: Vec) -> Option<(Team, Team)> let tank_count = slots.iter().filter(|r| **r == Role::Tank).count(); let support_count = slots.iter().filter(|r| **r == Role::Support).count(); - let dps_count = slots.iter().filter(|r| **r == Role::DPS).count(); + let dps_count = slots.iter().filter(|r| **r == Role::Dps).count(); let tank_combos = get_combinations(&tanks, tank_count); let dps_combos = get_combinations(&dps, dps_count); diff --git a/src/mixer/player.rs b/src/mixer/player.rs index ba98a8c..d4caaf3 100644 --- a/src/mixer/player.rs +++ b/src/mixer/player.rs @@ -36,7 +36,7 @@ impl Player { Rating::new(model.tank_rating, model.tank_rd, model.tank_volatility), ), ( - Role::DPS, + Role::Dps, Rating::new(model.dps_rating, model.dps_rd, model.dps_volatility), ), ( From 3877b0a36a878410570f2805b9c4090a92966612 Mon Sep 17 00:00:00 2001 From: Lionarius Date: Sat, 22 Apr 2023 19:21:54 +0300 Subject: [PATCH 02/11] remove useless format! --- src/bot/commands/lobby.rs | 2 +- src/bot/commands/preference.rs | 2 +- src/bot/commands/rating.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bot/commands/lobby.rs b/src/bot/commands/lobby.rs index 3762a8f..308d2ff 100644 --- a/src/bot/commands/lobby.rs +++ b/src/bot/commands/lobby.rs @@ -254,7 +254,7 @@ impl LobbyCommand { None => { interaction .edit_original_interaction_response(ctx, |response| { - response.content(format!("Failed to get players")) + response.content("Failed to get players") }) .await?; return Ok(()); diff --git a/src/bot/commands/preference.rs b/src/bot/commands/preference.rs index 9aa618a..4ee8f1d 100644 --- a/src/bot/commands/preference.rs +++ b/src/bot/commands/preference.rs @@ -122,7 +122,7 @@ impl MixerCommand for PreferenceCommand { response .kind(InteractionResponseType::ChannelMessageWithSource) .interaction_response_data(|message| { - message.content(format!("User not found")).ephemeral(true) + message.content("User not found").ephemeral(true) }) }) .await?; diff --git a/src/bot/commands/rating.rs b/src/bot/commands/rating.rs index d936945..64c16d2 100644 --- a/src/bot/commands/rating.rs +++ b/src/bot/commands/rating.rs @@ -87,7 +87,7 @@ impl MixerCommand for RatingCommand { response .kind(InteractionResponseType::ChannelMessageWithSource) .interaction_response_data(|message| { - message.content(format!("User not found")).ephemeral(true) + message.content("User not found").ephemeral(true) }) }) .await?; @@ -133,7 +133,7 @@ impl MixerCommand for RatingCommand { .kind(InteractionResponseType::ChannelMessageWithSource) .interaction_response_data(|message| { message - .content(format!("Rank must be between 1 and 5000")) + .content("Rank must be between 1 and 5000") .ephemeral(true) }) }) From 4802fbf5df60de93e4b5f9ca6d6ae65d98e107ca Mon Sep 17 00:00:00 2001 From: Lionarius Date: Tue, 25 Apr 2023 21:37:54 +0300 Subject: [PATCH 03/11] fix death --- src/bot/commands/lobby.rs | 3 ++- src/image_manipulation.rs | 25 ++++++++++--------------- src/mixer/player.rs | 2 +- src/mixer/team.rs | 6 +++--- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/bot/commands/lobby.rs b/src/bot/commands/lobby.rs index 308d2ff..ee8335d 100644 --- a/src/bot/commands/lobby.rs +++ b/src/bot/commands/lobby.rs @@ -348,9 +348,10 @@ impl LobbyCommand { let team1_rank = team1.average_rating(&players); let team2_rank = team2.average_rating(&players); - image_gen.draw_teams_to_png( + image_gen.draw_teams_to_vec( player_names, [team1_rank.value as i32, team2_rank.value as i32], + image::ImageOutputFormat::Png ) }; diff --git a/src/image_manipulation.rs b/src/image_manipulation.rs index 5e86f1a..921ae9f 100644 --- a/src/image_manipulation.rs +++ b/src/image_manipulation.rs @@ -1,8 +1,8 @@ -use image::{codecs::png, ImageEncoder}; +use image::ImageOutputFormat; use imageproc::drawing::text_size; use rusttype::{Font, Scale}; use serenity::prelude::TypeMapKey; -use std::{io::BufWriter, sync::Arc}; +use std::{io::Cursor, sync::Arc}; pub struct ImageGenerator<'a> { pub player_font: Font<'a>, @@ -11,7 +11,7 @@ pub struct ImageGenerator<'a> { } impl<'a> ImageGenerator<'a> { - pub fn draw_teams_to_png(&self, player_names: Vec, teams_rating: [i32; 2]) -> Vec { + pub fn draw_teams_to_vec(&self, player_names: Vec, teams_rating: [i32; 2], format: ImageOutputFormat) -> Vec { let mut image: image::ImageBuffer, Vec> = self.teams_image.clone(); let player_text_scale = Scale::uniform(60.0); @@ -47,8 +47,8 @@ impl<'a> ImageGenerator<'a> { } let rating_text_scale = Scale::uniform(86.5); - for i in 0..2 { - let rating = teams_rating[i].to_string(); + for (i, rating) in teams_rating.iter().enumerate() { + let rating = rating.to_string(); let size = text_size(rating_text_scale, &self.player_font, &rating); imageproc::drawing::draw_text_mut( @@ -62,16 +62,11 @@ impl<'a> ImageGenerator<'a> { ); } - let mut buf = BufWriter::new(Vec::new()); - png::PngEncoder::new(&mut buf) - .write_image( - image.as_raw(), - image.width(), - image.height(), - image::ColorType::Rgb8, - ) - .unwrap(); - buf.into_inner().unwrap() + + let mut buf = Cursor::new(Vec::new()); + image.write_to(&mut buf, format).unwrap(); + + buf.into_inner() } } diff --git a/src/mixer/player.rs b/src/mixer/player.rs index d4caaf3..8a6d1fd 100644 --- a/src/mixer/player.rs +++ b/src/mixer/player.rs @@ -77,7 +77,7 @@ impl Player { for (i, role) in self.priority_roles.iter().enumerate() { if let Some(role) = role { - priorities.insert(role.clone(), priority_points / (i + 1) as f32); + priorities.insert(*role, priority_points / (i + 1) as f32); } } diff --git a/src/mixer/team.rs b/src/mixer/team.rs index be0bcbd..26f2143 100644 --- a/src/mixer/team.rs +++ b/src/mixer/team.rs @@ -47,7 +47,7 @@ impl Team { } pub fn count_role(&self, role: &Role) -> usize { - self.count_role.get(role).unwrap().clone() + *self.count_role.get(role).unwrap() } pub fn full_rating(&self, players: &[Player]) -> Rating { @@ -55,7 +55,7 @@ impl Team { .iter() .map(|((role, _), index)| { if let Some(index) = index { - players[*index].ranks.get(role).unwrap().clone() + *players[*index].ranks.get(role).unwrap() } else { Rating::zero() } @@ -77,7 +77,7 @@ impl Team { .filter(|((r, _), _)| r == role) .map(|((_, _), index)| { if let Some(index) = index { - players[*index].ranks.get(&role).unwrap().clone() + *players[*index].ranks.get(role).unwrap() } else { Rating::zero() } From 49246af23244093b9ee82bd1fb6ddeede4a8c0d1 Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:36:32 +0300 Subject: [PATCH 04/11] replace ==0 with is_empty --- src/mixer/team.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mixer/team.rs b/src/mixer/team.rs index 26f2143..07bb10d 100644 --- a/src/mixer/team.rs +++ b/src/mixer/team.rs @@ -64,7 +64,7 @@ impl Team { } pub fn average_rating(&self, players: &[Player]) -> Rating { - if self.players.len() == 0 { + if self.players.is_empty() { return Rating::zero(); } From c89c6f92215b9298c5e133b6bd1d99030c18be1a Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:38:27 +0300 Subject: [PATCH 05/11] remove unnecessary conversion to Option --- src/bot/commands/lobby.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bot/commands/lobby.rs b/src/bot/commands/lobby.rs index ee8335d..c7ca645 100644 --- a/src/bot/commands/lobby.rs +++ b/src/bot/commands/lobby.rs @@ -307,7 +307,7 @@ impl LobbyCommand { .iter() .sorted_by(|((a, _), _), ((b, _), _)| i32::from(*a).cmp(&i32::from(*b))) .map(|(_, i)| async { - if let Some(user) = players[i.unwrap()].discord_id.to_user(ctx).await.ok() { + if let Ok(user) = players[i.unwrap()].discord_id.to_user(ctx).await { user.name } else { players[i.unwrap()] @@ -322,7 +322,7 @@ impl LobbyCommand { .iter() .sorted_by(|((a, _), _), ((b, _), _)| i32::from(*a).cmp(&i32::from(*b))) .map(|(_, i)| async { - if let Some(user) = players[i.unwrap()].discord_id.to_user(ctx).await.ok() { + if let Ok(user) = players[i.unwrap()].discord_id.to_user(ctx).await { user.name } else { players[i.unwrap()] From 593fb3b423b4dd96204f6455f80a984585bcbd9c Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:39:05 +0300 Subject: [PATCH 06/11] use map instead of manual mapping --- src/bot/commands/lobby.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/bot/commands/lobby.rs b/src/bot/commands/lobby.rs index c7ca645..f85da2c 100644 --- a/src/bot/commands/lobby.rs +++ b/src/bot/commands/lobby.rs @@ -608,25 +608,13 @@ impl LobbyCommand { .players .clone() .into_iter() - .filter_map(|((role, _), player)| { - if player.is_some() { - Some((role, player.unwrap())) - } else { - None - } - }) + .filter_map(|((role, _), player)| player.map(|id| (role, id))) .collect::>(); let team2 = team2 .players .clone() .into_iter() - .filter_map(|((role, _), player)| { - if player.is_some() { - Some((role, player.unwrap())) - } else { - None - } - }) + .filter_map(|((role, _), player)| player.map(|id| (role, id))) .collect::>(); let data = ctx.data.read().await; From 87788765d85a8a0aaed71fd02488ae4065ab1ede Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:39:31 +0300 Subject: [PATCH 07/11] replace into_iter with iter --- src/mixer/mixer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mixer/mixer.rs b/src/mixer/mixer.rs index 77cbc28..9feb36b 100644 --- a/src/mixer/mixer.rs +++ b/src/mixer/mixer.rs @@ -37,7 +37,7 @@ fn get_role_entries( fn get_combinations(entries: &[PlayerRoleEntry], count: usize) -> Vec> { entries - .into_iter() + .iter() .combinations(count) .sorted_by(|a, b| { let a = a.iter().map(|e| e.priority).sum::(); From 423bbb6c507ff6b2ab098ddd8735588fe4792290 Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:40:24 +0300 Subject: [PATCH 08/11] use is_some indead of let Some --- src/bot/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 1f9af32..6963ef3 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -64,9 +64,7 @@ impl EventHandler for MixerBot { let data = ctx.data.read().await; let db = data.get::().unwrap().read().await; - if let Some(_) = - LobbyQuery::lobby_by_channel_id(db.connection(), guild_id, channel_id).await - { + if LobbyQuery::lobby_by_channel_id(db.connection(), guild_id, channel_id).await.is_some() { if let Some(member) = new.member { if member.user.bot { return; From bcf63e6ab71fa25097c53134f86d29e044a3c099 Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:40:36 +0300 Subject: [PATCH 09/11] remove & --- src/bot/handlers/command_handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot/handlers/command_handler.rs b/src/bot/handlers/command_handler.rs index f12eec6..5363a27 100644 --- a/src/bot/handlers/command_handler.rs +++ b/src/bot/handlers/command_handler.rs @@ -42,7 +42,7 @@ impl MixerCommandHandler { interaction.user.id, command.name() ); - return command.execute(&ctx, interaction).await; + return command.execute(ctx, interaction).await; } else { info!( "User {} ({}) executed unknown command \"{}\"", From d57206973e1e913b7dde9ffd91affaf9b61f449c Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:41:03 +0300 Subject: [PATCH 10/11] simplify map --- src/bot/commands/lobby.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot/commands/lobby.rs b/src/bot/commands/lobby.rs index f85da2c..0cb35c6 100644 --- a/src/bot/commands/lobby.rs +++ b/src/bot/commands/lobby.rs @@ -263,7 +263,7 @@ impl LobbyCommand { let players = players .into_iter() - .map(|p| Player::new(p)) + .map(Player::new) .collect::>(); let slots = vec![ Role::Tank, From b83c9af73dc7955c4295f35c9bf5f0bacfea99b6 Mon Sep 17 00:00:00 2001 From: Lionarius Date: Wed, 26 Apr 2023 11:43:13 +0300 Subject: [PATCH 11/11] replace boundary check with range contains --- src/bot/commands/rating.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot/commands/rating.rs b/src/bot/commands/rating.rs index 64c16d2..30d7f2f 100644 --- a/src/bot/commands/rating.rs +++ b/src/bot/commands/rating.rs @@ -126,7 +126,7 @@ impl MixerCommand for RatingCommand { .as_u64() .unwrap(); - if rating < 1 || rating > 5000 { + if !(1..=5000).contains(&rating) { interaction .create_interaction_response(ctx, |response| { response