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

81.82% Statements 36/44
36.17% Branches 17/47
85.71% Functions 6/7
86.11% Lines 31/36

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    48x               48x   48x         46x 46x                                                   48x         317x 317x 317x     48x           84x 84x 84x             48x             83x 2x   81x 81x             48x           80x 80x 80x 80x 80x             48x             80x 8x   72x 72x 72x 72x            
import type { ScriptEventDef } from "lib/scriptEventsHandlers/handlerTypes";
import type { ScriptEventFieldSchema } from "shared/lib/entities/entitiesTypes";
import {
  isUnionPropertyValue,
  isUnionVariableValue,
} from "shared/lib/entities/entitiesHelpers";
import { ScriptEventArgs } from "shared/lib/resources/types";
 
export type ScriptEventDefs = Record<string, ScriptEventDef>;
 
const SECTION_TAB_KEY = "__section";
 
export const isFieldVisible = (
  field: ScriptEventFieldSchema,
  args: ScriptEventArgs,
  ignoreConditions?: string[],
) => {
  if (!field.conditions) {
    return true;
  }
  // Determine if field conditions are met
  return field.conditions.reduce((memo, condition) => {
    Iif (ignoreConditions?.includes(condition.key)) {
      return memo;
    }
    const keyValue = args[condition.key];
    return (
      memo &&
      (!condition.eq || keyValue === condition.eq) &&
      (!condition.ne || keyValue !== condition.ne) &&
      (!condition.gt || Number(keyValue) > Number(condition.gt)) &&
      (!condition.gte || Number(keyValue) >= Number(condition.gte)) &&
      (!condition.lt || Number(keyValue) > Number(condition.lt)) &&
      (!condition.lte || Number(keyValue) >= Number(condition.lte)) &&
      (!condition.in ||
        condition.in.indexOf(keyValue) >= 0 ||
        (keyValue === undefined && condition.in.indexOf(null) >= 0)) &&
      (condition.set === undefined ||
        (condition.set && keyValue !== undefined) ||
        (!condition.set && keyValue === undefined))
    );
  }, true);
};
 
const getField = (
  cmd: string,
  fieldName: string,
  scriptEventDefs: ScriptEventDefs,
): ScriptEventFieldSchema | undefined => {
  const event = scriptEventDefs[cmd];
  Iif (!event) return undefined;
  return event.fieldsLookup[fieldName];
};
 
export const isVariableField = (
  command: string,
  fieldName: string,
  args: ScriptEventArgs,
  scriptEventDefs: ScriptEventDefs,
) => {
  const field = getField(command, fieldName, scriptEventDefs);
  const argValue = args[fieldName];
  return (
    !!field &&
    (field.type === "variable" || isUnionVariableValue(argValue)) &&
    isFieldVisible(field, args, [SECTION_TAB_KEY])
  );
};
 
export const isActorField = (
  cmd: string,
  fieldName: string,
  args: ScriptEventArgs,
  scriptEventDefs: ScriptEventDefs,
) => {
  // Custom event calls
  if (fieldName.startsWith("$actor[")) {
    return true;
  }
  const field = getField(cmd, fieldName, scriptEventDefs);
  return (
    !!field &&
    field.type === "actor" &&
    isFieldVisible(field, args, [SECTION_TAB_KEY])
  );
};
 
export const isPropertyField = (
  cmd: string,
  fieldName: string,
  args: ScriptEventArgs,
  scriptEventDefs: ScriptEventDefs,
) => {
  const event = scriptEventDefs[cmd];
  Iif (!event) return false;
  const field = getField(cmd, fieldName, scriptEventDefs);
  const fieldValue = args[fieldName];
  return (
    !!field &&
    (field.type === "property" || isUnionPropertyValue(fieldValue)) &&
    isFieldVisible(field, args, [SECTION_TAB_KEY])
  );
};
 
export const isScriptValueField = (
  cmd: string,
  fieldName: string,
  args: ScriptEventArgs,
  scriptEventDefs: ScriptEventDefs,
) => {
  // Custom event calls
  if (fieldName.startsWith("$variable[")) {
    return true;
  }
  const event = scriptEventDefs[cmd];
  Iif (!event) return false;
  const field = getField(cmd, fieldName, scriptEventDefs);
  return (
    field &&
    field.type === "value" &&
    isFieldVisible(field, args, [SECTION_TAB_KEY])
  );
};