| 1 | import { formatQuantity } from "../../../shared/definitions/items.js"; |
| 2 | import { |
| 3 | bountyPayoutLetterBody, |
| 4 | formatBountyCopper, |
| 5 | bountyPayoffFromCoins, |
| 6 | BOUNTY_PAYOFF_COINS_PER_POINT, |
| 7 | bountyNextDecayAt |
| 8 | } from "../../../shared/bounty/pricing.js"; |
| 9 | import { BOUNTY_OFFICE_AUTHOR } from "../../../shared/courier/systemLetters.js"; |
| 10 | import { layersFromBag } from "../../../shared/appearance/layers.js"; |
| 11 | import { hairFromCharacterRow } from "../../../shared/appearance/hair.js"; |
| 12 | import { |
| 13 | addCoinsToSlots, |
| 14 | normalizeVault, |
| 15 | takeCoinsFromSlots |
| 16 | } from "../../../shared/items/inventory.js"; |
| 17 | import { isAdjacentTile } from "../../../shared/navigation/adjacency.js"; |
| 18 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 19 | |
| 20 | function userError (message) |
| 21 | { |
| 22 | const error = new Error(message); |
| 23 | error.userFacing = true; |
| 24 | return error; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Bounty office: place/raise bounties (near office), settle PvP kills into killer vault + letter. |
| 29 | */ |
| 30 | export class BountyService |
| 31 | { |
| 32 | constructor (stateDb, simulation, courier) |
| 33 | { |
| 34 | this.stateDb = stateDb; |
| 35 | this.simulation = simulation; |
| 36 | this.courier = courier; |
| 37 | } |
| 38 | |
| 39 | handleCommand (sessionId, command) |
| 40 | { |
| 41 | switch (command.type) |
| 42 | { |
| 43 | case "open-bounty": |
| 44 | return this.open(sessionId, command.characterId); |
| 45 | case "raise-bounty": |
| 46 | return this.placeOrRaise( |
| 47 | sessionId, |
| 48 | command.characterId, |
| 49 | command.firstName, |
| 50 | command.lastName, |
| 51 | command.amountCopper |
| 52 | ); |
| 53 | case "pay-bounty": |
| 54 | return this.payOff(sessionId, command.characterId, command.amountCopper); |
| 55 | case "get-character-bounty": |
| 56 | return this.lookup( |
| 57 | sessionId, |
| 58 | command.characterId, |
| 59 | command.targetCharacterId, |
| 60 | command.firstName, |
| 61 | command.lastName |
| 62 | ); |
| 63 | case "close-bounty": |
| 64 | return { events: [{ type: "bounty-closed", sessionId, characterId: command.characterId }] }; |
| 65 | default: |
| 66 | return null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | open (sessionId, characterId) |
| 71 | { |
| 72 | this.#requireNearOffice(characterId); |
| 73 | return { |
| 74 | events: [{ |
| 75 | type: "bounty-snapshot", |
| 76 | sessionId, |
| 77 | characterId, |
| 78 | ...this.#myBountyPayload(characterId) |
| 79 | }] |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Lookup target by characterId or first+last name. Adjacency not required (inspect UI). |
| 85 | */ |
| 86 | lookup (sessionId, requesterCharacterId, targetCharacterId, firstName, lastName) |
| 87 | { |
| 88 | let row = null; |
| 89 | if (targetCharacterId) |
| 90 | { row = this.stateDb.getCharacter(targetCharacterId | 0); } |
| 91 | else |
| 92 | { |
| 93 | const first = String(firstName ?? "").trim(); |
| 94 | const last = String(lastName ?? "").trim(); |
| 95 | if (!first || !last) |
| 96 | { throw userError("Enter a first and last name."); } |
| 97 | row = this.stateDb.getCharacterByName(first, last); |
| 98 | } |
| 99 | if (!row) |
| 100 | { throw userError("No adventurer by that name."); } |
| 101 | |
| 102 | const amountCopper = this.stateDb.getCharacterBounty(row.character_id, this.#now()); |
| 103 | const bag = this.#loadBagQuiet(row.character_id); |
| 104 | return { |
| 105 | events: [{ |
| 106 | type: "character-bounty", |
| 107 | sessionId, |
| 108 | characterId: requesterCharacterId, |
| 109 | target: { |
| 110 | characterId: row.character_id, |
| 111 | firstName: row.first_name, |
| 112 | lastName: row.last_name, |
| 113 | sex: row.sex, |
| 114 | layers: layersFromBag(row.sex, bag, hairFromCharacterRow(row)), |
| 115 | amountCopper, |
| 116 | amountLabel: formatBountyCopper(amountCopper) |
| 117 | } |
| 118 | }] |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | placeOrRaise (sessionId, placerCharacterId, firstName, lastName, amountCopper) |
| 123 | { |
| 124 | this.#requireNearOffice(placerCharacterId); |
| 125 | const first = String(firstName ?? "").trim(); |
| 126 | const last = String(lastName ?? "").trim(); |
| 127 | if (!first || !last) |
| 128 | { throw userError("Enter a first and last name."); } |
| 129 | const copper = Math.floor(Number(amountCopper) || 0); |
| 130 | if (!Number.isFinite(copper) || copper <= 0) |
| 131 | { throw userError("Enter a positive coin amount."); } |
| 132 | |
| 133 | const target = this.stateDb.getCharacterByName(first, last); |
| 134 | if (!target) |
| 135 | { throw userError("No adventurer by that name."); } |
| 136 | const targetId = target.character_id | 0; |
| 137 | if (targetId === (placerCharacterId | 0)) |
| 138 | { throw userError("You cannot place a bounty on yourself."); } |
| 139 | |
| 140 | const previous = this.stateDb.getCharacterBounty(targetId, this.#now()); |
| 141 | const bag = this.#loadBag(placerCharacterId); |
| 142 | const paid = takeCoinsFromSlots(bag.inventory ?? [], copper); |
| 143 | if (!paid.ok) |
| 144 | { throw userError(`Need ${formatQuantity(copper)} in your inventory.`); } |
| 145 | bag.inventory = paid.slots; |
| 146 | this.#persistBag(placerCharacterId, bag); |
| 147 | |
| 148 | const nextAmount = this.stateDb.addCharacterBounty(targetId, copper, this.#now()); |
| 149 | this.simulation.refreshCharacterSkull?.(targetId, { force: true }); |
| 150 | this.simulation.pendingPrivate.push({ |
| 151 | type: "toast", |
| 152 | characterId: targetId, |
| 153 | message: previous > 0 |
| 154 | ? `Someone raised the bounty on your head to ${formatBountyCopper(nextAmount)}.` |
| 155 | : `A bounty of ${formatBountyCopper(nextAmount)} has been placed on your head.` |
| 156 | }); |
| 157 | |
| 158 | return { |
| 159 | events: [{ |
| 160 | type: "bounty-snapshot", |
| 161 | sessionId, |
| 162 | characterId: placerCharacterId, |
| 163 | ...this.#myBountyPayload(placerCharacterId), |
| 164 | raised: { |
| 165 | characterId: targetId, |
| 166 | firstName: target.first_name, |
| 167 | lastName: target.last_name, |
| 168 | amountCopper: nextAmount, |
| 169 | amountLabel: formatBountyCopper(nextAmount), |
| 170 | addedCopper: copper, |
| 171 | message: previous > 0 |
| 172 | ? `Raised bounty on ${target.first_name} ${target.last_name} to ${formatBountyCopper(nextAmount)}.` |
| 173 | : `Placed a ${formatBountyCopper(nextAmount)} bounty on ${target.first_name} ${target.last_name}.` |
| 174 | } |
| 175 | }] |
| 176 | }; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Pay inventory coins to reduce your own bounty (10 coins → 1 bounty cleared). |
| 181 | */ |
| 182 | payOff (sessionId, characterId, amountCopper) |
| 183 | { |
| 184 | this.#requireNearOffice(characterId); |
| 185 | const offered = Math.floor(Number(amountCopper) || 0); |
| 186 | if (!Number.isFinite(offered) || offered <= 0) |
| 187 | { throw userError("Enter a positive coin amount."); } |
| 188 | |
| 189 | const current = this.stateDb.getCharacterBounty(characterId, this.#now()); |
| 190 | if (current <= 0) |
| 191 | { throw userError("There is no bounty on your head."); } |
| 192 | |
| 193 | const { reduce, costCopper } = bountyPayoffFromCoins(offered, current); |
| 194 | if (reduce <= 0) |
| 195 | { |
| 196 | throw userError( |
| 197 | `Need at least ${formatQuantity(BOUNTY_PAYOFF_COINS_PER_POINT)} to clear 1 bounty coin.` |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | const bag = this.#loadBag(characterId); |
| 202 | const paid = takeCoinsFromSlots(bag.inventory ?? [], costCopper); |
| 203 | if (!paid.ok) |
| 204 | { throw userError(`Need ${formatQuantity(costCopper)} in your inventory.`); } |
| 205 | bag.inventory = paid.slots; |
| 206 | this.#persistBag(characterId, bag); |
| 207 | |
| 208 | const nextAmount = this.stateDb.reduceCharacterBounty(characterId, reduce, this.#now()); |
| 209 | this.simulation.refreshCharacterSkull?.(characterId, { force: true }); |
| 210 | |
| 211 | return { |
| 212 | events: [{ |
| 213 | type: "bounty-snapshot", |
| 214 | sessionId, |
| 215 | characterId, |
| 216 | ...this.#myBountyPayload(characterId), |
| 217 | paidOff: { |
| 218 | spentCopper: costCopper, |
| 219 | clearedCopper: reduce, |
| 220 | amountCopper: nextAmount, |
| 221 | amountLabel: formatBountyCopper(nextAmount), |
| 222 | message: nextAmount > 0 |
| 223 | ? `Paid ${formatQuantity(costCopper)} to clear ${formatBountyCopper(reduce)}. Remaining: ${formatBountyCopper(nextAmount)}.` |
| 224 | : `Paid ${formatQuantity(costCopper)} — your bounty is cleared.` |
| 225 | } |
| 226 | }] |
| 227 | }; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Called when a player dies. Non-friend PvP killer collects full bounty into vault + office letter. |
| 232 | * Friend / same-account / mob kills leave the bounty untouched. |
| 233 | * @returns {{ paidCopper: number, victimName: string } | null} |
| 234 | */ |
| 235 | settleKill (victimCharacterId, attackerEntityId, deathTileX, deathTileY, deathDimensionId = null) |
| 236 | { |
| 237 | const attackerId = attackerEntityId | 0; |
| 238 | if (!attackerId) |
| 239 | { return null; } |
| 240 | const attackerRec = this.simulation.directory.get(attackerId); |
| 241 | const killerCharacterId = Number(attackerRec?.characterId) || 0; |
| 242 | if (!killerCharacterId || killerCharacterId === (victimCharacterId | 0)) |
| 243 | { return null; } |
| 244 | |
| 245 | if (this.stateDb.areFriends(victimCharacterId, killerCharacterId)) |
| 246 | { return null; } |
| 247 | |
| 248 | const paidCopper = this.stateDb.takeCharacterBounty(victimCharacterId, this.#now()); |
| 249 | if (paidCopper <= 0) |
| 250 | { return null; } |
| 251 | |
| 252 | const bag = this.#loadBag(killerCharacterId); |
| 253 | const vault = normalizeVault( |
| 254 | bag.vaultReserved != null |
| 255 | ? { slots: bag.vault, reserved: bag.vaultReserved } |
| 256 | : bag.vault |
| 257 | ); |
| 258 | const added = addCoinsToSlots(vault.slots, paidCopper, { growable: true }); |
| 259 | bag.vault = added.slots; |
| 260 | bag.vaultReserved = vault.reserved; |
| 261 | this.#persistBag(killerCharacterId, bag); |
| 262 | |
| 263 | const victim = this.stateDb.getCharacter(victimCharacterId); |
| 264 | const killer = this.stateDb.getCharacter(killerCharacterId); |
| 265 | const victimName = victim |
| 266 | ? `${victim.first_name} ${victim.last_name}`.trim() |
| 267 | : "someone"; |
| 268 | const killerName = killer |
| 269 | ? `${killer.first_name} ${killer.last_name}`.trim() |
| 270 | : "Adventurer"; |
| 271 | |
| 272 | this.#dispatchPayoutLetter({ |
| 273 | killerCharacterId, |
| 274 | killerName, |
| 275 | victimName, |
| 276 | paidCopper, |
| 277 | deathTileX, |
| 278 | deathTileY, |
| 279 | deathDimensionId |
| 280 | }); |
| 281 | |
| 282 | this.simulation.pendingPrivate.push({ |
| 283 | type: "toast", |
| 284 | characterId: killerCharacterId, |
| 285 | message: `Bounty collected: ${formatBountyCopper(paidCopper)} for defeating ${victimName} (sent to vault).` |
| 286 | }); |
| 287 | |
| 288 | this.simulation.refreshCharacterSkull?.(victimCharacterId, { force: true }); |
| 289 | |
| 290 | return { paidCopper, victimName }; |
| 291 | } |
| 292 | |
| 293 | #now () |
| 294 | { |
| 295 | return typeof this.simulation?.now === "number" ? this.simulation.now : Date.now(); |
| 296 | } |
| 297 | |
| 298 | #myBountyPayload (characterId) |
| 299 | { |
| 300 | const now = this.#now(); |
| 301 | const amountCopper = this.stateDb.getCharacterBounty(characterId, now); |
| 302 | return { |
| 303 | myAmountCopper: amountCopper, |
| 304 | myAmountLabel: formatBountyCopper(amountCopper), |
| 305 | nextDecayAt: bountyNextDecayAt(now), |
| 306 | payoffCoinsPerPoint: BOUNTY_PAYOFF_COINS_PER_POINT, |
| 307 | message: amountCopper > 0 |
| 308 | ? `Bounty on your head: ${formatBountyCopper(amountCopper)}.` |
| 309 | : "No bounty on your head." |
| 310 | }; |
| 311 | } |
| 312 | |
| 313 | #dispatchPayoutLetter ({ killerCharacterId, killerName, victimName, paidCopper, deathTileX, deathTileY, deathDimensionId = null }) |
| 314 | { |
| 315 | const office = this.courier?.nearestBountyOffice?.(deathTileX | 0, deathTileY | 0, deathDimensionId) |
| 316 | ?? { tileX: deathTileX | 0, tileY: deathTileY | 0, dimensionId: deathDimensionId }; |
| 317 | const body = bountyPayoutLetterBody({ |
| 318 | killerName, |
| 319 | victimName, |
| 320 | amountCopper: paidCopper |
| 321 | }); |
| 322 | try |
| 323 | { |
| 324 | this.courier.enqueueSystemLetter({ |
| 325 | characterId: killerCharacterId, |
| 326 | originTileX: office.tileX, |
| 327 | originTileY: office.tileY, |
| 328 | originDimensionId: office.dimensionId, |
| 329 | authorDisplayName: BOUNTY_OFFICE_AUTHOR, |
| 330 | body |
| 331 | }); |
| 332 | } |
| 333 | catch |
| 334 | { |
| 335 | /* Coins already vaulted; letter is best-effort if no bank exists. */ |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | #requireNearOffice (characterId) |
| 340 | { |
| 341 | const entityId = this.simulation.characterEntities.get(characterId); |
| 342 | const position = entityId ? this.simulation.positionOf(entityId) : null; |
| 343 | if (!position) |
| 344 | { throw userError("Enter the world before using a Bounty Office."); } |
| 345 | const tiles = this.courier?.bountyOfficeTiles ?? []; |
| 346 | if (!tiles.length) |
| 347 | { throw userError("No Bounty Office exists in this world yet."); } |
| 348 | const dim = normalizeDimensionId(position.dimensionId ?? OVERWORLD_ID); |
| 349 | const near = tiles.some((tile) => |
| 350 | normalizeDimensionId(tile.dimensionId ?? OVERWORLD_ID) === dim |
| 351 | && isAdjacentTile(position.tileX, position.tileY, tile.tileX, tile.tileY)); |
| 352 | if (!near) |
| 353 | { throw userError("Stand next to a Bounty Office."); } |
| 354 | return position; |
| 355 | } |
| 356 | |
| 357 | #loadBagQuiet (characterId) |
| 358 | { |
| 359 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 360 | return onlineId |
| 361 | ? this.simulation.bagOf(onlineId) |
| 362 | : this.stateDb.getBag(characterId); |
| 363 | } |
| 364 | |
| 365 | #loadBag (characterId) |
| 366 | { |
| 367 | const bag = this.#loadBagQuiet(characterId); |
| 368 | if (!bag) |
| 369 | { throw userError("No bag."); } |
| 370 | return bag; |
| 371 | } |
| 372 | |
| 373 | #persistBag (characterId, bag) |
| 374 | { |
| 375 | this.stateDb.saveBag(characterId, bag); |
| 376 | const onlineId = this.simulation.characterEntities.get(characterId); |
| 377 | if (onlineId) |
| 378 | { |
| 379 | this.simulation.applyBag(onlineId, bag); |
| 380 | this.simulation.pushInventory(characterId, bag); |
| 381 | } |
| 382 | } |
| 383 | } |