Files
server/src/events/gameMaster/mapEditor/request.ts
2025-01-05 04:01:43 +01:00

44 lines
1.5 KiB
TypeScript

import { BaseEvent } from '#application/base/baseEvent'
import { UUID } from '#application/types'
import { Map } from '#entities/map'
import MapRepository from '#repositories/mapRepository'
interface IPayload {
mapId: UUID
}
export default class MapRequestEvent extends BaseEvent {
public listen(): void {
this.socket.on('gm:map:request', this.handleEvent.bind(this))
}
private async handleEvent(data: IPayload, callback: (response: Map | null) => void): Promise<void> {
try {
if (!(await this.isCharacterGM())) return
this.logger.info(`User ${(await this.getCharacter())!.getId()} has requested map via map editor.`)
if (!data.mapId) {
this.logger.info(`User ${(await this.getCharacter())!.getId()} tried to request map but did not provide a map id.`)
return callback(null)
}
const mapRepository = new MapRepository()
const map = await mapRepository.getById(data.mapId)
if (!map) {
this.logger.info(`User ${(await this.getCharacter())!.getId()} tried to request map ${data.mapId} but it does not exist.`)
return callback(null)
}
// Populate map with mapEventTiles and placedMapObjects
await mapRepository.getEntityManager().populate(map, ['mapEffects', 'mapEventTiles', 'placedMapObjects', 'placedMapObjects.mapObject'])
return callback(map)
} catch (error: any) {
this.logger.error('gm:map:request error', error.message)
return callback(null)
}
}
}