Skip to content

Commit

Permalink
fix: improve timing on start-up to also wait for centered images
Browse files Browse the repository at this point in the history
  • Loading branch information
Laraakaa committed Mar 20, 2023
1 parent d71e9eb commit f8088fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
88 changes: 50 additions & 38 deletions StaticData.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { PluginContext } from '@rcv-prod-toolkit/types'
import axios from 'axios'
import { finished as streamFinished } from 'stream'
import { createWriteStream, createReadStream, existsSync } from 'fs'
import { stat, mkdir, writeFile } from 'fs/promises'
import { copy, remove } from 'fs-extra'
import { promisify } from 'util'
import { join } from 'path'
import { x } from 'tar'
import { get } from 'https'
import { SingleBar, MultiBar } from 'cli-progress'

const finishedWritePromise = promisify(streamFinished)

type IPluginContext = PluginContext & {
progress?: MultiBar
setProgressBar?: (percent: number, state?: string) => void
Expand Down Expand Up @@ -208,12 +212,12 @@ export default class StaticData {

const dataPath = join(__dirname, '..', 'frontend')

this.ctx.log.info('start unpacking dragontail.tgz')
this.ctx.log.info('Unpacking dragontail.tgz...')
createReadStream(tarFilePath)
.pipe(
x({ cwd: dataPath, newer: true }, dDragonPaths)
.on('finish', async () => {
this.ctx.log.info('finish unpacking dragontail.tgz')
this.ctx.log.info('Finished unpacking dragontail.tgz')
await remove(tarFilePath)
this.copyDDragonFiles()
})
Expand All @@ -229,7 +233,7 @@ export default class StaticData {
}

private async copyDDragonFiles() {
this.ctx.log.info('moving files to frontend')
this.ctx.log.info('Moving files to frontend...')

const dataPath = join(__dirname, '..', 'frontend')
const versionDirPath = join(
Expand All @@ -244,11 +248,11 @@ export default class StaticData {
this.removeVersionDir()
this.getAllCenteredImg()

this.ctx.log.info('finish moving files to frontend')
this.ctx.log.info('Finished moving files to frontend')
}

private async removeVersionDir() {
this.ctx.log.info('delete versioned folder')
this.ctx.log.info('Deleting versioned folder...')
const versionDirPath = join(
__dirname,
'..',
Expand All @@ -258,11 +262,11 @@ export default class StaticData {
await remove(versionDirPath)
this._finishedDragonTail = true
this._readyCheck()
this.ctx.log.info('finish deleting versioned folder')
this.ctx.log.info('Finished deleting versioned folder')
}

private async getAllCenteredImg() {
this.ctx.log.info('start downloading centered images')
this.ctx.log.info('Start downloading centered images...')

const base = join(
__dirname,
Expand All @@ -282,51 +286,61 @@ export default class StaticData {
)

try {
await Promise.all(
champions.map(async (champ) => {
const champId = champ.key
return await this._downloadCenteredImg(base, champId)
})
)
const centeredImageTasks = champions.map((champ) => {
const champId = champ.key
return this._downloadCenteredImg(base, champId)
})
await Promise.all(centeredImageTasks)
} catch (error) {
this.ctx.log.error(error)
this._errorReadyCheck()
}

this._finishedCenteredImg = true
this._readyCheck()
this.ctx.log.info('finish downloading centered images')
this.ctx.log.info('Finished downloading centered images')
}

private async _downloadCenteredImg(base: string, champId: number) {
const dest = join(base, champId.toString()) + '.jpg'
const url = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-splashes/${champId}/${champId}000.jpg`

let file = createWriteStream(dest)
get(url, (response) => {
response.pipe(file)
file.on('finish', () => {
file.close()
this.ctx.log.debug(`downloaded img for ${champId}`)
return Promise.resolve(true)

let response
try {
response = await axios({
method: 'get',
url,
responseType: 'stream'
})
}).on('error', async (err) => {
try {
await remove(dest)
this.ctx.log.error(
`downloaded failed img for ${champId} with: ${err.message}`
)
return Promise.reject(err)
} catch (error: any) {
this.ctx.log.error(error)
return Promise.reject(error)
}
})
} catch (error: any) {
await remove(dest)
this.ctx.log.error(
`Downloading centered splash image failed for champId=${champId}: ${error}`
)
return Promise.reject(error)
}

try {
response.data.pipe(file)
await finishedWritePromise(file)
} catch (error: any) {
await remove(dest)
this.ctx.log.error(
`Writing centered splash image failed for champId=${champId}: ${error}`
)
return Promise.reject(error)
}

this.ctx.log.debug(
`Downloaded centered splash image for champId=${champId}`
)
}

private async getAdditionalFiles() {
if (!this.version) return
this.ctx.log.info('start downloading additional files')
this.ctx.log.info('Start downloading additional files...')

try {
await Promise.all([
Expand All @@ -340,7 +354,7 @@ export default class StaticData {

this._finishedAdditionalFileDownloading = true
this._readyCheck()
this.ctx.log.info('finish downloading additional files')
this.ctx.log.info('Finished downloading additional files')
} catch (error: any) {
this.ctx.log.debug(error)
throw new Error(error)
Expand Down Expand Up @@ -384,15 +398,13 @@ export default class StaticData {
response.pipe(file)
file.on('finish', () => {
file.close()
this.ctx.log.debug(`downloaded items.bin.json`)
this.ctx.log.debug(`Downloaded items.bin.json`)
return Promise.resolve(true)
})
}).on('error', async (err) => {
try {
await remove(filePath)
this.ctx.log.error(
`downloaded failed for item.bin.json`
)
this.ctx.log.error(`Downloading item.bin.json failed: ${err}`)
return Promise.reject(err)
} catch (error: any) {
this.ctx.log.error(error)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "module-league-static",
"version": "3.9.5",
"version": "3.9.6",
"description": "Module that provides static data for League of Legends games",
"author": "RCVolus",
"license": "MIT",
Expand Down

0 comments on commit f8088fd

Please sign in to comment.