All files / src/shared/lib/scripts eventHelpers.ts

73.58% Statements 78/106
58.69% Branches 81/138
77.77% Functions 14/18
72.16% Lines 70/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 24912x                     12x 12x 12x         12x       11x 16x 1x     15x 15x 2x     13x             12x           30x 5x     25x   25x 2x     23x   28x 28x   28x 14x 14x 14x   14x 7x   28x         23x           12x       5x   11x 11x 2x 2x 2x   9x 4x     11x         5x           12x           26x 2x     24x   11x   11x                                   12x                                                                                                           12x         11x 11x 16x 18x 4x     4x       11x 17x     17x 1x 1x   16x               18x     18x     18x         16x 4x     11x     12x 7x 3x   4x    
import { EVENT_CALL_CUSTOM_EVENT, EVENT_FADE_IN } from "consts";
import type { ScriptEventDef } from "lib/scriptEventsHandlers/handlerTypes";
import type {
  ScriptEventNormalized,
  ScriptNormalized,
} from "shared/lib/entities/entitiesTypes";
import {
  Script,
  ScriptEvent,
  ScriptEventArgsOverride,
} from "shared/lib/resources/types";
import { walkNormalizedScript, walkScript } from "shared/lib/scripts/walk";
import { mapScriptValue } from "shared/lib/scriptValue/helpers";
import { isScriptValue } from "shared/lib/scriptValue/types";
import type { ScriptValue } from "shared/lib/scriptValue/types";
 
export type ScriptEventDefs = Record<string, ScriptEventDef>;
 
const remapActorReferencesInScriptValue = (
  scriptValue: ScriptValue,
  actorMapping: Record<string, string>,
) => {
  return mapScriptValue(scriptValue, (value) => {
    if (value.type !== "property") {
      return value;
    }
 
    const replacement = actorMapping[value.target];
    if (replacement === undefined) {
      return value;
    }
 
    return {
      ...value,
      target: replacement,
    };
  });
};
 
export const remapActorReferencesInEventArgs = (
  command: string,
  args: Record<string, unknown>,
  actorMapping: Record<string, string>,
  scriptEventDefs: ScriptEventDefs,
) => {
  if (command === EVENT_CALL_CUSTOM_EVENT) {
    return remapActorReferencesInCallScriptEventArgs(args, actorMapping);
  }
 
  const eventSchema = scriptEventDefs[command];
 
  if (!eventSchema) {
    return args;
  }
 
  const patchArgs = Object.keys(args).reduce(
    (memo, key) => {
      const field = eventSchema.fieldsLookup[key];
      const arg = args[key];
 
      if (field?.type === "actor" && typeof arg === "string") {
        const replacement = actorMapping[arg];
        Eif (replacement !== undefined) {
          memo[key] = replacement;
        }
      } else if (field && isScriptValue(arg)) {
        memo[key] = remapActorReferencesInScriptValue(arg, actorMapping);
      }
      return memo;
    },
    {} as Record<string, unknown>,
  );
 
  return {
    ...args,
    ...patchArgs,
  };
};
 
export const remapActorReferencesInCallScriptEventArgs = (
  args: Record<string, unknown>,
  actorMapping: Record<string, string>,
) => {
  const patchArgs = Object.keys(args).reduce(
    (memo, key) => {
      const arg = args[key];
      if (key.startsWith("$actor") && typeof arg === "string") {
        const replacement = actorMapping[arg];
        Eif (replacement !== undefined) {
          memo[key] = replacement;
        }
      } else if (key.startsWith("$variable") && isScriptValue(arg)) {
        memo[key] = remapActorReferencesInScriptValue(arg, actorMapping);
      }
 
      return memo;
    },
    {} as Record<string, unknown>,
  );
 
  return {
    ...args,
    ...patchArgs,
  };
};
 
