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

88.88% Statements 48/54
50.81% Branches 31/61
88.88% Functions 8/9
86.04% Lines 37/43

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    118x                             118x   118x 279x   118x         50x 50x                                                   118x         434x 434x 428x     118x           112x 112x 112x             118x             110x 2x   108x 108x             118x           104x 104x 101x 101x 101x             118x             15x     15x 15x 12x 12x             118x           104x 104x 101x 101x            
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>;
 
export type ScriptEventDefsFieldTypeLookup = Record<
  string,
  {
    fieldsLookup: Record<string, { type?: string }>;
  }
>;
 
const SECTION_TAB_KEY = "__section";
 
export const isVariableFieldType = (type: string | undefined): boolean =>
  type === "variable" || type === "variableElement";
 
export const isFieldVisible = (
  field: ScriptEventFieldSchema,
  args: ScriptEventArgs,
  ignoreConditions?: string[],
) => {
  Eif (!field.conditions) {
    return true;
  }
  // Determine if field conditions are met
  return field.conditions.reduce((memo, condition) => {
    if (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];
  if (!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 &&
    (isVariableFieldType(field.type) || 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];
  if (!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
  Iif (fieldName.startsWith("$variable[")) {
    return true;
  }
  const event = scriptEventDefs[cmd];
  if (!event) return false;
  const field = getField(cmd, fieldName, scriptEventDefs);
  return (
    field &&
    field.type === "value" &&
    isFieldVisible(field, args, [SECTION_TAB_KEY])
  );
};
 
export const isDataTableField = (
  cmd: string,
  fieldName: string,
  args: ScriptEventArgs,
  scriptEventDefs: ScriptEventDefs,
) => {
  const event = scriptEventDefs[cmd];
  if (!event) return false;
  const field = getField(cmd, fieldName, scriptEventDefs);
  return (
    field &&
    field.type === "dataTable" &&
    isFieldVisible(field, args, [SECTION_TAB_KEY])
  );
};