From 9949b51f3fb46f4aac92a3a6afd80af96254403e Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Fri, 20 Sep 2024 16:34:23 +0200 Subject: [PATCH] My 13th reason (2.0) --- src/utilities/http.ts | 50 +++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/utilities/http.ts b/src/utilities/http.ts index 818acdd..2af8718 100644 --- a/src/utilities/http.ts +++ b/src/utilities/http.ts @@ -14,25 +14,13 @@ import zoneRepository from '../repositories/zoneRepository' import zoneManager from '../managers/zoneManager' async function addHttpRoutes(app: Application) { - app.get('/assets', async (req: Request, res: Response) => { + /** + * Get all base sprite, assets + * @param req + * @param res + */ + app.get('/assets/sprites', async (req: Request, res: Response) => { let assets: TAsset[] = [] - const tiles = await tileRepository.getAll() - tiles.forEach((tile) => { - assets.push({ - key: tile.id, - url: '/assets/tiles/' + tile.id + '.png', - group: 'tiles' - }) - }) - - const objects = await objectRepository.getAll() - objects.forEach((object) => { - assets.push({ - key: object.id, - url: '/assets/objects/' + object.id + '.png', - group: 'objects' - }) - }) const sprites = await spriteRepository.getAll() // sprites all contain spriteActions, loop through these @@ -51,7 +39,12 @@ async function addHttpRoutes(app: Application) { res.json(assets) }) - app.get('/assets/:zoneId', async (req: Request, res: Response) => { + /** + * Get assets for a specific zone + * @param req + * @param res + */ + app.get('/assets/zone/:zoneId', async (req: Request, res: Response) => { const zoneId = req.params.zoneId if(!zoneId || parseInt(zoneId) === 0) { return res.status(400).json({ message: 'Invalid zone ID' }) @@ -78,6 +71,11 @@ async function addHttpRoutes(app: Application) { ]); }) + /** + * 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 @@ -91,18 +89,23 @@ async function addHttpRoutes(app: Application) { } if (!fs.existsSync(assetPath)) { - console.error(`File not found: ${assetPath}`) + logger.error(`File not found: ${assetPath}`) return res.status(404).send('Asset not found') } res.sendFile(assetPath, (err) => { if (err) { - console.error('Error sending file:', err) + logger.error('Error sending file:', err) res.status(500).send('Error downloading the asset') } }) }) + /** + * Login + * @param req + * @param res + */ app.post('/login', async (req: Request, res: Response) => { const { username, password } = req.body @@ -123,6 +126,11 @@ async function addHttpRoutes(app: Application) { return res.status(400).json({ message: 'Failed to login' }) }) + /** + * Register + * @param req + * @param res + */ app.post('/register', async (req: Request, res: Response) => { const { username, password } = req.body