86 lines
3.1 KiB
JavaScript
86 lines
3.1 KiB
JavaScript
const stat = require("./plStat.js");
|
|
|
|
const getRandomValue = (min, max) => {return Math.floor(Math.random() * (max - min + 1)) + min;}
|
|
const Random = (array) => {return array[Math.floor(Math.random() * array.length)];}
|
|
const RandomPlanet = (planet) => {
|
|
const keys = Object.keys(planet);
|
|
return keys[Math.floor(Math.random() * keys.length)];
|
|
}
|
|
|
|
const GeneratePlanet = () => {
|
|
const planetTypeGeneration = RandomPlanet(stat.planetType);
|
|
const planetName = Random(stat.planetNamePrefix) + Random(stat.planetNameSuffix);
|
|
const planetPopulationPeople = Random(stat.planetPopulationType);
|
|
const planetPopulationNumber = stat.planetType[planetTypeGeneration]["population"] * getRandomValue(5, 15) / 10;
|
|
|
|
const planetElements = stat.planetType[planetTypeGeneration].distributedElements;
|
|
let elementsDescription = '';
|
|
for (let element in planetElements) {
|
|
elementsDescription += `
|
|
${element}: ${planetElements[element].toFixed(2)}%`;
|
|
}
|
|
|
|
const planetDescription =
|
|
// Keep this formatting, or else play with the console.log of func PlanetLoop
|
|
(`
|
|
Planète : ${planetName}
|
|
|
|
Type : ${planetTypeGeneration}
|
|
|
|
Eléments :
|
|
${elementsDescription}
|
|
|
|
Population :
|
|
|
|
${planetPopulationNumber} milliards
|
|
Majoritairement ${planetPopulationPeople}`);
|
|
return planetDescription;
|
|
}
|
|
|
|
const PlanetLoop = (number) => {
|
|
let count = 1;
|
|
while (count <= number) {
|
|
const planet = GeneratePlanet();
|
|
console.log(planet + `\n\n------------------------------`);
|
|
count++;
|
|
}
|
|
}
|
|
|
|
// Apply distribution to the planet types
|
|
const distributeElementsRandomly = (elements) => {
|
|
if (elements.length === 0) return {};
|
|
if (elements.length === 1) return { [elements[0]]: 100 }
|
|
|
|
// Generate random percentages for each element except the last one
|
|
let percentages = Array.from({ length: elements.length - 1 }, () => Math.random());
|
|
|
|
// Sort the random numbers and use them to determine the boundaries of the segments
|
|
percentages.sort((a, b) => a - b);
|
|
|
|
// Convert these sorted random numbers into percentage segments
|
|
const distributedPercentages = percentages.map((p, i) => {
|
|
if (i === 0) return p * 100; // First segment from 0 to the first random number
|
|
else return (percentages[i] - percentages[i - 1]) * 100; // Segments between random numbers
|
|
});
|
|
|
|
// The last segment goes from the last random number to 1
|
|
distributedPercentages.push((1 - percentages[elements.length - 2]) * 100);
|
|
|
|
return Object.fromEntries(elements.map((element, index) => [element, distributedPercentages[index]]));
|
|
}
|
|
|
|
const distributePlanetTypesRandomly = (planetType) => {
|
|
for (const key in planetType) {
|
|
if (Array.isArray(planetType[key].elements)) {
|
|
const elements = planetType[key].elements;
|
|
const distributedElements = distributeElementsRandomly(elements);
|
|
planetType[key].distributedElements = distributedElements;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Apply distribution to your planet types
|
|
distributePlanetTypesRandomly(stat.planetType);
|
|
|
|
// Modules exports
|
|
exports.PlanetLoop = PlanetLoop; |