65 lines
2.2 KiB
TypeScript

import { BaseEvent } from '#application/base/baseEvent'
import ZoneManager from '#managers/zoneManager'
import CharacterHairRepository from '#repositories/characterHairRepository'
import CharacterRepository from '#repositories/characterRepository'
interface CharacterConnectPayload {
characterId: number
characterHairId?: number
}
export default class CharacterConnectEvent extends BaseEvent {
public listen(): void {
this.socket.on('character:connect', this.handleCharacterConnect.bind(this))
}
private async handleCharacterConnect({ characterId, characterHairId }: CharacterConnectPayload): Promise<void> {
if (!this.socket.userId) {
this.emitError('User not authenticated')
return
}
try {
if (await this.checkForActiveCharacters()) {
this.emitError('You are already connected to another character')
return
}
const character = await CharacterRepository.getByUserAndId(this.socket.userId, characterId)
if (!character) {
this.emitError('Character not found or does not belong to this user')
return
}
// Set character id
this.socket.characterId = character.id
// Set character hair
const characterHair = await CharacterHairRepository.getById(characterHairId ?? 0)
await character.setCharacterHair(characterHair).update()
// Emit character connect event
this.socket.emit('character:connect', character)
} catch (error) {
this.handleError('Failed to connect character', error) // @TODO : Make global error handler
}
}
private async checkForActiveCharacters(): Promise<boolean> {
const characters = await CharacterRepository.getByUserId(this.socket.userId!)
return characters?.some((char) => ZoneManager.getCharacterById(char.id)) ?? false
}
private emitError(message: string): void {
this.socket.emit('notification', { title: 'Server message', message })
this.logger.error('character:connect error', `Player ${this.socket.userId}: ${message}`)
}
private handleError(context: string, error: unknown): void {
const errorMessage = error instanceof Error ? error.message : String(error)
this.emitError(`${context}: ${errorMessage}`)
this.logger.error('character:connect error', errorMessage)
}
}