Joining, leaving rooms and teleporting works again + refactor

This commit is contained in:
2025-01-01 04:46:00 +01:00
parent 30b2028bd8
commit 495e9f192e
5 changed files with 175 additions and 97 deletions

View File

@ -1,9 +1,8 @@
import { BaseEvent } from '#application/base/baseEvent'
import ZoneManager from '#managers/zoneManager'
import zoneManager from '#managers/zoneManager'
import ZoneCharacter from '#models/zoneCharacter'
import ZoneRepository from '#repositories/zoneRepository'
import ChatService from '#services/chatService'
import TeleportService from '#services/teleportService'
type TypePayload = {
message: string
@ -14,9 +13,8 @@ export default class TeleportCommandEvent extends BaseEvent {
this.socket.on('chat:message', this.handleEvent.bind(this))
}
private async handleEvent(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
private async handleEvent(data: TypePayload, callback: (response: boolean) => void) {
try {
// Check if character exists
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
if (!zoneCharacter) {
this.logger.error('chat:message error', 'Character not found')
@ -25,7 +23,6 @@ export default class TeleportCommandEvent extends BaseEvent {
const character = zoneCharacter.character
// Check if the user is the GM
if (character.role !== 'gm') {
this.logger.info(`User ${character.id} tried to set time but is not a game master.`)
return
@ -35,54 +32,68 @@ export default class TeleportCommandEvent extends BaseEvent {
const args = ChatService.getArgs('teleport', data.message)
if (!args || args.length !== 1) {
this.socket.emit('notification', { title: 'Server message', message: 'Usage: /teleport <zoneId>' })
if (!args || args.length === 0 || args.length > 3) {
this.socket.emit('notification', {
title: 'Server message',
message: 'Usage: /teleport <zoneId> [x] [y]'
})
return
}
const zoneId = parseInt(args[0], 10)
if (isNaN(zoneId)) {
this.socket.emit('notification', { title: 'Server message', message: 'Invalid zone ID' })
const targetX = args[1] ? parseInt(args[1], 10) : 0
const targetY = args[2] ? parseInt(args[2], 10) : 0
if (isNaN(zoneId) || isNaN(targetX) || isNaN(targetY)) {
this.socket.emit('notification', {
title: 'Server message',
message: 'Invalid parameters. All values must be numbers.'
})
return
}
const zone = await ZoneRepository.getById(zoneId)
if (!zone) {
this.socket.emit('notification', { title: 'Server message', message: 'Zone not found' })
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' })
if (character.zone.id === zone.id && targetX === character.positionX && targetY === character.positionY) {
this.socket.emit('notification', {
title: 'Server message',
message: 'You are already at that location'
})
return
}
// Remove character from current zone
zoneManager.removeCharacter(character.id)
this.io.to(character.zoneId.toString()).emit('zone:character:leave', character.id)
this.socket.leave(character.zoneId.toString())
// Add character to new zone
zoneManager.getZoneById(zone.id)?.addCharacter(character)
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
zoneCharacter.isMoving = false
this.socket.emit('zone:character:teleport', {
zone,
characters: ZoneManager.getZoneById(zone.id)?.getCharactersInZone()
const success = await TeleportService.teleportCharacter(character.id, {
targetZoneId: zone.id,
targetX,
targetY,
rotation: character.rotation
})
this.socket.emit('notification', { title: 'Server message', message: `You have been teleported to ${zone.name}` })
this.logger.info('teleport', `Character ${character.id} teleported to zone ${zone.id}`)
if (!success) {
return this.socket.emit('notification', {
title: 'Server message',
message: 'Failed to teleport'
})
}
this.socket.emit('notification', {
title: 'Server message',
message: `Teleported to ${zone.name} (${targetX}, ${targetY})`
})
this.logger.info('teleport', `Character ${character.id} teleported to zone ${zone.id} at position (${targetX}, ${targetY})`)
} catch (error: any) {
this.logger.error(`Error in teleport command: ${error.message}`)
this.socket.emit('notification', { title: 'Server message', message: 'An error occurred while teleporting' })
this.socket.emit('notification', {
title: 'Server message',
message: 'An error occurred while teleporting'
})
}
}
}
}