From 23c437f0bcf299ea53ddac98a402d30886969f74 Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Sat, 21 Dec 2024 22:51:08 +0100 Subject: [PATCH] npm run format --- src/http/assets.ts | 2 +- src/http/auth.ts | 2 +- src/http/index.ts | 11 +-- .../gameMaster/assetManager/sprite/update.ts | 92 +++++++++---------- 4 files changed, 52 insertions(+), 55 deletions(-) diff --git a/src/http/assets.ts b/src/http/assets.ts index dc360c9..5610daf 100644 --- a/src/http/assets.ts +++ b/src/http/assets.ts @@ -110,4 +110,4 @@ router.get('/assets/:type/:spriteId?/:file', (req: Request, res: Response) => { }) }) -export default router \ No newline at end of file +export default router diff --git a/src/http/auth.ts b/src/http/auth.ts index 8585f19..fe4291d 100644 --- a/src/http/auth.ts +++ b/src/http/auth.ts @@ -87,4 +87,4 @@ router.post('/new-password', async (req: Request, res: Response) => { return res.status(400).json({ message: 'Failed to set new password' }) }) -export default router \ No newline at end of file +export default router diff --git a/src/http/index.ts b/src/http/index.ts index 585ed6b..7d41d17 100644 --- a/src/http/index.ts +++ b/src/http/index.ts @@ -4,12 +4,9 @@ import fs from 'fs' import path from 'path' async function addHttpRoutes(app: Application) { - const routeFiles = fs.readdirSync(__dirname) - .filter(file => { - return file !== 'index.ts' && - file !== 'index.js' && - (file.endsWith('.ts') || file.endsWith('.js')) - }) + const routeFiles = fs.readdirSync(__dirname).filter((file) => { + return file !== 'index.ts' && file !== 'index.js' && (file.endsWith('.ts') || file.endsWith('.js')) + }) for (const file of routeFiles) { const route = await import(path.join(__dirname, file)) @@ -21,4 +18,4 @@ async function addHttpRoutes(app: Application) { httpLogger.info('Web routes added') } -export { addHttpRoutes } \ No newline at end of file +export { addHttpRoutes } diff --git a/src/socketEvents/gameMaster/assetManager/sprite/update.ts b/src/socketEvents/gameMaster/assetManager/sprite/update.ts index fe52594..ec20036 100644 --- a/src/socketEvents/gameMaster/assetManager/sprite/update.ts +++ b/src/socketEvents/gameMaster/assetManager/sprite/update.ts @@ -71,17 +71,14 @@ export default class SpriteUpdateEvent { private async handleSpriteUpdate(payload: UpdatePayload, callback: (success: boolean) => void): Promise { try { - if (!await this.validateGameMasterAccess()) { + if (!(await this.validateGameMasterAccess())) { return callback(false) } const parsedActions = this.validateSpriteActions(payload.spriteActions) const processedActions = await this.processSprites(parsedActions) - await Promise.all([ - this.updateDatabase(payload.id, payload.name, processedActions), - this.saveSpritesToDisk(payload.id, processedActions) - ]) + await Promise.all([this.updateDatabase(payload.id, payload.name, processedActions), this.saveSpritesToDisk(payload.id, processedActions)]) callback(true) } catch (error) { @@ -107,23 +104,25 @@ export default class SpriteUpdateEvent { } private async processSprites(actions: SpriteActionInput[]): Promise { - return Promise.all(actions.map(async (action) => { - const spriteBuffers = await this.convertBase64ToBuffers(action.sprites) - const frameWidth = ISOMETRIC_CONFIG.tileWidth - const frameHeight = await this.calculateOptimalHeight(spriteBuffers) - const processedFrames = await this.normalizeFrames(spriteBuffers, frameWidth, frameHeight) + return Promise.all( + actions.map(async (action) => { + const spriteBuffers = await this.convertBase64ToBuffers(action.sprites) + const frameWidth = ISOMETRIC_CONFIG.tileWidth + const frameHeight = await this.calculateOptimalHeight(spriteBuffers) + const processedFrames = await this.normalizeFrames(spriteBuffers, frameWidth, frameHeight) - return { - ...action, - frameWidth, - frameHeight, - buffersWithDimensions: processedFrames - } - })) + return { + ...action, + frameWidth, + frameHeight, + buffersWithDimensions: processedFrames + } + }) + ) } private async convertBase64ToBuffers(sprites: string[]): Promise { - return sprites.map(sprite => Buffer.from(sprite.split(',')[1], 'base64')) + return sprites.map((sprite) => Buffer.from(sprite.split(',')[1], 'base64')) } private async normalizeFrames(buffers: Buffer[], frameWidth: number, frameHeight: number): Promise { @@ -141,7 +140,7 @@ export default class SpriteUpdateEvent { private async calculateOptimalHeight(buffers: Buffer[]): Promise { const heights = await Promise.all( - buffers.map(async buffer => { + buffers.map(async (buffer) => { const bounds = await this.findContentBounds(buffer) return bounds.height }) @@ -180,12 +179,14 @@ export default class SpriteUpdateEvent { background: { r: 0, g: 0, b: 0, alpha: 0 } } }) - .composite([{ - input: processedInput, - left: offset, - top: 0, - blend: 'over' - }]) + .composite([ + { + input: processedInput, + left: offset, + top: 0, + blend: 'over' + } + ]) .png({ compressionLevel: 9, adaptiveFiltering: false, @@ -249,17 +250,14 @@ export default class SpriteUpdateEvent { } private findSpinePosition(density: number[]): number { - return density.reduce((maxIdx, curr, idx, arr) => curr > arr[maxIdx] ? idx : maxIdx, 0) + return density.reduce((maxIdx, curr, idx, arr) => (curr > arr[maxIdx] ? idx : maxIdx), 0) } private calculateWeightedMassCenter(columnDensity: number[], upperBodyDensity: number[]): number { const upperMassCenter = this.calculateMassCenter(upperBodyDensity) const lowerMassCenter = this.calculateMassCenter(columnDensity) - return Math.round( - upperMassCenter * ISOMETRIC_CONFIG.bodyRatios.weightUpper + - lowerMassCenter * ISOMETRIC_CONFIG.bodyRatios.weightLower - ) + return Math.round(upperMassCenter * ISOMETRIC_CONFIG.bodyRatios.weightUpper + lowerMassCenter * ISOMETRIC_CONFIG.bodyRatios.weightLower) } private calculateMassCenter(density: number[]): number { @@ -271,10 +269,7 @@ export default class SpriteUpdateEvent { } private async findContentBounds(buffer: Buffer) { - const { data, info } = await sharp(buffer) - .raw() - .ensureAlpha() - .toBuffer({ resolveWithObject: true }) + const { data, info } = await sharp(buffer).raw().ensureAlpha().toBuffer({ resolveWithObject: true }) const width = info.width const height = info.height @@ -288,7 +283,8 @@ export default class SpriteUpdateEvent { for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const idx = (y * width + x) * 4 - if (data[idx + 3] > 0) { // If pixel is not transparent + if (data[idx + 3] > 0) { + // If pixel is not transparent left = Math.min(left, x) right = Math.max(right, x) top = Math.min(top, y) @@ -309,10 +305,12 @@ export default class SpriteUpdateEvent { const publicFolder = getPublicPath('sprites', id) await mkdir(publicFolder, { recursive: true }) - await Promise.all(actions.map(async (action) => { - const spritesheet = await this.createSpritesheet(action.buffersWithDimensions) - await writeFile(getPublicPath('sprites', id, `${action.action}.png`), spritesheet) - })) + await Promise.all( + actions.map(async (action) => { + const spritesheet = await this.createSpritesheet(action.buffersWithDimensions) + await writeFile(getPublicPath('sprites', id, `${action.action}.png`), spritesheet) + }) + ) } private async createSpritesheet(frames: ProcessedFrame[]): Promise { @@ -335,12 +333,14 @@ export default class SpriteUpdateEvent { .toBuffer() return sharp(background) - .composite(frames.map((frame, index) => ({ - input: frame.buffer, - left: index * ISOMETRIC_CONFIG.tileWidth, - top: 0, - blend: 'over' - }))) + .composite( + frames.map((frame, index) => ({ + input: frame.buffer, + left: index * ISOMETRIC_CONFIG.tileWidth, + top: 0, + blend: 'over' + })) + ) .png({ compressionLevel: 9, adaptiveFiltering: false, @@ -387,4 +387,4 @@ export default class SpriteUpdateEvent { private getErrorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error) } -} \ No newline at end of file +}