| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { makeCharacter, makeSimulation, stepTimes, walkTicks } from "./helpers/simulationHarness.js"; |
| 4 | |
| 5 | test("logout saves tile and login resumes nearby that tile", () => |
| 6 | { |
| 7 | const harness = makeSimulation({ workerCount: 1 }); |
| 8 | try |
| 9 | { |
| 10 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 11 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 12 | harness.simulation.enqueue(1, { type: "move-to", characterId: character.character_id, tileX: 12, tileY: 8 }); |
| 13 | stepTimes(harness.simulation, walkTicks(5)); |
| 14 | const before = harness.simulation.entityRecord(entityId); |
| 15 | assert.equal(before.tileX, 12); |
| 16 | assert.equal(before.tileY, 8); |
| 17 | |
| 18 | harness.simulation.despawnCharacter(character.character_id); |
| 19 | const saved = harness.stateDb.getCharacter(character.character_id); |
| 20 | assert.equal(saved.tile_x, 12); |
| 21 | assert.equal(saved.tile_y, 8); |
| 22 | |
| 23 | const again = harness.simulation.spawnCharacter(2, saved); |
| 24 | const resumed = harness.simulation.entityRecord(again); |
| 25 | assert.equal(resumed.tileX, 12); |
| 26 | assert.equal(resumed.tileY, 8); |
| 27 | } |
| 28 | finally |
| 29 | { harness.close(); } |
| 30 | }); |
| 31 | |
| 32 | test("login prefers a nearby free tile over a distant world spawn when blocked", () => |
| 33 | { |
| 34 | const harness = makeSimulation({ workerCount: 1 }); |
| 35 | try |
| 36 | { |
| 37 | const blocker = makeCharacter(harness.stateDb, { tileX: 12, tileY: 8, firstName: "Block" }); |
| 38 | harness.simulation.spawnCharacter(1, blocker); |
| 39 | const character = makeCharacter(harness.stateDb, { tileX: 12, tileY: 8, firstName: "Return" }); |
| 40 | harness.stateDb.saveCharacterPosition(character.character_id, 12, 8, "walk"); |
| 41 | const fresh = harness.stateDb.getCharacter(character.character_id); |
| 42 | const entityId = harness.simulation.spawnCharacter(2, fresh); |
| 43 | const resumed = harness.simulation.entityRecord(entityId); |
| 44 | assert.ok(Math.abs(resumed.tileX - 12) + Math.abs(resumed.tileY - 8) <= 2); |
| 45 | assert.ok(!(resumed.tileX === 12 && resumed.tileY === 8)); |
| 46 | } |
| 47 | finally |
| 48 | { harness.close(); } |
| 49 | }); |