1
0
forked from noxious/server
noxious_server/src/repositories/mapRepository.ts

79 lines
2.5 KiB
TypeScript

import { BaseRepository } from '#application/base/baseRepository'
import { UUID } from '#application/types'
import { Map } from '#entities/map'
import { MapEventTile } from '#entities/mapEventTile'
import { MapObject } from '#entities/mapObject'
class MapRepository extends BaseRepository {
async getFirst(): Promise<Map | null> {
try {
const repository = this.getEntityManager().getRepository(Map)
const result = await repository.findOne({ id: { $exists: true } })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get first map: ${error instanceof Error ? error.message : String(error)}`)
return null
}
}
async getAll(): Promise<Map[]> {
try {
const repository = this.getEntityManager().getRepository(Map)
const results = await repository.findAll()
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get all map: ${error.message}`)
return []
}
}
async getById(id: UUID) {
try {
const repository = this.getEntityManager().getRepository(Map)
const result = await repository.findOne({ id })
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get map by id: ${error.message}`)
return null
}
}
async getEventTiles(id: UUID): Promise<MapEventTile[]> {
try {
const repository = this.getEntityManager().getRepository(MapEventTile)
const results = await repository.find({ map: id })
for (const result of results) result.setEntityManager(this.getEntityManager())
return results
} catch (error: any) {
this.logger.error(`Failed to get map event tiles: ${error.message}`)
return []
}
}
async getFirstEventTile(mapId: UUID, positionX: number, positionY: number): Promise<MapEventTile | null> {
try {
const repository = this.getEntityManager().getRepository(MapEventTile)
const result = await repository.findOne({
map: mapId,
positionX: positionX,
positionY: positionY
})
if (result) result.setEntityManager(this.getEntityManager())
return result
} catch (error: any) {
this.logger.error(`Failed to get map event tile: ${error.message}`)
return null
}
}
}
export default MapRepository