Worked on http endpoints for dynamic tile loading
This commit is contained in:
@ -6,7 +6,9 @@ import { loginAccountSchema, registerAccountSchema } from './zodTypes'
|
||||
import fs from 'fs'
|
||||
import { httpLogger } from './logger'
|
||||
import { getPublicPath } from './storage'
|
||||
import zoneRepository from '../repositories/zoneRepository'
|
||||
import TileRepository from '../repositories/tileRepository'
|
||||
import { TAsset } from './types'
|
||||
import ZoneRepository from '../repositories/zoneRepository'
|
||||
|
||||
async function addHttpRoutes(app: Application) {
|
||||
/**
|
||||
@ -59,38 +61,61 @@ async function addHttpRoutes(app: Application) {
|
||||
})
|
||||
|
||||
/**
|
||||
* Get all tiles from a zone as an array of ids
|
||||
* Get all tiles
|
||||
* @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)
|
||||
// })
|
||||
app.get('/assets/list_tiles', async (req: Request, res: Response) => {
|
||||
// Get all tiles
|
||||
let assets: TAsset[] = []
|
||||
const tiles = await TileRepository.getAll()
|
||||
for (const tile of tiles) {
|
||||
assets.push({
|
||||
key: tile.id,
|
||||
url: '/assets/tiles/' + tile.id + '.png',
|
||||
group: 'tiles',
|
||||
updatedAt: tile.updatedAt
|
||||
})
|
||||
}
|
||||
|
||||
// Return the array
|
||||
res.json(tiles)
|
||||
})
|
||||
|
||||
/**
|
||||
* Get all tiles
|
||||
* @param req
|
||||
* @param res
|
||||
*/
|
||||
app.get('/assets/list_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' })
|
||||
}
|
||||
|
||||
// Get all tiles
|
||||
let assets: TAsset[] = []
|
||||
const tiles = await TileRepository.getByZoneId(parseInt(zoneId))
|
||||
for (const tile of tiles) {
|
||||
assets.push({
|
||||
key: tile.id,
|
||||
url: '/assets/tiles/' + tile.id + '.png',
|
||||
group: 'tiles',
|
||||
updatedAt: tile.updatedAt
|
||||
})
|
||||
}
|
||||
|
||||
// Return the array
|
||||
res.json(tiles)
|
||||
})
|
||||
|
||||
/**
|
||||
* Get a specific asset
|
||||
|
3
src/utilities/utilities.ts
Normal file
3
src/utilities/utilities.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function unduplicateArray(array: any[]) {
|
||||
return [...new Set(array.flat())]
|
||||
}
|
9
src/utilities/zone.ts
Normal file
9
src/utilities/zone.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export function FlattenZoneArray(tiles: string[][]) {
|
||||
const normalArray = []
|
||||
|
||||
for (const row of tiles) {
|
||||
normalArray.push(...row)
|
||||
}
|
||||
|
||||
return normalArray
|
||||
}
|
Reference in New Issue
Block a user