Skip to content

Commit

Permalink
Speed up delta calculation to a static one
Browse files Browse the repository at this point in the history
  • Loading branch information
KwikKill committed Jan 12, 2024
1 parent 609e6b0 commit 720d299
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
37 changes: 16 additions & 21 deletions commands/lol.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,23 +1293,7 @@ async function stats_summarized(client, interaction, discordaccount, champion, r
const query_values = [discordaccount];
const query_values2 = [discordaccount];

let query = "WITH COEF AS (" +
"SELECT champion, " +
"200/(carry+wr+kp+vs*25+10*cs) AS score " +
"FROM (" +
"SELECT champion, " +
"count(*), " +
"(count(*) FILTER (WHERE result = 'Win')*100.0/count(*)) as WR, " +
"(count(*) FILTER (WHERE (first_gold OR first_damages OR first_tanked))*100.0/count(*)) as CARRY, " +
"(avg(kill)+avg(assists))*100.0/avg(total_kills) as KP, " +
"cast(avg(vision_score) as float)/(avg(length)/60) as VS, " +
"cast(avg(cs) as float)/(avg(length)/60) as CS, " +
"(count(*) FILTER (WHERE first_gold AND first_damages AND first_tanked)*100.0/count(*)) as hardcarry " +
"FROM matchs " +
"GROUP BY champion " +
") AS t1 " +
")" +
"SELECT " +
let query = "SELECT " +
"AVG(gold) as avg_gold, " +
"AVG(kill) as avg_kills, " +
"AVG(deaths) as avg_deaths, " +
Expand All @@ -1331,9 +1315,8 @@ async function stats_summarized(client, interaction, discordaccount, champion, r
"SUM(CASE WHEN first_damages THEN 1 ELSE 0 END)*100.0 / count(*) as carry_damage, " +
"SUM(CASE WHEN first_tanked THEN 1 ELSE 0 END)*100.0 / count(*) as carry_tanked, " +
"SUM(CASE WHEN (first_gold AND first_damages AND first_tanked) THEN 1 ELSE 0 END)*100.0 / count(*) as hard_carry, " +
"SUM(CASE WHEN result = 'Win' THEN 1 ELSE 0 END)*100.0 / count(*) as win_rate, " +
"avg(score) as delta " +
"FROM summoners LEFT JOIN (matchs LEFT JOIN COEF ON COEF.champion = matchs.champion) ON matchs.player = summoners.puuid";
"SUM(CASE WHEN result = 'Win' THEN 1 ELSE 0 END)*100.0 / count(*) as win_rate " +
"FROM summoners LEFT JOIN matchs ON matchs.player = summoners.puuid";

let query3 = "SELECT " +
"gamemode, " +
Expand Down Expand Up @@ -1446,7 +1429,6 @@ async function stats_summarized(client, interaction, discordaccount, champion, r
const overall = Number(response.rows[0].carry);
const hard_carry = Number(response.rows[0].hard_carry);
const win = Number(response.rows[0].win_rate);
const delta = Number(response.rows[0].delta);

// 2) Average stats

Expand All @@ -1468,6 +1450,19 @@ async function stats_summarized(client, interaction, discordaccount, champion, r
average_damages = (average_damages / (length / 60)).toFixed(decimal);
average_tanked = (average_tanked / (length / 60)).toFixed(decimal);

// delta calculation

let delta = 0;
const champions_played = await client.pg.query({
name: "get-champions-played",
text: "SELECT champion FROM matchs, summoners WHERE matchs.player = summoners.puuid AND discordid=$1;",
values: [discordaccount]
});
for (let i = 0; i < champions_played.rows.length; i++) {
delta += client.lol.score[champions_played.rows[i].champion];
}
delta /= champions_played.rows.length;

// KwikScore

let score = 0;
Expand Down
12 changes: 12 additions & 0 deletions timers/lol.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ module.exports = {
console.log("[Rank Up] skipped update, already running");
return;
}
// if score update hasn't been done since 48h, do it
if (client.lol.scoretimestamp + 172800000 < Date.now()) {
if (config.verbose) {
console.log("[Rank Up] updating score...");
}
await client.channels.cache.get("1100720550923489280").send("[Rank Up] updating score...");
await client.lol.calculate_champion_score(client);
await client.channels.cache.get("1100720550923489280").send("[Rank Up] updating score...");
if (config.verbose) {
console.log("[Rank Up] score updated");
}
}
await client.commands.get("adminlol").update(client);
if (config.verbose) {
console.log("lol timer done");
Expand Down
36 changes: 36 additions & 0 deletions util/lol_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ module.exports = {
queue_length: 0,
api_limit: false,

scores: {},
score_timestamp: 0,

trackers: [],

/**
Expand Down Expand Up @@ -1456,5 +1459,38 @@ module.exports = {
}
}
});
},

/**
* Calculate the score of each champions
* @function calculate_champion_score
*/
async calculate_champion_score() {
// run the query to get the score of each champion
const response = await this.client.pg.query({
name: "get_champion_score",
text: "SELECT champion, " +
"200/(carry+wr+kp+vs*25+10*cs) AS score " +
"FROM (" +
"SELECT champion," +
"count(*)," +
"(count(*) FILTER (WHERE result = 'Win')*100.0/count(*)) as WR, " +
"(count(*) FILTER (WHERE (first_gold OR first_damages OR first_tanked))*100.0/count(*)) as CARRY, " +
"(avg(kill)+avg(assists))*100.0/avg(total_kills) as KP, " +
"cast(avg(vision_score) as float)/(avg(length)/60) as VS, " +
"cast(avg(cs) as float)/(avg(length)/60) as CS, " +
"(count(*) FILTER (WHERE first_gold AND first_damages AND first_tanked)*100.0/count(*)) as hardcarry " +
"FROM matchs " +
"GROUP BY champion " +
") AS t1"
});

// store the result in a dictionnary
for (const row of response.rows) {
this.scores[row["champion"]] = row["score"];
}
this.scoretimestamp = Date.now();

return;
}
};

0 comments on commit 720d299

Please sign in to comment.