| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { Component } from "../src/ecs/components.js"; |
| 6 | import { MobId } from "../../shared/definitions/mobs.js"; |
| 7 | import { mobComponents } from "../src/simulation/spawners.js"; |
| 8 | import { createEmptyChunkData, encodeChunkBlob, decodeChunkBlob } from "../src/world/chunkData.js"; |
| 9 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 10 | import { emptyEquipment, emptySlots, addCoinsToSlots } from "../../shared/items/inventory.js"; |
| 11 | import { COMBAT_LOGOUT_MS, combatLogoutCoinThreshold, cowardiceHourKey } from "../../shared/combat/combatLogout.js"; |
| 12 | import { combatLevelFromSkills } from "../../shared/combat/combatLevel.js"; |
| 13 | import { defaultPlayerSkills } from "../../shared/skills/state.js"; |
| 14 | import |
| 15 | { |
| 16 | CHURCH_LETTER_AUTHOR, |
| 17 | QUESTWARD_GIFT_AUTHOR, |
| 18 | QUESTWARD_GIFT_ORG, |
| 19 | formatCombatLogoutCowardiceBody, |
| 20 | formatChurchOfflineResurrectionBody |
| 21 | } from "../../shared/courier/systemLetters.js"; |
| 22 | import { applyDamageAndKill } from "../src/combat/service.js"; |
| 23 | import { WorkerMessage } from "../src/simulation/protocol.js"; |
| 24 | |
| 25 | function componentsOf (simulation, entityId) |
| 26 | { |
| 27 | const record = simulation.entityRecord(entityId); |
| 28 | const worker = simulation.workers[record.workerId]; |
| 29 | const handle = worker.world.ecs.handleOf(entityId); |
| 30 | return worker.world.ecs.componentsOf(handle); |
| 31 | } |
| 32 | |
| 33 | function placeBlock (worldDb, tileX, tileY, typeId) |
| 34 | { |
| 35 | const chunkX = Math.floor(tileX / 16); |
| 36 | const chunkY = Math.floor(tileY / 16); |
| 37 | const revisionId = worldDb.getActiveRevisionId(); |
| 38 | const row = worldDb.getChunkVersion(chunkX, chunkY, revisionId); |
| 39 | const data = row |
| 40 | ? decodeChunkBlob(row.data_blob, undefined, chunkX, chunkY) |
| 41 | : createEmptyChunkData(); |
| 42 | data.open = true; |
| 43 | const localX = tileX - chunkX * 16; |
| 44 | const localY = tileY - chunkY * 16; |
| 45 | data.blocks[localY * 16 + localX] = { typeId, state: 0 }; |
| 46 | worldDb.saveChunkVersion(chunkX, chunkY, revisionId, true, encodeChunkBlob(data)); |
| 47 | } |
| 48 | |
| 49 | function heldLetters (stateDb, characterId) |
| 50 | { |
| 51 | return stateDb.listHeldCourierDeliveries(characterId) |
| 52 | .map((hold) => hold.cargo?.stack) |
| 53 | .filter((stack) => stack?.itemId === ItemId.letter); |
| 54 | } |
| 55 | |
| 56 | function engageAndLogout (harness, character, playerId, targetEntityId) |
| 57 | { |
| 58 | harness.simulation.enqueue(1, { |
| 59 | type: "attack", |
| 60 | characterId: character.character_id, |
| 61 | targetEntityId |
| 62 | }); |
| 63 | harness.simulation.step(); |
| 64 | assert.equal(harness.simulation.beginCombatLogout(character.character_id), true); |
| 65 | assert.equal(harness.simulation.isCombatLogout(character.character_id), true); |
| 66 | return playerId; |
| 67 | } |
| 68 | |
| 69 | function clearCombatAndExpireQuiet (harness, characterId, entityId) |
| 70 | { |
| 71 | const record = harness.simulation.entityRecord(entityId); |
| 72 | if (record) |
| 73 | { |
| 74 | harness.simulation.workers[record.workerId].call({ |
| 75 | type: WorkerMessage.clearCombat, |
| 76 | entityId, |
| 77 | targetEntityId: entityId |
| 78 | }); |
| 79 | } |
| 80 | const track = harness.simulation.combatTracking.get(characterId); |
| 81 | if (track) |
| 82 | { |
| 83 | track.lastCombatActivityAt = harness.now() - COMBAT_LOGOUT_MS - 1; |
| 84 | track.wasInCombat = false; |
| 85 | } |
| 86 | const logout = harness.simulation.combatLogouts.get(characterId); |
| 87 | if (logout) |
| 88 | { logout.startedAt = harness.now() - COMBAT_LOGOUT_MS - 1; } |
| 89 | for (let i = 0; i < 60; i++) |
| 90 | { harness.simulation.step(); } |
| 91 | } |
| 92 | |
| 93 | test("combat logout keeps the character until quiet timeout", () => |
| 94 | { |
| 95 | const harness = makeSimulation({ workerCount: 1 }); |
| 96 | try |
| 97 | { |
| 98 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 99 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 100 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 101 | tileX: 9, |
| 102 | tileY: 8, |
| 103 | mobTypeId: MobId.deer, |
| 104 | spawnerId: null |
| 105 | })); |
| 106 | engageAndLogout(harness, character, playerId, deerId); |
| 107 | |
| 108 | const worker = harness.simulation.workers[0]; |
| 109 | const spells = worker.world.ecs.get(worker.world.ecs.handleOf(playerId), Component.spells); |
| 110 | assert.equal(spells.autoCast, true); |
| 111 | assert.ok(harness.simulation.isCharacterInWorld(character.character_id)); |
| 112 | assert.equal(harness.simulation.isCharacterOnline(character.character_id), false); |
| 113 | |
| 114 | clearCombatAndExpireQuiet(harness, character.character_id, playerId); |
| 115 | assert.equal(harness.simulation.isCharacterInWorld(character.character_id), false); |
| 116 | assert.equal(harness.simulation.isCombatLogout(character.character_id), false); |
| 117 | } |
| 118 | finally |
| 119 | { harness.close(); } |
| 120 | }); |
| 121 | |
| 122 | test("combat activity delays quiet expiry but hard-caps from disconnect", () => |
| 123 | { |
| 124 | const harness = makeSimulation({ workerCount: 1 }); |
| 125 | try |
| 126 | { |
| 127 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 128 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 129 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 130 | tileX: 9, |
| 131 | tileY: 8, |
| 132 | mobTypeId: MobId.deer, |
| 133 | spawnerId: null |
| 134 | })); |
| 135 | engageAndLogout(harness, character, playerId, deerId); |
| 136 | const logout = harness.simulation.combatLogouts.get(character.character_id); |
| 137 | const startedAt = logout.startedAt; |
| 138 | |
| 139 | // Fresh combat activity shortly before the quiet window would end. |
| 140 | harness.advance(COMBAT_LOGOUT_MS - 5_000); |
| 141 | for (let i = 0; i < 3; i++) |
| 142 | { harness.simulation.step(); } |
| 143 | assert.equal(harness.simulation.isCharacterInWorld(character.character_id), true); |
| 144 | const track = harness.simulation.combatTracking.get(character.character_id); |
| 145 | track.lastCombatActivityAt = harness.now(); |
| 146 | |
| 147 | // Still within the hard cap from original disconnect → stay. |
| 148 | harness.advance(2_000); |
| 149 | for (let i = 0; i < 3; i++) |
| 150 | { harness.simulation.step(); } |
| 151 | assert.equal(harness.simulation.isCharacterInWorld(character.character_id), true); |
| 152 | assert.equal(logout.startedAt, startedAt); |
| 153 | |
| 154 | // Past hard cap from disconnect even with fresh activity → despawn. |
| 155 | harness.advance(COMBAT_LOGOUT_MS); |
| 156 | track.lastCombatActivityAt = harness.now(); |
| 157 | // Combat-logout is checked about once per second of sim ticks. |
| 158 | for (let i = 0; i < 60; i++) |
| 159 | { harness.simulation.step(); } |
| 160 | assert.equal(harness.simulation.isCharacterInWorld(character.character_id), false); |
| 161 | assert.equal(harness.simulation.isCombatLogout(character.character_id), false); |
| 162 | } |
| 163 | finally |
| 164 | { harness.close(); } |
| 165 | }); |
| 166 | |
| 167 | test("offline combat-logout death resurrects at altar with church letter", () => |
| 168 | { |
| 169 | const harness = makeSimulation({ workerCount: 1 }); |
| 170 | try |
| 171 | { |
| 172 | placeBlock(harness.worldDb, 20, 8, BlockType.churchAltar); |
| 173 | placeBlock(harness.worldDb, 7, 8, BlockType.bank); |
| 174 | harness.simulation.reloadWorld(); |
| 175 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 176 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 177 | const deerId = harness.simulation.spawnEntity(mobComponents({ |
| 178 | tileX: 9, |
| 179 | tileY: 8, |
| 180 | mobTypeId: MobId.deer, |
| 181 | spawnerId: null |
| 182 | })); |
| 183 | engageAndLogout(harness, character, playerId, deerId); |
| 184 | |
| 185 | const worker = harness.simulation.workers[0]; |
| 186 | componentsOf(harness.simulation, playerId).health.current = 1; |
| 187 | const events = applyDamageAndKill(worker.world, deerId, playerId, 999, harness.now()); |
| 188 | const died = events.find((event) => event.type === "player-died"); |
| 189 | assert.ok(died); |
| 190 | harness.simulation.applyPlayerDiedEvent(died); |
| 191 | |
| 192 | assert.equal(harness.stateDb.getCharacter(character.character_id).is_dead, 0); |
| 193 | assert.equal(harness.simulation.isCharacterInWorld(character.character_id), false); |
| 194 | const letters = heldLetters(harness.stateDb, character.character_id); |
| 195 | assert.ok(letters.some((stack) => |
| 196 | stack.instance?.authorDisplayName === CHURCH_LETTER_AUTHOR |
| 197 | && stack.instance?.body === formatChurchOfflineResurrectionBody() |
| 198 | )); |
| 199 | const row = harness.stateDb.getCharacter(character.character_id); |
| 200 | assert.equal( |
| 201 | Math.max(Math.abs(row.tile_x - 20), Math.abs(row.tile_y - 8)), |
| 202 | 1 |
| 203 | ); |
| 204 | } |
| 205 | finally |
| 206 | { harness.close(); } |
| 207 | }); |
| 208 | |
| 209 | test("PvP combat-logout quiet end applies skull after hourly forgiveness", () => |
| 210 | { |
| 211 | const harness = makeSimulation({ workerCount: 1 }); |
| 212 | try |
| 213 | { |
| 214 | placeBlock(harness.worldDb, 7, 8, BlockType.bank); |
| 215 | harness.simulation.reloadWorld(); |
| 216 | const victim = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8, firstName: "Vic" }); |
| 217 | const attacker = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8, firstName: "Atk" }); |
| 218 | const victimId = harness.simulation.spawnCharacter(1, victim); |
| 219 | const attackerId = harness.simulation.spawnCharacter(2, attacker); |
| 220 | engageAndLogout(harness, victim, victimId, attackerId); |
| 221 | assert.equal( |
| 222 | harness.simulation.combatLogouts.get(victim.character_id).pendingCowardice, |
| 223 | true |
| 224 | ); |
| 225 | |
| 226 | const skills = harness.stateDb.getSkills(victim.character_id) ?? defaultPlayerSkills(); |
| 227 | const threshold = combatLogoutCoinThreshold(combatLevelFromSkills(skills)); |
| 228 | const hour = cowardiceHourKey(harness.now()); |
| 229 | |
| 230 | clearCombatAndExpireQuiet(harness, victim.character_id, victimId); |
| 231 | assert.equal(harness.simulation.isCharacterInWorld(victim.character_id), false); |
| 232 | assert.equal(harness.stateDb.getCowardiceForgivenHour(victim.character_id), hour); |
| 233 | assert.equal(harness.stateDb.getKillCounterState(victim.character_id).killCounter, 0); |
| 234 | assert.equal(heldLetters(harness.stateDb, victim.character_id).length, 0); |
| 235 | |
| 236 | const victimRow = harness.stateDb.getCharacter(victim.character_id); |
| 237 | const victimId2 = harness.simulation.spawnCharacter(1, victimRow); |
| 238 | const standIn = makeCharacter(harness.stateDb, { tileX: 10, tileY: 8, firstName: "Stand" }); |
| 239 | const standId = harness.simulation.spawnCharacter(3, standIn); |
| 240 | engageAndLogout(harness, victim, victimId2, standId); |
| 241 | clearCombatAndExpireQuiet(harness, victim.character_id, victimId2); |
| 242 | |
| 243 | assert.equal( |
| 244 | harness.stateDb.getKillCounterState(victim.character_id).killCounter, |
| 245 | threshold |
| 246 | ); |
| 247 | const letters = heldLetters(harness.stateDb, victim.character_id); |
| 248 | assert.ok(letters.some((stack) => |
| 249 | stack.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 250 | && stack.instance?.authorOrganization === QUESTWARD_GIFT_ORG |
| 251 | && stack.instance?.body === formatCombatLogoutCowardiceBody() |
| 252 | )); |
| 253 | void attackerId; |
| 254 | } |
| 255 | finally |
| 256 | { harness.close(); } |
| 257 | }); |
| 258 | |
| 259 | test("reattach after PvP combat-logout applies cowardice and restores autoCast", () => |
| 260 | { |
| 261 | const harness = makeSimulation({ workerCount: 1 }); |
| 262 | try |
| 263 | { |
| 264 | placeBlock(harness.worldDb, 7, 8, BlockType.bank); |
| 265 | harness.simulation.reloadWorld(); |
| 266 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 267 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 268 | const other = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8, firstName: "Other" }); |
| 269 | const otherId = harness.simulation.spawnCharacter(2, other); |
| 270 | |
| 271 | const worker = harness.simulation.workers[0]; |
| 272 | const spells = worker.world.ecs.get(worker.world.ecs.handleOf(playerId), Component.spells); |
| 273 | spells.autoCast = false; |
| 274 | harness.stateDb.saveSpells(character.character_id, { |
| 275 | known: spells.known ?? [], |
| 276 | favorites: spells.favorites ?? [], |
| 277 | autoCast: false, |
| 278 | castChances: spells.castChances ?? {} |
| 279 | }); |
| 280 | |
| 281 | engageAndLogout(harness, character, playerId, otherId); |
| 282 | assert.equal(spells.autoCast, true); |
| 283 | harness.stateDb.setCowardiceForgivenHour(character.character_id, cowardiceHourKey(harness.now())); |
| 284 | |
| 285 | const reattached = harness.simulation.reattachCharacterSession(character.character_id, 99); |
| 286 | assert.equal(reattached, playerId); |
| 287 | assert.equal(harness.simulation.isCombatLogout(character.character_id), false); |
| 288 | assert.equal(spells.autoCast, false); |
| 289 | |
| 290 | const skills = harness.stateDb.getSkills(character.character_id) ?? defaultPlayerSkills(); |
| 291 | const threshold = combatLogoutCoinThreshold(combatLevelFromSkills(skills)); |
| 292 | assert.equal( |
| 293 | harness.stateDb.getKillCounterState(character.character_id).killCounter, |
| 294 | threshold |
| 295 | ); |
| 296 | assert.ok(heldLetters(harness.stateDb, character.character_id).some((stack) => |
| 297 | stack.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 298 | )); |
| 299 | } |
| 300 | finally |
| 301 | { harness.close(); } |
| 302 | }); |
| 303 | |
| 304 | test("offline PvP death with enough coins skips cowardice skull", () => |
| 305 | { |
| 306 | const harness = makeSimulation({ workerCount: 1 }); |
| 307 | try |
| 308 | { |
| 309 | placeBlock(harness.worldDb, 20, 8, BlockType.churchAltar); |
| 310 | placeBlock(harness.worldDb, 7, 8, BlockType.bank); |
| 311 | harness.simulation.reloadWorld(); |
| 312 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 313 | const skills = harness.stateDb.getSkills(character.character_id) ?? defaultPlayerSkills(); |
| 314 | const threshold = combatLogoutCoinThreshold(combatLevelFromSkills(skills)); |
| 315 | const funded = addCoinsToSlots(emptySlots(), threshold + 10); |
| 316 | assert.equal(funded.ok, true); |
| 317 | harness.stateDb.saveBag(character.character_id, { |
| 318 | inventory: funded.slots, |
| 319 | equipment: emptyEquipment(), |
| 320 | vault: [], |
| 321 | vaultReserved: [] |
| 322 | }); |
| 323 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 324 | const other = makeCharacter(harness.stateDb, { tileX: 9, tileY: 8, firstName: "Foe" }); |
| 325 | const otherId = harness.simulation.spawnCharacter(2, other); |
| 326 | engageAndLogout(harness, character, playerId, otherId); |
| 327 | harness.stateDb.setCowardiceForgivenHour(character.character_id, cowardiceHourKey(harness.now())); |
| 328 | |
| 329 | const worker = harness.simulation.workers[0]; |
| 330 | componentsOf(harness.simulation, playerId).health.current = 1; |
| 331 | const events = applyDamageAndKill(worker.world, otherId, playerId, 999, harness.now()); |
| 332 | const died = events.find((event) => event.type === "player-died"); |
| 333 | harness.simulation.applyPlayerDiedEvent(died); |
| 334 | |
| 335 | assert.equal(harness.stateDb.getKillCounterState(character.character_id).killCounter, 0); |
| 336 | assert.ok(heldLetters(harness.stateDb, character.character_id).some((stack) => |
| 337 | stack.instance?.authorDisplayName === CHURCH_LETTER_AUTHOR |
| 338 | )); |
| 339 | assert.equal( |
| 340 | heldLetters(harness.stateDb, character.character_id).some((stack) => |
| 341 | stack.instance?.authorDisplayName === QUESTWARD_GIFT_AUTHOR |
| 342 | ), |
| 343 | false |
| 344 | ); |
| 345 | } |
| 346 | finally |
| 347 | { harness.close(); } |
| 348 | }); |
| 349 | |
| 350 | test("leave without recent combat despawns immediately", () => |
| 351 | { |
| 352 | const harness = makeSimulation({ workerCount: 1 }); |
| 353 | try |
| 354 | { |
| 355 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 356 | harness.simulation.spawnCharacter(1, character); |
| 357 | assert.equal(harness.simulation.beginCombatLogout(character.character_id), false); |
| 358 | harness.simulation.despawnCharacter(character.character_id); |
| 359 | assert.equal(harness.simulation.isCharacterOnline(character.character_id), false); |
| 360 | } |
| 361 | finally |
| 362 | { harness.close(); } |
| 363 | }); |
| 364 | |
| 365 | test("combat logout letter copy is fixed", () => |
| 366 | { |
| 367 | assert.match(formatChurchOfflineResurrectionBody(), /priesthood found your lifeless body/); |
| 368 | assert.match(formatCombatLogoutCowardiceBody(), /cowardice in honourable combat/); |
| 369 | }); |