45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../../../utilities/types'
|
|
import { Zone } from '@prisma/client'
|
|
import ZoneRepository from '../../../repositories/zoneRepository'
|
|
import CharacterRepository from '../../../repositories/characterRepository'
|
|
import { gameMasterLogger } from '../../../utilities/logger'
|
|
|
|
interface IPayload {}
|
|
|
|
export default class ZoneListEvent {
|
|
constructor(
|
|
private readonly io: Server,
|
|
private readonly socket: TSocket
|
|
) {}
|
|
|
|
public listen(): void {
|
|
this.socket.on('gm:zone_editor:zone:list', this.handleZoneList.bind(this))
|
|
}
|
|
|
|
private async handleZoneList(data: IPayload, callback: (response: Zone[]) => void): Promise<void> {
|
|
try {
|
|
const character = await CharacterRepository.getById(this.socket.characterId as number)
|
|
if (!character) {
|
|
gameMasterLogger.error('gm:zone_editor:zone:list error', 'Character not found')
|
|
callback([])
|
|
return
|
|
}
|
|
|
|
if (character.role !== 'gm') {
|
|
gameMasterLogger.info(`User ${character.id} tried to list zones but is not a game master.`)
|
|
callback([])
|
|
return
|
|
}
|
|
|
|
gameMasterLogger.info(`User ${character.id} has requested zone list via zone editor.`)
|
|
|
|
const zones = await ZoneRepository.getAll()
|
|
callback(zones)
|
|
} catch (error: any) {
|
|
gameMasterLogger.error('gm:zone_editor:zone:list error', error.message)
|
|
callback([])
|
|
}
|
|
}
|
|
}
|