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