export const remapActorReferencesInEventOverrides = (
  eventOverrides: Record<string, ScriptEventArgsOverride> | null | undefined,
  scriptEventsLookup: Record<string, ScriptEventNormalized>,
  actorMapping: Record<string, string>,
  scriptEventDefs: ScriptEventDefs,
) => {
  if (!eventOverrides) {
    return eventOverrides;
  }
 
  return Object.fromEntries(
    Object.entries(eventOverrides).map(([scriptEventId, override]) => {
      const command = scriptEventsLookup[scriptEventId]?.command;
 
      return [
        scriptEventId,
        {
          ...override,
          args: command
            ? remapActorReferencesInEventArgs(
                command,
                override.args,
                actorMapping,
                scriptEventDefs,
              )
            : override.args,
        },
      ];
    }),
  );
};
 
export const calculateAutoFadeEventIdNormalized = (
  script: string[],
  scriptEventsLookup: Record<string, ScriptEventNormalized>,
  customEventsLookup: Record<string, ScriptNormalized>,
  scriptEventDefs: ScriptEventDefs,
) => {
  const events = scriptEventDefs;
  let fadeEventId = "";
  const checkEvent =
    (eventId: string) => (scriptEvent: ScriptEventNormalized) => {
      if (!fadeEventId && events[scriptEvent.command]?.waitUntilAfterInitFade) {
        if (scriptEvent.command === EVENT_FADE_IN) {
          fadeEventId = "MANUAL";
        } else {
          fadeEventId = eventId;
        }
      }
    };
  for (const eventValue of script) {
    const scriptEvent = scriptEventsLookup[eventValue];
    if (scriptEvent?.args?.__comment) {
      continue;
    }
    if (scriptEvent?.command === EVENT_FADE_IN) {
      fadeEventId = "MANUAL";
      break;
    }
    walkNormalizedScript(
      [eventValue],
      scriptEventsLookup,
      {
        customEvents: {
          lookup: customEventsLookup,
          maxDepth: 5,
        },
        filter: (childEvent) => {
          if (childEvent?.args?.__comment) {
            return false;
          }
          if (events[childEvent.command]?.allowChildrenBeforeInitFade) {
            return false;
          }
          return true;
        },
      },
      checkEvent(eventValue),
    );
    if (fadeEventId.length > 0) {
      break;
    }
  }
  return fadeEventId;
};
 
export const calculateAutoFadeEventId = (
  script: ScriptEvent[],
  customEventsLookup: Record<string, Script>,
  scriptEventDefs: ScriptEventDefs,
) => {
  const events = scriptEventDefs;
  let fadeEventId = "";
  const checkEvent = (eventId: string) => (scriptEvent: ScriptEvent) => {
    if (!fadeEventId && events[scriptEvent.command]?.waitUntilAfterInitFade) {
      Iif (scriptEvent.command === EVENT_FADE_IN) {
        fadeEventId = "MANUAL";
      } else {
        fadeEventId = eventId;
      }
    }
  };
  for (const scriptEvent of script) {
    Iif (scriptEvent?.args?.__comment) {
      continue;
    }
    if (scriptEvent?.command === EVENT_FADE_IN) {
      fadeEventId = "MANUAL";
      break;
    }
    walkScript(
      [scriptEvent],
      {
        customEvents: {
          lookup: customEventsLookup,
          maxDepth: 5,
        },
        filter: (childEvent) => {
          Iif (childEvent?.args?.__comment) {
            return false;
          }
          Iif (events[childEvent.command]?.allowChildrenBeforeInitFade) {
            return false;
          }
          return true;
        },
      },
      checkEvent(scriptEvent.id),
    );
    if (fadeEventId.length > 0) {
      break;
    }
  }
  return fadeEventId;
};
 
export const isEmptyScript = (script: ScriptEvent[]) => {
  if (script.length === 0) {
    return true;
  }
  return script.every((scriptEvent) => scriptEvent?.args?.__comment);
};