forked from noxious/server
84 lines
3.0 KiB
TypeScript
84 lines
3.0 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { ExtendedCharacter, TSocket } from '../../../utilities/types'
|
|
import { getArgs, isCommand } from '../../../utilities/chat'
|
|
import CharacterRepository from '../../../repositories/characterRepository'
|
|
import ZoneRepository from '../../../repositories/zoneRepository'
|
|
import CharacterManager from '../../../managers/characterManager'
|
|
import logger from '../../../utilities/logger'
|
|
import prisma from '../../../utilities/prisma'
|
|
|
|
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 {
|
|
if (!this.socket.character) {
|
|
this.socket.emit('notification', { title: 'Server message', message: 'Character not found' })
|
|
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
|
|
}
|
|
|
|
// Remove character from current zone
|
|
this.io.to(this.socket.character.zoneId.toString()).emit('zone:character:leave', this.socket.character.id)
|
|
this.socket.leave(this.socket.character.zoneId.toString())
|
|
await CharacterManager.removeCharacter(this.socket.character)
|
|
|
|
// Add character to new zone
|
|
this.io.to(zone.id.toString()).emit('zone:character:join', this.socket.character)
|
|
this.socket.join(zone.id.toString())
|
|
|
|
this.socket.character.zoneId = zone.id
|
|
this.socket.character.positionX = 0
|
|
this.socket.character.positionY = 0
|
|
|
|
// Update character in CharacterManager
|
|
CharacterManager.initCharacter(this.socket.character)
|
|
|
|
this.io.to(this.socket.id).emit('zone:character:load_assets', zone.id);
|
|
|
|
this.socket.emit('zone:teleport', {
|
|
zone,
|
|
characters: CharacterManager.getCharactersInZone(zone)
|
|
})
|
|
|
|
this.socket.emit('notification', { title: 'Server message', message: `You have been teleported to ${zone.name}` })
|
|
logger.info('teleport', `Character ${this.socket.character.id} teleported to zone ${zone.id}`)
|
|
|
|
callback(true)
|
|
} catch (error: any) {
|
|
logger.error(`Error in teleport command: ${error.message}`)
|
|
this.socket.emit('notification', { title: 'Server message', message: 'An error occurred while teleporting' })
|
|
}
|
|
}
|
|
} |