Added hacked together TP command for testing purposes (:teleport (zoneId))
This commit is contained in:
92
src/events/chat/gameMaster/teleportCommand.ts
Normal file
92
src/events/chat/gameMaster/teleportCommand.ts
Normal file
@ -0,0 +1,92 @@
|
||||
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 (!isCommand(data.message, 'teleport')) return
|
||||
|
||||
const args = getArgs('teleport', data.message)
|
||||
|
||||
if (!args || args.length !== 1) {
|
||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'Usage: /teleport <zoneId>' })
|
||||
return
|
||||
}
|
||||
|
||||
const zoneId = parseInt(args[0], 10)
|
||||
if (isNaN(zoneId)) {
|
||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'Invalid zone ID' })
|
||||
return
|
||||
}
|
||||
|
||||
const character = await CharacterRepository.getByUserAndId(this.socket.user?.id as number, this.socket.character?.id as number)
|
||||
if (!character) {
|
||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'Character not found' })
|
||||
return
|
||||
}
|
||||
|
||||
const zone = await ZoneRepository.getById(zoneId)
|
||||
if (!zone) {
|
||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'Zone not found' })
|
||||
return
|
||||
}
|
||||
|
||||
this.io.to(character.zoneId.toString()).emit('zone:character:leave', character.id)
|
||||
this.io.to(zone.id.toString()).emit('zone:character:join', character)
|
||||
await CharacterManager.removeCharacter(character as ExtendedCharacter)
|
||||
|
||||
this.socket.leave(character.zoneId.toString())
|
||||
this.socket.join(zone.id.toString())
|
||||
|
||||
// Add character to new zone
|
||||
character.zoneId = zone.id
|
||||
character.positionX = 0
|
||||
character.positionY = 0
|
||||
|
||||
// Update character in database
|
||||
await prisma.character.update({
|
||||
where: { id: character.id },
|
||||
data: {
|
||||
zoneId: character.zoneId,
|
||||
positionX: 0,
|
||||
positionY: 0
|
||||
}
|
||||
})
|
||||
|
||||
// Update character in CharacterManager
|
||||
CharacterManager.initCharacter(character as ExtendedCharacter)
|
||||
|
||||
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 ${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: 'Teleport Error', message: 'An error occurred while teleporting' })
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user