pokemon-showdown/tools/set-import/stats
Guangcong Luo 13189fdb02
Update Dex API (#8181)
This is the change that renames:

- `Dex.getMove` -> `Dex.moves.get`
- `Dex.getAbility` -> `Dex.abilities.get`
- `Dex.getItem` -> `Dex.items.get`
- `Dex.getSpecies` -> `Dex.species.get`
- `Dex.getEffect` -> `Dex.conditions.get`
- `Dex.getNature` -> `Dex.natures.get`
- `Dex.getType` -> `Dex.types.get`
- `Dex.getFormat` -> `Dex.formats.get`

In addition, some other APIs have been updated:

- `getByID` methods have also been added to every other table.
- `Dex.moves.all()` now gets an array of all moves
  - Plus equivalent methods for `abilities`, `items`, `species`, `formats`, `natures`, `types`
  - Note: there's no `Dex.conditions.all()`
- new API: `Dex.stats` for naming/iterating stats
- `Dex.getEffectByID` -> `Dex.conditions.getByID`
- `Dex.getType` -> `Dex.types.get`
- `Dex.data.Formats` -> `Dex.data.Rulesets`
- `Dex.formats` -> now an array `Dex.formats.all()`
- `Dex.getRuleTable` -> `Dex.formats.getRuleTable`
- `Dex.validateFormat` -> `Dex.formats.validate`

Team functions have been split off into a new `sim/teams` package:

- `Dex.packTeam` -> `Teams.pack`
- `Dex.fastUnpackTeam` -> `Teams.unpack`
- `Dex.generateTeam` -> `Teams.generate`
- `Dex.stringifyTeam` -> `Teams.export`

`Teams.export` has also been rewritten to better match how it works in client.

This implements #8178
2021-04-08 03:00:37 -07:00

53 lines
1.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
// Determines total battle counts per format since the beginning of stats
// collection in order to determine what the fallback dates for the importer's
// `STATISTICS` map should be.
'use strict';
const Dex = require('../../.sim-dist/dex').Dex;
global.toID = Dex.getId;
const smogon = require('smogon');
const importer = require('./importer');
const formats = new Map();
for (let gen = 1; gen <= 8; gen++) {
for (const tier of importer.TIERS) {
const format = Dex.formats.get(`gen${gen}${tier}`);
if (format.exists) {
formats.set(format.id, {});
}
}
}
(async () => {
const index = await importer.fetch(smogon.Statistics.URL);
const begin = new Date('Nov 2014');
const end = new Date(smogon.Statistics.latest(index));
end.setDate(end.getDate() + 1);
for (const d = begin; d <= end; d.setMonth(d.getMonth() + 1)) {
const month = `${d.getMonth() + 1}`.padStart(2, '0');
const date = `${1900 + d.getYear()}-${month}`;
for (const format of formats.keys()) {
try {
// The JSON files are quite large and needing to download and parse them to then
// extract the 'number of battles' field is much slower than instead grabbing the
// basic stats file and doing the comparatively cheap regex search.
const url = smogon.Statistics.url(date, format, 0).replace('chaos/', '').replace('.json', '.txt');
const usage = await importer.fetch(url);
if (usage) {
// https://www.smogon.com/stats/2016-10/cap-*.txt is invalid and doesn't match... *sigh*
const m = usage.match(/^ Total battles: (.*)/);
if (m) formats.get(format)[date] = Number(m[1]);
}
} catch (err) {
if (!err.message.startsWith('HTTP 404')) throw err;
}
}
}
console.log(JSON.stringify(Array.from(formats.entries()), null, 2));
})().catch(err => console.error(err));