| 1 | import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js"; |
| 2 | import { chunkKey, tileToChunk } from "../world/coordinates.js"; |
| 3 | |
| 4 | /** |
| 5 | * Resolve which dimension an FX / replication event belongs to. |
| 6 | * Prefer an explicit event.dimensionId; otherwise ask the directory for any |
| 7 | * involved entity id (entity / attacker / caster / target). |
| 8 | * |
| 9 | * @param {object} event |
| 10 | * @param {(entityId: number) => string|null|undefined} resolveEntityDimensionId |
| 11 | */ |
| 12 | export function resolveEventDimensionId (event, resolveEntityDimensionId) |
| 13 | { |
| 14 | if (event?.dimensionId != null) |
| 15 | { return normalizeDimensionId(event.dimensionId); } |
| 16 | const entityIds = [ |
| 17 | event?.entityId, |
| 18 | event?.attackerId, |
| 19 | event?.casterId, |
| 20 | event?.ownerEntityId, |
| 21 | event?.targetId, |
| 22 | event?.targetEntityId |
| 23 | ]; |
| 24 | for (const id of entityIds) |
| 25 | { |
| 26 | if (id == null) |
| 27 | { continue; } |
| 28 | const dim = resolveEntityDimensionId?.(id); |
| 29 | if (dim != null) |
| 30 | { return normalizeDimensionId(dim); } |
| 31 | } |
| 32 | return OVERWORLD_ID; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Chunk interest keys for publishing an event to subscribed clients. |
| 37 | * @returns {{ fromKey: string|null, toKey: string|null, dimensionId: string, fromDimensionId: string }} |
| 38 | */ |
| 39 | export function eventInterestKeys (event, resolveEntityDimensionId) |
| 40 | { |
| 41 | const toDim = resolveEventDimensionId(event, resolveEntityDimensionId); |
| 42 | const fromDim = event?.fromDimensionId != null |
| 43 | ? normalizeDimensionId(event.fromDimensionId) |
| 44 | : toDim; |
| 45 | const fromKey = event?.fromTileX != null && event?.fromTileY != null |
| 46 | ? chunkKey(tileToChunk(event.fromTileX), tileToChunk(event.fromTileY), fromDim) |
| 47 | : null; |
| 48 | let toKey = event?.tileX != null && event?.tileY != null |
| 49 | ? chunkKey(tileToChunk(event.tileX), tileToChunk(event.tileY), toDim) |
| 50 | : null; |
| 51 | if (toKey == null && event?.chunkX != null && event?.chunkY != null) |
| 52 | { toKey = chunkKey(event.chunkX | 0, event.chunkY | 0, toDim); } |
| 53 | return { |
| 54 | fromKey, |
| 55 | toKey, |
| 56 | dimensionId: toDim, |
| 57 | fromDimensionId: fromDim |
| 58 | }; |
| 59 | } |