1
0
forked from noxious/server
noxious_server/src/events/chat/gameMaster/teleportCommand.ts
2025-02-12 00:14:05 +01:00

98 lines
3.1 KiB
TypeScript

import type { UUID } from '@/application/types'
import { BaseEvent } from '@/application/base/baseEvent'
import { SocketEvent } from '@/application/enums'
import MapManager from '@/managers/mapManager'
import MapRepository from '@/repositories/mapRepository'
import TeleportService from '@/services/characterTeleportService'
import ChatService from '@/services/chatService'
type TypePayload = {
message: string
}
export default class TeleportCommandEvent extends BaseEvent {
public listen(): void {
this.socket.on(SocketEvent.CHAT_MESSAGE, this.handleEvent.bind(this))
}
private async handleEvent(data: TypePayload, callback: (response: boolean) => void) {
try {
// Check if command is teleport
if (!ChatService.isCommand(data.message, 'teleport')) return
// Check if character exists and is GM
if (!(await this.isCharacterGM())) return
const character = await this.getCharacter()
if (!character) return
const args = ChatService.getArgs('teleport', data.message)
if (!args || args.length === 0 || args.length > 3) {
this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message',
message: 'Usage: /teleport <mapId> [x] [y]'
})
return
}
const mapId = args[0] as UUID
const targetX = args[1] ? parseInt(args[1], 10) : 0
const targetY = args[2] ? parseInt(args[2], 10) : 0
if (!mapId || isNaN(targetX) || isNaN(targetY)) {
this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message',
message: 'Invalid parameters. X and Y coordinates must be numbers.'
})
return
}
const mapRepository = new MapRepository()
const map = await mapRepository.getById(mapId)
if (!map) {
this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message',
message: 'Map not found'
})
return
}
if (character.map.id === map.id && targetX === character.positionX && targetY === character.positionY) {
this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message',
message: 'You are already at that location'
})
return
}
const success = await TeleportService.teleportCharacter(character.id, {
targetMapId: map.id,
targetX,
targetY,
rotation: character.rotation
})
if (!success) {
return this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message',
message: 'Failed to teleport'
})
}
this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message',
message: `Teleported to ${map.name} (${targetX}, ${targetY})`
})
this.logger.info('teleport', `Character ${character.id} teleported to map ${map.id} at position (${targetX}, ${targetY})`)
} catch (error: any) {
this.logger.error(`Error in teleport command: ${error.message}`)
this.socket.emit(SocketEvent.NOTIFICATION, {
title: 'Server message',
message: 'An error occurred while teleporting'
})
}
}
}