101 lines
3.1 KiB
TypeScript
101 lines
3.1 KiB
TypeScript
import { BaseEvent } from '#application/base/baseEvent'
|
|
import { UUID } from '#application/types'
|
|
import ZoneManager from '#managers/zoneManager'
|
|
import ZoneRepository from '#repositories/zoneRepository'
|
|
import ChatService from '#services/chatService'
|
|
import TeleportService from '#services/teleportService'
|
|
|
|
type TypePayload = {
|
|
message: string
|
|
}
|
|
|
|
export default class TeleportCommandEvent extends BaseEvent {
|
|
public listen(): void {
|
|
this.socket.on('chat:message', this.handleEvent.bind(this))
|
|
}
|
|
|
|
private async handleEvent(data: TypePayload, callback: (response: boolean) => void) {
|
|
try {
|
|
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
|
|
if (!zoneCharacter) {
|
|
this.logger.error('chat:message error', 'Character not found')
|
|
return
|
|
}
|
|
|
|
const character = zoneCharacter.character
|
|
|
|
if (character.role !== 'gm') {
|
|
this.logger.info(`User ${character.id} tried to set time but is not a game master.`)
|
|
return
|
|
}
|
|
|
|
if (!ChatService.isCommand(data.message, 'teleport')) return
|
|
|
|
const args = ChatService.getArgs('teleport', data.message)
|
|
|
|
if (!args || args.length === 0 || args.length > 3) {
|
|
this.socket.emit('notification', {
|
|
title: 'Server message',
|
|
message: 'Usage: /teleport <zoneId> [x] [y]'
|
|
})
|
|
return
|
|
}
|
|
|
|
const zoneId = args[0] as UUID
|
|
const targetX = args[1] ? parseInt(args[1], 10) : 0
|
|
const targetY = args[2] ? parseInt(args[2], 10) : 0
|
|
|
|
if (!zoneId || isNaN(targetX) || isNaN(targetY)) {
|
|
this.socket.emit('notification', {
|
|
title: 'Server message',
|
|
message: 'Invalid parameters. X and Y coordinates must be numbers.'
|
|
})
|
|
return
|
|
}
|
|
|
|
const zone = await ZoneRepository.getById(zoneId)
|
|
if (!zone) {
|
|
this.socket.emit('notification', {
|
|
title: 'Server message',
|
|
message: 'Zone not found'
|
|
})
|
|
return
|
|
}
|
|
|
|
if (character.zone.id === zone.id && targetX === character.positionX && targetY === character.positionY) {
|
|
this.socket.emit('notification', {
|
|
title: 'Server message',
|
|
message: 'You are already at that location'
|
|
})
|
|
return
|
|
}
|
|
|
|
const success = await TeleportService.teleportCharacter(character.id, {
|
|
targetZoneId: zone.id,
|
|
targetX,
|
|
targetY,
|
|
rotation: character.rotation
|
|
})
|
|
|
|
if (!success) {
|
|
return this.socket.emit('notification', {
|
|
title: 'Server message',
|
|
message: 'Failed to teleport'
|
|
})
|
|
}
|
|
|
|
this.socket.emit('notification', {
|
|
title: 'Server message',
|
|
message: `Teleported to ${zone.name} (${targetX}, ${targetY})`
|
|
})
|
|
this.logger.info('teleport', `Character ${character.id} teleported to zone ${zone.id} at position (${targetX}, ${targetY})`)
|
|
} catch (error: any) {
|
|
this.logger.error(`Error in teleport command: ${error.message}`)
|
|
this.socket.emit('notification', {
|
|
title: 'Server message',
|
|
message: 'An error occurred while teleporting'
|
|
})
|
|
}
|
|
}
|
|
}
|