diff --git a/app/constants.ts b/app/constants.ts index d26b8a7e..6729ba5a 100644 --- a/app/constants.ts +++ b/app/constants.ts @@ -111,6 +111,8 @@ const constants: AppConfig = { }, }; +export const BODY_TILE_RADIUS = 5; + export const BUILDING_WINDOW_WIDTH = 800; export const BUILDING_WINDOW_SCROLL = 400; export const BUILDING_WINDOW_SCROLL_SMALL = 200; diff --git a/app/helpers/rearrangeBuildings.ts b/app/helpers/rearrangeBuildings.ts index 2175fea7..0a05b06d 100644 --- a/app/helpers/rearrangeBuildings.ts +++ b/app/helpers/rearrangeBuildings.ts @@ -1,21 +1,28 @@ import _ from 'lodash'; import { types } from '@tlecommunity/client'; import { BuildingCoordinates, Matrix } from 'app/interfaces/rearrangeBuildings'; +import * as util from 'app/util'; +import { BODY_TILE_RADIUS } from 'app/constants'; type Buildings = types.Body.GetBuildingsResponse['buildings']; type Arrangement = types.Body.RearrangeBuildingsParams['arrangement']; -// -// Pure translations between the server's building map and the 11x11 grid the rearrange window -// edits. These moved here from app/services when that layer was replaced by app/queries. -// +const createEmptyMatrix = (): Matrix => { + // Ensure x columns are populated so that we don't get index access errors + const matrix: Matrix = []; + + for (let i = 0 - BODY_TILE_RADIUS; i <= BODY_TILE_RADIUS; i++) { + matrix[i] = []; + } + + return matrix; +}; export const buildingsToMatrix = (buildings: Buildings): Matrix => { - const matrix: Matrix = []; + const matrix: Matrix = createEmptyMatrix(); _.each(buildings, (building) => { - matrix[building.x] ||= []; - matrix[building.x][building.y] = building; + matrix[util.int(building.x)][util.int(building.y)] = building; }); return matrix;