More improvements
This commit is contained in:
parent
30dc69ab4b
commit
0fe060ff99
4
src/application/base/baseEvent.ts
Normal file
4
src/application/base/baseEvent.ts
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
export abstract class BaseEvent {
|
||||
|
||||
}
|
@ -33,7 +33,7 @@ class ZoneManager {
|
||||
return this.zones.get(zoneId)
|
||||
}
|
||||
|
||||
public getCharacter(characterId: number): ZoneCharacter | undefined {
|
||||
public getCharacterById(characterId: number): ZoneCharacter | undefined {
|
||||
for (const zone of this.zones.values()) {
|
||||
const character = zone.getCharactersInZone().find((char) => char.character.id === characterId)
|
||||
if (character) return character
|
||||
|
@ -1,6 +1,5 @@
|
||||
import { AStar } from '#application/character/aStar'
|
||||
import Rotation from '#application/character/rotation'
|
||||
import { Database } from '#application/database'
|
||||
import { appLogger, gameLogger } from '#application/logger'
|
||||
import { Character } from '#entities/character'
|
||||
import { Zone } from '#entities/zone'
|
||||
|
@ -4,7 +4,6 @@ import NodeMailer from 'nodemailer'
|
||||
import PasswordResetTokenService from './passwordResetTokenService' // @TODO: Correctly implement this
|
||||
|
||||
import config from '#application/config'
|
||||
import { Database } from '#application/database'
|
||||
import { httpLogger } from '#application/logger'
|
||||
import { PasswordResetToken } from '#entities/passwordResetToken'
|
||||
import { User } from '#entities/user'
|
||||
|
@ -5,7 +5,6 @@ import { TSocket } from '#application/types'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
import CharacterHairRepository from '#repositories/characterHairRepository'
|
||||
import CharacterRepository from '#repositories/characterRepository'
|
||||
import { CharacterService } from '#services/characterService'
|
||||
|
||||
interface CharacterConnectPayload {
|
||||
characterId: number
|
||||
@ -29,7 +28,7 @@ export default class CharacterConnectEvent {
|
||||
}
|
||||
|
||||
try {
|
||||
if (await this.hasActiveCharacter()) {
|
||||
if (await this.checkForActiveCharacters()) {
|
||||
this.emitError('You are already connected to another character')
|
||||
return
|
||||
}
|
||||
@ -41,19 +40,23 @@ export default class CharacterConnectEvent {
|
||||
return
|
||||
}
|
||||
|
||||
// Set character id
|
||||
this.socket.characterId = character.id
|
||||
|
||||
// Set character hair
|
||||
const characterHair = await CharacterHairRepository.getById(characterHairId ?? 0)
|
||||
await character.setCharacterHair(characterHair).save()
|
||||
|
||||
this.socket.characterId = character.id
|
||||
// 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 hasActiveCharacter(): Promise<boolean> {
|
||||
private async checkForActiveCharacters(): Promise<boolean> {
|
||||
const characters = await CharacterRepository.getByUserId(this.socket.userId!)
|
||||
return characters?.some((char) => ZoneManager.getCharacter(char.id)) ?? false
|
||||
return characters?.some((char) => ZoneManager.getCharacterById(char.id)) ?? false
|
||||
}
|
||||
|
||||
private emitError(message: string): void {
|
||||
|
@ -25,7 +25,7 @@ export default class TeleportCommandEvent {
|
||||
private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
|
||||
try {
|
||||
// Check if character exists
|
||||
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
||||
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
|
||||
if (!zoneCharacter) {
|
||||
gameLogger.error('chat:message error', 'Character not found')
|
||||
return
|
||||
|
@ -27,7 +27,7 @@ export default class ChatMessageEvent {
|
||||
return callback(false)
|
||||
}
|
||||
|
||||
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
||||
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
|
||||
if (!zoneCharacter) {
|
||||
gameLogger.error('chat:message error', 'Character not found')
|
||||
return callback(false)
|
||||
|
@ -23,7 +23,7 @@ export default class DisconnectEvent {
|
||||
|
||||
this.io.emit('user:disconnect', this.socket.userId)
|
||||
|
||||
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
||||
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
|
||||
if (!zoneCharacter) {
|
||||
gameLogger.info('User disconnected but had no character set')
|
||||
return
|
||||
|
@ -37,18 +37,14 @@ export default class CharacterJoinEvent {
|
||||
return
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: If zone is not found, spawn back to the start
|
||||
*/
|
||||
const zone = await ZoneRepository.getById(character.zone!.id)
|
||||
const zone = character.zone
|
||||
|
||||
if (!zone) {
|
||||
// @TODO: If zone is not found, spawn back to the start
|
||||
gameLogger.error('zone:character:join error', 'Zone not found')
|
||||
return
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: If zone is not found, spawn back to the start
|
||||
*/
|
||||
const loadedZone = ZoneManager.getZoneById(zone.id)
|
||||
if (!loadedZone) {
|
||||
gameLogger.error('zone:character:join error', 'Loaded zone not found')
|
||||
@ -60,7 +56,7 @@ export default class CharacterJoinEvent {
|
||||
this.socket.join(zone.id.toString())
|
||||
|
||||
// Let other clients know of new character
|
||||
this.io.to(zone.id.toString()).emit('zone:character:join', zoneManager.getCharacter(character.id))
|
||||
this.io.to(zone.id.toString()).emit('zone:character:join', zoneManager.getCharacterById(character.id))
|
||||
|
||||
// Log
|
||||
gameLogger.info(`User ${character.id} joined zone ${zone.id}`)
|
||||
|
@ -7,10 +7,7 @@ import CharacterRepository from '#repositories/characterRepository'
|
||||
import ZoneRepository from '#repositories/zoneRepository'
|
||||
|
||||
export default class ZoneLeaveEvent {
|
||||
constructor(
|
||||
private readonly io: Server,
|
||||
private readonly socket: TSocket
|
||||
) {}
|
||||
constructor(private readonly io: Server, private readonly socket: TSocket) {}
|
||||
|
||||
public listen(): void {
|
||||
this.socket.on('zone:character:leave', this.handleZoneLeave.bind(this))
|
||||
@ -32,7 +29,7 @@ export default class ZoneLeaveEvent {
|
||||
/**
|
||||
* @TODO: If zone is not found, spawn back to the start
|
||||
*/
|
||||
const zone = await ZoneRepository.getById(character.zoneId)
|
||||
const zone = character.zone
|
||||
if (!zone) {
|
||||
gameLogger.error('zone:character:join error', 'Zone not found')
|
||||
return
|
||||
|
@ -23,7 +23,7 @@ export default class CharacterMove {
|
||||
}
|
||||
|
||||
private async handleCharacterMove({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
|
||||
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
||||
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
|
||||
if (!zoneCharacter?.character) {
|
||||
gameLogger.error('character:move error', 'Character not found or not initialized')
|
||||
return
|
||||
@ -88,7 +88,7 @@ export default class CharacterMove {
|
||||
}
|
||||
|
||||
private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> {
|
||||
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!)
|
||||
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
|
||||
if (!zoneCharacter) {
|
||||
gameLogger.error('character:move error', 'Character not found')
|
||||
return
|
||||
|
@ -16,7 +16,7 @@ export default class Weather {
|
||||
|
||||
private async handleEvent(): Promise<void> {
|
||||
try {
|
||||
const weather = await WeatherManager.getWeatherState()
|
||||
const weather = WeatherManager.getWeatherState()
|
||||
this.socket.emit('weather', weather)
|
||||
} catch (error: any) {
|
||||
gameLogger.error('error', error.message)
|
||||
|
Loading…
x
Reference in New Issue
Block a user