48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { Server } from 'socket.io'
|
|
import { TSocket } from '../utilities/types'
|
|
import { gameLogger } from '../utilities/logger'
|
|
import ZoneManager from '../managers/zoneManager'
|
|
|
|
export default class DisconnectEvent {
|
|
constructor(
|
|
private readonly io: Server,
|
|
private readonly socket: TSocket
|
|
) {}
|
|
|
|
public listen(): void {
|
|
this.socket.on('disconnect', this.handleEvent.bind(this))
|
|
}
|
|
|
|
private async handleEvent(data: any): Promise<void> {
|
|
try {
|
|
if (!this.socket.userId) {
|
|
gameLogger.info('User disconnected but had no user set')
|
|
return
|
|
}
|
|
|
|
this.io.emit('user:disconnect', this.socket.userId)
|
|
|
|
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
|
if (!zoneCharacter) {
|
|
gameLogger.info('User disconnected but had no character set')
|
|
return
|
|
}
|
|
|
|
const character = zoneCharacter.character
|
|
|
|
// Save character position and remove from zone
|
|
zoneCharacter.isMoving = false
|
|
await zoneCharacter.savePosition()
|
|
ZoneManager.removeCharacter(this.socket.characterId!)
|
|
|
|
gameLogger.info('User disconnected along with their character')
|
|
|
|
// Inform other clients that the character has left
|
|
this.io.in(character.zoneId.toString()).emit('zone:character:leave', character.id)
|
|
this.io.emit('character:disconnect', character.id)
|
|
} catch (error: any) {
|
|
gameLogger.error('disconnect error', error.message)
|
|
}
|
|
}
|
|
}
|