Worked on http endpoints for dynamic tile loading
This commit is contained in:
parent
8f8f019ab7
commit
8460d0b535
@ -1,5 +1,8 @@
|
|||||||
import prisma from '../utilities/prisma' // Import the global Prisma instance
|
import prisma from '../utilities/prisma' // Import the global Prisma instance
|
||||||
import { Tile } from '@prisma/client'
|
import { Tile } from '@prisma/client'
|
||||||
|
import zoneRepository from './zoneRepository'
|
||||||
|
import { unduplicateArray } from '../utilities/utilities'
|
||||||
|
import { FlattenZoneArray } from '../utilities/zone'
|
||||||
|
|
||||||
class TileRepository {
|
class TileRepository {
|
||||||
async getById(id: string): Promise<Tile | null> {
|
async getById(id: string): Promise<Tile | null> {
|
||||||
@ -8,9 +11,28 @@ class TileRepository {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getByIds(ids: string[]): Promise<Tile[]> {
|
||||||
|
return prisma.tile.findMany({
|
||||||
|
where: {
|
||||||
|
id: {
|
||||||
|
in: ids
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async getAll(): Promise<Tile[]> {
|
async getAll(): Promise<Tile[]> {
|
||||||
return prisma.tile.findMany()
|
return prisma.tile.findMany()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getByZoneId(zoneId: number): Promise<Tile[]> {
|
||||||
|
const zone = await zoneRepository.getById(zoneId)
|
||||||
|
if (!zone) return []
|
||||||
|
|
||||||
|
const zoneTileArray = unduplicateArray(FlattenZoneArray(JSON.parse(JSON.stringify(zone.tiles))))
|
||||||
|
|
||||||
|
return this.getByIds(zoneTileArray)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new TileRepository()
|
export default new TileRepository()
|
||||||
|
@ -91,52 +91,6 @@ class ZoneRepository {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getZoneAssets(zone_id: number): Promise<TAsset[]> {
|
|
||||||
const zone = await this.getById(zone_id)
|
|
||||||
if (!zone) return []
|
|
||||||
|
|
||||||
const assets: TAsset[] = []
|
|
||||||
|
|
||||||
// zone.tiles is prisma jsonvalue
|
|
||||||
let tiles = JSON.parse(JSON.stringify(zone.tiles))
|
|
||||||
tiles = [...new Set(tiles.flat())]
|
|
||||||
|
|
||||||
// Add tile assets
|
|
||||||
for (const tile of tiles) {
|
|
||||||
const tileInfo = await tileRepository.getById(tile)
|
|
||||||
if (!tileInfo) continue
|
|
||||||
|
|
||||||
assets.push({
|
|
||||||
key: tileInfo.id,
|
|
||||||
url: '/assets/tiles/' + tileInfo.id + '.png',
|
|
||||||
group: 'tiles',
|
|
||||||
updatedAt: tileInfo?.updatedAt || new Date()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add object assets
|
|
||||||
for (const zoneObject of zone.zoneObjects) {
|
|
||||||
if (!zoneObject.object) continue
|
|
||||||
|
|
||||||
assets.push({
|
|
||||||
key: zoneObject.object.id,
|
|
||||||
url: '/assets/objects/' + zoneObject.object.id + '.png',
|
|
||||||
group: 'objects',
|
|
||||||
updatedAt: zoneObject.object.updatedAt || new Date()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter out duplicate assets
|
|
||||||
return assets.reduce((acc: TAsset[], current) => {
|
|
||||||
const x = acc.find((item) => item.key === current.key && item.group === current.group)
|
|
||||||
if (!x) {
|
|
||||||
return acc.concat([current])
|
|
||||||
} else {
|
|
||||||
return acc
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new ZoneRepository()
|
export default new ZoneRepository()
|
||||||
|
@ -1,4 +1,14 @@
|
|||||||
import prisma from '../utilities/prisma'
|
import prisma from '../utilities/prisma'
|
||||||
|
import { TAsset } from '../utilities/types'
|
||||||
|
import tileRepository from '../repositories/tileRepository'
|
||||||
|
import zoneRepository from '../repositories/zoneRepository'
|
||||||
|
import { Object, Zone, ZoneObject } from '@prisma/client'
|
||||||
|
|
||||||
|
type getZoneAsetsZoneType = Zone & {
|
||||||
|
zoneObjects: (ZoneObject & {
|
||||||
|
object: Object
|
||||||
|
})[]
|
||||||
|
}
|
||||||
|
|
||||||
class ZoneService {
|
class ZoneService {
|
||||||
async createDemoZone(): Promise<boolean> {
|
async createDemoZone(): Promise<boolean> {
|
||||||
@ -27,6 +37,49 @@ class ZoneService {
|
|||||||
console.log('Demo zone created.')
|
console.log('Demo zone created.')
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getZoneAssets(zone: getZoneAsetsZoneType): Promise<TAsset[]> {
|
||||||
|
const assets: TAsset[] = []
|
||||||
|
|
||||||
|
// zone.tiles is prisma jsonvalue
|
||||||
|
let tiles = JSON.parse(JSON.stringify(zone.tiles))
|
||||||
|
tiles = [...new Set(tiles.flat())]
|
||||||
|
|
||||||
|
// Add tile assets
|
||||||
|
for (const tile of tiles) {
|
||||||
|
const tileInfo = await tileRepository.getById(tile)
|
||||||
|
if (!tileInfo) continue
|
||||||
|
|
||||||
|
assets.push({
|
||||||
|
key: tileInfo.id,
|
||||||
|
url: '/assets/tiles/' + tileInfo.id + '.png',
|
||||||
|
group: 'tiles',
|
||||||
|
updatedAt: tileInfo?.updatedAt || new Date()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add object assets
|
||||||
|
for (const zoneObject of zone.zoneObjects) {
|
||||||
|
if (!zoneObject.object) continue
|
||||||
|
|
||||||
|
assets.push({
|
||||||
|
key: zoneObject.object.id,
|
||||||
|
url: '/assets/objects/' + zoneObject.object.id + '.png',
|
||||||
|
group: 'objects',
|
||||||
|
updatedAt: zoneObject.object.updatedAt || new Date()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter out duplicate assets
|
||||||
|
return assets.reduce((acc: TAsset[], current) => {
|
||||||
|
const x = acc.find((item) => item.key === current.key && item.group === current.group)
|
||||||
|
if (!x) {
|
||||||
|
return acc.concat([current])
|
||||||
|
} else {
|
||||||
|
return acc
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ZoneService
|
export default ZoneService
|
||||||
|
@ -6,7 +6,9 @@ import { loginAccountSchema, registerAccountSchema } from './zodTypes'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { httpLogger } from './logger'
|
import { httpLogger } from './logger'
|
||||||
import { getPublicPath } from './storage'
|
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) {
|
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 req
|
||||||
* @param res
|
* @param res
|
||||||
*/
|
*/
|
||||||
// app.get('/assets/tiles/:zoneId', async (req: Request, res: Response) => {
|
app.get('/assets/list_tiles', async (req: Request, res: Response) => {
|
||||||
// const zoneId = req.params.zoneId
|
// Get all tiles
|
||||||
//
|
let assets: TAsset[] = []
|
||||||
// // Check if zoneId is valid number
|
const tiles = await TileRepository.getAll()
|
||||||
// if (!zoneId || parseInt(zoneId) === 0) {
|
for (const tile of tiles) {
|
||||||
// return res.status(400).json({ message: 'Invalid zone ID' })
|
assets.push({
|
||||||
// }
|
key: tile.id,
|
||||||
//
|
url: '/assets/tiles/' + tile.id + '.png',
|
||||||
// // Get zone by id
|
group: 'tiles',
|
||||||
// const zone = await zoneRepository.getById(parseInt(zoneId))
|
updatedAt: tile.updatedAt
|
||||||
// if (!zone) {
|
})
|
||||||
// return res.status(404).json({ message: 'Zone not found' })
|
}
|
||||||
// }
|
|
||||||
//
|
// Return the array
|
||||||
// let tiles = zone.tiles;
|
res.json(tiles)
|
||||||
//
|
})
|
||||||
// // Convert to array
|
|
||||||
// tiles = JSON.parse(JSON.stringify(tiles)) as string[]
|
/**
|
||||||
//
|
* Get all tiles
|
||||||
// // Flatten the array
|
* @param req
|
||||||
// tiles = [...new Set(tiles.flat())]
|
* @param res
|
||||||
//
|
*/
|
||||||
// // Remove duplicates
|
app.get('/assets/list_tiles/:zoneId', async (req: Request, res: Response) => {
|
||||||
// tiles = tiles.filter((value, index, self) => self.indexOf(value) === index);
|
const zoneId = req.params.zoneId
|
||||||
//
|
|
||||||
// // Return the array
|
// Check if zoneId is valid number
|
||||||
// res.json(tiles)
|
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
|
* 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user