forked from noxious/server
95 lines
3.3 KiB
TypeScript
95 lines
3.3 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { ExtendedCharacter, TSocket } from '../../../utilities/types'
|
|
import { getArgs, isCommand } from '../../../utilities/chat'
|
|
import ZoneRepository from '../../../repositories/zoneRepository'
|
|
import CharacterManager from '../../../managers/characterManager'
|
|
import { gameLogger, gameMasterLogger } from '../../../utilities/logger'
|
|
import CharacterRepository from '../../../repositories/characterRepository'
|
|
|
|
type TypePayload = {
|
|
message: string
|
|
}
|
|
|
|
export default class TeleportCommandEvent {
|
|
constructor(
|
|
private readonly io: Server,
|
|
private readonly socket: TSocket
|
|
) {}
|
|
|
|
public listen(): void {
|
|
this.socket.on('chat:send_message', this.handleTeleportCommand.bind(this))
|
|
}
|
|
|
|
private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
|
try {
|
|
// Check if character exists
|
|
const character = await CharacterRepository.getByUserAndId(this.socket.user?.id as number, this.socket.characterId as number) as ExtendedCharacter
|
|
if (!character) {
|
|
gameLogger.error('chat:alert_command error', 'Character not found')
|
|
callback(false)
|
|
return
|
|
}
|
|
|
|
// Check if the user is the GM
|
|
if (character.role !== 'gm') {
|
|
gameLogger.info(`User ${character.id} tried to set time but is not a game master.`)
|
|
callback(false)
|
|
return
|
|
}
|
|
|
|
if (!isCommand(data.message, 'teleport')) return
|
|
|
|
const args = getArgs('teleport', data.message)
|
|
|
|
if (!args || args.length !== 1) {
|
|
this.socket.emit('notification', { title: 'Server message', message: 'Usage: /teleport <zoneId>' })
|
|
return
|
|
}
|
|
|
|
const zoneId = parseInt(args[0], 10)
|
|
if (isNaN(zoneId)) {
|
|
this.socket.emit('notification', { title: 'Server message', message: 'Invalid zone ID' })
|
|
return
|
|
}
|
|
|
|
const zone = await ZoneRepository.getById(zoneId)
|
|
if (!zone) {
|
|
this.socket.emit('notification', { title: 'Server message', message: 'Zone not found' })
|
|
return
|
|
}
|
|
|
|
if (character.zoneId === zone.id) {
|
|
this.socket.emit('notification', { title: 'Server message', message: 'You are already in that zone' })
|
|
return
|
|
}
|
|
|
|
// Remove character from current zone
|
|
this.io.to(character.zoneId.toString()).emit('zone:character:leave', character.id)
|
|
this.socket.leave(character.zoneId.toString())
|
|
|
|
// Add character to new zone
|
|
this.io.to(zone.id.toString()).emit('zone:character:join', character)
|
|
this.socket.join(zone.id.toString())
|
|
|
|
character.zoneId = zone.id
|
|
character.positionX = 0
|
|
character.positionY = 0
|
|
|
|
character.resetMovement = true
|
|
|
|
this.socket.emit('zone:character:teleport', {
|
|
zone,
|
|
characters: CharacterManager.getCharactersInZone(zone)
|
|
})
|
|
|
|
this.socket.emit('notification', { title: 'Server message', message: `You have been teleported to ${zone.name}` })
|
|
gameMasterLogger.info('teleport', `Character ${character.id} teleported to zone ${zone.id}`)
|
|
|
|
callback(true)
|
|
} catch (error: any) {
|
|
gameMasterLogger.error(`Error in teleport command: ${error.message}`)
|
|
this.socket.emit('notification', { title: 'Server message', message: 'An error occurred while teleporting' })
|
|
}
|
|
}
|
|
}
|