forked from RedSparr0w/Discord-bot-pokeclicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateData.js
186 lines (150 loc) · 6.16 KB
/
updateData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* eslint-disable no-undef */
const puppeteer = require('puppeteer');
const fs = require('fs');
let { website, wikiWebsite } = require('./config.js');
website = website.endsWith('/') ? website : `${website}/`;
wikiWebsite = wikiWebsite.endsWith('/') ? wikiWebsite : `${wikiWebsite}/`;
// Tidy up the result data with our eslint rules
const { ESLint } = require('eslint');
const cli = new ESLint({
fix: true,
});
(async () => {
const browser = await puppeteer.launch({
headless: true,
executablePath: process.env.CHROMIUM_PATH || null,
args: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-dev-shm-usage', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
console.log('=== PokéClicker Game ===');
console.log(`navigate to ${website}\nwaiting for webpage to load..`);
await page.goto(website);
console.log('webpage loaded!\nwaiting for data to load..');
page.evaluate(() => {
App.start();
});
await page.waitForFunction(() => App.game && App.game.update && App.game.update.version);
console.log('data loaded!\nupdating data..');
const result = await page.evaluate(() => {
App.game.specialEvents.events.forEach(event => {
if (event.hasStarted()) event.end();
});
const getRouteTypes = () => {
const regionRoutes = {};
Routes.regionRoutes.forEach(routeData => {
// Check if data exist, otherwise create it as empty
if (!regionRoutes[routeData.region]) regionRoutes[routeData.region] = {};
if (!regionRoutes[routeData.region][routeData.number]) regionRoutes[routeData.region][routeData.number] = {};
let totalPokemon = 0;
// Go over all the pokemon in the route
Object.values(routeData.pokemon).flat().forEach(pName => {
// Note that we are excluding any special encounters at the moment
// possibly add weather encounters later on (with an optional param on Discord)
const pokemon = pokemonMap[pName];
// Ignore MissingNo. and other pokemon with negative IDs (also special encounters currently)
if (!pokemon || pokemon.id <= 0) return;
// Make sure this types data exists on the route
if (!regionRoutes[routeData.region][routeData.number][pokemon.type[0]]) {
regionRoutes[routeData.region][routeData.number][pokemon.type[0]] = 0;
}
// Increment this type on the route
regionRoutes[routeData.region][routeData.number][pokemon.type[0]]++;
if (pokemon.type[1]) {
// Make sure this types data exists on the route
if (!regionRoutes[routeData.region][routeData.number][pokemon.type[1]]) {
regionRoutes[routeData.region][routeData.number][pokemon.type[1]] = 0;
}
// Increment the 2nd type on the route
regionRoutes[routeData.region][routeData.number][pokemon.type[1]]++;
}
totalPokemon++;
});
// Calculate percentage of each type on route
Object.entries(regionRoutes[routeData.region][routeData.number]).forEach(([type, amount]) => {
regionRoutes[routeData.region][routeData.number][type] = +((amount / totalPokemon) * 100).toFixed(2);
});
});
return regionRoutes;
};
const gyms = {};
Object.entries(GymList).map(([key, value]) => {
delete value.parent;
gyms[key] = value;
});
// So we always get the correct weather/day requirements
Weather.currentWeather = () => -1;
DayOfWeekRequirement.prototype.getProgress = () => 0;
Requirement.prototype.toJSON = function() {
const req = this.__proto__.constructor.name === 'LazyRequirementWrapper'
? this.unwrap()
: this;
let hint = '';
try {
hint = req.hint();
} catch (e) {
hint = 'unknown method';
}
return {
...Object.fromEntries(Object.entries(req)),
hint,
__class: req.__proto__.constructor.name,
};
};
const pokeclickerData = {
gameVersion: App.game.update.version,
shopItems: App.game.discord.codes,
regionRoutes: Routes.regionRoutes,
RouteGemTypes: getRouteTypes(),
PokemonLocationType,
PokemonType,
LevelType,
levelRequirements,
EvolutionType,
EvoTrigger,
WeatherType,
pokemonList: pokemonList.map(p => {
p.locations = PokemonLocations.getPokemonLocations(p.name);
p.catchRatePercent = PokemonFactory.catchRateHelper(p.catchRate, true);
p.eggSteps = App.game.breeding.getSteps(p.eggCycles);
return p;
}),
UndergroundItemValueType,
GameConstants,
BadgeEnums,
GymList: gyms,
berryType: BerryType,
berryList: App.game.farming.berryData.map(b => {
const mutation = App.game.farming.mutations.find(m => m.mutatedBerry == b.type);
if (mutation) b.hint = mutation.hint;
return b;
}),
StoneType,
};
return `module.exports = ${JSON.stringify(pokeclickerData, null, 2)}`;
});
const results = await cli.lintText(result);
const res = results[0];
// Get the output after running through eslint
const output = res.output;
// Save the data
await fs.writeFileSync('./helpers/pokeclicker.js', output);
console.log('PokéClicker Game data updated!');
console.log({ fileSise: output.length, errorCount: res.errorCount, warningCount: res.warningCount });
// Update wiki data:
if (wikiWebsite) {
console.log('=== PokéClicker Wiki ===');
console.log(`navigate to ${wikiWebsite}\nwaiting for webpage to load..`);
await page.goto(wikiWebsite);
console.log('webpage loaded!\ngathering data...');
const wikiData = await page.evaluate(() => Wiki.searchOptions);
const wikiResults = await cli.lintText(`module.exports = {\nwikiLinks: ${JSON.stringify(wikiData, null, 2)}}`);
const wikiResult = wikiResults[0];
// Get the output after running through eslint
const wikiOutput = wikiResult.output;
// Save the data
await fs.writeFileSync('./helpers/pokeclickerWiki.js', wikiOutput);
console.log('PokéClicker Wiki data updated!');
console.log({ fileSise: wikiOutput.length });
}
await browser.close();
})();