npm update, http asset endpoint changes
This commit is contained in:
@ -2,6 +2,8 @@ import { Zone, ZoneEventTile, ZoneEventTileType, ZoneObject } from '@prisma/clie
|
||||
import prisma from '../utilities/prisma'
|
||||
import { ZoneEventTileWithTeleport } from '../socketEvents/zone/characterMove'
|
||||
import { appLogger } from '../utilities/logger'
|
||||
import { TAsset } from '../utilities/types'
|
||||
import tileRepository from './tileRepository'
|
||||
|
||||
class ZoneRepository {
|
||||
async getFirst(): Promise<Zone | null> {
|
||||
@ -22,7 +24,7 @@ class ZoneRepository {
|
||||
}
|
||||
}
|
||||
|
||||
async getById(id: number): Promise<Zone | null> {
|
||||
async getById(id: number) {
|
||||
try {
|
||||
return await prisma.zone.findUnique({
|
||||
where: {
|
||||
@ -77,7 +79,7 @@ class ZoneRepository {
|
||||
}
|
||||
}
|
||||
|
||||
async getObjects(id: number): Promise<ZoneObject[]> {
|
||||
async getZoneObjects(id: number): Promise<ZoneObject[]> {
|
||||
try {
|
||||
return await prisma.zoneObject.findMany({
|
||||
where: {
|
||||
@ -89,6 +91,52 @@ class ZoneRepository {
|
||||
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()
|
||||
|
Reference in New Issue
Block a user