server/src/events/disconnect.ts

32 lines
997 B
TypeScript

import { BaseEvent } from '@/application/base/baseEvent'
import { SocketEvent } from '@/application/enums'
import MapManager from '@/managers/mapManager'
export default class DisconnectEvent extends BaseEvent {
public listen(): void {
this.socket.on(SocketEvent.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(SocketEvent.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)
}
}
}