Skip to content

Commit

Permalink
fix(smogontiers): fix updater and update tiers manually
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed May 3, 2020
1 parent ec7bf6c commit c4593c0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 75 deletions.
2 changes: 1 addition & 1 deletion scripts/smogonTiersData.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"lastSha":"46b4ad47685518043daea4a4081147019a321ede"}
{ "lastSha": "28bf388b98cd5cc83a696a09befd7ece89a9e5e1" }
12 changes: 6 additions & 6 deletions scripts/updateSmogonTiers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-console */
import fetch from 'node-fetch';
import { constants, Timestamp } from '@klasa/timestamp';
import { readJSON, writeJSONAtomic } from 'fs-nextra';
import fetch from 'node-fetch';
import { join } from 'path';
import { constants, Timestamp } from '@klasa/timestamp';
import { needFile, Formats, DataJSON } from './utils';
import { DataJSON, needFile } from './utils';

const DATA_FILE = join(__dirname, 'smogonTiersData.json');
const FORMATS_FILE = join(__dirname, '../src/assets/formats.json');
Expand All @@ -21,7 +21,7 @@ const TIMESTAMP = new Timestamp('YYYY-MM-DD[T]HH:mm:ssZ').display(TEN_DAYS_AGO);
const [commits, { lastSha }] = await Promise.all([request.json(), UPDATED_FORMATS_DATA]);

const data = { sha: commits.length ? commits[0].sha : null, length: commits.length };
const output: Formats = {};
const output: Record<string, string> = {};
if (!data) {
console.log('no data from request');

Expand All @@ -34,11 +34,11 @@ const TIMESTAMP = new Timestamp('YYYY-MM-DD[T]HH:mm:ssZ').display(TEN_DAYS_AGO);
return process.exit(0);
}

const { BattleFormatsData } = await needFile('https://raw.githubusercontent.com/smogon/pokemon-showdown/master/data/formats-data.js');
const { BattleFormatsData } = await needFile('https://raw.githubusercontent.com/smogon/pokemon-showdown/master/data/formats-data.ts');

for (const mon in BattleFormatsData) {
const tier = BattleFormatsData[mon].isNonstandard || BattleFormatsData[mon].tier;
output[mon] = tier;
output[mon] = tier || 'Refer to base form / unknown';
}

const writePromises: Promise<void>[] = [];
Expand Down
76 changes: 53 additions & 23 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,67 @@
import { writeFileAtomic, remove } from 'fs-nextra';
import fetch from 'node-fetch';
import { dirname } from 'path';
import { resolve } from 'path';
import ts from 'typescript';

export const mapToJson = (map: Map<string, unknown>) => JSON.stringify([...map]);

export const needFile = async (url: string) => {
const NodeModule: Module = module.constructor as Module;
const request = await fetch(url);
const body: string = await request.text();
const nodeModule = new NodeModule(url, module.parent);
nodeModule.fileName = url;
/* eslint-disable no-underscore-dangle */
nodeModule.paths = NodeModule._nodeModulePaths(dirname(url));
nodeModule._compile(body, url);

/* eslint-enable no-underscore-dangle */
return nodeModule.exports;
};
const body = await request.text();

export interface TierEntry {
tier: string;
}
const result = ts.transpileModule(body, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
newLine: ts.NewLineKind.LineFeed,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
target: ts.ScriptTarget.ES2019,
removeComments: true,
declaration: false,
sourceMap: false,
declarationMap: false,
incremental: false,
importHelpers: false
}
});

const temporaryOutputFile = resolve(__dirname, 'tiers.ts');

await writeFileAtomic(temporaryOutputFile, result.outputText);
const TiersData = (await import(resolve(__dirname, 'tiers.ts'))) as { BattleFormatsData: Record<string, SpeciesFormatsData> };

await remove(temporaryOutputFile);

return TiersData;
};

export interface DataJSON {
lastSha: string;
}

export type Formats = Record<string, TierEntry>;

interface Module extends Function {
/* eslint-disable @typescript-eslint/no-explicit-any */
_nodeModulePaths: any;

new (url: string, parents: NodeModule | null): any;
interface SpeciesFormatsData {
comboMoves?: readonly string[];
doublesTier?: string;
essentialMove?: string;
exclusiveMoves?: readonly string[];
isNonstandard?: Nonstandard | null;
maleOnlyHidden?: boolean;
randomBattleMoves?: readonly string[];
randomDoubleBattleMoves?: readonly string[];
randomSets?: readonly Gen2RandomSet[];
tier?: string;
unreleasedHidden?: boolean | 'Past';
}

/* eslint-enable @typescript-eslint/no-explicit-any */
type Nonstandard = 'Past' | 'Future' | 'Unobtainable' | 'CAP' | 'LGPE' | 'Custom';
interface Gen2RandomSet {
chance: number;
item?: string[];
baseMove1?: string;
baseMove2?: string;
baseMove3?: string;
baseMove4?: string;
fillerMoves1?: string[];
fillerMoves2?: string[];
fillerMoves3?: string[];
fillerMoves4?: string[];
}
Loading

0 comments on commit c4593c0

Please sign in to comment.