|
| 1 | +import type { |
| 2 | + CodexPetRandomAction, |
| 3 | + CodexPetRandomActionRunner, |
| 4 | + CodexPetRandomActionRunnerOptions, |
| 5 | + CodexPetRandomActionTarget |
| 6 | +} from "./types.js"; |
| 7 | + |
| 8 | +const DEFAULT_MIN_INTERVAL_SECONDS = 30; |
| 9 | +const DEFAULT_MAX_INTERVAL_MULTIPLIER = 3; |
| 10 | +const SECONDS_TO_MS = 1000; |
| 11 | + |
| 12 | +type TimeoutHandle = ReturnType<typeof setTimeout>; |
| 13 | + |
| 14 | +function clamp(value: number, min: number, max: number): number { |
| 15 | + return Math.min(Math.max(value, min), max); |
| 16 | +} |
| 17 | + |
| 18 | +function getPositiveNumber(value: number | undefined, fallback: number): number { |
| 19 | + return Number.isFinite(value) && value !== undefined && value > 0 |
| 20 | + ? value |
| 21 | + : fallback; |
| 22 | +} |
| 23 | + |
| 24 | +function getDelayMs( |
| 25 | + options: CodexPetRandomActionRunnerOptions, |
| 26 | + random: () => number |
| 27 | +): number { |
| 28 | + const average = getPositiveNumber(options.averageIntervalSeconds, 120); |
| 29 | + const min = getPositiveNumber( |
| 30 | + options.minIntervalSeconds, |
| 31 | + DEFAULT_MIN_INTERVAL_SECONDS |
| 32 | + ); |
| 33 | + const max = getPositiveNumber( |
| 34 | + options.maxIntervalSeconds, |
| 35 | + average * DEFAULT_MAX_INTERVAL_MULTIPLIER |
| 36 | + ); |
| 37 | + const sample = -Math.log(1 - clamp(random(), 0, 0.999999)) * average; |
| 38 | + |
| 39 | + return clamp(sample, min, Math.max(min, max)) * SECONDS_TO_MS; |
| 40 | +} |
| 41 | + |
| 42 | +function pickWeightedAction( |
| 43 | + actions: CodexPetRandomAction[], |
| 44 | + random: () => number |
| 45 | +): CodexPetRandomAction | null { |
| 46 | + const weightedActions = actions |
| 47 | + .map((action) => ({ |
| 48 | + ...action, |
| 49 | + weight: getPositiveNumber(action.weight, 1) |
| 50 | + })) |
| 51 | + .filter((action) => action.weight > 0); |
| 52 | + const totalWeight = weightedActions.reduce( |
| 53 | + (total, action) => total + action.weight, |
| 54 | + 0 |
| 55 | + ); |
| 56 | + |
| 57 | + if (totalWeight <= 0) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + let cursor = clamp(random(), 0, 0.999999) * totalWeight; |
| 62 | + |
| 63 | + for (const action of weightedActions) { |
| 64 | + if (cursor < action.weight) { |
| 65 | + return action; |
| 66 | + } |
| 67 | + |
| 68 | + cursor -= action.weight; |
| 69 | + } |
| 70 | + |
| 71 | + return weightedActions[weightedActions.length - 1] ?? null; |
| 72 | +} |
| 73 | + |
| 74 | +function canPlayRandomAction(target: CodexPetRandomActionTarget): boolean { |
| 75 | + const snapshot = target.getSnapshot(); |
| 76 | + |
| 77 | + return ( |
| 78 | + snapshot.state === "idle" && |
| 79 | + !snapshot.hidden && |
| 80 | + !snapshot.paused && |
| 81 | + !snapshot.removed && |
| 82 | + snapshot.mounted |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +export function createCodexPetRandomActionRunner( |
| 87 | + target: CodexPetRandomActionTarget, |
| 88 | + options: CodexPetRandomActionRunnerOptions |
| 89 | +): CodexPetRandomActionRunner { |
| 90 | + const random = options.random ?? Math.random; |
| 91 | + let timeout: TimeoutHandle | null = null; |
| 92 | + let running = false; |
| 93 | + |
| 94 | + function clear(): void { |
| 95 | + if (timeout === null) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + clearTimeout(timeout); |
| 100 | + timeout = null; |
| 101 | + } |
| 102 | + |
| 103 | + function schedule(): void { |
| 104 | + clear(); |
| 105 | + |
| 106 | + if (!running) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + timeout = setTimeout(run, getDelayMs(options, random)); |
| 111 | + } |
| 112 | + |
| 113 | + function run(): void { |
| 114 | + timeout = null; |
| 115 | + |
| 116 | + if (running && canPlayRandomAction(target)) { |
| 117 | + const action = pickWeightedAction(options.actions, random); |
| 118 | + |
| 119 | + if (action) { |
| 120 | + target.play(action.state, { loops: action.loops ?? 1 }); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + schedule(); |
| 125 | + } |
| 126 | + |
| 127 | + return { |
| 128 | + start: () => { |
| 129 | + if (running) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + running = true; |
| 134 | + schedule(); |
| 135 | + }, |
| 136 | + stop: () => { |
| 137 | + running = false; |
| 138 | + clear(); |
| 139 | + }, |
| 140 | + destroy: () => { |
| 141 | + running = false; |
| 142 | + clear(); |
| 143 | + } |
| 144 | + }; |
| 145 | +} |
0 commit comments