Address the problem of potential data loss

Any errors while iterating the states or burgs could potentially lose the index 0 metadata stored in the arrays. This will instead track the index and ignore the 0th result.
This commit is contained in:
Michael DiRienzo 2020-07-09 17:50:55 -04:00
parent d24f92efab
commit 913c9b0f01

View file

@ -357,24 +357,25 @@
// cell or center cell's (respectively) culture. // cell or center cell's (respectively) culture.
const resetCultures = function () { const resetCultures = function () {
console.time('resetCulturesForBurgsAndStates'); console.time('resetCulturesForBurgsAndStates');
// Pull out index 0 nodes so iterators don't touch them.
const burgTreeNode = pack.burgs.shift();
const neutralsStateNode = pack.states.shift();
// Assign the culture associated with the burgs cell. // Assign the culture associated with the burgs cell.
pack.burgs = pack.burgs.map( (burg) => { pack.burgs = pack.burgs.map( (burg, index) => {
// Ignore metadata burg
if(index === 0) {
return burg;
}
return {...burg, culture: pack.cells.culture[burg.cell]}; return {...burg, culture: pack.cells.culture[burg.cell]};
}); });
// Assign the culture associated with the states' center cell. // Assign the culture associated with the states' center cell.
pack.states = pack.states.map( (state) => { pack.states = pack.states.map( (state, index) => {
// Ignore neutrals state
if(index === 0) {
return state;
}
return {...state, culture: pack.cells.culture[state.center]}; return {...state, culture: pack.cells.culture[state.center]};
}); });
// Prepend the index 0 nodes back onto the packgroups.
pack.burgs.unshift(burgTreeNode);
pack.states.unshift(neutralsStateNode);
console.timeEnd('resetCulturesForBurgsAndStates'); console.timeEnd('resetCulturesForBurgsAndStates');
} }