tibiamaps / tibia-kill-stats

4 stars 2 forks source link

proposal: add total data per world #19

Open rmobis opened 5 months ago

rmobis commented 5 months ago

I propose we add <world-name>/_total.json files which would include the sum of killed bosses/players for that given world.

This could be useful in a few scenarios, but the one I used it for is to analyze how my world compares to other worlds in regards to boss hunting and analyzing which ones are easier and which ones are harder to find. GuildStats provides such information in their bosses page for that world or in the specific boss page, but they don't track as many creatures and bosses as this repository does.

I've quickly hacked the code below to alleviate my immediate needs, but it could be very easily be polished and adapted to do a complete job for all worlds. Let me know if you're interested and I'll work on a PR.

Initial Hacky Implementation

import fs from 'node:fs/promises';
import glob from 'glob';
import { toPrettyName } from './normalize-names.mjs';

const fileNames = glob.sync('./data/etebra/*.json', {
    ignore: [
        './data/etebra/latest.json',
    ],
});

// race => totalKilled
const map = new Map();

for (const fileName of fileNames) {
    const json = await fs.readFile(fileName, 'utf8');
    const stats = JSON.parse(json).killstatistics.entries;

    for (const {race, last_day_killed} of stats) {
        if (last_day_killed <= 0) continue;

        map.set(race, (map.get(race) ?? 0) + last_day_killed);
    }
}

const entries = [...map.entries()];
const sorted = entries
    .filter(([_race, kills]) => kills > 0)
    .map(([race, kills]) => {
        return [toPrettyName(race), kills];
    })
    .sort((a, b) => {
        if (a[1] === b[1]) {
            return a[0].localeCompare(b[0]);
        }

        return a[1] - b[1];
    });

{
    const object = Object.fromEntries(sorted);
    const json = JSON.stringify(object, null, '\t');
    await fs.writeFile('./data/etebra/_total.json', `${json}\n`);
}
mathiasbynens commented 4 months ago

Interested!

mathiasbynens commented 4 months ago

Rather than a standalone script, we should integrate this into https://github.com/tibiamaps/tibia-kill-stats/blob/main/analyze-global-total-kills.mjs to avoid reading all the same files from disk again.