-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-data.js
130 lines (122 loc) · 5.93 KB
/
build-data.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
const axios = require('axios')
const fs = require('fs')
const gql = require('graphql-request')
const requireFromString = require('require-from-string')
axios.get('https://raw.githubusercontent.com/smogon/pokemon-showdown/master/data/pokedex.ts').then((resp) => {
const { Pokedex } = requireFromString(resp.data.replace(/^[^=]+=\s+/, 'exports.Pokedex = '))
axios.get('https://raw.githubusercontent.com/smogon/pokemon-showdown/master/data/moves.ts').then((resp) => {
const { Moves } = requireFromString(resp.data.replace(/^[^=]+=\s+/, 'exports.Moves = ').replace(/\t\t\t.*/g, ''))
axios.get('https://raw.githubusercontent.com/smogon/pokemon-showdown/master/data/learnsets.ts').then((resp) => {
const { Learnsets } = requireFromString(resp.data.replace(/^[^=]+=\s+/, 'exports.Learnsets = '))
gql.request('https://beta.pokeapi.co/graphql/v1beta', fs.readFileSync('./forms.gql').toString()).then((all) => {
function strSort(x, y) {
return x.localeCompare(y)
}
function normalize(s) {
return s.replace(/[^A-z0-9]/, '').toLowerCase()
}
const idConversionMap = {}
const idToMoves = {}
all.species.forEach((spec) => {
spec.pokemon.forEach((pkmn) => {
pkmn.forms.forEach((form) => {
idConversionMap[normalize(pkmn.name)] = pkmn.id
idConversionMap[normalize(form.name)] = pkmn.id
idConversionMap[normalize(spec.name+form.formName)] = pkmn.id
if (form.default && !idConversionMap[normalize(pkmn.name)]) {
idConversionMap[normalize(pkmn.name)] = pkmn.id
}
})
if (pkmn.default && !idConversionMap[normalize(spec.name)]) {
idConversionMap[normalize(spec.name)] = pkmn.id
}
// console.log(pkmn.moves);
idToMoves[pkmn.id] = pkmn.moves.filter((move) => {
return move && move.move && move.move.name
}).map((move) => {
return move.move.name.replaceAll('-', '').replaceAll(' ', '').replaceAll('_', '')
})
})
})
const nameToPkmn = {}
Object.entries(Pokedex).forEach((kvp) => {
const pkmn = kvp[1]
const id = kvp[0]
pkmn.id = id
nameToPkmn[pkmn.name] = pkmn
})
const nameExcludes = /(mega|gmax|busted|totem|original|eternal|phony|gulping|gorging|sunshine|antique|hangry|dada|zen|fancy|pokeball|poke-ball|shock|chill|douse|burn)/
const allPokemon = []
function fixName(name) {
return name.match(/-[oO]$/)? name: name.split('-').join(' ')
}
Object.values(Pokedex).forEach((pkmn) => {
if (
// pkmn.evos ||
pkmn.num <= 0 || !idConversionMap[normalize(nameToPkmn[pkmn.name].id)] ||
// ((pkmn.tags || []).concat((nameToPkmn[pkmn.baseSpecies || ''] || {}).tags || [])).join('').toLowerCase().match(/legendary/) ||
(pkmn.forme || '').toLowerCase().match(nameExcludes) //|| pkmn.name.toLowerCase().match(/arceus/)
) {
return
}
if (pkmn.forme === 'F') {
pkmn.forme = 'Female'
}
if (pkmn.forme === 'M') {
pkmn.forme = 'Male'
}
const evolutions = []
let current = pkmn.prevo || (nameToPkmn[pkmn.baseSpecies] || {}).prevo
while(current && nameToPkmn[current]) {
evolutions.unshift({
name: fixName(nameToPkmn[current].name),
id: (
idConversionMap[normalize(nameToPkmn[current].id)] ||
idConversionMap[normalize(nameToPkmn[current].name)] ||
idConversionMap[normalize((nameToPkmn[current].baseSpecies || '') + (nameToPkmn[current].forme || ''))]
),
})
current = nameToPkmn[current].prevo || (nameToPkmn[current].baseSpecies || {}).prevo
}
evolutions.push({
name: fixName(pkmn.name),
id: (
idConversionMap[normalize(nameToPkmn[pkmn.name].id)] ||
idConversionMap[normalize(pkmn.name)] ||
idConversionMap[normalize((pkmn.baseSpecies || '') + (pkmn.forme || ''))]
),
})
allPokemon.push({
id: evolutions[evolutions.length - 1].id,
name: fixName(pkmn.name),
moves: Array.from(
new Set(
Object.keys(
(Learnsets[nameToPkmn[pkmn.name].id] || {}).learnset || {}
).concat(
Object.keys(
(Learnsets[(nameToPkmn[pkmn.baseSpecies] || {}).id] || {}).learnset || {}
)
).concat(...evolutions.map((e) => idToMoves[e.id]))
)
).filter((mv) => Moves[mv] !== undefined).map((mv) => {
return {name: Moves[mv].name, type: Moves[mv].type.toLowerCase()}
}).sort((x, y) => { return strSort(x.name, y.name) }),
types: pkmn.types.map((t) => { return t.toLowerCase() }),
evolutions: evolutions,
abilities: Object.values(pkmn.abilities || nameToPkmn[pkmn.baseSpecies].abilities || []).sort(strSort),
})
})
if (fs.existsSync('./public/data/pokemon')) {
fs.rmSync('./public/data/pokemon/', { recursive: true, force: true })
}
fs.mkdirSync('./public/data/pokemon/')
// because its almost 1MB of data, its being split up individually since it is unlikely that a user would ever need to load all of them (or even more than 10% at one time)
fs.writeFileSync('./public/data/all-pokemon.json', JSON.stringify(allPokemon.sort((x, y) => { return strSort(x.name, y.name) }).map((pkmn) => { return {name: pkmn.name, id: pkmn.id} }), null, 0))
allPokemon.forEach((pkmn) => {
fs.writeFileSync( `./public/data/pokemon/${pkmn.id}.json`, JSON.stringify(pkmn, null, 0))
})
})
})
})
})