-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStaticData.ts
More file actions
359 lines (306 loc) · 9.62 KB
/
StaticData.ts
File metadata and controls
359 lines (306 loc) · 9.62 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import type { PluginContext } from '@rcv-prod-toolkit/types'
import axios from 'axios'
import { Agent } from 'https'
import { createWriteStream, createReadStream, existsSync } from 'fs'
import { stat, mkdir, writeFile } from 'fs/promises'
import { copy, remove } from 'fs-extra'
import { join } from 'path'
import { x } from 'tar'
import { get } from 'https'
import { SingleBar, MultiBar } from 'cli-progress'
type IPluginContext = PluginContext & {
progress?: MultiBar
setProgressBar?: (percent: number, state?: string) => void
}
export default class StaticData {
private readyHandler?: () => void
private _finishedAdditionalFileDownloading = false
private _finishedDragonTail = false
public version?: string
private versionIndex = 0
/**
* @param ctx Plugin Context
* @param config
*/
constructor(private ctx: IPluginContext, private config: any) {
this._startUp()
}
private async _startUp() {
if (!this.config.gameVersion) {
await this.setCurrentVersion()
} else {
this.version = this.config.gameVersion
}
if (!this.config['last-downloaded-version']) {
try {
await this.getAdditionalFiles()
await this.getDDragon()
} catch (error) {
this._errorReadyCheck()
return
}
} else if (
this.config['last-downloaded-version'] !== this.version
) {
const continueUpdate = await this.ctx.LPTE.prompt({
questions: {
type: 'confirm',
name: 'update',
message: `There are new static files available (${this.version})! Do you want to install them?`,
default: true
}
}, 1000 * 10)
if (!continueUpdate.update) {
this._finishedAdditionalFileDownloading = true
this._finishedDragonTail = true
if (this.readyHandler) this.readyHandler()
} else {
try {
await this.getAdditionalFiles()
await this.getDDragon()
} catch (error) {
this._errorReadyCheck()
return
}
}
} else {
this._finishedAdditionalFileDownloading = true
this._finishedDragonTail = true
if (this.readyHandler) this.readyHandler()
}
}
public onReady(handler: () => void): void {
if (
this._finishedDragonTail &&
this._finishedAdditionalFileDownloading
) {
handler()
} else {
this.readyHandler = handler
}
}
private _readyCheck() {
if (!this.readyHandler) return
if (
!this._finishedDragonTail ||
!this._finishedAdditionalFileDownloading
)
return
this.readyHandler()
this._setDownloadVersion()
}
private _errorReadyCheck() {
if (!this.readyHandler) return
if (
!this.config['last-downloaded-version'] ||
this.config['last-downloaded-version'] === ''
) {
this.ctx.log.warn(
'The latest patch information could not be downloaded. Trying to get data from an earlier patch'
)
this.versionIndex += 1
return this._startUp()
}
this.ctx.log.warn(
`The latest patch information could not be downloaded, data from the previous patch (${this.config['last-downloaded-version']}) will be used`
)
this.readyHandler()
}
private _setDownloadVersion() {
this.ctx.LPTE.emit({
meta: {
type: 'set',
namespace: 'plugin-config',
version: 1
},
config: {
'last-downloaded-version': this.version
}
})
}
private async setCurrentVersion() {
const gvRequest = await axios.get(
'https://ddragon.leagueoflegends.com/api/versions.json'
)
const gvJson = gvRequest.data
return (this.version = gvJson[this.versionIndex] as string)
}
private async getDDragon() {
const tarFileName = `dragontail-${this.version}.tgz`
const tarFilePath = join(__dirname, '..', 'frontend', tarFileName)
const tarURI = `https://ddragon.leagueoflegends.com/cdn/${tarFileName}`
const file = createWriteStream(tarFilePath)
this.ctx.log.info('start downloading dragontail.tgz')
let progress: SingleBar
get(tarURI, (response) => {
response.pipe(file)
if (response.headers['content-length']) {
var len = parseInt(response.headers['content-length'], 10)
var cur = 0
var total = len / 1048576
if (progress === undefined) {
if (typeof this.ctx.progress?.create === 'function') {
progress = this.ctx.progress.create(Math.round(total), 0, {
task: 'downloading DataDragon'
})
} else if (typeof this.ctx.setProgressBar === 'function') {
this.ctx.setProgressBar(0, 'Downloading DataDragon')
}
}
response.on('data', (chunk: any) => {
cur += chunk.length
if (progress !== undefined) {
progress.update(Math.round(cur / 1048576))
} else if (typeof this.ctx.setProgressBar === 'function') {
this.ctx.setProgressBar(
cur / 1048576 / total,
'Downloading DataDragon'
)
}
})
}
file.on('finish', () => {
this.ctx.log.info('\n finish downloading dragontail.tgz')
file.close()
progress?.stop()
this.unpackDDragon()
})
}).on('error', async (err) => {
progress?.stop() // Handle errors
try {
await remove(tarFilePath)
this.ctx.log.error(err.message)
this._errorReadyCheck()
} catch (error: any) {
this.ctx.log.debug(`\n${tarFilePath}file removed`)
}
})
}
private async unpackDDragon() {
const tarFileName = `dragontail-${this.version}.tgz`
const tarFilePath = join(__dirname, '..', 'frontend', tarFileName)
const stats = await stat(tarFilePath)
if (stats === undefined || stats.size <= 0) {
return this._errorReadyCheck()
}
const dDragonPaths = [
`${this.version}/img/champion`,
`${this.version}/img/item`,
`${this.version}/img/profileicon`,
`${this.version}/data/en_US/map.json`,
`${this.version}/data/en_US/runesReforged.json`,
`${this.version}/data/en_US/champion.json`,
`${this.version}/data/en_US/item.json`,
`img/champion`,
`img/perk-images/Styles`
]
const dataPath = join(__dirname, '..', 'frontend')
this.ctx.log.info('Unpacking dragontail.tgz...')
createReadStream(tarFilePath)
.pipe(
x({ cwd: dataPath, newer: true }, dDragonPaths)
.on('finish', async () => {
this.ctx.log.info('Finished unpacking dragontail.tgz')
await remove(tarFilePath)
this.copyDDragonFiles()
})
.on('error', (e) => {
this.ctx.log.error(e)
this._errorReadyCheck()
})
)
.on('error', (e) => {
this.ctx.log.error(e)
this._errorReadyCheck()
})
}
private async copyDDragonFiles() {
this.ctx.log.info('Moving files to frontend...')
const dataPath = join(__dirname, '..', 'frontend')
const versionDirPath = join(
__dirname,
'..',
'frontend',
this.version as string
)
await copy(versionDirPath, dataPath)
this.removeVersionDir()
this.ctx.log.info('Finished moving files to frontend')
}
private async removeVersionDir() {
this.ctx.log.info('Deleting versioned folder...')
const versionDirPath = join(
__dirname,
'..',
'frontend',
this.version as string
)
await remove(versionDirPath)
this._finishedDragonTail = true
this._readyCheck()
this.ctx.log.info('Finished deleting versioned folder')
}
private async getAdditionalFiles() {
if (!this.version) return
this.ctx.log.info('Start downloading additional files...')
try {
await Promise.all([
await this.getItemBin(),
await this.getConstants('gameModes'),
await this.getConstants('gameTypes'),
await this.getConstants('queues'),
await this.getConstants('seasons'),
await this.getConstants('maps')
])
this._finishedAdditionalFileDownloading = true
this._readyCheck()
this.ctx.log.info('Finished downloading additional files')
} catch (error: any) {
this.ctx.log.debug(error)
throw new Error(error)
}
}
async getConstants(name: string) {
const base = join(__dirname, '..', 'frontend', 'data', 'constants')
if (!existsSync(base)) {
await mkdir(base)
}
const filePath = join(base, `${name}.json`)
const uri = `https://static.developer.riotgames.com/docs/lol/${name}.json`
const res = await axios.get(uri, {
httpsAgent: new Agent({ rejectUnauthorized: false })
})
const data = res.data
if (res.status !== 200) {
this.ctx.log.debug(`${name} could not be downloaded`)
throw new Error(res.statusText)
}
return writeFile(filePath, JSON.stringify(data))
}
async getItemBin() {
const base = join(__dirname, '..', 'frontend', 'data')
if (!existsSync(base)) {
await mkdir(base)
}
const filePath = join(base, 'item.bin.json')
const url = `https://raw.communitydragon.org/latest/game/items.cdtb.bin.json`
let file = createWriteStream(filePath)
get(url, (response) => {
response.pipe(file)
file.on('finish', () => {
file.close()
this.ctx.log.debug(`Downloaded items.bin.json`)
return Promise.resolve(true)
})
}).on('error', async (err) => {
try {
await remove(filePath)
this.ctx.log.error(`Downloading item.bin.json failed: ${err}`)
return Promise.reject(err)
} catch (error: any) {
this.ctx.log.error(error)
return Promise.reject(error)
}
})
}
}