1
0
forked from noxious/server

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

@ -3,6 +3,7 @@ import Database from '#application/database'
import ZoneManager from '#managers/zoneManager'
import CharacterHairRepository from '#repositories/characterHairRepository'
import CharacterRepository from '#repositories/characterRepository'
import TeleportService from '#services/teleportService'
interface CharacterConnectPayload {
characterId: number
@ -59,21 +60,14 @@ export default class CharacterConnectEvent extends BaseEvent {
// wait 300 ms, @TODO: Find a better way to do this
await new Promise(resolve => setTimeout(resolve, 100))
const zone = ZoneManager.getZoneById(character.zone!.id)
if (!zone) {
this.logger.error('zone:character:join error: Zone not found')
return
}
zone.addCharacter(character)
const zoneCharacter = ZoneManager.getCharacterById(character.id)
if (!zoneCharacter) {
this.logger.error('zone:character:join error: Zone character not found')
return
}
await zoneCharacter.teleport(character.zone!.id, character.positionX, character.positionY)
await TeleportService.teleportCharacter(character.id, {
targetZoneId: character.zone!.id,
targetX: character.positionX,
targetY: character.positionY,
rotation: character.rotation,
isInitialJoin: true,
character
})
} catch (error) {
this.handleError('Failed to connect character', error)
}

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'
})
}
}
}
}

View File

@ -6,7 +6,7 @@ export default class DisconnectEvent extends BaseEvent {
this.socket.on('disconnect', this.handleEvent.bind(this))
}
private async handleEvent(data: any): Promise<void> {
private async handleEvent(): Promise<void> {
try {
if (!this.socket.userId) {
this.logger.info('User disconnected but had no user set')
@ -21,20 +21,10 @@ export default class DisconnectEvent extends BaseEvent {
return
}
const character = zoneCharacter.character
// Save character position and remove from zone
zoneCharacter.isMoving = false
await zoneCharacter.savePosition()
ZoneManager.removeCharacter(this.socket.characterId!)
await zoneCharacter.disconnect(this.socket, this.io)
this.logger.info('User disconnected along with their character')
// Inform other clients that the character has left
this.io.in(character.zone!.id.toString()).emit('zone:character:leave', character.id)
this.io.emit('character:disconnect', character.id)
} catch (error: any) {
this.logger.error('disconnect error: ' + error.message)
}
}
}
}