forked from noxious/server
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
|
||||
|
||||
const character = new Character()
|
||||
character.name = name
|
||||
character.user = user
|
||||
character.setName(name).setUser(user)
|
||||
|
||||
return await character.save()
|
||||
}
|
||||
@ -34,12 +33,12 @@ export class CharacterService {
|
||||
if (!character) return null
|
||||
|
||||
if (characterHairId === null) {
|
||||
character.characterHair = undefined
|
||||
character.setCharacterHair(undefined)
|
||||
return await character.save()
|
||||
}
|
||||
|
||||
const characterHair = await CharacterHairRepository.getById(characterHairId)
|
||||
character.characterHair = characterHair ?? undefined
|
||||
character.setCharacterHair(characterHair ?? undefined)
|
||||
|
||||
return await character.save()
|
||||
}
|
||||
@ -61,10 +60,11 @@ export class CharacterService {
|
||||
const character = await CharacterRepository.getById(id)
|
||||
if (!character) return null
|
||||
|
||||
character.positionX = positionX
|
||||
character.positionY = positionY
|
||||
character.rotation = rotation
|
||||
character.zone = (await ZoneRepository.getById(zoneId)) as Zone
|
||||
character
|
||||
.setPositionX(positionX)
|
||||
.setPositionY(positionY)
|
||||
.setRotation(rotation)
|
||||
.setZone((await ZoneRepository.getById(zoneId)) as Zone)
|
||||
|
||||
await character.save()
|
||||
|
||||
@ -80,12 +80,12 @@ export class CharacterService {
|
||||
positionX: position.x,
|
||||
positionY: 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> {
|
||||
const zone = ZoneManager.getZoneById(character.zone.id)
|
||||
const zone = ZoneManager.getZoneById(character.zone!.id)
|
||||
const grid = await zone?.getGrid()
|
||||
|
||||
if (!grid?.length) {
|
||||
|
@ -45,10 +45,8 @@ class UserService {
|
||||
return false
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10)
|
||||
|
||||
const newUser = new User()
|
||||
newUser.setUsername(username).setEmail(email).setPassword(hashedPassword)
|
||||
newUser.setUsername(username).setEmail(email).setPassword(password)
|
||||
await newUser.save()
|
||||
|
||||
return newUser
|
||||
@ -114,20 +112,16 @@ class UserService {
|
||||
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
|
||||
const orm = await Database.getInstance()
|
||||
const em = orm.em.fork()
|
||||
const user = await em.findOne(User, { id: tokenData.userId })
|
||||
user.setPassword(password)
|
||||
await user.save()
|
||||
|
||||
if (!user) return false
|
||||
|
||||
user.password = hashedPassword
|
||||
await em.persistAndFlush(user)
|
||||
|
||||
// Delete the token
|
||||
await em.removeAndFlush(tokenData)
|
||||
// Delete the token using MikroORM entity method
|
||||
await tokenData.delete()
|
||||
|
||||
return true
|
||||
} catch (error: any) {
|
||||
|
@ -50,7 +50,6 @@ export default class CharacterMove {
|
||||
const { character } = zoneCharacter
|
||||
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
// Exit if movement was cancelled or interrupted
|
||||
if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) {
|
||||
return
|
||||
}
|
||||
@ -67,11 +66,17 @@ export default class CharacterMove {
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
// Only finalize if this path wasn't interrupted
|
||||
if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) {
|
||||
this.finalizeMovement(zoneCharacter)
|
||||
}
|
||||
@ -91,6 +96,12 @@ export default class CharacterMove {
|
||||
|
||||
private finalizeMovement(zoneCharacter: ZoneCharacter): void {
|
||||
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