1
0
forked from noxious/server

Don't attack if isMoving

This commit is contained in:
Dennis Postma 2025-02-12 03:01:07 +01:00
parent d75ed7a44f
commit 8cca44a3b4
3 changed files with 23 additions and 8 deletions

View File

@ -2,6 +2,8 @@ import { SocketEvent } from '@/application/enums'
import Logger, { LoggerType } from '@/application/logger'
import type { TSocket } from '@/application/types'
import { Character } from '@/entities/character'
import MapManager from '@/managers/mapManager'
import type MapCharacter from '@/models/mapCharacter'
import CharacterRepository from '@/repositories/characterRepository'
import { Server } from 'socket.io'
@ -27,6 +29,11 @@ export abstract class BaseEvent {
return false
}
protected getMapCharacter(): MapCharacter | null {
if (!this.socket.characterId) return null
return MapManager.getCharacterById(this.socket.characterId)
}
protected async getCharacter(): Promise<Character | null> {
if (!this.socket.characterId) return null
const characterRepository = new CharacterRepository()

View File

@ -11,8 +11,16 @@ export default class CharacterMove extends BaseEvent {
private async handleEvent(data: any, callback: (response: any) => void): Promise<void> {
try {
console.log('attack', this.socket.characterId)
await this.characterAttackService.attack(this.socket.characterId)
if (!this.socket.characterId) {
this.logger.error('map:character:attack error: Character not found or not initialized')
return
}
// Don't attack if the character is already moving
if (this.getMapCharacter()?.isMoving) return
// Start attack
await this.characterAttackService.attack(this.socket.characterId!)
} catch (error) {
this.logger.error('map:character:attack error', error)
return callback(false)

View File

@ -37,12 +37,12 @@ class MapManager {
return this.maps[mapId]
}
public getCharacterById(characterId: UUID): MapCharacter | undefined {
for (const map of Object.values(this.maps)) {
const character = map.getCharactersInMap().find((char) => char.character.id === characterId)
if (character) return character
}
return undefined
public getCharacterById(characterId: UUID): MapCharacter | null {
return (
Object.values(this.maps)
.flatMap((map) => map.getCharactersInMap())
.find((char) => char.character.id === characterId) ?? null
)
}
public removeCharacter(characterId: UUID): void {