Merge pull request #3 from Suiranoil/master

sync dev with master
This commit is contained in:
Suiranoil
2023-04-26 11:51:19 +03:00
committed by GitHub
12 changed files with 42 additions and 60 deletions

View File

@@ -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(());
@@ -263,12 +263,12 @@ impl LobbyCommand {
let players = players
.into_iter()
.map(|p| Player::new(p))
.map(Player::new)
.collect::<Vec<Player>>();
let slots = vec![
Role::Tank,
Role::DPS,
Role::DPS,
Role::Dps,
Role::Dps,
Role::Support,
Role::Support,
];
@@ -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()]
@@ -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
)
};
@@ -607,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::<Vec<_>>();
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::<Vec<_>>();
let data = ctx.data.read().await;

View File

@@ -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?;

View File

@@ -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?;
@@ -126,14 +126,14 @@ impl MixerCommand for RatingCommand {
.as_u64()
.unwrap();
if rating < 1 || rating > 5000 {
if !(1..=5000).contains(&rating) {
interaction
.create_interaction_response(ctx, |response| {
response
.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)
})
})

View File

@@ -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(),

View File

@@ -44,7 +44,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 \"{}\"",

View File

@@ -64,9 +64,7 @@ impl EventHandler for MixerBot {
let data = ctx.data.read().await;
let db = data.get::<DatabaseContainer>().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;

View File

@@ -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<Role> 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<Self, Self::Error> {
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<Role> for i32 {
fn from(role: Role) -> Self {
match role {
Role::Tank => 0,
Role::DPS => 1,
Role::Dps => 1,
Role::Support => 2,
}
}

View File

@@ -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);

View File

@@ -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<String>, teams_rating: [i32; 2]) -> Vec<u8> {
pub fn draw_teams_to_vec(&self, player_names: Vec<String>, teams_rating: [i32; 2], format: ImageOutputFormat) -> Vec<u8> {
let mut image: image::ImageBuffer<image::Rgb<u8>, Vec<u8>> = 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()
}
}

View File

@@ -30,14 +30,14 @@ fn get_role_entries(
let (tanks, rest) = entries
.into_iter()
.partition::<Vec<_>, _>(|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)
}
fn get_combinations(entries: &[PlayerRoleEntry], count: usize) -> Vec<Vec<&PlayerRoleEntry>> {
entries
.into_iter()
.iter()
.combinations(count)
.sorted_by(|a, b| {
let a = a.iter().map(|e| e.priority).sum::<f32>();
@@ -55,7 +55,7 @@ pub fn mix_players(players: &[Player], slots: Vec<Role>) -> 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);

View File

@@ -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),
),
(
@@ -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);
}
}

View File

@@ -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()
}
@@ -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();
}
@@ -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()
}