npm update, http asset endpoint changes
This commit is contained in:
@ -3,95 +3,12 @@ import UserService from '../services/userService'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import config from './config'
|
||||
import { loginAccountSchema, registerAccountSchema } from './zodTypes'
|
||||
import { TAsset } from './types'
|
||||
import tileRepository from '../repositories/tileRepository'
|
||||
import objectRepository from '../repositories/objectRepository'
|
||||
import spriteRepository from '../repositories/spriteRepository'
|
||||
import fs from 'fs'
|
||||
import { httpLogger } from './logger'
|
||||
import { getPublicPath } from './storage'
|
||||
import zoneRepository from '../repositories/zoneRepository'
|
||||
|
||||
async function addHttpRoutes(app: Application) {
|
||||
/**
|
||||
* Get all base sprite, assets
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
app.get('/assets', async (req: Request, res: Response) => {
|
||||
let assets: TAsset[] = []
|
||||
|
||||
const sprites = await spriteRepository.getAll()
|
||||
const tiles = await tileRepository.getAll()
|
||||
const objects = await objectRepository.getAll()
|
||||
|
||||
// sprites all contain spriteActions, loop through these
|
||||
sprites.forEach((sprite) => {
|
||||
sprite.spriteActions.forEach((spriteAction) => {
|
||||
assets.push({
|
||||
key: sprite.id + '-' + spriteAction.action,
|
||||
url: '/assets/sprites/' + sprite.id + '/' + spriteAction.action + '.png',
|
||||
group: spriteAction.isAnimated ? 'sprite_animations' : 'sprites',
|
||||
updatedAt: sprite.updatedAt,
|
||||
frameCount: JSON.parse(JSON.stringify(spriteAction.sprites)).length,
|
||||
frameWidth: spriteAction.frameWidth,
|
||||
frameHeight: spriteAction.frameHeight,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Get all tiles
|
||||
tiles.forEach((tile) => {
|
||||
assets.push({
|
||||
key: tile.id,
|
||||
url: '/assets/tiles/' + tile.id + '.png',
|
||||
group: 'tiles',
|
||||
updatedAt: tile.updatedAt
|
||||
})
|
||||
})
|
||||
|
||||
// Get all objects
|
||||
objects.forEach((object) => {
|
||||
assets.push({
|
||||
key: object.id,
|
||||
url: '/assets/objects/' + object.id + '.png',
|
||||
group: 'objects',
|
||||
updatedAt: object.updatedAt
|
||||
})
|
||||
})
|
||||
|
||||
res.json(assets)
|
||||
})
|
||||
|
||||
/**
|
||||
* Get a specific asset
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
app.get('/assets/:type/:spriteId?/:file', (req: Request, res: Response) => {
|
||||
const assetType = req.params.type
|
||||
const spriteId = req.params.spriteId
|
||||
const fileName = req.params.file
|
||||
|
||||
let assetPath
|
||||
if (assetType === 'sprites' && spriteId) {
|
||||
assetPath = getPublicPath(assetType, spriteId, fileName)
|
||||
} else {
|
||||
assetPath = getPublicPath(assetType, fileName)
|
||||
}
|
||||
|
||||
if (!fs.existsSync(assetPath)) {
|
||||
httpLogger.error(`File not found: ${assetPath}`)
|
||||
return res.status(404).send('Asset not found')
|
||||
}
|
||||
|
||||
res.sendFile(assetPath, (err) => {
|
||||
if (err) {
|
||||
httpLogger.error('Error sending file:', err)
|
||||
res.status(500).send('Error downloading the asset')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* Login
|
||||
* @param req
|
||||
@ -141,6 +58,70 @@ async function addHttpRoutes(app: Application) {
|
||||
return res.status(400).json({ message: 'Failed to register user' })
|
||||
})
|
||||
|
||||
/**
|
||||
* Get all tiles from a zone as an array of ids
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
app.get('/assets/tiles/:zoneId', async (req: Request, res: Response) => {
|
||||
const zoneId = req.params.zoneId
|
||||
|
||||
// Check if zoneId is valid number
|
||||
if (!zoneId || parseInt(zoneId) === 0) {
|
||||
return res.status(400).json({ message: 'Invalid zone ID' })
|
||||
}
|
||||
|
||||
// Get zone by id
|
||||
const zone = await zoneRepository.getById(parseInt(zoneId))
|
||||
if (!zone) {
|
||||
return res.status(404).json({ message: 'Zone not found' })
|
||||
}
|
||||
|
||||
let tiles = zone.tiles;
|
||||
|
||||
// Convert to array
|
||||
tiles = JSON.parse(JSON.stringify(tiles)) as string[]
|
||||
|
||||
// Flatten the array
|
||||
tiles = [...new Set(tiles.flat())]
|
||||
|
||||
// Remove duplicates
|
||||
tiles = tiles.filter((value, index, self) => self.indexOf(value) === index);
|
||||
|
||||
// Return the array
|
||||
res.json(tiles)
|
||||
})
|
||||
|
||||
/**
|
||||
* Get a specific asset
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
app.get('/assets/:type/:spriteId?/:file', (req: Request, res: Response) => {
|
||||
const assetType = req.params.type
|
||||
const spriteId = req.params.spriteId
|
||||
const fileName = req.params.file
|
||||
|
||||
let assetPath
|
||||
if (assetType === 'sprites' && spriteId) {
|
||||
assetPath = getPublicPath(assetType, spriteId, fileName)
|
||||
} else {
|
||||
assetPath = getPublicPath(assetType, fileName)
|
||||
}
|
||||
|
||||
if (!fs.existsSync(assetPath)) {
|
||||
httpLogger.error(`File not found: ${assetPath}`)
|
||||
return res.status(404).send('Asset not found')
|
||||
}
|
||||
|
||||
res.sendFile(assetPath, (err) => {
|
||||
if (err) {
|
||||
httpLogger.error('Error sending file:', err)
|
||||
res.status(500).send('Error downloading the asset')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
httpLogger.info('Web routes added')
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user