diff --git a/src/socketEvents/gameMaster/assetManager/sprite/update.ts b/src/socketEvents/gameMaster/assetManager/sprite/update.ts index ec20036..f81c269 100644 --- a/src/socketEvents/gameMaster/assetManager/sprite/update.ts +++ b/src/socketEvents/gameMaster/assetManager/sprite/update.ts @@ -76,9 +76,45 @@ export default class SpriteUpdateEvent { } 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)]) + // Get existing sprite actions from database + const existingSprite = await prisma.sprite.findUnique({ + where: { id: payload.id }, + include: { spriteActions: true } + }) + + // Process only actions with changed sprites + const processedActions = await Promise.all(parsedActions.map(async (action) => { + const existing = existingSprite?.spriteActions.find(sa => sa.action === action.action) + + // If sprites array matches existing, return existing data + if (existing && JSON.stringify(existing.sprites) === JSON.stringify(action.sprites)) { + return { + ...action, + frameWidth: existing.frameWidth, + frameHeight: existing.frameHeight, + buffersWithDimensions: [] // No need to process frames + } + } + + // Otherwise process the new sprites + 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 + } + })) + + await Promise.all([ + this.updateDatabase(payload.id, payload.name, processedActions), + this.saveSpritesToDisk(payload.id, processedActions.filter(a => a.buffersWithDimensions.length > 0)) + ]) callback(true) } catch (error) { @@ -103,24 +139,6 @@ 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 { - ...action, - frameWidth, - frameHeight, - buffersWithDimensions: processedFrames - } - }) - ) - } - private async convertBase64ToBuffers(sprites: string[]): Promise { return sprites.map((sprite) => Buffer.from(sprite.split(',')[1], 'base64')) } @@ -139,6 +157,8 @@ export default class SpriteUpdateEvent { } private async calculateOptimalHeight(buffers: Buffer[]): Promise { + if (!buffers.length) return ISOMETRIC_CONFIG.tileHeight // Return default height if no buffers + const heights = await Promise.all( buffers.map(async (buffer) => { const bounds = await this.findContentBounds(buffer)