All files / src/store/features/entities helpers.ts

63.16% Statements 108/171
31.25% Branches 15/48
81.08% Functions 30/37
56.25% Lines 72/128

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 20946x           46x 46x       46x 35x   46x 7x   46x 1x   46x 4x   46x     46x 33x   46x 2x   46x 6x   46x 31x   46x 2x   46x 6x   46x 9x   46x     8x   46x 30x   46x 13x   46x 1x   46x     46x 8x   46x 14x   46x 7x   46x 1x   46x     46x     46x 78x   46x     46x     46x         54x 54x 4x 4x 4x 4x                 4x 4x                         54x       46x           19x 17x 17x 16x 16x 16x       2x 2x 2x 2x 2x 2x                                                                                                                        
import uuid from "uuid";
import type {
  EntitiesState,
  ScriptEventParentType,
} from "shared/lib/entities/entitiesTypes";
import { ScriptEventArgsOverride } from "shared/lib/resources/types";
import { scriptEventsAdapter } from "store/features/entities/adapters";
import { assertUnreachable } from "shared/lib/helpers/assert";
 
// Local Selectors (only for use internally within EntitiesState reducers)
 
export const localSceneSelectById = (state: EntitiesState, id: string) =>
  state.scenes.entities[id];
 
export const localSceneSelectAll = (state: EntitiesState) =>
  state.scenes.ids.map((id) => state.scenes.entities[id]);
 
export const localSceneSelectTotal = (state: EntitiesState) =>
  state.scenes.ids.length;
 
export const localNoteSelectById = (state: EntitiesState, id: string) =>
  state.notes.entities[id];
 
export const localNoteSelectTotal = (state: EntitiesState) =>
  state.notes.ids.length;
 
export const localActorSelectById = (state: EntitiesState, id: string) =>
  state.actors.entities[id];
 
export const localActorSelectEntities = (state: EntitiesState) =>
  state.actors.entities;
 
export const localActorSelectAll = (state: EntitiesState) =>
  state.actors.ids.map((id) => state.actors.entities[id]);
 
export const localTriggerSelectById = (state: EntitiesState, id: string) =>
  state.triggers.entities[id];
 
export const localTriggerSelectEntities = (state: EntitiesState) =>
  state.triggers.entities;
 
export const localTriggerSelectAll = (state: EntitiesState) =>
  state.triggers.ids.map((id) => state.triggers.entities[id]);
 
export const localActorPrefabSelectById = (state: EntitiesState, id: string) =>
  state.actorPrefabs.entities[id];
 
export const localTriggerPrefabSelectById = (
  state: EntitiesState,
  id: string,
) => state.triggerPrefabs.entities[id];
 
export const localScriptEventSelectById = (state: EntitiesState, id: string) =>
  state.scriptEvents.entities[id];
 
export const localScriptEventSelectAll = (state: EntitiesState) =>
  state.scriptEvents.ids.map((id) => state.scriptEvents.entities[id]);
 
export const localCustomEventSelectTotal = (state: EntitiesState) =>
  state.customEvents.ids.length;
 
export const localSpriteSheetSelectById = (state: EntitiesState, id: string) =>
  state.spriteSheets.entities[id];
 
export const localSpriteSheetSelectAll = (state: EntitiesState) =>
  state.spriteSheets.ids.map((id) => state.spriteSheets.entities[id]);
 
export const localBackgroundSelectById = (state: EntitiesState, id: string) =>
  state.backgrounds.entities[id];
 
export const localBackgroundSelectAll = (state: EntitiesState) =>
  state.backgrounds.ids.map((id) => state.backgrounds.entities[id]);
 
export const localPaletteSelectTotal = (state: EntitiesState) =>
  state.palettes.ids.length;
 
export const localMusicSelectById = (state: EntitiesState, id: string) =>
  state.music.entities[id];
 
export const localMusicSelectAll = (state: EntitiesState) =>
  state.music.ids.map((id) => state.music.entities[id]);
 
export const localVariableSelectById = (state: EntitiesState, id: string) =>
  state.variables.entities[id];
 
export const localConstantSelectById = (state: EntitiesState, id: string) =>
  state.constants.entities[id];
 
export const localConstantSelectTotal = (state: EntitiesState) =>
  state.constants.ids.length;
 
export const duplicateScript = (
  state: EntitiesState,
  scriptEventIds: string[],
  overrides?: Record<string, ScriptEventArgsOverride>,
): string[] => {
  const newIds = scriptEventIds.map(() => uuid());
  scriptEventIds.forEach((scriptEventId, index) => {
    const scriptEvent = localScriptEventSelectById(state, scriptEventId);
    if (scriptEvent) {
      const duplicatedChildren: Record<string, string[]> = {};
      Iif (scriptEvent.children) {
        for (const [key, childIds] of Object.entries(scriptEvent.children)) {
          duplicatedChildren[key] = duplicateScript(
            state,
            childIds || [],
            overrides,
          );
        }
      }
      const override = overrides?.[scriptEvent.id];
      scriptEventsAdapter.addOne(state.scriptEvents, {
        ...scriptEvent,
        args: override
          ? {
              ...scriptEvent.args,
              ...override.args,
            }
          : scriptEvent.args,
        id: newIds[index],
        children: duplicatedChildren,
      });
    }
  });
  return newIds;
};
 
// @todo CM: likely should rename to something like getOrCreateScriptIds as it can mutate input
export const selectScriptIds = (
  state: EntitiesState,
  parentType: ScriptEventParentType,
  parentId: string,
  parentKey: string,
): string[] | undefined => {
  if (parentType === "scene") {
    const scene = state.scenes.entities[parentId];
    if (!scene) return;
    const script = scene[parentKey as "script"];
    if (script) {
      return script;
    }
    const newScript = (scene[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "scriptEvent") {
    const scriptEvent = state.scriptEvents.entities[parentId];
    Iif (!scriptEvent) return;
    const script = scriptEvent.children?.[parentKey];
    if (script) {
      return script;
    }
    if (!scriptEvent.children) {
      scriptEvent.children = {
        [parentKey]: [],
      };
      return scriptEvent.children?.[parentKey];
    } else {
      scriptEvent.children[parentKey] = [];
      return scriptEvent.children[parentKey];
    }
  } else Eif (parentType === "actor") {
    const actor = state.actors.entities[parentId];
    Iif (!actor) return;
    const script = actor[parentKey as "script"];
    Iif (script) {
      return script;
    }
    const newScript = (actor[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "trigger") {
    const trigger = state.triggers.entities[parentId];
    Iif (!trigger) return;
    const script = trigger[parentKey as "script"];
    Iif (script) {
      return script;
    }
    const newScript = (trigger[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "customEvent") {
    const customEvent = state.customEvents.entities[parentId];
    Iif (!customEvent) return;
    const script = customEvent[parentKey as "script"];
    Iif (script) {
      return script;
    }
    const newScript = (customEvent[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "actorPrefab") {
    const actorPrefab = state.actorPrefabs.entities[parentId];
    Iif (!actorPrefab) return;
    const script = actorPrefab[parentKey as "script"];
    Iif (script) {
      return script;
    }
    const newScript = (actorPrefab[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "triggerPrefab") {
    const triggerPrefab = state.triggerPrefabs.entities[parentId];
    Iif (!triggerPrefab) return;
    const script = triggerPrefab[parentKey as "script"];
    Iif (script) {
      return script;
    }
    const newScript = (triggerPrefab[parentKey as "script"] = []);
    return newScript;
  } else {
    assertUnreachable(parentType);
  }
};