npm run frmat

This commit is contained in:
Dennis Postma 2024-09-20 12:34:34 +02:00
parent 5a40fb4734
commit 2101d20835
6 changed files with 27 additions and 29 deletions

View File

@ -46,7 +46,7 @@ export default class ObjectUploadEvent {
originX: 0, originX: 0,
originY: 0, originY: 0,
frameWidth: width, frameWidth: width,
frameHeight: height, frameHeight: height
} }
}) })

View File

@ -27,16 +27,14 @@ class ZoneManager {
} }
public async getZoneAssets(zone: Zone): Promise<ZoneAssets> { public async getZoneAssets(zone: Zone): Promise<ZoneAssets> {
const tiles: string[] = this.getUnique( const tiles: string[] = this.getUnique((JSON.parse(JSON.stringify(zone.tiles)) as string[][]).reduce((acc, val) => [...acc, ...val]))
(JSON.parse(JSON.stringify(zone.tiles)) as string[][]).reduce((acc, val) => [...acc, ...val]) const objects = await zoneRepository.getObjects(zone.id)
); const mappedObjects = this.getUnique(objects.map((x) => x.objectId))
const objects = await zoneRepository.getObjects(zone.id);
const mappedObjects = this.getUnique(objects.map(x => x.objectId));
return { return {
tiles: tiles, tiles: tiles,
objects: mappedObjects, objects: mappedObjects
} as ZoneAssets; } as ZoneAssets
} }
private getUnique<T>(array: T[]) { private getUnique<T>(array: T[]) {
@ -47,7 +45,7 @@ class ZoneManager {
public async loadZone(zone: Zone) { public async loadZone(zone: Zone) {
const loadedZone = new LoadedZone(zone) const loadedZone = new LoadedZone(zone)
this.loadedZones.push(loadedZone) this.loadedZones.push(loadedZone)
await this.getZoneAssets(zone); await this.getZoneAssets(zone)
logger.info(`Zone ID ${zone.id} loaded`) logger.info(`Zone ID ${zone.id} loaded`)
} }

View File

@ -32,7 +32,7 @@ class LoadedZone {
} }
}) })
console.log(grid); console.log(grid)
return grid return grid
} }

View File

@ -63,13 +63,13 @@ class ZoneRepository {
async getEventTeleportTiles(id: number): Promise<ZoneEventTileWithTeleport[]> { async getEventTeleportTiles(id: number): Promise<ZoneEventTileWithTeleport[]> {
try { try {
return await prisma.zoneEventTile.findMany({ return (await prisma.zoneEventTile.findMany({
where: { where: {
zoneId: id, zoneId: id,
type: ZoneEventTileType.TELEPORT type: ZoneEventTileType.TELEPORT
}, },
include: { teleport: true } include: { teleport: true }
}) as unknown as ZoneEventTileWithTeleport[] })) as unknown as ZoneEventTileWithTeleport[]
} catch (error: any) { } catch (error: any) {
logger.error(`Failed to get zone event tiles: ${error.message}`) logger.error(`Failed to get zone event tiles: ${error.message}`)
return [] return []

View File

@ -52,45 +52,45 @@ async function addHttpRoutes(app: Application) {
}) })
app.get('/assets/:zoneId', async (req: Request, res: Response) => { app.get('/assets/:zoneId', async (req: Request, res: Response) => {
const zoneId = parseInt(req.params.zoneId); const zoneId = parseInt(req.params.zoneId)
if (isNaN(zoneId) || zoneId === 0) { if (isNaN(zoneId) || zoneId === 0) {
return res.status(400).json({ message: 'Invalid zone ID' }); return res.status(400).json({ message: 'Invalid zone ID' })
} }
const zone = await zoneRepository.getById(zoneId); const zone = await zoneRepository.getById(zoneId)
if (!zone) { if (!zone) {
return res.status(404).json({ message: 'Zone not found' }); return res.status(404).json({ message: 'Zone not found' })
} }
const assets = await zoneManager.getZoneAssets(zone); const assets = await zoneManager.getZoneAssets(zone)
const sprites = await spriteRepository.getAll(); const sprites = await spriteRepository.getAll()
const spritesAssets = sprites.flatMap(sprite => const spritesAssets = sprites.flatMap((sprite) =>
sprite.spriteActions.map(action => ({ sprite.spriteActions.map((action) => ({
key: `${sprite.id}-${action.action}`, key: `${sprite.id}-${action.action}`,
url: `/assets/sprites/${sprite.id}/${action.action}.png`, url: `/assets/sprites/${sprite.id}/${action.action}.png`,
group: action.isAnimated ? 'sprite_animations' : 'sprites', group: action.isAnimated ? 'sprite_animations' : 'sprites',
frameWidth: action.frameWidth, frameWidth: action.frameWidth,
frameHeight: action.frameHeight frameHeight: action.frameHeight
})) }))
); )
const tilesAssets = assets.tiles.map(tile => ({ const tilesAssets = assets.tiles.map((tile) => ({
key: tile, key: tile,
url: `/assets/tiles/${tile}.png`, url: `/assets/tiles/${tile}.png`,
group: 'tiles' group: 'tiles'
})); }))
const objectsAssets = assets.objects.map(object => ({ const objectsAssets = assets.objects.map((object) => ({
key: object, key: object,
url: `/assets/objects/${object}.png`, url: `/assets/objects/${object}.png`,
group: 'objects' group: 'objects'
})); }))
res.json([...spritesAssets, ...tilesAssets, ...objectsAssets]); res.json([...spritesAssets, ...tilesAssets, ...objectsAssets])
}); })
app.get('/assets/:type/:spriteId?/:file', (req: Request, res: Response) => { app.get('/assets/:type/:spriteId?/:file', (req: Request, res: Response) => {
const assetType = req.params.type const assetType = req.params.type