pogo-data-generator
Version:
Pokemon GO project data generator
292 lines (291 loc) • 12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
if (typeof fetch === 'undefined') {
global.fetch = require('node-fetch');
}
class Masterfile {
constructor() {
this.customFieldNames = {};
this.genders = {
1: 'Male',
2: 'Female',
};
}
static templateMerger(template, base) {
const baseline = base;
const merged = {};
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,
};
if (!template.translations.options.questTitleTermsToSkip) {
merged.translations.options.questTitleTermsToSkip =
base.translations.options.questTitleTermsToSkip;
}
}
});
return merged;
}
async fetch(url, text = false) {
try {
const data = await fetch(url);
if (!data.ok) {
throw new Error(`${data.status} ${data.statusText} URL: ${url}`);
}
return text ? data.text() : data.json();
}
catch (e) {
if (e instanceof Error) {
console.warn(e.message, `Unable to fetch ${url}`);
}
}
}
capitalize(string) {
if (string) {
const capitalizeList = ['pvp', 'xl', 'npc', 'cp', 'poi', 'gbl'];
try {
string = string.toLowerCase();
if (string.split('_').length > 1) {
let processed = '';
string.split('_').forEach((word) => {
if (capitalizeList.includes(word)) {
processed += ` ${word.toUpperCase()}`;
}
else {
processed += ` ${word.charAt(0).toUpperCase() + word.slice(1)}`;
}
});
return processed.slice(1);
}
else {
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
catch (e) {
if (e instanceof Error) {
console.warn(e.message, '\n', string);
}
}
}
}
compare(formData, parentData) {
try {
if (formData && parentData) {
try {
return (formData.every((x, i) => x === parentData[i]) &&
formData.length === parentData.length);
}
catch (e) {
console.warn(e, '\nForm:', formData, '\nParent:', parentData);
}
}
}
catch (e) {
console.warn(e, `Failed to compare ${formData} and ${parentData}`);
}
}
templater(data, settings, reference = {}) {
const { template, options } = settings;
if (!options.customFields) {
options.customFields = {};
}
if (!options.customChildObj) {
options.customChildObj = {};
}
if (!options.makeSingular) {
options.makeSingular = {};
}
const resolved = options.keys.main ? {} : [];
const parseData = (fieldKey, fieldValue, templateChild, data) => {
const isObj = options.keys[fieldKey];
let returnValue = isObj ? {} : [];
if (!Array.isArray(fieldValue)) {
fieldValue = [fieldValue];
}
fieldValue.forEach((x) => {
const child = loopFields(fieldKey, x, templateChild, data);
if (child) {
if (isObj) {
const customKey = reference[fieldKey]
? this.keyResolver(fieldKey, reference[fieldKey][x], options)
: this.keyResolver(fieldKey, x, options);
if (fieldKey === 'encounters') {
if (returnValue[customKey]) {
returnValue[customKey].push(child);
}
else if (typeof child === 'object') {
if (Object.keys(child || {}).length) {
returnValue[customKey] = [child];
}
else {
returnValue[customKey] = [];
}
}
else {
returnValue[customKey] = [child];
}
}
else {
returnValue[customKey] = child;
}
}
else if (options.makeSingular[fieldKey]) {
returnValue = child;
}
else if (Array.isArray(returnValue)) {
returnValue.push(child);
}
}
});
if (options.processFormsSeparately && fieldKey === 'forms') {
returnValue = returnValue[0];
}
return fieldKey === 'type' && !isObj ? returnValue[0] : returnValue;
};
const loopFields = (fieldKey, x, templateChild, data) => {
let returnedObj = {};
const ref = reference[fieldKey] ? reference[fieldKey][x] : x;
try {
Object.entries(ref).forEach((subField) => {
let [subFieldKey, subFieldValue] = subField;
if (templateChild[fieldKey] === subFieldKey) {
returnedObj = subFieldValue;
}
else if (templateChild[fieldKey][subFieldKey]) {
const customKey = this.keyFormatter(subFieldKey, options);
if (typeof subFieldValue === 'object' ||
(reference[subFieldKey] && subFieldValue)) {
if (subFieldKey === 'evolutions' &&
(x === 776 || x === 777 || x === 778)) {
subFieldValue =
data.pokedexId === 29 ? ref.evolutions[0] : ref.evolutions[1];
}
returnedObj[customKey] = parseData(subFieldKey, subFieldValue, templateChild[fieldKey], data);
}
else {
if (options.customChildObj[subFieldKey]) {
customChildObj(returnedObj, subFieldKey, customKey, subFieldValue);
}
else if (subFieldValue !== undefined) {
returnedObj[customKey] = subFieldValue;
}
}
}
});
}
catch (e) {
console.warn(`Ref or X is undefined and it probably shouldn't be for ${reference}[${fieldKey}][${x}]`);
}
return returnedObj;
};
const customChildObj = (target, key, customKey, field) => {
const customObj = options.customChildObj[key];
if (!target[customObj]) {
target[customObj] = {};
}
target[customObj][customKey] = field;
};
Object.keys(data).forEach((id) => {
let parent = {};
const mainKey = this.keyResolver('main', data[id], options);
try {
Object.entries(data[id]).forEach((field) => {
const [fieldKey, fieldValue] = field;
if (template === fieldKey || template[fieldKey] === fieldKey) {
parent = fieldValue;
}
else if (template[fieldKey] && fieldValue !== undefined) {
const customKey = this.keyFormatter(fieldKey, options);
if (typeof fieldValue === 'object' || reference[fieldKey]) {
parent[customKey] = parseData(fieldKey, fieldValue, template, data[id]);
}
else {
if (options.customChildObj[fieldKey]) {
customChildObj(parent, fieldKey, customKey, fieldValue);
}
else {
parent[customKey] = fieldValue;
}
}
}
});
if (mainKey !== undefined && mainKey !== null) {
resolved[mainKey] = parent;
}
else if (Array.isArray(resolved)) {
resolved.push(parent);
}
}
catch (e) {
console.warn(e, '\n', mainKey, data[id]);
}
});
return resolved;
}
keyFormatter(key, options) {
if (options.customFields[key]) {
return options.customFields[key];
}
if (options.snake_case) {
return key.replace(/([a-z](?=[A-Z]))/g, '$1_').toLowerCase();
}
return key;
}
keyResolver(key, data, options) {
try {
if (options.keys[key]) {
const split = options.keys[key].split(' ');
let newKey = '';
if (split.length === 1) {
newKey = data[split[0]];
}
else {
split.forEach((field) => {
newKey +=
data[field] || data[field] === 0
? `${data[field].toString().replace(' ', options.keyJoiner)}${options.keyJoiner}`
: '';
});
if (newKey.endsWith(options.keyJoiner)) {
newKey = newKey.slice(0, -options.keyJoiner.length);
}
}
return newKey;
}
}
catch (e) {
console.warn(e, '\n', data);
}
}
}
exports.default = Masterfile;