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

82.35% Statements 42/51
39.22% Branches 20/51
87.5% Functions 7/8
87.8% Lines 36/41

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    65x               65x   65x         52x 52x                                                   65x         423x 423x 423x     65x           89x 89x 89x             65x             88x 2x   86x 86x             65x           85x 85x 85x 85x 85x             65x             85x 8x   77x 77x 77x 77x             65x           86x 86x 86x 86x            
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])
  );
};
 
export const isDataTableField = (
  cmd: string,
  fieldName: string,
  args: ScriptEventArgs,
  scriptEventDefs: ScriptEventDefs,
) => {
  const event = scriptEventDefs[cmd];
  Iif (!event) return false;
  const field = getField(cmd, fieldName, scriptEventDefs);
  return (
    field &&
    field.type === "dataTable" &&
    isFieldVisible(field, args, [SECTION_TAB_KEY])
  );
};