server/src/events/disconnect.ts
2025-01-02 17:31:24 +01:00

31 lines
925 B
TypeScript

import { BaseEvent } from '#application/base/baseEvent'
import MapManager from '#managers/mapManager'
export default class DisconnectEvent extends BaseEvent {
public listen(): void {
this.socket.on('disconnect', this.handleEvent.bind(this))
}
private async handleEvent(): Promise<void> {
try {
if (!this.socket.userId) {
this.logger.info('User disconnected but had no user set')
return
}
this.io.emit('user:disconnect', this.socket.userId)
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
if (!mapCharacter) {
this.logger.info('User disconnected but had no character set')
return
}
await mapCharacter.disconnect(this.socket, this.io)
this.logger.info('User disconnected along with their character')
} catch (error: any) {
this.logger.error('disconnect error: ' + error.message)
}
}
}