Formatted code

This commit is contained in:
Dennis Postma 2025-01-31 02:20:24 +01:00
parent 60753cb2db
commit f0bfa0b983

View File

@ -1,9 +1,11 @@
import fs from 'fs'
import sharp from 'sharp'
import { BaseEvent } from '#application/base/baseEvent' import { BaseEvent } from '#application/base/baseEvent'
import { UUID } from '#application/types' import { UUID } from '#application/types'
import SpriteRepository from '#repositories/spriteRepository'
import { SpriteAction } from '#entities/spriteAction' import { SpriteAction } from '#entities/spriteAction'
import sharp from 'sharp' import SpriteRepository from '#repositories/spriteRepository'
import fs from 'fs'
interface SpriteImage { interface SpriteImage {
url: string url: string
@ -59,7 +61,7 @@ export default class SpriteUpdateEvent extends BaseEvent {
// First verify all sprite sheets can be generated // First verify all sprite sheets can be generated
for (const actionData of data.spriteActions) { for (const actionData of data.spriteActions) {
if (!await this.generateSpriteSheet(actionData.sprites, sprite.getId(), actionData.action)) { if (!(await this.generateSpriteSheet(actionData.sprites, sprite.getId(), actionData.action))) {
return callback(false) return callback(false)
} }
} }
@ -74,13 +76,13 @@ export default class SpriteUpdateEvent extends BaseEvent {
// Create new actions // Create new actions
for (const actionData of data.spriteActions) { for (const actionData of data.spriteActions) {
// Process images and calculate dimensions // Process images and calculate dimensions
const imageData = await Promise.all(actionData.sprites.map(sprite => this.processImage(sprite))) const imageData = await Promise.all(actionData.sprites.map((sprite) => this.processImage(sprite)))
const effectiveDimensions = imageData.map(dimensions => this.calculateEffectiveDimensions(dimensions)) const effectiveDimensions = imageData.map((dimensions) => this.calculateEffectiveDimensions(dimensions))
// Calculate total height needed for the sprite sheet // Calculate total height needed for the sprite sheet
const maxHeight = Math.max(...effectiveDimensions.map(d => d.height)) const maxHeight = Math.max(...effectiveDimensions.map((d) => d.height))
const maxTop = Math.max(...effectiveDimensions.map(d => d.top)) const maxTop = Math.max(...effectiveDimensions.map((d) => d.top))
const maxBottom = Math.max(...effectiveDimensions.map(d => d.bottom)) const maxBottom = Math.max(...effectiveDimensions.map((d) => d.bottom))
const totalHeight = maxHeight + maxTop + maxBottom const totalHeight = maxHeight + maxTop + maxBottom
const spriteAction = new SpriteAction() const spriteAction = new SpriteAction()
@ -111,14 +113,14 @@ export default class SpriteUpdateEvent extends BaseEvent {
if (!sprites.length) return true if (!sprites.length) return true
// Process all images and get their dimensions // Process all images and get their dimensions
const imageData = await Promise.all(sprites.map(sprite => this.processImage(sprite))) const imageData = await Promise.all(sprites.map((sprite) => this.processImage(sprite)))
const effectiveDimensions = imageData.map(dimensions => this.calculateEffectiveDimensions(dimensions)) const effectiveDimensions = imageData.map((dimensions) => this.calculateEffectiveDimensions(dimensions))
// Calculate maximum dimensions // Calculate maximum dimensions
const maxWidth = Math.max(...effectiveDimensions.map(d => d.width)) const maxWidth = Math.max(...effectiveDimensions.map((d) => d.width))
const maxHeight = Math.max(...effectiveDimensions.map(d => d.height)) const maxHeight = Math.max(...effectiveDimensions.map((d) => d.height))
const maxTop = Math.max(...effectiveDimensions.map(d => d.top)) const maxTop = Math.max(...effectiveDimensions.map((d) => d.top))
const maxBottom = Math.max(...effectiveDimensions.map(d => d.bottom)) const maxBottom = Math.max(...effectiveDimensions.map((d) => d.bottom))
// Calculate total height needed // Calculate total height needed
const totalHeight = maxHeight + maxTop + maxBottom const totalHeight = maxHeight + maxTop + maxBottom
@ -142,9 +144,9 @@ export default class SpriteUpdateEvent extends BaseEvent {
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
} }
}) })
.composite([{ input: buffer, left, top: verticalOffset }]) .composite([{ input: buffer, left, top: verticalOffset }])
.png() .png()
.toBuffer() .toBuffer()
}) })
) )
@ -157,15 +159,15 @@ export default class SpriteUpdateEvent extends BaseEvent {
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
} }
}) })
.composite( .composite(
processedImages.map((buffer, index) => ({ processedImages.map((buffer, index) => ({
input: buffer, input: buffer,
left: index * maxWidth, left: index * maxWidth,
top: 0 top: 0
})) }))
) )
.png() .png()
.toBuffer() .toBuffer()
// Ensure directory exists // Ensure directory exists
const dir = `public/sprites/${spriteId}` const dir = `public/sprites/${spriteId}`
@ -207,10 +209,10 @@ export default class SpriteUpdateEvent extends BaseEvent {
if (!sprites.length) return 0 if (!sprites.length) return 0
// Process all images and get their dimensions // Process all images and get their dimensions
const imageData = await Promise.all(sprites.map(sprite => this.processImage(sprite))) const imageData = await Promise.all(sprites.map((sprite) => this.processImage(sprite)))
const effectiveDimensions = imageData.map(dimensions => this.calculateEffectiveDimensions(dimensions)) const effectiveDimensions = imageData.map((dimensions) => this.calculateEffectiveDimensions(dimensions))
// Calculate maximum width needed // Calculate maximum width needed
return Math.max(...effectiveDimensions.map(d => d.width)) return Math.max(...effectiveDimensions.map((d) => d.width))
} }
} }