1
0
forked from noxious/server

npm run format

This commit is contained in:
Dennis Postma 2024-07-26 17:27:20 +02:00
parent fb6e9d2fcc
commit cc59bc4150

View File

@ -26,44 +26,48 @@ export default function (socket: TSocket, io: Server) {
try { try {
// Parse and validate spriteActions // Parse and validate spriteActions
let parsedSpriteActions: SpriteActionInput[]; let parsedSpriteActions: SpriteActionInput[]
try { try {
parsedSpriteActions = JSON.parse(JSON.stringify(data.spriteActions)) as SpriteActionInput[]; parsedSpriteActions = JSON.parse(JSON.stringify(data.spriteActions)) as SpriteActionInput[]
if (!Array.isArray(parsedSpriteActions)) { if (!Array.isArray(parsedSpriteActions)) {
throw new Error('spriteActions is not an array'); throw new Error('spriteActions is not an array')
} }
} catch (error) { } catch (error) {
console.error('Error parsing spriteActions:', error); console.error('Error parsing spriteActions:', error)
callback(false); callback(false)
return; return
} }
// Process the sprites to determine the largest dimensions // Process the sprites to determine the largest dimensions
const processedActions = await Promise.all(parsedSpriteActions.map(async (spriteAction) => { const processedActions = await Promise.all(
const { action, sprites } = spriteAction; parsedSpriteActions.map(async (spriteAction) => {
const { action, sprites } = spriteAction
if (!Array.isArray(sprites) || sprites.length === 0) { if (!Array.isArray(sprites) || sprites.length === 0) {
throw new Error(`Invalid sprites array for action: ${action}`); throw new Error(`Invalid sprites array for action: ${action}`)
} }
// Convert base64 strings to buffers and get dimensions // Convert base64 strings to buffers and get dimensions
const buffersWithDimensions = await Promise.all(sprites.map(async (sprite: string) => { const buffersWithDimensions = await Promise.all(
const buffer = Buffer.from(sprite.split(',')[1], 'base64'); sprites.map(async (sprite: string) => {
const { width, height } = await sharp(buffer).metadata(); const buffer = Buffer.from(sprite.split(',')[1], 'base64')
return { buffer, width, height }; const { width, height } = await sharp(buffer).metadata()
})); return { buffer, width, height }
})
)
// Find the largest width and height // Find the largest width and height
const frameWidth = Math.max(...buffersWithDimensions.map(b => b.width || 0)); const frameWidth = Math.max(...buffersWithDimensions.map((b) => b.width || 0))
const frameHeight = Math.max(...buffersWithDimensions.map(b => b.height || 0)); const frameHeight = Math.max(...buffersWithDimensions.map((b) => b.height || 0))
return { return {
...spriteAction, ...spriteAction,
frameWidth, frameWidth,
frameHeight, frameHeight,
buffersWithDimensions buffersWithDimensions
}; }
})); })
)
// Update the database with the new sprite actions (including calculated frame sizes) // Update the database with the new sprite actions (including calculated frame sizes)
await prisma.sprite.update({ await prisma.sprite.update({
@ -81,47 +85,49 @@ export default function (socket: TSocket, io: Server) {
isLooping: spriteAction.isLooping, isLooping: spriteAction.isLooping,
frameWidth: spriteAction.frameWidth, frameWidth: spriteAction.frameWidth,
frameHeight: spriteAction.frameHeight, frameHeight: spriteAction.frameHeight,
frameSpeed: spriteAction.frameSpeed, frameSpeed: spriteAction.frameSpeed
})) }))
} }
} }
}); })
const public_folder = path.join(process.cwd(), 'public', 'sprites', data.id); const public_folder = path.join(process.cwd(), 'public', 'sprites', data.id)
await fs.mkdir(public_folder, { recursive: true }); await fs.mkdir(public_folder, { recursive: true })
// Process and save each spriteAction // Process and save each spriteAction
await Promise.all(processedActions.map(async (spriteAction) => { await Promise.all(
const { action, buffersWithDimensions, frameWidth, frameHeight } = spriteAction; processedActions.map(async (spriteAction) => {
const { action, buffersWithDimensions, frameWidth, frameHeight } = spriteAction
// Combine all sprites into a single image // Combine all sprites into a single image
const combinedImage = await sharp({ const combinedImage = await sharp({
create: { create: {
width: frameWidth * buffersWithDimensions.length, width: frameWidth * buffersWithDimensions.length,
height: frameHeight, height: frameHeight,
channels: 4, channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
} }
})
.composite(
buffersWithDimensions.map(({ buffer }, index) => ({
input: buffer,
left: index * frameWidth,
top: 0
}))
)
.png()
.toBuffer()
// Save the combined image
const filename = path.join(public_folder, `${action}.png`)
await writeFile(filename, combinedImage)
}) })
.composite( )
buffersWithDimensions.map(({ buffer }, index) => ({
input: buffer,
left: index * frameWidth,
top: 0
}))
)
.png()
.toBuffer();
// Save the combined image callback(true)
const filename = path.join(public_folder, `${action}.png`);
await writeFile(filename, combinedImage);
}));
callback(true);
} catch (error) { } catch (error) {
console.error('Error updating sprite:', error); console.error('Error updating sprite:', error)
callback(false); callback(false)
} }
}); })
} }