| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { Component } from "../src/ecs/components.js"; |
| 4 | import { makeCharacter, makeSimulation, stepTimes, walkTicks } from "./helpers/simulationHarness.js"; |
| 5 | |
| 6 | test("logout saves tile and login resumes nearby that tile", () => |
| 7 | { |
| 8 | const harness = makeSimulation({ workerCount: 1 }); |
| 9 | try |
| 10 | { |
| 11 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 12 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 13 | harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 12, tileY: 8 }); |
| 14 | stepTimes(harness.simulation, walkTicks(5)); |
| 15 | const before = harness.simulation.entityRecord(entityId); |
| 16 | assert.equal(before.tileX, 12); |
| 17 | assert.equal(before.tileY, 8); |
| 18 | |
| 19 | harness.simulation.despawnCharacter(character.character_id); |
| 20 | const saved = harness.stateDb.getCharacter(character.character_id); |
| 21 | assert.equal(saved.tile_x, 12); |
| 22 | assert.equal(saved.tile_y, 8); |
| 23 | |
| 24 | const again = harness.simulation.spawnCharacter(2, saved); |
| 25 | const resumed = harness.simulation.entityRecord(again); |
| 26 | assert.equal(resumed.tileX, 12); |
| 27 | assert.equal(resumed.tileY, 8); |
| 28 | } |
| 29 | finally |
| 30 | { harness.close(); } |
| 31 | }); |
| 32 | |
| 33 | test("login prefers a nearby free tile over a distant world spawn when blocked", () => |
| 34 | { |
| 35 | const harness = makeSimulation({ workerCount: 1 }); |
| 36 | try |
| 37 | { |
| 38 | const blocker = makeCharacter(harness.stateDb, { tileX: 12, tileY: 8, firstName: "Block" }); |
| 39 | harness.simulation.spawnCharacter(1, blocker); |
| 40 | const character = makeCharacter(harness.stateDb, { tileX: 12, tileY: 8, firstName: "Return" }); |
| 41 | harness.stateDb.saveCharacterPosition(character.character_id, 12, 8, "walk"); |
| 42 | const fresh = harness.stateDb.getCharacter(character.character_id); |
| 43 | const entityId = harness.simulation.spawnCharacter(2, fresh); |
| 44 | const resumed = harness.simulation.entityRecord(entityId); |
| 45 | assert.ok(Math.abs(resumed.tileX - 12) + Math.abs(resumed.tileY - 8) <= 2); |
| 46 | assert.ok(!(resumed.tileX === 12 && resumed.tileY === 8)); |
| 47 | } |
| 48 | finally |
| 49 | { harness.close(); } |
| 50 | }); |
| 51 | |
| 52 | test("logout saves current health and login restores it", () => |
| 53 | { |
| 54 | const harness = makeSimulation({ workerCount: 1 }); |
| 55 | try |
| 56 | { |
| 57 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 58 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 59 | const record = harness.simulation.directory.get(entityId); |
| 60 | const worker = harness.simulation.workers[record.workerId]; |
| 61 | const handle = worker.world.ecs.handleOf(entityId); |
| 62 | const health = worker.world.ecs.get(handle, Component.health); |
| 63 | health.current = 4; |
| 64 | |
| 65 | harness.simulation.despawnCharacter(character.character_id); |
| 66 | const saved = harness.stateDb.getCharacter(character.character_id); |
| 67 | assert.equal(saved.health_current, 4); |
| 68 | |
| 69 | const again = harness.simulation.spawnCharacter(2, saved); |
| 70 | const resumed = harness.simulation.entityRecord(again); |
| 71 | assert.equal(resumed.health, 4); |
| 72 | assert.equal(resumed.maxHealth, 5); |
| 73 | } |
| 74 | finally |
| 75 | { harness.close(); } |
| 76 | }); |