1
0
forked from noxious/server

More improvements

This commit is contained in:
Dennis Postma 2024-12-27 03:36:45 +01:00
parent 30dc69ab4b
commit 0fe060ff99
12 changed files with 25 additions and 27 deletions

View File

@ -0,0 +1,4 @@
export abstract class BaseEvent {
}

View File

@ -33,7 +33,7 @@ class ZoneManager {
return this.zones.get(zoneId) return this.zones.get(zoneId)
} }
public getCharacter(characterId: number): ZoneCharacter | undefined { public getCharacterById(characterId: number): ZoneCharacter | undefined {
for (const zone of this.zones.values()) { for (const zone of this.zones.values()) {
const character = zone.getCharactersInZone().find((char) => char.character.id === characterId) const character = zone.getCharactersInZone().find((char) => char.character.id === characterId)
if (character) return character if (character) return character

View File

@ -1,6 +1,5 @@
import { AStar } from '#application/character/aStar' import { AStar } from '#application/character/aStar'
import Rotation from '#application/character/rotation' import Rotation from '#application/character/rotation'
import { Database } from '#application/database'
import { appLogger, gameLogger } from '#application/logger' import { appLogger, gameLogger } from '#application/logger'
import { Character } from '#entities/character' import { Character } from '#entities/character'
import { Zone } from '#entities/zone' import { Zone } from '#entities/zone'

View File

@ -4,7 +4,6 @@ import NodeMailer from 'nodemailer'
import PasswordResetTokenService from './passwordResetTokenService' // @TODO: Correctly implement this import PasswordResetTokenService from './passwordResetTokenService' // @TODO: Correctly implement this
import config from '#application/config' import config from '#application/config'
import { Database } from '#application/database'
import { httpLogger } from '#application/logger' import { httpLogger } from '#application/logger'
import { PasswordResetToken } from '#entities/passwordResetToken' import { PasswordResetToken } from '#entities/passwordResetToken'
import { User } from '#entities/user' import { User } from '#entities/user'

View File

@ -5,7 +5,6 @@ import { TSocket } from '#application/types'
import ZoneManager from '#managers/zoneManager' import ZoneManager from '#managers/zoneManager'
import CharacterHairRepository from '#repositories/characterHairRepository' import CharacterHairRepository from '#repositories/characterHairRepository'
import CharacterRepository from '#repositories/characterRepository' import CharacterRepository from '#repositories/characterRepository'
import { CharacterService } from '#services/characterService'
interface CharacterConnectPayload { interface CharacterConnectPayload {
characterId: number characterId: number
@ -29,7 +28,7 @@ export default class CharacterConnectEvent {
} }
try { try {
if (await this.hasActiveCharacter()) { if (await this.checkForActiveCharacters()) {
this.emitError('You are already connected to another character') this.emitError('You are already connected to another character')
return return
} }
@ -41,19 +40,23 @@ export default class CharacterConnectEvent {
return return
} }
// Set character id
this.socket.characterId = character.id
// Set character hair
const characterHair = await CharacterHairRepository.getById(characterHairId ?? 0) const characterHair = await CharacterHairRepository.getById(characterHairId ?? 0)
await character.setCharacterHair(characterHair).save() await character.setCharacterHair(characterHair).save()
this.socket.characterId = character.id // Emit character connect event
this.socket.emit('character:connect', character) this.socket.emit('character:connect', character)
} catch (error) { } catch (error) {
this.handleError('Failed to connect character', error) // @TODO : Make global error handler 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!) 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 { private emitError(message: string): void {

View File

@ -25,7 +25,7 @@ export default class TeleportCommandEvent {
private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> { private async handleTeleportCommand(data: TypePayload, callback: (response: boolean) => void): Promise<void> {
try { try {
// Check if character exists // Check if character exists
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!) const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
if (!zoneCharacter) { if (!zoneCharacter) {
gameLogger.error('chat:message error', 'Character not found') gameLogger.error('chat:message error', 'Character not found')
return return

View File

@ -27,7 +27,7 @@ export default class ChatMessageEvent {
return callback(false) return callback(false)
} }
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!) const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
if (!zoneCharacter) { if (!zoneCharacter) {
gameLogger.error('chat:message error', 'Character not found') gameLogger.error('chat:message error', 'Character not found')
return callback(false) return callback(false)

View File

@ -23,7 +23,7 @@ export default class DisconnectEvent {
this.io.emit('user:disconnect', this.socket.userId) this.io.emit('user:disconnect', this.socket.userId)
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!) const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
if (!zoneCharacter) { if (!zoneCharacter) {
gameLogger.info('User disconnected but had no character set') gameLogger.info('User disconnected but had no character set')
return return

View File

@ -37,18 +37,14 @@ export default class CharacterJoinEvent {
return return
} }
/** const zone = character.zone
* @TODO: If zone is not found, spawn back to the start
*/
const zone = await ZoneRepository.getById(character.zone!.id)
if (!zone) { if (!zone) {
// @TODO: If zone is not found, spawn back to the start
gameLogger.error('zone:character:join error', 'Zone not found') gameLogger.error('zone:character:join error', 'Zone not found')
return return
} }
/**
* @TODO: If zone is not found, spawn back to the start
*/
const loadedZone = ZoneManager.getZoneById(zone.id) const loadedZone = ZoneManager.getZoneById(zone.id)
if (!loadedZone) { if (!loadedZone) {
gameLogger.error('zone:character:join error', 'Loaded zone not found') gameLogger.error('zone:character:join error', 'Loaded zone not found')
@ -60,7 +56,7 @@ export default class CharacterJoinEvent {
this.socket.join(zone.id.toString()) this.socket.join(zone.id.toString())
// Let other clients know of new character // 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 // Log
gameLogger.info(`User ${character.id} joined zone ${zone.id}`) gameLogger.info(`User ${character.id} joined zone ${zone.id}`)

View File

@ -7,10 +7,7 @@ import CharacterRepository from '#repositories/characterRepository'
import ZoneRepository from '#repositories/zoneRepository' import ZoneRepository from '#repositories/zoneRepository'
export default class ZoneLeaveEvent { export default class ZoneLeaveEvent {
constructor( constructor(private readonly io: Server, private readonly socket: TSocket) {}
private readonly io: Server,
private readonly socket: TSocket
) {}
public listen(): void { public listen(): void {
this.socket.on('zone:character:leave', this.handleZoneLeave.bind(this)) 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 * @TODO: If zone is not found, spawn back to the start
*/ */
const zone = await ZoneRepository.getById(character.zoneId) const zone = character.zone
if (!zone) { if (!zone) {
gameLogger.error('zone:character:join error', 'Zone not found') gameLogger.error('zone:character:join error', 'Zone not found')
return return

View File

@ -23,7 +23,7 @@ export default class CharacterMove {
} }
private async handleCharacterMove({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> { 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) { if (!zoneCharacter?.character) {
gameLogger.error('character:move error', 'Character not found or not initialized') gameLogger.error('character:move error', 'Character not found or not initialized')
return return
@ -88,7 +88,7 @@ export default class CharacterMove {
} }
private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> { private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> {
const zoneCharacter = ZoneManager.getCharacter(this.socket.characterId!) const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)
if (!zoneCharacter) { if (!zoneCharacter) {
gameLogger.error('character:move error', 'Character not found') gameLogger.error('character:move error', 'Character not found')
return return

View File

@ -16,7 +16,7 @@ export default class Weather {
private async handleEvent(): Promise<void> { private async handleEvent(): Promise<void> {
try { try {
const weather = await WeatherManager.getWeatherState() const weather = WeatherManager.getWeatherState()
this.socket.emit('weather', weather) this.socket.emit('weather', weather)
} catch (error: any) { } catch (error: any) {
gameLogger.error('error', error.message) gameLogger.error('error', error.message)