All files / src/store/features/entities/reducers customEventsReducers.ts

78.18% Statements 43/55
82.69% Branches 43/52
50% Functions 6/12
78.18% Lines 43/55

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          83x   83x           83x               83x       83x                     83x 1x 1x                   1x                 83x                             83x 2x 2x 2x     2x 1x 1x 1x     1x                     83x 3x 3x 3x     3x 1x             83x                         83x 4x 4x   4x 10x       5x       4x 1x   3x   3x                 4x                       83x                                   83x                           83x               83x             3x                                                                       83x  
import {
  PayloadAction,
  CaseReducer,
  SliceCaseReducers,
} from "@reduxjs/toolkit";
import { EVENT_CALL_CUSTOM_EVENT } from "consts";
import { ScriptEventDefs } from "shared/lib/scripts/scriptDefHelpers";
import { v4 as uuid } from "uuid";
import {
  EntitiesState,
  ScriptNormalized,
} from "shared/lib/entities/entitiesTypes";
import type { ScriptVariable } from "shared/lib/resources/types";
import {
  genEntitySymbol,
  updateEntitySymbol,
  defaultLocalisedCustomEventName,
  updateCustomEventArgs,
  applyReparentFolderToCollection,
  applyReparentEntityToCollection,
} from "shared/lib/entities/entitiesHelpers";
import {
  scriptEventsAdapter,
  customEventsAdapter,
} from "store/features/entities/adapters";
import {
  localCustomEventSelectTotal,
  localScriptEventSelectAll,
} from "store/features/entities/helpers";
 
const addCustomEvent: CaseReducer<
  EntitiesState,
  PayloadAction<{
    customEventId: string;
    defaults?: Partial<ScriptNormalized>;
  }>
> = (state, action) => {
  const customEventsTotal = localCustomEventSelectTotal(state);
  const newCustomEvent: ScriptNormalized = {
    id: action.payload.customEventId,
    name: defaultLocalisedCustomEventName(customEventsTotal),
    description: "",
    variables: {},
    actors: {},
    ...(action.payload.defaults || {}),
    symbol: genEntitySymbol(state, `script_${customEventsTotal + 1}`),
    script: [],
  };
  customEventsAdapter.addOne(state.customEvents, newCustomEvent);
};
 
const editCustomEvent: CaseReducer<
  EntitiesState,
  PayloadAction<{
    customEventId: string;
    changes: Partial<ScriptNormalized>;
  }>
> = (state, action) => {
  const patch = { ...action.payload.changes };
  customEventsAdapter.updateOne(state.customEvents, {
    id: action.payload.customEventId,
    changes: patch,
  });
};
 
const editCustomEventVariablePassByReference: CaseReducer<
  EntitiesState,
  PayloadAction<{
    customEventId: string;
    variableId: string;
    passByReference: ScriptVariable["passByReference"];
  }>
> = (state, action) => {
  const customEvent = state.customEvents.entities[action.payload.customEventId];
  const variable = customEvent?.variables[action.payload.variableId];
  Iif (!customEvent || !variable) {
    return;
  }
  if (action.payload.passByReference === "array") {
    variable.passByReference = "array";
    Eif (variable.passByReference === "array") {
      variable.length = variable.length ?? 5;
    }
  } else {
    variable.passByReference = action.payload.passByReference;
  }
};
 
const editCustomEventVariableLength: CaseReducer<
  EntitiesState,
  PayloadAction<{
    customEventId: string;
    variableId: string;
    length: number;
  }>
> = (state, action) => {
  const customEvent = state.customEvents.entities[action.payload.customEventId];
  const variable = customEvent?.variables[action.payload.variableId];
  Iif (!customEvent || !variable) {
    return;
  }
  if (variable.passByReference === "array") {
    variable.length = action.payload.length;
  }
};
 
const setCustomEventSymbol: CaseReducer<
  EntitiesState,
  PayloadAction<{ customEventId: string; symbol: string }>
> = (state, action) => {
  updateEntitySymbol(
    state,
    state.customEvents,
    customEventsAdapter,
    action.payload.customEventId,
    action.payload.symbol,
  );
};
 
const removeCustomEvent: CaseReducer<
  EntitiesState,
  PayloadAction<{ customEventId: string; deleteReferences?: boolean }>
> = (state, action) => {
  const allScriptEvents = localScriptEventSelectAll(state);
  const referenceIds: string[] = [];
 
  for (const scriptEvent of allScriptEvents) {
    if (
      scriptEvent.command === EVENT_CALL_CUSTOM_EVENT &&
      scriptEvent.args?.customEventId === action.payload.customEventId
    ) {
      referenceIds.push(scriptEvent.id);
    }
  }
 
  if (action.payload.deleteReferences) {
    scriptEventsAdapter.removeMany(state.scriptEvents, referenceIds);
  } else {
    scriptEventsAdapter.updateMany(
      state.scriptEvents,
      referenceIds.map((id) => ({
        id,
        changes: {
          args: { customEventId: undefined },
        },
      })),
    );
  }
 
  customEventsAdapter.removeOne(
    state.customEvents,
    action.payload.customEventId,
  );
};
 
const refreshCustomEventArgs: CaseReducer<
  EntitiesState,
  PayloadAction<{
    customEventId: string;
    scriptEventDefs: ScriptEventDefs;
  }>
> = (state, action) => {
  const customEvent = state.customEvents.entities[action.payload.customEventId];
  if (!customEvent) {
    return;
  }
  updateCustomEventArgs(
    customEvent,
    state.scriptEvents.entities,
    action.payload.scriptEventDefs,
  );
};
 
const reparentCustomEventsFolder: CaseReducer<
  EntitiesState,
  PayloadAction<{
    fromPath: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentFolderToCollection(
    state.customEvents.entities,
    action.payload.fromPath,
    action.payload.toPath,
  );
};
 
const reparentCustomEvent: CaseReducer<
  EntitiesState,
  PayloadAction<{
    customEventId: string;
    toPath: string;
  }>
> = (state, action) => {
  applyReparentEntityToCollection(
    state.customEvents.entities,
    action.payload.customEventId,
    action.payload.toPath,
  );
};
 
const customEventsReducers = {
  addCustomEvent: {
    reducer: addCustomEvent,
    prepare: (payload?: {
      customEventId?: string;
      defaults?: Partial<ScriptNormalized>;
    }) => {
      return {
        payload: {
          customEventId: payload?.customEventId ?? uuid(),
          defaults: payload?.defaults,
        },
      };
    },
  },
 
  editCustomEvent,
  editCustomEventVariablePassByReference,
  editCustomEventVariableLength,
  setCustomEventSymbol,
  removeCustomEvent,
  refreshCustomEventArgs: {
    reducer: refreshCustomEventArgs,
    prepare: (payload: {
      customEventId: string;
      scriptEventDefs: ScriptEventDefs;
    }) => {
      return {
        payload: {
          customEventId: payload.customEventId,
          scriptEventDefs: payload.scriptEventDefs,
        },
        meta: {
          throttle: 1000,
          key: `refresh_${payload.customEventId}`,
        },
      };
    },
  },
  reparentCustomEventsFolder,
  reparentCustomEvent,
} satisfies SliceCaseReducers<EntitiesState>;
 
export default customEventsReducers;