diff --git a/javascript/app.js b/javascript/app.js new file mode 100644 index 0000000..acf2afa --- /dev/null +++ b/javascript/app.js @@ -0,0 +1,5 @@ +//const stat = require("./plStat.js"); +const func = require("./planetFunctions.js"); + +// Generate the planets +func.PlanetLoop(5); \ No newline at end of file diff --git a/javascript/plStat.js b/javascript/plStat.js index 8091bd6..6eabf96 100644 --- a/javascript/plStat.js +++ b/javascript/plStat.js @@ -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"] } }; diff --git a/javascript/planetFunctions.js b/javascript/planetFunctions.js new file mode 100644 index 0000000..35cce3a --- /dev/null +++ b/javascript/planetFunctions.js @@ -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; \ No newline at end of file diff --git a/javascript/planets.js b/javascript/planets.js deleted file mode 100644 index 00224a8..0000000 --- a/javascript/planets.js +++ /dev/null @@ -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); \ No newline at end of file