Skip to content
Permalink
Browse files

overhauled how prune flight listings are generated

fixes #41
  • Loading branch information...
snollygolly committed Jun 29, 2019
1 parent f29641b commit f4a9a051421e4ec57c2b69f7312db1220149dc69
Showing with 38 additions and 1 deletion.
  1. +20 −0 helpers/common.js
  2. +18 −1 models/game/airport.js
@@ -64,6 +64,26 @@ module.exports.randomShrinkArr = function randomShrinkArr(arr, newSize) {
return returnArr;
};

module.exports.shuffleArr = function shuffleArr(arr) {
const clonedArr = JSON.parse(JSON.stringify(arr));
let currentIndex = clonedArr.length;
let temporaryValue;
let randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = clonedArr[currentIndex];
clonedArr[currentIndex] = clonedArr[randomIndex];
clonedArr[randomIndex] = temporaryValue;
}
return clonedArr;
};

module.exports.rollDice = function rollDice(min, max, luck) {
if (typeof(luck) == "undefined") {
if (max < min) {
@@ -61,7 +61,7 @@ module.exports.generateAirportListings = function generateAirportListings(life)
const listingMulti = (life.current.location.size * game.market.size_affect) / game.market.size_max;
const listingLength = Math.ceil(listingMulti * placesJSON.length);
// remove random amounts
const prunedPlacesJSON = common.randomShrinkArr(placesJSON, listingLength);
const prunedPlacesJSON = generatePrunedPlaces(placesJSON, location.country, listingLength);
for (const place of prunedPlacesJSON) {
const priceObj = {
id: place.id,
@@ -138,4 +138,21 @@ module.exports.generateAirportListings = function generateAirportListings(life)

return Math.ceil(hours * turnsPerHour);
}

function generatePrunedPlaces(places, country, size) {
const shuffledArr = common.shuffleArr(places);
const returnArr = [];
let currentSize = 0;
for (const place of shuffledArr) {
if (place.country === country) {
returnArr.push(place);
continue;
}
if (currentSize < size) {
returnArr.push(place);
currentSize++;
}
}
return returnArr;
}
};

0 comments on commit f4a9a05

Please sign in to comment.
You can’t perform that action at this time.