Skip to content

Commit

Permalink
More metric stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Strikeeaglechase committed Jul 15, 2024
1 parent 315697b commit 842969c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 49 deletions.
65 changes: 18 additions & 47 deletions src/elo/utils/eloForTopOverTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,27 @@ import { EloEvent } from "../eloBackUpdater.js";
import { ProdDBBackUpdater } from "./eloUtils.js";

class TopEloOverTimeUpdater extends ProdDBBackUpdater {
private currentTopElo: number = 2000;
private currentTopUser: User;

public history: Record<string, number> = {};
public historyAvg: Record<string, number> = {};
public topTenElosOverTime: Record<string, number[]> = {};
public numUsersOvert2000: Record<string, number> = {};

private currentDay: string;

private topPlayers: User[] = [];
public log: string[] = [];

protected override onUserUpdate(user: User, event: EloEvent, eloDelta: number): void {
if (event.type != "kill") return;
if (!this.currentTopUser) this.currentTopUser = user;
if (!this.currentDay) this.currentDay = this.day(event.time);

if (user.id != this.currentTopUser.id) {
if (user.elo > this.currentTopElo) {
this.currentTopElo = user.elo;
this.currentTopUser = user;
}
} else {
if (event.event.killer.ownerId == this.currentTopUser.id) {
this.currentTopElo = user.elo;
} else {
// Top user died, find new top user
const possibleUsers = this.users.filter(u => u.elo > this.currentTopElo - 500);
let newTopUser: User;
let newTopElo = 0;
for (const u of possibleUsers) {
if (u.elo > newTopElo) {
newTopElo = u.elo;
newTopUser = u;
}
}

this.currentTopElo = newTopElo;
this.currentTopUser = newTopUser;
}
if (this.topPlayers.includes(user)) {
this.topPlayers.sort((a, b) => b.elo - a.elo);
let logStr = `${new Date(event.time).toISOString()}`;
this.topPlayers.forEach(u => {
logStr += `,${u.elo}`;
});
this.log.push(logStr);
}

this.pushHistory(event.time);

const day = this.day(event.time);
if (day != this.currentDay) {
this.currentDay = day;
Expand All @@ -55,14 +35,10 @@ class TopEloOverTimeUpdater extends ProdDBBackUpdater {

private updateHistoryAvg() {
this.users.sort((a, b) => b.elo - a.elo);
const topUsers = this.users.slice(0, 10);
let sum = 0;
for (const u of topUsers) {
sum += u.elo;
}
sum /= topUsers.length;

this.historyAvg[this.currentDay] = sum;
const topUsers = this.users.slice(0, 20);
const topElos = topUsers.map(u => u.elo);
this.topTenElosOverTime[this.currentDay] = topElos;
this.topPlayers = topUsers;

const numUsersOvert2000 = this.users.filter(u => u.elo > 2000).length;
this.numUsersOvert2000[this.currentDay] = numUsersOvert2000;
Expand All @@ -71,16 +47,10 @@ class TopEloOverTimeUpdater extends ProdDBBackUpdater {
private day(time: number) {
return new Date(time).toISOString().substring(0, 10);
}

private pushHistory(time: number) {
const ts = new Date(time).toISOString().substring(0, 10);
this.history[ts] = this.currentTopElo;
}
}

async function run() {
const updater = new TopEloOverTimeUpdater();
updater.history["2023-04-11"] = 2000;
await updater.runBackUpdate();

let result = "";
Expand All @@ -89,12 +59,13 @@ async function run() {
});

let resultAvg = "";
Object.entries(updater.historyAvg).forEach(([ts, elo]) => {
resultAvg += `${ts},${elo}\n`;
Object.entries(updater.topTenElosOverTime).forEach(([ts, elo]) => {
resultAvg += `${ts},${elo.join(",")}\n`;
});

fs.writeFileSync("../../../out.csv", result);
fs.writeFileSync("../../../out-avg.csv", resultAvg);
fs.writeFileSync("../../../out-big.csv", updater.log.join("\n"));
}

run();
12 changes: 10 additions & 2 deletions src/elo/utils/eloUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "fs";
import path from "path";

import Database from "../../db/database.js";
import { Death, Kill } from "../../structures.js";
import { Death, Kill, User } from "../../structures.js";
import { EloBackUpdater } from "../eloBackUpdater.js";

config({ path: "../../.env" });
Expand Down Expand Up @@ -40,6 +40,7 @@ class ComparisonUpdater extends ProdDBBackUpdater {
// Compare the locally computed elo to the current elo in the database
const currentDbUsers = await this.userDb.collection.find({ $or: [{ deaths: { $gt: 0 } }, { elo: { $ne: 2000 } }] }).toArray();
console.log(`Loaded ${currentDbUsers.length} users from the database.`);
const results: { user: User; oldElo: number; newElo: number }[] = [];
currentDbUsers.forEach(user => {
const localUser = this.usersMap[user.id];
if (!localUser) {
Expand All @@ -50,8 +51,15 @@ class ComparisonUpdater extends ProdDBBackUpdater {
if (Math.abs(localUser.elo - user.elo) > 25) {
console.log(`${user.pilotNames[0]} (${user.id}). ${user.elo.toFixed(0)} -> ${localUser.elo.toFixed(0)}`);
}

results.push({ user: user, oldElo: user.elo, newElo: localUser.elo });
});

results.sort((a, b) => b.newElo - a.newElo);
for (let i = 0; i < 30; i++) {
console.log(`${i + 1}) ${results[i].user.pilotNames[0]} (${results[i].user.id}). ${results[i].oldElo.toFixed(0)} -> ${results[i].newElo.toFixed(0)}`);
}

const u = this.usersMap["76561199442641427"];
fs.writeFileSync("../../out-log.txt", u.history.join("\n"));
}
Expand Down Expand Up @@ -108,5 +116,5 @@ async function runComparison() {
export { ProdDBBackUpdater };

// getInterestingMetrics();
runComparison();
// runComparison();
// pullOfflineLoad();

0 comments on commit 842969c

Please sign in to comment.