forked from WatWowMap/Masterfile-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
161 lines (150 loc) · 5.11 KB
/
generate.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
const fs = require('fs')
const { exec } = require('child_process')
const Fetch = require('node-fetch')
const { generate } = require('pogo-data-generator')
const fetch = async (url) => {
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}`)
}
}
let sha = ''
exec('git rev-parse HEAD', (err, stdout) => {
try {
if (err || typeof stdout !== 'string' || !stdout.trim()) {
throw new Error('Unable to get current sha', err)
}
sha = stdout.trim().substring(0, 7)
} catch (e) {
console.warn(
'[UPDATE] Unable to get current SHA',
e.message,
'Branch:',
branch
)
}
})
async function masterfile() {
const templates = await fs.promises.readdir('./templates')
const pmsfQuestTypes = await fetch(
'https://raw.githubusercontent.com/pmsf/PMSF/develop/static/data/questtype.json'
)
if (!fs.existsSync(`./previous-versions/${sha}`)) {
fs.mkdirSync(`./previous-versions/${sha}`)
}
await Promise.all(
templates.map(async (templateName) => {
const previousFilePath =
__dirname + `./previous-versions/${sha}/${templateName}`
const exists = fs.existsSync(previousFilePath)
if (exists) {
try {
fs.writeFileSync(
previousFilePath,
fs.readFileSync(`./${templateName}`)
)
} catch (e) {
console.warn(`Previous version of ${templateName} does not exist`)
}
}
try {
console.log('Generating', templateName)
const template = JSON.parse(
fs.readFileSync(`./templates/${templateName}`)
)
const newData = await generate({
template,
raw: templateName === 'master-latest-raw.json',
// translationApkUrl:
// 'https://raw.githubusercontent.com/turtiesocks/pogo_assets/master/Texts/Latest%20APK/JSON/i18n_english.json',
})
if (
templateName === 'master-latest-poracle.json' ||
templateName === 'master-latest.json'
) {
const mergedQuestTypes = {}
const questKey =
templateName === 'master-latest-poracle.json'
? 'questTypes'
: 'quest_types'
Object.keys(newData[questKey]).forEach((key) => {
mergedQuestTypes[key] =
pmsfQuestTypes[key] && pmsfQuestTypes[key].text.includes('{')
? pmsfQuestTypes[key]
: newData[questKey][key]
})
newData[questKey] = mergedQuestTypes
if (templateName === 'master-latest.json') {
newData.type_ids = JSON.parse(
fs.readFileSync('./master-latest-react-map.json')
).types
newData.throw_types = {
10: 'Nice',
11: 'Great',
12: 'Excellent',
13: 'Curveball',
}
}
}
if (
templateName === 'master-latest-poracle.json' ||
templateName === 'master-latest-everything.json'
) {
delete newData.translations
}
fs.writeFileSync(`./${templateName}`, JSON.stringify(newData, null, 2))
// compare content of both files
if (exists) {
const newFile = fs.readFileSync(`./${templateName}`)
const previousFile = fs.readFileSync(previousFilePath)
if (previousFile.equals(newFile)) {
fs.unlinkSync(previousFilePath, (err) => {
if (err) console.error(err)
else {
console.log(`Deleted file ${templateName}`)
}
})
}
}
if (templateName === 'master-latest.json') {
const pokedex = []
for (const [pokemonId, pokemon] of Object.entries(newData.pokemon)) {
if (!(pokemon.attack && pokemon.defense && pokemon.stamina))
continue
const pushEntry = (stats, name) =>
pokedex.push(
`{id:${pokemonId},name:` +
JSON.stringify(
name === null ? pokemon.name : `${pokemon.name} (${name})`
) +
`,at:${stats.attack},df:${stats.defense},st:${stats.stamina}}`
)
pushEntry(pokemon, null)
for (const form of Object.values(pokemon.forms)) {
if (form.attack && form.defense && form.stamina)
pushEntry(form, form.name)
}
for (const [id, evo] of Object.entries(
pokemon.temp_evolutions || {}
)) {
if (evo.attack && evo.defense && evo.stamina)
pushEntry(evo, ['Unset', 'Mega', 'Mega X', 'Mega Y', 'Primal'][id])
}
}
fs.writeFileSync('./pokedex.js', `pokedex=[${pokedex.join(',')}]`)
}
} catch (e) {
console.error(e, `Unable to process ${templateName}`)
}
})
)
}
module.exports.masterfile = masterfile
if (require.main === module) {
masterfile().then(() => console.log('Masterfiles Generated'))
}