Skip to content

Commit

Permalink
re-implement evaluator using weighted kda from config
Browse files Browse the repository at this point in the history
  • Loading branch information
briankoehler committed Jan 8, 2025
1 parent d4cb21c commit f64c89e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 105 deletions.
3 changes: 3 additions & 0 deletions bin/bot/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::evaluator::MatchStatsEvaluator;
use serde::Deserialize;
use std::path::{Path, PathBuf};
use tokio::fs::read_to_string;
Expand All @@ -8,6 +9,8 @@ pub struct Config {
pub discord_token: String,
pub rgapi_key: String,
pub message_templates_path: PathBuf,
// TODO: Consider making this also a path
pub match_stats_evaluator: MatchStatsEvaluator,
}

impl Config {
Expand Down
103 changes: 0 additions & 103 deletions bin/bot/src/evaluator.rs

This file was deleted.

55 changes: 55 additions & 0 deletions bin/bot/src/evaluator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use serde::Deserialize;
use std::collections::HashMap;
use the_collector_db::model::{Match, SummonerMatch};
use weight::{WeightedKda, Weights};

pub mod weight;

#[derive(Debug, PartialEq, Eq, Hash, Deserialize)]
#[serde(rename_all = "snake_case")]
enum Role {
Top,
Jungle,
Mid,
Bot,
Support,
Other,
}

impl From<&str> for Role {
fn from(value: &str) -> Self {
// Match to values from Riot API
match value.to_lowercase().as_str() {
"top" => Role::Top,
"jungle" => Role::Jungle,
"middle" => Role::Mid,
"bottom" => Role::Bot,
"utility" => Role::Support,
_ => Role::Other,
}
}
}

#[derive(Debug, Deserialize, Default)]
pub struct MatchStatsEvaluator {
kda_weights: HashMap<Role, Weights>,
kda_limit: WeightedKda,
}

impl MatchStatsEvaluator {
pub fn is_int(&self, match_stats: &SummonerMatch, _match_data: &Match) -> bool {
// Don't send a message for insignificant scoreline
if match_stats.deaths <= 4 && match_stats.kills >= 1 {
return false;
}

// If their weighted KDA is less than <= 0, evaluate as an int
let role = match_stats
.position
.as_ref()
.map(String::as_str)
.unwrap_or("other")
.into();
self.kda_weights[&role].calculate_weighted_kda(match_stats) <= self.kda_limit
}
}
21 changes: 21 additions & 0 deletions bin/bot/src/evaluator/weight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use serde::Deserialize;
use the_collector_db::model::SummonerMatch;

#[derive(Debug, PartialEq, PartialOrd, Deserialize, Default)]
pub struct WeightedKda(pub f32);

#[derive(Debug, PartialEq, PartialOrd, Deserialize)]
pub struct Weights {
kill_weight: f32,
death_weight: f32,
assist_weight: f32,
}

impl Weights {
pub fn calculate_weighted_kda(&self, stats: &SummonerMatch) -> WeightedKda {
let inner = (self.kill_weight * stats.kills as f32)
+ (self.death_weight * stats.deaths as f32)
+ (self.assist_weight * stats.assists as f32);
WeightedKda(inner)
}
}
3 changes: 1 addition & 2 deletions bin/bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::Context as _;
use command::Data;
use config::Config;
use ddragon::DataDragon;
use evaluator::MatchStatsEvaluator;
use handler::bot::BotHandler;
use handler::message::MessageHandler;
use message::MessageBuilder;
Expand Down Expand Up @@ -81,7 +80,7 @@ async fn main() -> anyhow::Result<()> {
let summoner_match_handler = MessageHandler {
db_handler: db_handler.clone(),
subscriber: IpcSubscriber::new(IPC_SUMMONER_MATCH_PATH)?,
evaluator: MatchStatsEvaluator::new(),
evaluator: config.match_stats_evaluator,
message_builder: MessageBuilder::new(config.message_templates_path).await?,
http: client.http.clone(),
};
Expand Down

0 comments on commit f64c89e

Please sign in to comment.