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 Logger, { LoggerType } from '@/application/logger'
import type { TSocket } from '@/application/types' import type { TSocket } from '@/application/types'
import { Character } from '@/entities/character' import { Character } from '@/entities/character'
import MapManager from '@/managers/mapManager'
import type MapCharacter from '@/models/mapCharacter'
import CharacterRepository from '@/repositories/characterRepository' import CharacterRepository from '@/repositories/characterRepository'
import { Server } from 'socket.io' import { Server } from 'socket.io'
@ -27,6 +29,11 @@ export abstract class BaseEvent {
return false return false
} }
protected getMapCharacter(): MapCharacter | null {
if (!this.socket.characterId) return null
return MapManager.getCharacterById(this.socket.characterId)
}
protected async getCharacter(): Promise<Character | null> { protected async getCharacter(): Promise<Character | null> {
if (!this.socket.characterId) return null if (!this.socket.characterId) return null
const characterRepository = new CharacterRepository() 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> { private async handleEvent(data: any, callback: (response: any) => void): Promise<void> {
try { try {
console.log('attack', this.socket.characterId) if (!this.socket.characterId) {
await this.characterAttackService.attack(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) { } catch (error) {
this.logger.error('map:character:attack error', error) this.logger.error('map:character:attack error', error)
return callback(false) return callback(false)

View File

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