forked from noxious/server
87 lines
2.1 KiB
TypeScript
87 lines
2.1 KiB
TypeScript
import { Zone, ZoneEventTile, ZoneEventTileType, ZoneObject } from '@prisma/client'
|
|
import prisma from '#utilities/prisma'
|
|
import { ZoneEventTileWithTeleport } from '#utilities/types'
|
|
import { appLogger } from '#utilities/logger'
|
|
|
|
class ZoneRepository {
|
|
async getAll(): Promise<Zone[]> {
|
|
try {
|
|
return await prisma.zone.findMany()
|
|
} catch (error: any) {
|
|
appLogger.error(`Failed to get all zone: ${error.message}`)
|
|
return []
|
|
}
|
|
}
|
|
|
|
async getById(id: number) {
|
|
try {
|
|
return await prisma.zone.findUnique({
|
|
where: {
|
|
id: id
|
|
},
|
|
include: {
|
|
zoneEventTiles: {
|
|
include: {
|
|
zone: true,
|
|
teleport: true
|
|
}
|
|
},
|
|
zoneObjects: {
|
|
include: {
|
|
object: true
|
|
}
|
|
},
|
|
zoneEffects: true
|
|
}
|
|
})
|
|
} catch (error: any) {
|
|
appLogger.error(`Failed to get zone by id: ${error.message}`)
|
|
return null
|
|
}
|
|
}
|
|
|
|
async getEventTiles(id: number): Promise<ZoneEventTile[]> {
|
|
try {
|
|
return await prisma.zoneEventTile.findMany({
|
|
where: {
|
|
zoneId: id
|
|
}
|
|
})
|
|
} catch (error: any) {
|
|
appLogger.error(`Failed to get zone event tiles: ${error.message}`)
|
|
return []
|
|
}
|
|
}
|
|
|
|
async getFirstEventTile(zoneId: number, positionX: number, positionY: number): Promise<ZoneEventTile | null> {
|
|
try {
|
|
return await prisma.zoneEventTile.findFirst({
|
|
where: {
|
|
zoneId: zoneId,
|
|
positionX: positionX,
|
|
positionY: positionY
|
|
},
|
|
include: { teleport: true }
|
|
})
|
|
} catch (error: any) {
|
|
appLogger.error(`Failed to get zone event tile: ${error.message}`)
|
|
return null
|
|
}
|
|
}
|
|
|
|
async getZoneObjects(id: number): Promise<ZoneObject[]> {
|
|
try {
|
|
return await prisma.zoneObject.findMany({
|
|
where: {
|
|
zoneId: id
|
|
}
|
|
})
|
|
} catch (error: any) {
|
|
appLogger.error(`Failed to get zone objects: ${error.message}`)
|
|
return []
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new ZoneRepository()
|