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

86.79% Statements 46/53
83.33% Branches 40/48
66.66% Functions 6/9
86% Lines 43/50

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            83x         83x 83x         83x 83x         83x             83x 2x         2x       2x 2x   2x                   2x 1x                           2x 2x 2x           83x                         83x                                         83x                             83x 7x 7x 6x 2x   4x 4x         3x 3x 3x             3x 3x         83x 8x 8x 8x 4x 8x 8x 4x 4x       83x             83x  
import {
  PayloadAction,
  CaseReducer,
  SliceCaseReducers,
} from "@reduxjs/toolkit";
import { EntitiesState } from "shared/lib/entities/entitiesTypes";
import {
  ensureSymbolsUnique,
  updateEntitySymbol,
  upsertAssetEntity,
} from "shared/lib/entities/entitiesHelpers";
import keyBy from "lodash/keyBy";
import { monoOverrideForFilename } from "shared/lib/assets/backgrounds";
import {
  BackgroundAsset,
  CompressedBackgroundResourceAsset,
} from "shared/lib/resources/types";
import { backgroundsAdapter } from "store/features/entities/adapters";
import {
  localBackgroundSelectById,
  localBackgroundSelectAll,
  localSceneSelectAll,
} from "store/features/entities/helpers";
import { resizeTiles } from "shared/lib/helpers/tiles";
 
const loadBackground: CaseReducer<
  EntitiesState,
  PayloadAction<{
    data: CompressedBackgroundResourceAsset;
  }>
> = (state, action) => {
  const existingBackground = localBackgroundSelectById(
    state,
    action.payload.data.id,
  );
  const modifiedSize =
    existingBackground &&
    (existingBackground.width !== action.payload.data.width ||
      existingBackground.height !== action.payload.data.height);
 
  const originalWidth = existingBackground?.width ?? 0;
  const originalHeight = existingBackground?.height ?? 0;
 
  upsertAssetEntity(
    state.backgrounds,
    backgroundsAdapter,
    {
      ...action.payload.data,
      tileColors: [],
    },
    ["id", "symbol", "autoColor", "tileColors"],
  );
 
  if (modifiedSize) {
    backgroundsAdapter.updateOne(state.backgrounds, {
      id: existingBackground.id,
      changes: {
        tileColors: resizeTiles(
          existingBackground.tileColors,
          originalWidth,
          originalHeight,
          action.payload.data.width,
          action.payload.data.height,
        ),
      },
    });
  }
 
  fixAllScenesWithModifiedBackgrounds(state);
  updateMonoOverrideIds(state);
  ensureSymbolsUnique(state);
};
 
const setBackgroundSymbol: CaseReducer<
  EntitiesState,
  PayloadAction<{ backgroundId: string; symbol: string }>
> = (state, action) => {
  updateEntitySymbol(
    state,
    state.backgrounds,
    backgroundsAdapter,
    action.payload.backgroundId,
    action.payload.symbol,
  );
};
 
const editBackgroundAutoColor: CaseReducer<
  EntitiesState,
  PayloadAction<{ backgroundId: string; autoColor: boolean }>
> = (state, action) => {
  const background = localBackgroundSelectById(
    state,
    action.payload.backgroundId,
  );
  if (background) {
    backgroundsAdapter.updateOne(state.backgrounds, {
      id: background.id,
      changes: {
        autoColor: action.payload.autoColor,
      },
    });
  }
};
 
const editBackgroundAutoTileFlipOverride: CaseReducer<
  EntitiesState,
  PayloadAction<{
    backgroundId: string;
    autoTileFlipOverride: boolean | undefined;
  }>
> = (state, action) => {
  const background = localBackgroundSelectById(
    state,
    action.payload.backgroundId,
  );
  if (background) {
    backgroundsAdapter.updateOne(state.backgrounds, {
      id: background.id,
      changes: {
        autoTileFlipOverride: action.payload.autoTileFlipOverride,
      },
    });
  }
};
 
export const fixAllScenesWithModifiedBackgrounds = (state: EntitiesState) => {
  const scenes = localSceneSelectAll(state);
  for (const scene of scenes) {
    if (scene.tilemap) {
      continue;
    }
    const background = localBackgroundSelectById(state, scene.backgroundId);
    if (
      !background ||
      scene.width !== background.width ||
      scene.height !== background.height
    ) {
      const newWidth = background ? background.width : 32;
      const newHeight = background ? background.height : 32;
      scene.collisions = resizeTiles(
        scene.collisions,
        scene.width,
        scene.height,
        newWidth,
        newHeight,
      );
      scene.width = newWidth;
      scene.height = newHeight;
    }
  }
};
 
export const updateMonoOverrideIds = (state: EntitiesState) => {
  const backgrounds = localBackgroundSelectAll(state);
  const getKey = (b: BackgroundAsset) => `${b.plugin ?? ""}_${b.filename}`;
  const getMonoKey = (b: BackgroundAsset) =>
    `${b.plugin ?? ""}_${monoOverrideForFilename(b.filename)}`;
  const monoOverrideLookup = keyBy(backgrounds, getKey);
  backgrounds.forEach((b) => {
    const monoKey = getMonoKey(b);
    b.monoOverrideId = monoOverrideLookup[monoKey]?.id;
  });
};
 
const backgroundsReducers = {
  loadBackground,
  setBackgroundSymbol,
  editBackgroundAutoColor,
  editBackgroundAutoTileFlipOverride,
} satisfies SliceCaseReducers<EntitiesState>;
 
export default backgroundsReducers;