Ajout de la fonction de génération des % d'éléments + refacto
This commit is contained in:
5
javascript/app.js
Normal file
5
javascript/app.js
Normal file
@@ -0,0 +1,5 @@
|
||||
//const stat = require("./plStat.js");
|
||||
const func = require("./planetFunctions.js");
|
||||
|
||||
// Generate the planets
|
||||
func.PlanetLoop(5);
|
||||
@@ -1,39 +1,51 @@
|
||||
exports.planetType = {
|
||||
"Tempérée" : {
|
||||
"population" : 100
|
||||
"population" : 100,
|
||||
"elements" : ["Roches", "Eau", "Bois", "Poissons communs", "Plantes communes"]
|
||||
},
|
||||
"Glacée" : {
|
||||
"population" : 1
|
||||
"population" : 1,
|
||||
"elements" : ["Roches", "Glace", "Gaz"]
|
||||
},
|
||||
"Volcanique" : {
|
||||
"population" : 2
|
||||
"population" : 2,
|
||||
"elements" : ["Gaz", "Roches", "Carbone"]
|
||||
},
|
||||
"Marécageuse" : {
|
||||
"population" : 10
|
||||
"population" : 10,
|
||||
"elements" : ["Eau", "Bois", "Terre", "Faune sauvage"]
|
||||
},
|
||||
"Forestière" : {
|
||||
"population" : 20
|
||||
"population" : 20,
|
||||
"elements" : ["Bois", "Bois ancien", "Plantes rares"]
|
||||
},
|
||||
"Océanique" : {
|
||||
"population" : 25
|
||||
"population" : 25,
|
||||
"elements" : ["Eau"]
|
||||
},
|
||||
"Oecuménopole" : {
|
||||
"population" : 2000
|
||||
"population" : 2000,
|
||||
"elements" : ["Roches"]
|
||||
},
|
||||
"Désert" : {
|
||||
"population" : 50
|
||||
"population" : 50,
|
||||
"elements" : ["Sable", "Roches"]
|
||||
},
|
||||
"Minéralogique" : {
|
||||
"population" : 3
|
||||
"population" : 3,
|
||||
"elements" : ["Roches"]
|
||||
},
|
||||
"Gazeuse" : {
|
||||
"population" : 1
|
||||
"population" : 1,
|
||||
"elements" : ["Gaz"]
|
||||
},
|
||||
"Acide" : {
|
||||
"population" : 1
|
||||
"population" : 1,
|
||||
"elements" : ["Gaz", "Acide"]
|
||||
},
|
||||
"Monde usine" : {
|
||||
"population" : 500
|
||||
"population" : 500,
|
||||
"elements" : ["Acide", "Roche"]
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
86
javascript/planetFunctions.js
Normal file
86
javascript/planetFunctions.js
Normal file
@@ -0,0 +1,86 @@
|
||||
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;
|
||||
@@ -1,37 +0,0 @@
|
||||
//import { planetPopulationType, planetNamePrefix, planetNameSuffix } from "./pl_stat.js";
|
||||
let 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 planetDescription =
|
||||
// Keep this formatting, or else play with the console.log of func PlanetLoop
|
||||
(`
|
||||
Planète : ${planetName}
|
||||
Type : ${planetTypeGeneration}
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
PlanetLoop(5);
|
||||
Reference in New Issue
Block a user