From cbee4612a27feca45f7eb655676937294cd06001 Mon Sep 17 00:00:00 2001 From: TurtIeSocks <58572875+TurtIeSocks@users.noreply.github.com> Date: Sun, 26 Sep 2021 15:40:10 -0400 Subject: [PATCH 1/2] Custom Invasions - Add support for adding invasions not found on Pogoinfo - Merge invasion method - Moved merge template method into Masterfile class as a static method - Updated readme - Cleaned up typings to be more accurate for the template - Cleaned up tsconfig --- README.md | 10 ++- src/base.json | 3 +- src/classes/Invasion.ts | 27 +++++++- src/classes/Masterfile.ts | 44 +++++++++++- src/classes/Translations.ts | 2 +- src/index.ts | 58 +++------------- src/typings/inputs.ts | 133 ++++++++++++++++++++---------------- src/typings/pogoinfo.ts | 12 ++-- tsconfig.json | 85 ++++------------------- 9 files changed, 185 insertions(+), 189 deletions(-) diff --git a/README.md b/README.md index 026a66a..57bc2aa 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,8 @@ This is the full template example with notes on each field. The default template Invasions function only needs the invasion template object +Custom invasions can be added by setting the customInvasions object using the same format as PogoInfo's grunts JSON. + ```js const template = { globalOptions: { @@ -178,7 +180,12 @@ const template = { skipNormalIfUnset: false, // If form is unset, Normal form will be skipped skipForms: [], // Can be used to skip forms, such as Shadow/Purified includeProtos: true, // Adds unreleased forms from the protos - includeEstimatedPokemon: true, // Includes mega info for Mega Evos that do not officially exist in Pogo as well as Pokemon data from the PokeAPI for Pokemon that are in the protos but not in the GM yet. + includeEstimatedPokemon: { + // Includes mega info for Mega Evos that do not officially exist in Pogo as well as Pokemon data from the PokeAPI for Pokemon that are in the protos but not in the GM yet.baseStats: true, + mega: true, + primal: true, + gmax: true, + }, processFormsSeparately: false, // Full Pokemon obj for each form includeRawForms: false, // Returns a "forms" obj with all individual forms includeUnset: false, //includes Pokemon that have unset forms @@ -556,6 +563,7 @@ const template = { genderString: false, placeholderData: false, snake_case: false, + customInvasions: {}, }, template: { id: false, diff --git a/src/base.json b/src/base.json index 27371b4..3c08000 100644 --- a/src/base.json +++ b/src/base.json @@ -259,7 +259,8 @@ "second": "second", "third": "third" }, - "placeholderData": true + "placeholderData": true, + "customInvasions": {} }, "template": { "id": false, diff --git a/src/classes/Invasion.ts b/src/classes/Invasion.ts index 6cdb7e3..764383e 100644 --- a/src/classes/Invasion.ts +++ b/src/classes/Invasion.ts @@ -15,6 +15,31 @@ export default class Invasion extends Masterfile { this.parsedInvasions = {} } + async customInvasions(): Promise { + if (this.options.customInvasions === true) { + return this.fetch('https://raw.githubusercontent.com/WatWowMap/Masterfile-Generator/master/custom-invasions.json') + } else if (this.options.customInvasions) { + return this.options.customInvasions as InvasionInfo + } else { + return {} + } + } + + mergeInvasions(existing: InvasionInfo, custom: InvasionInfo = {}) { + const invasions = existing + Object.entries(custom).forEach(([key, info]) => { + if (invasions[key] === undefined) { + invasions[key] = info + } else { + invasions[key] = { + ...invasions[key], + ...info, + } + } + }) + return invasions + } + formatGrunts(character: string) { const base = character.replace('CHARACTER_', '').replace('_MALE', '').replace('_FEMALE', '') const type = base.replace('EXECUTIVE_', '').replace('_GRUNT', '').replace('EVENT_', '') @@ -30,7 +55,7 @@ export default class Invasion extends Masterfile { } } - invasions(invasionData: { [id: string]: InvasionInfo }) { + invasions(invasionData: InvasionInfo) { Object.entries(Rpc.EnumWrapper.InvasionCharacter).forEach(proto => { try { const [name, id] = proto diff --git a/src/classes/Masterfile.ts b/src/classes/Masterfile.ts index 01253ab..d3d904a 100644 --- a/src/classes/Masterfile.ts +++ b/src/classes/Masterfile.ts @@ -1,6 +1,6 @@ import Fetch from 'node-fetch' -import { Options } from '../typings/inputs' +import { Options, FullTemplate } from '../typings/inputs' import { FinalResult } from '../typings/dataTypes' export default class Masterfile { customFieldNames: { [id: string]: string } @@ -14,6 +14,44 @@ export default class Masterfile { } } + static templateMerger(template: { [key: string]: any }, base: FullTemplate): FullTemplate { + const baseline: { [key: string]: any } = base + const merged: { [key: string]: any } = {} + Object.keys(base).forEach(category => { + merged[category] = template[category] || {} + Object.keys(baseline[category]).forEach(subKey => { + if (merged[category][subKey] === undefined) { + merged[category][subKey] = + typeof baseline[category][subKey] === 'boolean' ? false : baseline[category][subKey] + } + }) + if (category !== 'globalOptions') { + const globalOptions = template.globalOptions || baseline.globalOptions + Object.entries(globalOptions).forEach(option => { + const [optionKey, optionValue] = option + if (merged[category].options[optionKey] === undefined) { + if (template.globalOptions) { + merged[category].options[optionKey] = optionValue + } else { + merged[category].options[optionKey] = typeof optionValue === 'boolean' ? false : optionValue + } + } + }) + } + if (category === 'translations' && template.translations) { + merged.translations.options.questVariables = { + ...base.translations.options.questVariables, + ...template.translations.options.questVariables, + } + merged.translations.options.prefix = { + ...base.translations.options.prefix, + ...template.translations.options.prefix, + } + } + }) + return merged + } + async fetch(url: string): Promise { return new Promise(resolve => { Fetch(url) @@ -44,7 +82,7 @@ export default class Masterfile { } } catch (e) { console.warn(e, '\n', string) - } + } } } @@ -212,7 +250,7 @@ export default class Masterfile { // combines values together if parent objects have custom keys try { if (options.keys[key]) { - const split = options.keys[key].split(' ') + const split = (options.keys[key] as string).split(' ') let newKey = '' if (split.length === 1) { newKey = data[split[0]] diff --git a/src/classes/Translations.ts b/src/classes/Translations.ts index d467c88..f4f586b 100644 --- a/src/classes/Translations.ts +++ b/src/classes/Translations.ts @@ -240,7 +240,7 @@ export default class Translations extends Masterfile { languageRef(locale: string) { try { if (!this.reference) { - this.reference = this.parsedTranslations[this.options.useLanguageAsRef] + this.reference = this.parsedTranslations[(this.options.useLanguageAsRef as string)] } const languageRef: TranslationKeys = {} Object.keys(this.parsedTranslations[locale]).forEach(category => { diff --git a/src/index.ts b/src/index.ts index 440618a..5bf2f1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import * as fs from 'fs' +import Masterfile from './classes/Masterfile' import Pokemon from './classes/Pokemon' import Items from './classes/Item' import Moves from './classes/Move' @@ -11,48 +12,11 @@ import Translations from './classes/Translations' import PokeApi from './classes/PokeApi' import base from './base.json' -import { Input, FullTemplate, InvasionsOnly } from './typings/inputs' +import { Input, InvasionsOnly, PokemonTemplate, TranslationsTemplate } from './typings/inputs' import { FinalResult } from './typings/dataTypes' import { InvasionInfo } from './typings/pogoinfo' import { NiaMfObj } from './typings/general' -const templateMerger = (template: { [key: string]: any }): FullTemplate => { - const baseline: { [key: string]: any } = base - const merged: { [key: string]: any } = {} - Object.keys(base).forEach(category => { - merged[category] = template[category] || {} - Object.keys(baseline[category]).forEach(subKey => { - if (merged[category][subKey] === undefined) { - merged[category][subKey] = typeof baseline[category][subKey] === 'boolean' ? false : baseline[category][subKey] - } - }) - if (category !== 'globalOptions') { - const globalOptions = template.globalOptions || baseline.globalOptions - Object.entries(globalOptions).forEach(option => { - const [optionKey, optionValue] = option - if (merged[category].options[optionKey] === undefined) { - if (template.globalOptions) { - merged[category].options[optionKey] = optionValue - } else { - merged[category].options[optionKey] = typeof optionValue === 'boolean' ? false : optionValue - } - } - }) - } - if (category === 'translations' && template.translations) { - merged.translations.options.questVariables = { - ...base.translations.options.questVariables, - ...template.translations.options.questVariables, - } - merged.translations.options.prefix = { - ...base.translations.options.prefix, - ...template.translations.options.prefix, - } - } - }) - return merged -} - export async function generate({ template, url, test, raw, pokeApi }: Input = {}) { const start: number = new Date().getTime() const final: FinalResult = {} @@ -70,7 +34,7 @@ export async function generate({ template, url, test, raw, pokeApi }: Input = {} invasions, weather, translations, - } = templateMerger(template || base) + } = Masterfile.templateMerger(template || base, base) const localeCheck = translations.enabled && translations.options.masterfileLocale const AllPokemon = new Pokemon(pokemon.options) @@ -136,10 +100,10 @@ export async function generate({ template, url, test, raw, pokeApi }: Input = {} AllPokemon.parsePokeApi(await getDataSource('baseStats'), await getDataSource('tempEvos')) } - if (pokemon.template.little) { + if ((pokemon.template as PokemonTemplate).little) { AllPokemon.littleCup() } - if (pokemon.template.jungle) { + if ((pokemon.template as PokemonTemplate).jungle) { AllPokemon.jungleEligibility() } if (pokemon.options.processFormsSeparately) { @@ -152,11 +116,11 @@ export async function generate({ template, url, test, raw, pokeApi }: Input = {} AllMoves.protoMoves() } AllWeather.buildWeather() - if (invasions.enabled || translations.template.characters) { - const invasionData: { [id: string]: InvasionInfo } = await AllInvasions.fetch( + if (invasions.enabled || (translations.template as TranslationsTemplate).characters) { + const invasionData: InvasionInfo = await AllInvasions.fetch( 'https://raw.githubusercontent.com/ccev/pogoinfo/v2/active/grunts.json' ) - AllInvasions.invasions(invasionData) + AllInvasions.invasions(AllInvasions.mergeInvasions(invasionData, await AllInvasions.customInvasions())) } if (translations.enabled) { @@ -233,7 +197,7 @@ export async function generate({ template, url, test, raw, pokeApi }: Input = {} types: AllTypes.parsedTypes, weather: AllWeather.parsedWeather, }, - translations.options.masterfileLocale, + (translations.options.masterfileLocale as string), pokemon.options.processFormsSeparately ) } @@ -336,10 +300,10 @@ export async function generate({ template, url, test, raw, pokeApi }: Input = {} export async function invasions({ template, test }: InvasionsOnly = {}) { const finalTemplate = template || base.invasions const AllInvasions = new Invasions(finalTemplate.options) - const invasionData: { [id: string]: InvasionInfo } = await AllInvasions.fetch( + const invasionData: InvasionInfo = await AllInvasions.fetch( 'https://raw.githubusercontent.com/ccev/pogoinfo/v2/active/grunts.json' ) - AllInvasions.invasions(invasionData) + AllInvasions.invasions(AllInvasions.mergeInvasions(invasionData, await AllInvasions.customInvasions())) const final = AllInvasions.templater(AllInvasions.parsedInvasions, finalTemplate) if (test) { fs.writeFile('./invasions.json', JSON.stringify(final, null, 2), 'utf8', () => {}) diff --git a/src/typings/inputs.ts b/src/typings/inputs.ts index 38a0321..2457feb 100644 --- a/src/typings/inputs.ts +++ b/src/typings/inputs.ts @@ -1,7 +1,11 @@ +import { InvasionInfo } from './pogoinfo' + +type StringBool = string | boolean + export interface Options { topLevelName?: string keys?: { - [key: string]: string + [key: string]: StringBool } customFields?: { [key: string]: string @@ -34,21 +38,22 @@ export interface Options { | true minTrainerLevel?: number placeholderData?: boolean - masterfileLocale?: string + masterfileLocale?: StringBool manualTranslations?: boolean mergeCategories?: boolean processFormsSeparately?: boolean includeRawForms?: boolean includeBalloons?: boolean - useLanguageAsRef?: string + useLanguageAsRef?: StringBool includeUnset?: boolean unsetFormName?: string allUnset?: boolean pokeApiIds?: number[] noFormPlaceholders?: boolean + customInvasions?: InvasionInfo | boolean } -interface PokemonTemplate extends Form { +export interface PokemonTemplate extends Form { pokedexId?: boolean pokemonName?: boolean forms?: Form @@ -63,7 +68,7 @@ interface PokemonTemplate extends Form { mythic?: boolean buddyGroupNumber?: boolean buddyDistance?: boolean - buddyMegaEnergy?: number + buddyMegaEnergy?: boolean thirdMoveStardust?: boolean thirdMoveCandy?: boolean gymDefenderEligible?: boolean @@ -85,29 +90,31 @@ interface Form extends BaseStats { proto?: boolean formId?: boolean isCostume?: boolean - evolutions?: { - evoId?: boolean - formId?: boolean - genderRequirement?: boolean - candyCost?: boolean - itemRequirement?: boolean - tradeBonus?: boolean - mustBeBuddy?: boolean - onlyDaytime?: boolean - onlyNighttime?: boolean - questRequirement?: - | { - target?: boolean - assetsRef?: boolean - i18n?: boolean - questType?: boolean - translated?: boolean - } - | string - } - tempEvolutions?: TempEvolution | string - quickMoves?: Move | string - chargedMoves?: Move | string + evolutions?: + | { + evoId?: boolean + formId?: boolean + genderRequirement?: boolean + candyCost?: boolean + itemRequirement?: boolean + tradeBonus?: boolean + mustBeBuddy?: boolean + onlyDaytime?: boolean + onlyNighttime?: boolean + questRequirement?: + | { + target?: boolean + assetsRef?: boolean + i18n?: boolean + questType?: boolean + translated?: boolean + } + | StringBool + } + | StringBool + tempEvolutions?: TempEvolution | StringBool + quickMoves?: Move | StringBool + chargedMoves?: Move | StringBool family?: boolean little?: boolean } @@ -121,7 +128,7 @@ type Move = { typeId?: boolean type?: boolean } - | string + | StringBool } interface TempEvolution extends BaseStats { @@ -137,10 +144,12 @@ type BaseStats = { stamina?: boolean height?: boolean weight?: boolean - types?: { - typeId?: boolean - typeName?: boolean - } + types?: + | { + typeId?: boolean + typeName?: boolean + } + | StringBool } export interface TypesTempOpt { @@ -149,12 +158,12 @@ export interface TypesTempOpt { template?: TypesTemplate } -type TypesTemplate = { +export interface TypesTemplate { typeId?: boolean typeName?: boolean } -type MoveTemplate = { +export interface MoveTemplate { moveId?: boolean moveName?: boolean proto?: boolean @@ -163,11 +172,11 @@ type MoveTemplate = { typeId?: boolean typeName?: boolean } - | string + | StringBool power?: boolean } -type ItemTemplate = { +export interface ItemTemplate { itemId?: boolean itemName?: boolean proto?: boolean @@ -176,36 +185,40 @@ type ItemTemplate = { minTrainerLevel?: boolean } -type QuestTemplate = { +export interface QuestTemplate { id?: boolean proto?: boolean formatted?: boolean } -type InvasionTemplate = { +export interface InvasionTemplate { id?: boolean type?: boolean gender?: boolean grunt?: boolean secondReward?: boolean - encounters?: { - id: boolean - formId: boolean - position: boolean - } + encounters?: + | { + id: boolean + formId: boolean + position: boolean + } + | StringBool } -type WeatherTemplate = { +export interface WeatherTemplate { weatherId?: boolean weatherName?: boolean proto?: boolean - types?: { - typeId?: boolean - typeName?: boolean - } + types?: + | { + typeId?: boolean + typeName?: boolean + } + | StringBool } -type TranslationsTemplate = { +export interface TranslationsTemplate { pokemon?: { names?: boolean forms?: boolean @@ -241,52 +254,52 @@ export interface FullTemplate { pokemon?: { enabled?: boolean options: Options - template: PokemonTemplate + template: PokemonTemplate | string } costumes?: { enabled?: boolean options: Options - template: CostumeTemplate + template: CostumeTemplate | string } types?: { enabled?: boolean options: Options - template: TypesTemplate + template: TypesTemplate | string } moves?: { enabled?: boolean options: Options - template: MoveTemplate + template: MoveTemplate | string } items?: { enabled?: boolean options: Options - template: ItemTemplate + template: ItemTemplate | string } questTypes?: { enabled?: boolean options: Options - template: QuestTemplate + template: QuestTemplate | string } questConditions?: { enabled?: boolean options: Options - template: QuestTemplate + template: QuestTemplate | string } questRewardTypes?: { enabled?: boolean options: Options - template: QuestTemplate + template: QuestTemplate | string } invasions?: { enabled?: boolean options: Options - template: InvasionTemplate + template: InvasionTemplate | string } weather?: { enabled?: boolean options: Options - template: WeatherTemplate + template: WeatherTemplate | string } translations?: { enabled?: boolean diff --git a/src/typings/pogoinfo.ts b/src/typings/pogoinfo.ts index f46c4fd..649fbd0 100644 --- a/src/typings/pogoinfo.ts +++ b/src/typings/pogoinfo.ts @@ -1,9 +1,11 @@ export interface InvasionInfo { - active?: boolean - character?: Character - lineup?: { - rewards: number[] - team: InvasionTeam[][] + [id: string]: { + active?: boolean + character?: Character + lineup?: { + rewards: number[] + team: InvasionTeam[][] + } } } diff --git a/tsconfig.json b/tsconfig.json index 0e617a0..729e459 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,73 +1,18 @@ { "compilerOptions": { - /* Visit https://aka.ms/tsconfig.json to read more about this file */ - - /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ - "target": "es2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ - "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ - // "lib": [], /* Specify library files to be included in the compilation. */ - // "allowJs": true, /* Allow javascript files to be compiled. */ - // "checkJs": true, /* Report errors in .js files. */ - // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ - "declaration": true, /* Generates corresponding '.d.ts' file. */ - // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ - // "sourceMap": true, /* Generates corresponding '.map' file. */ - // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./dist", /* Redirect output structure to the directory. */ - "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ - // "composite": true, /* Enable project compilation */ - // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ - "removeComments": true, /* Do not emit comments to output. */ - // "noEmit": true, /* Do not emit outputs. */ - // "importHelpers": true, /* Import emit helpers from 'tslib'. */ - // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ - // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ - - /* Strict Type-Checking Options */ - "strict": true, /* Enable all strict type-checking options. */ - // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ - "strictNullChecks": false, /* Enable strict null checks. */ - // "strictFunctionTypes": true, /* Enable strict checking of function types. */ - // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ - // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ - // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ - // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ - - /* Additional Checks */ - "noUnusedLocals": true, /* Report errors on unused locals. */ - "noUnusedParameters": true, /* Report errors on unused parameters. */ - // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ - // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ - // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ - // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */ - // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */ - - /* Module Resolution Options */ - // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ - // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ - // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ - // "typeRoots": [], /* List of folders to include type definitions from. */ - // "types": [], /* Type declaration files to be included in compilation. */ - // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ - "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ - // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - - /* Source Map Options */ - // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ - // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ - - /* Experimental Options */ - // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ - // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ - - /* Advanced Options */ - "skipLibCheck": true, /* Skip type checking of declaration files. */ - "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */ - "resolveJsonModule": true, /* Resolve modules whose names end with .json. */ + "target": "es2020", + "module": "commonjs", + "declaration": true, + "outDir": "./dist", + "rootDir": "./src", + "removeComments": true, + "strict": true, + "strictNullChecks": false, + "noUnusedLocals": true, + "noUnusedParameters": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "resolveJsonModule": true, }, -} +} \ No newline at end of file From f2d24bf45d2d820a1e18ab0f824491ea795ea5e3 Mon Sep 17 00:00:00 2001 From: TurtIeSocks <58572875+TurtIeSocks@users.noreply.github.com> Date: Sun, 26 Sep 2021 16:03:16 -0400 Subject: [PATCH 2/2] Version Bump - Version Bump - Modern syntax for Fetch - Extra options for custom invasions --- package-lock.json | 2 +- package.json | 2 +- src/base.json | 3 +-- src/classes/Invasion.ts | 20 +++++++++++++------- src/classes/Masterfile.ts | 16 +++++++++------- src/index.ts | 2 +- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8bb3d5a..921bf16 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "pogo-data-generator", - "version": "1.2.5", + "version": "1.2.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 29d994b..a16da3c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pogo-data-generator", - "version": "1.2.5", + "version": "1.2.6", "description": "Pokemon GO project data generator", "author": "TurtIeSocks", "license": "Apache-2.0", diff --git a/src/base.json b/src/base.json index 3c08000..27371b4 100644 --- a/src/base.json +++ b/src/base.json @@ -259,8 +259,7 @@ "second": "second", "third": "third" }, - "placeholderData": true, - "customInvasions": {} + "placeholderData": true }, "template": { "id": false, diff --git a/src/classes/Invasion.ts b/src/classes/Invasion.ts index 764383e..504b0d5 100644 --- a/src/classes/Invasion.ts +++ b/src/classes/Invasion.ts @@ -15,13 +15,19 @@ export default class Invasion extends Masterfile { this.parsedInvasions = {} } - async customInvasions(): Promise { - if (this.options.customInvasions === true) { - return this.fetch('https://raw.githubusercontent.com/WatWowMap/Masterfile-Generator/master/custom-invasions.json') - } else if (this.options.customInvasions) { - return this.options.customInvasions as InvasionInfo - } else { - return {} + async customInvasions(override: boolean = false): Promise { + try { + if (this.options.customInvasions === true || (this.options.customInvasions === undefined && override)) { + return this.fetch( + 'https://raw.githubusercontent.com/WatWowMap/Masterfile-Generator/master/custom-invasions.json' + ) + } else if (this.options.customInvasions) { + return this.options.customInvasions as InvasionInfo + } else { + return {} + } + } catch (e) { + console.warn(e, 'Unable to get custom invasions') } } diff --git a/src/classes/Masterfile.ts b/src/classes/Masterfile.ts index d3d904a..f1fa8d7 100644 --- a/src/classes/Masterfile.ts +++ b/src/classes/Masterfile.ts @@ -53,13 +53,15 @@ export default class Masterfile { } async fetch(url: string): Promise { - return new Promise(resolve => { - Fetch(url) - .then(res => res.json()) - .then((json: any) => { - return resolve(json) - }) - }) + try { + const data = await Fetch(url) + if (!data.ok) { + throw new Error(`${data.status} ${data.statusText} URL: ${url}`) + } + return await data.json() + } catch (e) { + console.error(e, `Unable to fetch ${url}`) + } } capitalize(string: string) { diff --git a/src/index.ts b/src/index.ts index 5bf2f1a..d4b8769 100644 --- a/src/index.ts +++ b/src/index.ts @@ -303,7 +303,7 @@ export async function invasions({ template, test }: InvasionsOnly = {}) { const invasionData: InvasionInfo = await AllInvasions.fetch( 'https://raw.githubusercontent.com/ccev/pogoinfo/v2/active/grunts.json' ) - AllInvasions.invasions(AllInvasions.mergeInvasions(invasionData, await AllInvasions.customInvasions())) + AllInvasions.invasions(AllInvasions.mergeInvasions(invasionData, await AllInvasions.customInvasions(true))) const final = AllInvasions.templater(AllInvasions.parsedInvasions, finalTemplate) if (test) { fs.writeFile('./invasions.json', JSON.stringify(final, null, 2), 'utf8', () => {})