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

65.53% Statements 116/177
28.12% Branches 18/64
87.17% Functions 34/39
59.09% Lines 78/132

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 21583x           83x 83x       83x 197x   83x 9x   83x 2x   83x 4x   83x     83x 33x   83x 14x   83x 6x   83x 31x   83x 14x   83x 6x   83x 9x   83x     8x   83x 30x   83x 22x   83x 1x   83x     83x 8x   83x 37x   83x 138x   83x 10x   83x 1x   83x     83x     83x 78x   83x 8x   83x 1x   83x 4x   83x         54x 54x 4x 4x 4x 4x                 4x 4x                         54x       83x           19x 17x 17x 16x 16x 16x       2x 2x 2x 2x 2x 2x                                                                                                                        
import { v4 as 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 localTilesetSelectById = (state: EntitiesState, id: string) =>
  state.tilesets.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 localVariableSelectTotal = (state: EntitiesState) =>
  state.variables.ids.length;
 
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);
    Eif (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"];
    Eif (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];
    Eif (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];
    if (!actor) return;
    const script = actor[parentKey as "script"];
    if (script) {
      return script;
    }
    const newScript = (actor[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "trigger") {
    const trigger = state.triggers.entities[parentId];
    if (!trigger) return;
    const script = trigger[parentKey as "script"];
    if (script) {
      return script;
    }
    const newScript = (trigger[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "customEvent") {
    const customEvent = state.customEvents.entities[parentId];
    if (!customEvent) return;
    const script = customEvent[parentKey as "script"];
    if (script) {
      return script;
    }
    const newScript = (customEvent[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "actorPrefab") {
    const actorPrefab = state.actorPrefabs.entities[parentId];
    if (!actorPrefab) return;
    const script = actorPrefab[parentKey as "script"];
    if (script) {
      return script;
    }
    const newScript = (actorPrefab[parentKey as "script"] = []);
    return newScript;
  } else if (parentType === "triggerPrefab") {
    const triggerPrefab = state.triggerPrefabs.entities[parentId];
    if (!triggerPrefab) return;
    const script = triggerPrefab[parentKey as "script"];
    if (script) {
      return script;
    }
    const newScript = (triggerPrefab[parentKey as "script"] = []);
    return newScript;
  } else {
    assertUnreachable(parentType);
  }
};