1
0
forked from noxious/server

Attempt fix memory leak

This commit is contained in:
Dennis Postma 2025-02-12 12:47:50 +01:00
parent 26405433a8
commit 7da0323153
4 changed files with 68 additions and 25 deletions

1
.prettierignore Normal file
View File

@ -0,0 +1 @@
migrations

View File

@ -99,36 +99,34 @@ export default class CharacterMove extends BaseEvent {
if (!path?.length) return if (!path?.length) return
try { let lastMoveTime = Date.now()
let lastMoveTime = Date.now() let currentTile, nextTile
try {
for (let i = 0; i < path.length - 1; i++) { for (let i = 0; i < path.length - 1; i++) {
if (!mapCharacter.isMoving || mapCharacter.currentPath !== path) { if (!mapCharacter.isMoving || mapCharacter.currentPath !== path) {
return return
} }
// Ensure minimum time between moves // Ensure minimum time between moves using a single Date.now() call
const currentTime = Date.now() const timeSinceLastMove = Date.now() - lastMoveTime
const timeSinceLastMove = currentTime - lastMoveTime
if (timeSinceLastMove < this.STEP_DELAY) { if (timeSinceLastMove < this.STEP_DELAY) {
await new Promise((resolve) => setTimeout(resolve, this.STEP_DELAY - timeSinceLastMove)) await new Promise((resolve) => setTimeout(resolve, this.STEP_DELAY - timeSinceLastMove))
} }
const [currentTile, nextTile] = [path[i], path[i + 1]] currentTile = path[i]
nextTile = path[i + 1]
if (!currentTile || !nextTile) { if (!currentTile || !nextTile || !this.isValidStep(currentTile, nextTile)) {
this.logger.error('Invalid movement step detected') this.logger.error('Invalid movement step detected')
break break
} }
// Validate step // Update character rotation and position in a single operation
if (!this.isValidStep(currentTile, nextTile)) { character
this.logger.error('Invalid movement step detected') .setRotation(CharacterService.calculateRotation(currentTile.positionX, currentTile.positionY, nextTile.positionX, nextTile.positionY))
break .setPositionX(nextTile.positionX)
} .setPositionY(nextTile.positionY)
// Update character rotation
character.setRotation(CharacterService.calculateRotation(currentTile.positionX, currentTile.positionY, nextTile.positionX, nextTile.positionY))
// Check for map events at the next tile // Check for map events at the next tile
const mapEventTile = await this.checkMapEvents(character, nextTile) const mapEventTile = await this.checkMapEvents(character, nextTile)
@ -140,9 +138,6 @@ export default class CharacterMove extends BaseEvent {
} }
} }
// Move character to next tile
character.setPositionX(nextTile.positionX).setPositionY(nextTile.positionY)
// Broadcast movement // Broadcast movement
this.broadcastMovement(character, true) this.broadcastMovement(character, true)
@ -151,9 +146,10 @@ export default class CharacterMove extends BaseEvent {
await new Promise((resolve) => setTimeout(resolve, this.STEP_DELAY)) await new Promise((resolve) => setTimeout(resolve, this.STEP_DELAY))
} }
// Update last known position and move time
this.lastKnownPosition = { this.lastKnownPosition = {
x: character.getPositionX(), x: nextTile.positionX,
y: character.getPositionY() y: nextTile.positionY
} }
lastMoveTime = Date.now() lastMoveTime = Date.now()
} }

View File

@ -15,7 +15,7 @@ export default defineConfig({
user: serverConfig.DB_USER, user: serverConfig.DB_USER,
password: serverConfig.DB_PASS, password: serverConfig.DB_PASS,
dbName: serverConfig.DB_NAME, dbName: serverConfig.DB_NAME,
debug: false, debug: true,
driverOptions: { driverOptions: {
allowPublicKeyRetrieval: true allowPublicKeyRetrieval: true
}, },

View File

@ -12,16 +12,62 @@ class PriorityQueue<T> {
enqueue(item: T): void { enqueue(item: T): void {
this.items.push(item) this.items.push(item)
this.items.sort(this.compare) this.siftUp(this.items.length - 1)
} }
dequeue(): T | undefined { dequeue(): T | undefined {
return this.items.shift() if (this.items.length === 0) return undefined
const result = this.items[0]
const last = this.items.pop()
if (this.items.length > 0 && last !== undefined) {
this.items[0] = last
this.siftDown(0)
}
return result
} }
get length(): number { get length(): number {
return this.items.length return this.items.length
} }
private siftUp(index: number): void {
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2)
if (this.compare(this.items[index]!, this.items[parentIndex]!) < 0) {
;[this.items[index], this.items[parentIndex]] = [this.items[parentIndex]!, this.items[index]!]
index = parentIndex
} else {
break
}
}
}
private siftDown(index: number): void {
const length = this.items.length
while (true) {
let minIndex = index
const leftChild = 2 * index + 1
const rightChild = 2 * index + 2
if (leftChild < length && this.compare(this.items[leftChild]!, this.items[minIndex]!) < 0) {
minIndex = leftChild
}
if (rightChild < length && this.compare(this.items[rightChild]!, this.items[minIndex]!) < 0) {
minIndex = rightChild
}
if (minIndex === index) break
;[this.items[index], this.items[minIndex]] = [this.items[minIndex]!, this.items[index]!]
index = minIndex
}
}
clear(): void {
this.items = []
}
} }
class CharacterMoveService extends BaseService { class CharacterMoveService extends BaseService {
@ -144,14 +190,14 @@ class CharacterMoveService extends BaseService {
const h = this.getDistance(neighbor, end) const h = this.getDistance(neighbor, end)
const f = g + h const f = g + h
const node: Node = {...neighbor, g, h, f, parent: current} const node: Node = { ...neighbor, g, h, f, parent: current }
openList.enqueue(node) openList.enqueue(node)
} }
} }
return [] // No path found return [] // No path found
} finally { } finally {
while(openList.length > 0) openList.dequeue() while (openList.length > 0) openList.dequeue()
closedSet.clear() closedSet.clear()
} }
} }