forked from noxious/server
Fixed up teleport command
This commit is contained in:
parent
117216acd3
commit
29dad4914a
@ -23,57 +23,47 @@ export default class TeleportCommandEvent {
|
|||||||
|
|
||||||
private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
if (!this.socket.character) {
|
||||||
|
this.socket.emit('notification', { title: 'Server message', message: 'Character not found' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!isCommand(data.message, 'teleport')) return
|
if (!isCommand(data.message, 'teleport')) return
|
||||||
|
|
||||||
const args = getArgs('teleport', data.message)
|
const args = getArgs('teleport', data.message)
|
||||||
|
|
||||||
if (!args || args.length !== 1) {
|
if (!args || args.length !== 1) {
|
||||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'Usage: /teleport <zoneId>' })
|
this.socket.emit('notification', { title: 'Server message', message: 'Usage: /teleport <zoneId>' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const zoneId = parseInt(args[0], 10)
|
const zoneId = parseInt(args[0], 10)
|
||||||
if (isNaN(zoneId)) {
|
if (isNaN(zoneId)) {
|
||||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'Invalid zone ID' })
|
this.socket.emit('notification', { title: 'Server message', 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const zone = await ZoneRepository.getById(zoneId)
|
const zone = await ZoneRepository.getById(zoneId)
|
||||||
if (!zone) {
|
if (!zone) {
|
||||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'Zone not found' })
|
this.socket.emit('notification', { title: 'Server message', message: 'Zone not found' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.io.to(character.zoneId.toString()).emit('zone:character:leave', character.id)
|
// Remove character from current zone
|
||||||
this.io.to(zone.id.toString()).emit('zone:character:join', character)
|
this.io.to(this.socket.character.zoneId.toString()).emit('zone:character:leave', this.socket.character.id)
|
||||||
await CharacterManager.removeCharacter(character as ExtendedCharacter)
|
this.socket.leave(this.socket.character.zoneId.toString())
|
||||||
|
await CharacterManager.removeCharacter(this.socket.character)
|
||||||
this.socket.leave(character.zoneId.toString())
|
|
||||||
this.socket.join(zone.id.toString())
|
|
||||||
|
|
||||||
// Add character to new zone
|
// Add character to new zone
|
||||||
character.zoneId = zone.id
|
this.io.to(zone.id.toString()).emit('zone:character:join', this.socket.character)
|
||||||
character.positionX = 0
|
this.socket.join(zone.id.toString())
|
||||||
character.positionY = 0
|
|
||||||
|
|
||||||
// Update character in database
|
this.socket.character.zoneId = zone.id
|
||||||
await prisma.character.update({
|
this.socket.character.positionX = 0
|
||||||
where: { id: character.id },
|
this.socket.character.positionY = 0
|
||||||
data: {
|
|
||||||
zoneId: character.zoneId,
|
|
||||||
positionX: 0,
|
|
||||||
positionY: 0
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Update character in CharacterManager
|
// Update character in CharacterManager
|
||||||
CharacterManager.initCharacter(character as ExtendedCharacter)
|
CharacterManager.initCharacter(this.socket.character)
|
||||||
|
|
||||||
this.socket.emit('zone:teleport', {
|
this.socket.emit('zone:teleport', {
|
||||||
zone,
|
zone,
|
||||||
@ -81,12 +71,12 @@ export default class TeleportCommandEvent {
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.socket.emit('notification', { title: 'Server message', message: `You have been teleported to ${zone.name}` })
|
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}`)
|
logger.info('teleport', `Character ${this.socket.character.id} teleported to zone ${zone.id}`)
|
||||||
|
|
||||||
callback(true)
|
callback(true)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.error(`Error in teleport command: ${error.message}`)
|
logger.error(`Error in teleport command: ${error.message}`)
|
||||||
this.socket.emit('notification', { title: 'Teleport Error', message: 'An error occurred while teleporting' })
|
this.socket.emit('notification', { title: 'Server message', message: 'An error occurred while teleporting' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user