|
| 1 | +import { EffectInstance, EffectList } from "../../types"; |
| 2 | +import { EffectManager } from "../effects/effect-manager"; |
| 3 | +import { containsAll, getRandomInt, shuffleArray } from "../utils"; |
| 4 | +import logger from "../logwrapper"; |
| 5 | + |
| 6 | +interface CacheEntry { |
| 7 | + /** |
| 8 | + * The queue of effect IDs to be executed in order |
| 9 | + */ |
| 10 | + queue: string[]; |
| 11 | + /** |
| 12 | + * The current set of effect IDs in the effect list, used to determine if the effect list has changed since last execution |
| 13 | + */ |
| 14 | + currentEffectIds: string[]; |
| 15 | + /** |
| 16 | + * The last effect ID that was executed from this list |
| 17 | + */ |
| 18 | + lastEffectId?: string; |
| 19 | +} |
| 20 | + |
| 21 | +type EffectListCache = Record<string, CacheEntry>; |
| 22 | + |
| 23 | +const sequentialCache: EffectListCache = {}; |
| 24 | +const randomCache: EffectListCache = {}; |
| 25 | + |
| 26 | +function getEntryFromCache(effectListId: string, effectIds: string[], cache: EffectListCache, shuffle: boolean): CacheEntry { |
| 27 | + |
| 28 | + // try to find queue in cache |
| 29 | + let cacheEntry = cache[effectListId]; |
| 30 | + if (!cacheEntry) { |
| 31 | + // we don't have a preexisting queue in the cache, create a new one |
| 32 | + cacheEntry = { |
| 33 | + queue: shuffleArray(effectIds), |
| 34 | + currentEffectIds: effectIds |
| 35 | + }; |
| 36 | + |
| 37 | + // add to the cache |
| 38 | + cache[effectListId] = cacheEntry; |
| 39 | + } else { |
| 40 | + // theres an existing queue in the cache, check if the effect list has changed at all since last time |
| 41 | + // and if so, rebuild the queue |
| 42 | + const effectsHaventChanged = containsAll(effectIds, cacheEntry.currentEffectIds); |
| 43 | + if (!effectsHaventChanged) { |
| 44 | + cacheEntry.queue = shuffle ? shuffleArray(effectIds) : effectIds; |
| 45 | + cacheEntry.currentEffectIds = effectIds; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // if empty, we need to rebuild the queue |
| 50 | + if (cacheEntry.queue.length === 0) { |
| 51 | + if (shuffle) { |
| 52 | + if (effectIds.length > 1) { |
| 53 | + let shuffledEffectIds: string[] = []; |
| 54 | + |
| 55 | + // keep shuffling until we get a different first effect than the last played effect |
| 56 | + do { |
| 57 | + shuffledEffectIds = shuffleArray(effectIds); |
| 58 | + } while (cacheEntry.lastEffectId && shuffledEffectIds[0] === cacheEntry.lastEffectId); |
| 59 | + |
| 60 | + cacheEntry.queue = shuffledEffectIds; |
| 61 | + } else { |
| 62 | + // if we don't have more than one effect, there is nothing to shuffle |
| 63 | + cacheEntry.queue = effectIds; |
| 64 | + } |
| 65 | + } else { |
| 66 | + cacheEntry.queue = effectIds; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return cacheEntry; |
| 71 | +} |
| 72 | + |
| 73 | +function getValidEffects(effectList: EffectList): EffectInstance[] { |
| 74 | + return effectList.list.filter((e) => { |
| 75 | + if (e.active != null && !e.active) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + const effectType = EffectManager.getEffectById(e.type); |
| 79 | + return effectType?.definition?.isNoOp !== true; |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +function getSequentialEffect(effectList: EffectList): EffectInstance | null { |
| 84 | + const validEffects = getValidEffects(effectList); |
| 85 | + |
| 86 | + if (!validEffects.length) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + const effectIds = validEffects.map(e => e.id); |
| 91 | + |
| 92 | + const cacheEntry = getEntryFromCache(effectList.id, effectIds, sequentialCache, false); |
| 93 | + |
| 94 | + // gets the next effect from beginning of queue and removes it |
| 95 | + const nextEffectId = cacheEntry.queue.shift(); |
| 96 | + cacheEntry.lastEffectId = nextEffectId; |
| 97 | + |
| 98 | + return effectList.list.find(e => e.id === nextEffectId); |
| 99 | +} |
| 100 | + |
| 101 | +function getRandomEffect(effectList: EffectList): EffectInstance | null { |
| 102 | + const validEffects = getValidEffects(effectList); |
| 103 | + |
| 104 | + if (!validEffects.length) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + let chosenEffect: EffectInstance | null = null; |
| 109 | + |
| 110 | + const dontRepeat = effectList.dontRepeatUntilAllUsed === true; |
| 111 | + const weighted = effectList.weighted === true; |
| 112 | + |
| 113 | + if (weighted) { |
| 114 | + const sumOfAllWeights = validEffects.reduce((acc, e) => acc + (e.percentWeight ?? 0.5), 0); |
| 115 | + const effectsWithPercentages = validEffects.map(e => ({ |
| 116 | + effect: e, |
| 117 | + chance: ((e.percentWeight ?? 0.5) / sumOfAllWeights) * 100 |
| 118 | + })); |
| 119 | + |
| 120 | + const min = 0.0001, max = 100.0; |
| 121 | + const random = Math.random() * (max - min) + min; |
| 122 | + |
| 123 | + logger.debug("Random effect chance roll: ", random); |
| 124 | + |
| 125 | + let currentChance = 0; |
| 126 | + for (let i = 0; i < effectsWithPercentages.length; i++) { |
| 127 | + const effectWithPercentage = effectsWithPercentages[i]; |
| 128 | + currentChance += effectWithPercentage.chance; |
| 129 | + if (random <= currentChance) { |
| 130 | + chosenEffect = effectWithPercentage.effect; |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + } else if (dontRepeat) { |
| 135 | + // if we shouldnt repeat, we need to use the cache to queue effects |
| 136 | + |
| 137 | + // get array of effect ids in this random effect |
| 138 | + const effectIds = validEffects.map(e => e.id); |
| 139 | + |
| 140 | + // try to find queue in cache |
| 141 | + const cacheEntry = getEntryFromCache(effectList.id, effectIds, randomCache, true); |
| 142 | + |
| 143 | + // gets the next effect from beginning of queue and removes it |
| 144 | + const chosenEffectId = cacheEntry.queue.shift(); |
| 145 | + cacheEntry.lastEffectId = chosenEffectId; |
| 146 | + chosenEffect = effectList.list.find(e => e.id === chosenEffectId); |
| 147 | + |
| 148 | + } else { |
| 149 | + // we don't care about repeats, just get an effect via random index |
| 150 | + const randomIndex = getRandomInt(0, validEffects.length - 1); |
| 151 | + chosenEffect = validEffects[randomIndex]; |
| 152 | + } |
| 153 | + |
| 154 | + //removed any cached entries if we are no longer using them |
| 155 | + if ((weighted || !dontRepeat) && randomCache[effectList.id]) { |
| 156 | + delete randomCache[effectList.id]; |
| 157 | + } |
| 158 | + |
| 159 | + return chosenEffect; |
| 160 | +} |
| 161 | + |
| 162 | +export function resolveEffectsForExecution(effectList: EffectList): EffectInstance[] { |
| 163 | + const mode = effectList.runMode ?? "all"; |
| 164 | + if (mode === "all") { |
| 165 | + return effectList.list; |
| 166 | + } |
| 167 | + if (mode === "sequential") { |
| 168 | + const nextEffect = getSequentialEffect(effectList); |
| 169 | + return nextEffect ? [nextEffect] : []; |
| 170 | + } |
| 171 | + if (mode === "random") { |
| 172 | + const nextEffect = getRandomEffect(effectList); |
| 173 | + return nextEffect ? [nextEffect] : []; |
| 174 | + } |
| 175 | + return []; |
| 176 | +} |
| 177 | + |
0 commit comments