Skip to content

Commit

Permalink
Protect against empty clusters.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeychernyshev committed Mar 28, 2024
1 parent c2e461f commit 5a9dc9f
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ function getMapTileCoordinates(n) {
* @returns {int} a 1-based number of the tile
*/
function getTileNumber(cluster, numberOfTileVariations) {
const totalLinesInCluster = cluster.reduce(
(acc, [file]) => acc + file.Lines,
0
);
let tileNumber = 0;

try {
const totalLinesInCluster = cluster.reduce(
(acc, [file]) => acc + file.Lines,
0
);

return (totalLinesInCluster % numberOfTileVariations) + 1;
tileNumber = (totalLinesInCluster % numberOfTileVariations) + 1;
} catch (error) {
tileNumber = Math.floor(Math.random() * numberOfTileVariations) + 1;
}

return tileNumber;
}

export const generateMapHTML = function (gameConfig, clusters) {
Expand All @@ -76,10 +84,8 @@ export const generateMapHTML = function (gameConfig, clusters) {

const tiles = [];

console.log(clusters);

for (let i = 0; i < clusters.length; i++) {
const blockCoordinates = getMapTileCoordinates(i + 1);
for (let i = clusters.length; i >= 1; i--) {
const blockCoordinates = getMapTileCoordinates(i);

const isoX =
(blockCoordinates.x * tileWidth) / 2 - blockCoordinates.y * tileHeight;
Expand All @@ -97,7 +103,7 @@ export const generateMapHTML = function (gameConfig, clusters) {
highestIsoY = isoY;
}

const tileNumber = getTileNumber(clusters[i], numberOfTileVariations);
const tileNumber = getTileNumber(clusters[i - 1], numberOfTileVariations);

tiles.push({ tileNumber, isoX, isoY });
}
Expand Down

0 comments on commit 5a9dc9f

Please sign in to comment.