Moved funcs to basEvent
This commit is contained in:
parent
8781bf43a1
commit
ff01e41c8f
@ -10,4 +10,15 @@ export abstract class BaseEvent {
|
||||
readonly io: Server,
|
||||
readonly socket: TSocket
|
||||
) {}
|
||||
|
||||
protected emitError(message: string): void {
|
||||
this.socket.emit('notification', { title: 'Server message', message })
|
||||
this.logger.error('character:connect error', `Player ${this.socket.userId}: ${message}`)
|
||||
}
|
||||
|
||||
protected 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)
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ export default class CharacterConnectEvent extends BaseEvent {
|
||||
this.socket.on('character:connect', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent({ characterId, characterHairId }: CharacterConnectPayload): Promise<void> {
|
||||
private async handleEvent(data: CharacterConnectPayload, callback: (response: boolean) => void): Promise<void> {
|
||||
if (!this.socket.userId) {
|
||||
this.emitError('User not authenticated')
|
||||
return
|
||||
@ -25,7 +25,7 @@ export default class CharacterConnectEvent extends BaseEvent {
|
||||
return
|
||||
}
|
||||
|
||||
const character = await CharacterRepository.getByUserAndId(this.socket.userId, characterId)
|
||||
const character = await CharacterRepository.getByUserAndId(this.socket.userId, data.characterId)
|
||||
|
||||
if (!character) {
|
||||
this.emitError('Character not found or does not belong to this user')
|
||||
@ -36,7 +36,7 @@ export default class CharacterConnectEvent extends BaseEvent {
|
||||
this.socket.characterId = character.id
|
||||
|
||||
// Set character hair
|
||||
const characterHair = await CharacterHairRepository.getById(characterHairId ?? 0)
|
||||
const characterHair = await CharacterHairRepository.getById(data.characterHairId ?? 0)
|
||||
await character.setCharacterHair(characterHair).update()
|
||||
|
||||
// Emit character connect event
|
||||
@ -50,15 +50,4 @@ export default class CharacterConnectEvent extends BaseEvent {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
import { BaseEvent } from '#application/base/baseEvent'
|
||||
import { Zone } from '#entities/zone'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
import zoneManager from '#managers/zoneManager'
|
||||
import zoneCharacter from '#models/zoneCharacter'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
|
||||
interface IResponse {
|
||||
zone: Zone
|
||||
characters: zoneCharacter[]
|
||||
}
|
||||
|
||||
export default class CharacterJoinEvent extends BaseEvent {
|
||||
public listen(): void {
|
||||
this.socket.on('zone:character:join', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent(callback: (response: IResponse) => void): Promise<void> {
|
||||
try {
|
||||
if (!this.socket.characterId) {
|
||||
this.logger.error('zone:character:join error: Zone requested but no character id set')
|
||||
return
|
||||
}
|
||||
|
||||
const character = await CharacterRepository.getById(this.socket.characterId)
|
||||
if (!character) {
|
||||
this.logger.error('zone:character:join error: Character not found')
|
||||
return
|
||||
}
|
||||
|
||||
const zone = character.zone
|
||||
|
||||
if (!zone) {
|
||||
// @TODO: If zone is not found, spawn back to the start
|
||||
this.logger.error('zone:character:join error: Zone not found')
|
||||
return
|
||||
}
|
||||
|
||||
const loadedZone = ZoneManager.getZoneById(zone.id)
|
||||
if (!loadedZone) {
|
||||
this.logger.error('zone:character:join error: Loaded zone not found')
|
||||
return
|
||||
}
|
||||
|
||||
loadedZone.addCharacter(character)
|
||||
|
||||
this.socket.join(zone.id.toString())
|
||||
|
||||
// Let other clients know of new character
|
||||
this.io.to(zone.id.toString()).emit('zone:character:join', zoneManager.getCharacterById(character.id))
|
||||
|
||||
// Log
|
||||
this.logger.info(`User ${character.id} joined zone ${zone.id}`)
|
||||
|
||||
// Send over zone and characters to socket
|
||||
callback({ zone, characters: loadedZone.getCharactersInZone() })
|
||||
} catch (error: any) {
|
||||
this.logger.error('zone:character:join error: ' + error.message)
|
||||
this.socket.disconnect()
|
||||
}
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
import { BaseEvent } from '#application/base/baseEvent'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
|
||||
export default class ZoneLeaveEvent extends BaseEvent {
|
||||
public listen(): void {
|
||||
this.socket.on('zone:character:leave', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent(): Promise<void> {
|
||||
try {
|
||||
if (!this.socket.characterId) {
|
||||
this.logger.error('zone:character:leave error: Zone requested but no character id set')
|
||||
return
|
||||
}
|
||||
|
||||
const character = await CharacterRepository.getById(this.socket.characterId)
|
||||
if (!character) {
|
||||
this.logger.error('zone:character:leave error: Character not found')
|
||||
return
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: If zone is not found, spawn back to the start
|
||||
*/
|
||||
const zone = character.zone
|
||||
if (!zone) {
|
||||
this.logger.error('zone:character:leave error: Zone not found')
|
||||
return
|
||||
}
|
||||
|
||||
const loadedZone = ZoneManager.getZoneById(zone.id)
|
||||
if (!loadedZone) {
|
||||
this.logger.error('zone:character:leave error: Loaded zone not found')
|
||||
return
|
||||
}
|
||||
|
||||
this.socket.leave(zone.id.toString())
|
||||
|
||||
// let other clients know of character leaving
|
||||
this.io.to(zone.id.toString()).emit('zone:character:leave', character.id)
|
||||
|
||||
// remove character from zone manager
|
||||
await loadedZone.removeCharacter(character.id)
|
||||
|
||||
this.logger.info('zone:character:leave ' + `Character ${character.id} left zone ${zone.id}`)
|
||||
} catch (error: any) {
|
||||
this.logger.error('zone:character:leave error: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user