forked from noxious/server
npm run format
This commit is contained in:
parent
fb6e9d2fcc
commit
cc59bc4150
@ -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}`);
|
||||
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 };
|
||||
}));
|
||||
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));
|
||||
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
|
||||
};
|
||||
}));
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// Update the database with the new sprite actions (including calculated frame sizes)
|
||||
await prisma.sprite.update({
|
||||
@ -81,18 +85,19 @@ 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({
|
||||
@ -111,17 +116,18 @@ export default function (socket: TSocket, io: Server) {
|
||||
}))
|
||||
)
|
||||
.png()
|
||||
.toBuffer();
|
||||
.toBuffer()
|
||||
|
||||
// Save the combined image
|
||||
const filename = path.join(public_folder, `${action}.png`);
|
||||
await writeFile(filename, combinedImage);
|
||||
}));
|
||||
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)
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user