Skip to content

Commit

Permalink
Merge pull request #39 from WatWowMap/custom-invasions
Browse files Browse the repository at this point in the history
Custom Invasions
  • Loading branch information
TurtIeSocks authored Sep 26, 2021
2 parents ac4792d + f2d24bf commit e7a97ce
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 196 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -556,6 +563,7 @@ const template = {
genderString: false,
placeholderData: false,
snake_case: false,
customInvasions: {},
},
template: {
id: false,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
33 changes: 32 additions & 1 deletion src/classes/Invasion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ export default class Invasion extends Masterfile {
this.parsedInvasions = {}
}

async customInvasions(override: boolean = false): Promise<InvasionInfo> {
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')
}
}

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_', '')
Expand All @@ -30,7 +61,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
Expand Down
58 changes: 49 additions & 9 deletions src/classes/Masterfile.ts
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -14,14 +14,54 @@ export default class Masterfile {
}
}

async fetch(url: string): Promise<any> {
return new Promise(resolve => {
Fetch(url)
.then(res => res.json())
.then((json: any) => {
return resolve(json)
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<any> {
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) {
Expand All @@ -44,7 +84,7 @@ export default class Masterfile {
}
} catch (e) {
console.warn(e, '\n', string)
}
}
}
}

Expand Down Expand Up @@ -212,7 +252,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]]
Expand Down
2 changes: 1 addition & 1 deletion src/classes/Translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
58 changes: 11 additions & 47 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 = {}
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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(true)))
const final = AllInvasions.templater(AllInvasions.parsedInvasions, finalTemplate)
if (test) {
fs.writeFile('./invasions.json', JSON.stringify(final, null, 2), 'utf8', () => {})
Expand Down
Loading

0 comments on commit e7a97ce

Please sign in to comment.