BlueGit

timfa questward

Files

questward / server / src / combat / combatLogout.js

combatLogout.js

combatLogout.js 3.5 KiB Download Raw
1import { ItemId } from "../../../shared/definitions/items.js";
2import { combatLevelFromSkills } from "../../../shared/combat/combatLevel.js";
3import
4 {
5 combatLogoutCoinThreshold,
6 cowardiceHourKey
7 } from "../../../shared/combat/combatLogout.js";
8import
9 {
10 CHURCH_LETTER_AUTHOR,
11 QUESTWARD_GIFT_AUTHOR,
12 QUESTWARD_GIFT_ORG,
13 formatChurchOfflineResurrectionBody,
14 formatCombatLogoutCowardiceBody
15 } from "../../../shared/courier/systemLetters.js";
16import { defaultPlayerSkills } from "../../../shared/skills/state.js";
17
18/**
19 * Apply PvP combat-logout cowardice (skull + bank letter), honouring the
20 * once-per-hour forgiveness. Returns whether a penalty was applied.
21 */
22export function applyCombatLogoutCowardice (simulation, characterId, {
23 coinsDropped = null,
24 now = Date.now()
25} = {})
26{
27 const id = characterId | 0;
28 const skills = simulation.stateDb.getSkills(id) ?? defaultPlayerSkills();
29 const threshold = combatLogoutCoinThreshold(combatLevelFromSkills(skills));
30 let amount;
31 if (coinsDropped == null)
32 { amount = threshold; }
33 else if ((coinsDropped | 0) > threshold)
34 { return { applied: false, forgiven: false, amount: 0, threshold }; }
35 else
36 { amount = Math.max(0, threshold - (coinsDropped | 0)); }
37
38 if (amount <= 0)
39 { return { applied: false, forgiven: false, amount: 0, threshold }; }
40
41 const hour = cowardiceHourKey(now);
42 const forgivenHour = simulation.stateDb.getCowardiceForgivenHour(id);
43 if (forgivenHour !== hour)
44 {
45 simulation.stateDb.setCowardiceForgivenHour(id, hour);
46 return { applied: false, forgiven: true, amount, threshold };
47 }
48
49 simulation.stateDb.addKillCounter(id, amount, now);
50 simulation.refreshCharacterSkull?.(id, { force: true });
51 dispatchCombatLogoutBankLetter(simulation, id);
52 return { applied: true, forgiven: false, amount, threshold };
53}
54
55export function dispatchCombatLogoutBankLetter (simulation, characterId)
56{
57 const row = simulation.stateDb.getCharacter(characterId);
58 if (!row || !simulation.courier)
59 { return; }
60 const originTileX = row.tile_x | 0;
61 const originTileY = row.tile_y | 0;
62 const bank = simulation.courier.nearestBank?.(originTileX, originTileY);
63 simulation.courier.enqueueSystemLetter({
64 characterId,
65 originTileX: bank?.tileX ?? originTileX,
66 originTileY: bank?.tileY ?? originTileY,
67 authorDisplayName: QUESTWARD_GIFT_AUTHOR,
68 authorOrganization: QUESTWARD_GIFT_ORG,
69 body: formatCombatLogoutCowardiceBody()
70 });
71}
72
73export function dispatchChurchOfflineResurrectionLetter (simulation, characterId, tileX, tileY)
74{
75 if (!simulation.courier)
76 { return; }
77 const altar = nearestAltarTile(simulation, tileX, tileY);
78 simulation.courier.enqueueSystemLetter({
79 characterId,
80 originTileX: altar?.tileX ?? (tileX | 0),
81 originTileY: altar?.tileY ?? (tileY | 0),
82 authorDisplayName: CHURCH_LETTER_AUTHOR,
83 body: formatChurchOfflineResurrectionBody()
84 });
85}
86
87function nearestAltarTile (simulation, tileX, tileY)
88{
89 const altars = simulation.altarTiles ?? [];
90 if (!altars.length)
91 { return null; }
92 let best = null;
93 let bestDist = Infinity;
94 for (const altar of altars)
95 {
96 const dist = Math.hypot((altar.tileX | 0) - (tileX | 0), (altar.tileY | 0) - (tileY | 0));
97 if (dist < bestDist)
98 {
99 best = altar;
100 bestDist = dist;
101 }
102 }
103 return best;
104}
105
106export function coinsDroppedFromDeathEvents (events)
107{
108 let total = 0;
109 for (const event of events ?? [])
110 {
111 if (event.type !== "ground-item-appear")
112 { continue; }
113 const itemId = event.stack?.itemId ?? event.itemId;
114 if ((itemId | 0) !== ItemId.coins)
115 { continue; }
116 total += Math.max(0, event.stack?.quantity | 0);
117 }
118 return total;
119}