Started improving character move event
This commit is contained in:
parent
4a963b4359
commit
b7f448cb17
@ -23,8 +23,7 @@ export class CharacterService {
|
|||||||
if (!user) return null
|
if (!user) return null
|
||||||
|
|
||||||
const character = new Character()
|
const character = new Character()
|
||||||
character.name = name
|
character.setName(name).setUser(user)
|
||||||
character.user = user
|
|
||||||
|
|
||||||
return await character.save()
|
return await character.save()
|
||||||
}
|
}
|
||||||
@ -34,12 +33,12 @@ export class CharacterService {
|
|||||||
if (!character) return null
|
if (!character) return null
|
||||||
|
|
||||||
if (characterHairId === null) {
|
if (characterHairId === null) {
|
||||||
character.characterHair = undefined
|
character.setCharacterHair(undefined)
|
||||||
return await character.save()
|
return await character.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
const characterHair = await CharacterHairRepository.getById(characterHairId)
|
const characterHair = await CharacterHairRepository.getById(characterHairId)
|
||||||
character.characterHair = characterHair ?? undefined
|
character.setCharacterHair(characterHair ?? undefined)
|
||||||
|
|
||||||
return await character.save()
|
return await character.save()
|
||||||
}
|
}
|
||||||
@ -61,10 +60,11 @@ export class CharacterService {
|
|||||||
const character = await CharacterRepository.getById(id)
|
const character = await CharacterRepository.getById(id)
|
||||||
if (!character) return null
|
if (!character) return null
|
||||||
|
|
||||||
character.positionX = positionX
|
character
|
||||||
character.positionY = positionY
|
.setPositionX(positionX)
|
||||||
character.rotation = rotation
|
.setPositionY(positionY)
|
||||||
character.zone = (await ZoneRepository.getById(zoneId)) as Zone
|
.setRotation(rotation)
|
||||||
|
.setZone((await ZoneRepository.getById(zoneId)) as Zone)
|
||||||
|
|
||||||
await character.save()
|
await character.save()
|
||||||
|
|
||||||
@ -80,12 +80,12 @@ export class CharacterService {
|
|||||||
positionX: position.x,
|
positionX: position.x,
|
||||||
positionY: position.y,
|
positionY: position.y,
|
||||||
rotation: Rotation.calculate(character.positionX, character.positionY, position.x, position.y),
|
rotation: Rotation.calculate(character.positionX, character.positionY, position.x, position.y),
|
||||||
zoneId: newZoneId ?? character.zone.id
|
zoneId: newZoneId ?? character.zone!.id
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
||||||
const zone = ZoneManager.getZoneById(character.zone.id)
|
const zone = ZoneManager.getZoneById(character.zone!.id)
|
||||||
const grid = await zone?.getGrid()
|
const grid = await zone?.getGrid()
|
||||||
|
|
||||||
if (!grid?.length) {
|
if (!grid?.length) {
|
||||||
|
@ -45,10 +45,8 @@ class UserService {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const hashedPassword = await bcrypt.hash(password, 10)
|
|
||||||
|
|
||||||
const newUser = new User()
|
const newUser = new User()
|
||||||
newUser.setUsername(username).setEmail(email).setPassword(hashedPassword)
|
newUser.setUsername(username).setEmail(email).setPassword(password)
|
||||||
await newUser.save()
|
await newUser.save()
|
||||||
|
|
||||||
return newUser
|
return newUser
|
||||||
@ -114,20 +112,16 @@ class UserService {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const hashedPassword = await bcrypt.hash(password, 10)
|
const user = await UserRepository.getById(tokenData.userId)
|
||||||
|
if (!user) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Update user password using MikroORM
|
user.setPassword(password)
|
||||||
const orm = await Database.getInstance()
|
await user.save()
|
||||||
const em = orm.em.fork()
|
|
||||||
const user = await em.findOne(User, { id: tokenData.userId })
|
|
||||||
|
|
||||||
if (!user) return false
|
// Delete the token using MikroORM entity method
|
||||||
|
await tokenData.delete()
|
||||||
user.password = hashedPassword
|
|
||||||
await em.persistAndFlush(user)
|
|
||||||
|
|
||||||
// Delete the token
|
|
||||||
await em.removeAndFlush(tokenData)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
@ -50,7 +50,6 @@ export default class CharacterMove {
|
|||||||
const { character } = zoneCharacter
|
const { character } = zoneCharacter
|
||||||
|
|
||||||
for (let i = 0; i < path.length - 1; i++) {
|
for (let i = 0; i < path.length - 1; i++) {
|
||||||
// Exit if movement was cancelled or interrupted
|
|
||||||
if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) {
|
if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -67,11 +66,17 @@ export default class CharacterMove {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.characterService.updatePosition(character, end)
|
this.characterService.updatePosition(character, end)
|
||||||
this.io.in(character.zone!.id.toString()).emit('character:move', zoneCharacter)
|
// Send minimal data
|
||||||
|
this.io.in(character.zone!.id.toString()).emit('character:move', {
|
||||||
|
id: character.id,
|
||||||
|
positionX: end.x,
|
||||||
|
positionY: end.y,
|
||||||
|
rotation: character.rotation,
|
||||||
|
isMoving: true
|
||||||
|
})
|
||||||
await this.characterService.applyMovementDelay()
|
await this.characterService.applyMovementDelay()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only finalize if this path wasn't interrupted
|
|
||||||
if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) {
|
if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) {
|
||||||
this.finalizeMovement(zoneCharacter)
|
this.finalizeMovement(zoneCharacter)
|
||||||
}
|
}
|
||||||
@ -91,6 +96,12 @@ export default class CharacterMove {
|
|||||||
|
|
||||||
private finalizeMovement(zoneCharacter: ZoneCharacter): void {
|
private finalizeMovement(zoneCharacter: ZoneCharacter): void {
|
||||||
zoneCharacter.isMoving = false
|
zoneCharacter.isMoving = false
|
||||||
this.io.in(zoneCharacter.character.zone!.id.toString()).emit('character:move', zoneCharacter)
|
this.io.in(zoneCharacter.character.zone!.id.toString()).emit('character:move', {
|
||||||
|
id: zoneCharacter.character.id,
|
||||||
|
positionX: zoneCharacter.character.positionX,
|
||||||
|
positionY: zoneCharacter.character.positionY,
|
||||||
|
rotation: zoneCharacter.character.rotation,
|
||||||
|
isMoving: false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user