All files / src/renderer/lib variables.tsx

92.86% Statements 52/56
80% Branches 12/15
88.89% Functions 16/18
91.3% Lines 42/46

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  2x         2x 2x                     2x 1060x   2x 2x 2x 2x                                       2x         7x 2x 2x     5x 5x           2x       3x 30x           1536x                 2x       5x 30x           10x           2560x                 2x                         2x 3644x 7x 9888x 19x                 2x 5x 5x 3x 3x   2x     2x 6x 6x 3x 3x   3x    
import { ScriptEditorCtx } from "shared/lib/scripts/context";
import uniq from "lodash/uniq";
import {
  CustomEventNormalized,
  Variable,
} from "shared/lib/entities/entitiesTypes";
import l10n from "shared/lib/lang/l10n";
import {
  customEventVariableCode,
  customEventVariableName,
  globalVariableCode,
  globalVariableName,
  localVariableCode,
  localVariableName,
  tempVariableCode,
  tempVariableName,
} from "shared/lib/variables/variableNames";
 
const arrayNStrings = (n: number) =>
  Array.from(Array(n).keys()).map((n) => String(n));
 
export const allVariables = arrayNStrings(512);
export const localVariables = arrayNStrings(6);
export const tempVariables = arrayNStrings(2);
export const customEventVariables = arrayNStrings(10);
 
type VariablesLookup = { [name: string]: Variable | undefined };
 
export interface NamedVariable {
  id: string; // The id to use in dropdown value
  code: string; // The code to use in dialogue (when wrapped by $ or #)
  name: string; // The user defined name or default when not named
  group: string; // Group name that variable belongs to
}
 
export interface VariableGroup {
  name: string; // The group name
  variables: NamedVariable[]; // Variables in the group
}
 
/******************************************************************************
 * Available Variables List (for using in Dropdowns etc)
 */
 
export const namedVariablesByContext = (
  context: ScriptEditorCtx,
  variablesLookup: VariablesLookup,
  customEvent: CustomEventNormalized | undefined
): NamedVariable[] => {
  if (context.type === "script") {
    if (customEvent) {
      return namedCustomEventVariables(customEvent, variablesLookup);
    }
    return [];
  } else if (context.type === "entity" || context.type === "prefab") {
    return namedEntityVariables(context.entityId, variablesLookup);
  } else E{
    return namedGlobalVariables(variablesLookup);
  }
};
 
export const namedCustomEventVariables = (
  customEvent: CustomEventNormalized,
  variablesLookup: VariablesLookup
): NamedVariable[] => {
  return ([] as NamedVariable[]).concat(
    customEventVariables.map((variable) => ({
      id: customEventVariableCode(variable),
      code: customEventVariableCode(variable),
      name: customEventVariableName(variable, customEvent),
      group: l10n("SIDEBAR_PARAMETERS"),
    })),
    allVariables.map((variable) => ({
      id: variable,
      code: globalVariableCode(variable),
      name: globalVariableName(variable, variablesLookup),
      group: l10n("FIELD_GLOBAL"),
    }))
  );
};
 
export const namedEntityVariables = (
  entityId: string,
  variablesLookup: VariablesLookup
): NamedVariable[] => {
  return ([] as NamedVariable[]).concat(
    localVariables.map((variable) => ({
      id: localVariableCode(variable),
      code: localVariableCode(variable),
      name: localVariableName(variable, entityId, variablesLookup),
      group: l10n("FIELD_LOCAL"),
    })),
    tempVariables.map((variable) => ({
      id: tempVariableCode(variable),
      code: tempVariableCode(variable),
      name: tempVariableName(variable),
      group: l10n("FIELD_TEMPORARY"),
    })),
    allVariables.map((variable) => ({
      id: variable,
      code: globalVariableCode(variable),
      name: globalVariableName(variable, variablesLookup),
      group: l10n("FIELD_GLOBAL"),
    }))
  );
};
 
export const namedGlobalVariables = (
  variablesLookup: VariablesLookup
): NamedVariable[] => {
  return ([] as NamedVariable[]).concat(
    allVariables.map((variable) => ({
      id: variable,
      code: globalVariableCode(variable),
      name: globalVariableName(variable, variablesLookup),
      group: l10n("FIELD_GLOBAL"),
    }))
  );
};
 
export const groupVariables = (variables: NamedVariable[]): VariableGroup[] => {
  const groups = uniq(variables.map((f) => f.group));
  return groups.map((g) => {
    const groupVariables = variables.filter((f) => f.group === g);
    return {
      name: g,
      variables: groupVariables,
    };
  });
};
 
/*****************************************************************************/
 
export const prevVariable = (variable = "0") => {
  const start = variable[0];
  if (start === "T" || start === "L") {
    const value = parseInt(variable.substr(1), 10) - 1;
    return `${start}${value}`;
  }
  return String(parseInt(variable, 10) - 1);
};
 
export const nextVariable = (variable = "0") => {
  const start = variable[0];
  if (start === "T" || start === "L") {
    const value = parseInt(variable.substr(1), 10) + 1;
    return `${start}${value}`;
  }
  return String(parseInt(variable, 10) + 1);
};