Refactored the selection of Pokémon
It was causing issues because there were lots of recursions causing errors for large sets (>950 occurrences)
This commit is contained in:
		@@ -35,6 +35,8 @@
 | 
			
		||||
 | 
			
		||||
						<h1>Flashdex</h1>
 | 
			
		||||
 | 
			
		||||
						<p>✨ <strong>Now including the first monsters from Pokémon Scarlet & Violet!</strong></p>
 | 
			
		||||
 | 
			
		||||
						<p>The Flashdex is a very simple Pokédex, useful to see your favorite creatures in good resolution. Perfect for learning new names, and to see the different generations and different Pokémon forms!</p>
 | 
			
		||||
 | 
			
		||||
						<div id="flashdex_config">
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										89
									
								
								poketest.js
									
									
									
									
									
								
							
							
						
						
									
										89
									
								
								poketest.js
									
									
									
									
									
								
							@@ -18,7 +18,8 @@ $(document).ready(function() {
 | 
			
		||||
	var image_name;				// preload
 | 
			
		||||
	var image_memory;			// preload
 | 
			
		||||
	var gen_list = new Array();
 | 
			
		||||
	var previous_pokemon = new Array();
 | 
			
		||||
	var available_pokemon = new Array();
 | 
			
		||||
	var domain = "https://poketest.marchal.dev/index.html"; 
 | 
			
		||||
 | 
			
		||||
	/****** Generations ******/
 | 
			
		||||
	var G1 		= [1, 151];
 | 
			
		||||
@@ -33,6 +34,8 @@ $(document).ready(function() {
 | 
			
		||||
	var MEGA 	= [909, 958]; 
 | 
			
		||||
	var GIGA	= [959, 990];
 | 
			
		||||
	var REG		= [991, 1043];
 | 
			
		||||
	var gen_number = 9;
 | 
			
		||||
	var extra_number = 3; // mega, giga, reg
 | 
			
		||||
 | 
			
		||||
	/************ UTIL *************/
 | 
			
		||||
	/*******************************/
 | 
			
		||||
@@ -76,7 +79,6 @@ $(document).ready(function() {
 | 
			
		||||
				$('article #container').append('<div id="score"><div id="top_game"><h1 class="whois"></h1><h1 class="chrono"></h1></div></div>');
 | 
			
		||||
				$('article #container #score').append('<p class="pokemon"></p><div id="answers"></div>');
 | 
			
		||||
				generations();
 | 
			
		||||
				choosePokemon();
 | 
			
		||||
				max_chrono = $('article select.time option:selected').attr('id');
 | 
			
		||||
				max_questions = $('article select.questions option:selected').attr('id');
 | 
			
		||||
				if (max_questions == "infinite") infinite = true;
 | 
			
		||||
@@ -88,8 +90,9 @@ $(document).ready(function() {
 | 
			
		||||
					if (pokemon_generation_list.length > 0)
 | 
			
		||||
						max_questions = pokemon_generation_list.length;
 | 
			
		||||
					else // else, it's all the generation, from the first one to the regional forms
 | 
			
		||||
						max_questions = REG[1];
 | 
			
		||||
						max_questions = REG[1] + 1;
 | 
			
		||||
				}
 | 
			
		||||
				choosePokemon();
 | 
			
		||||
				game();
 | 
			
		||||
			});
 | 
			
		||||
		}
 | 
			
		||||
@@ -126,22 +129,31 @@ $(document).ready(function() {
 | 
			
		||||
					for (i in gen_list) {
 | 
			
		||||
						evaluateByGeneration(i, "1g", G1); evaluateByGeneration(i, "2g", G2); evaluateByGeneration(i, "3g", G3);
 | 
			
		||||
						evaluateByGeneration(i, "4g", G4); evaluateByGeneration(i, "5g", G5); evaluateByGeneration(i, "6g", G6);
 | 
			
		||||
						evaluateByGeneration(i, "7g", G7); evaluateByGeneration(i, "8g", G8);
 | 
			
		||||
						evaluateByGeneration(i, "7g", G7); evaluateByGeneration(i, "8g", G8); evaluateByGeneration(i, "9g", G9);
 | 
			
		||||
						evaluateByGeneration(i, "mega", MEGA); evaluateByGeneration(i, "gigantamax", GIGA); evaluateByGeneration(i, "regional", REG); 
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			// we parse the list of generation, then check if there are other forms
 | 
			
		||||
			if ( $('article input[name="gen"]:checked').attr('id') === "all" )
 | 
			
		||||
				return;
 | 
			
		||||
			for (i = 0; i < 9; i++)
 | 
			
		||||
				createGenerationList((i + "g"));
 | 
			
		||||
			createGenerationList("mega");
 | 
			
		||||
			createGenerationList("gigantamax");
 | 
			
		||||
			createGenerationList("regional");
 | 
			
		||||
			generatePokemonList();
 | 
			
		||||
			{
 | 
			
		||||
				gen_list.push("1g"); gen_list.push("2g"); gen_list.push("3g"); gen_list.push("4g"); gen_list.push("5g");
 | 
			
		||||
				gen_list.push("6g"); gen_list.push("7g"); gen_list.push("8g"); gen_list.push("9g");
 | 
			
		||||
				gen_list.push("mega"); gen_list.push("gigantamax"); gen_list.push("regional");
 | 
			
		||||
				generatePokemonList();
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				for (i = 0; i < gen_number; i++)
 | 
			
		||||
					createGenerationList((i + "g"));
 | 
			
		||||
				createGenerationList("mega");
 | 
			
		||||
				createGenerationList("gigantamax");
 | 
			
		||||
				createGenerationList("regional");
 | 
			
		||||
				generatePokemonList();
 | 
			
		||||
			}
 | 
			
		||||
			available_pokemon = [...pokemon_generation_list];
 | 
			
		||||
			max_questions = pokemon_generation_list.length;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// multiple artworks
 | 
			
		||||
@@ -179,11 +191,7 @@ $(document).ready(function() {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function recursiveOptions(options, lang) {
 | 
			
		||||
			// if it's not all generation, we check list of available pokemons
 | 
			
		||||
			if (pokemon_generation_list.length > 0)
 | 
			
		||||
				tmp = pokemon_generation_list[random(0, pokemon_generation_list.length - 1)];
 | 
			
		||||
			else // else, it's all the generation, from the first one to the regional forms
 | 
			
		||||
				tmp = random(0, REG[1] - 1);
 | 
			
		||||
			tmp = pokemon_generation_list[random(0, pokemon_generation_list.length - 1)];
 | 
			
		||||
 | 
			
		||||
			// checking if it's already there, if it is, recursion
 | 
			
		||||
			if (!findInArray(options, pklist.mn[tmp][lang]))
 | 
			
		||||
@@ -201,42 +209,16 @@ $(document).ready(function() {
 | 
			
		||||
			options = shuffleArray(options);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// avoid repetition of the same pokemon in a short lapse of time
 | 
			
		||||
		function checkPreviousPokemon(pkmn) {
 | 
			
		||||
			if (!whole_set) { // if it's not the whole set, we have to delete some older occurrences
 | 
			
		||||
				if (pokemon_generation_list.length > 0) {
 | 
			
		||||
					if (previous_pokemon.length + 6 > pokemon_generation_list.length)
 | 
			
		||||
						previous_pokemon.splice(0, 6);
 | 
			
		||||
				}
 | 
			
		||||
				else {
 | 
			
		||||
					if (previous_pokemon.length + 6 > REG[1] - 1)
 | 
			
		||||
						previous_pokemon.splice(0, 6);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			return findInArray(previous_pokemon, pkmn);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		function choosePokemon() {
 | 
			
		||||
			// if it's not all generation, we check list of available pokemons
 | 
			
		||||
			if (pokemon_generation_list.length > 0)
 | 
			
		||||
				result = pokemon_generation_list[random(0, pokemon_generation_list.length - 1)];
 | 
			
		||||
			else // else, it's all the generation, from the first one to the regional forms
 | 
			
		||||
				result = random(0, REG[1] - 1);
 | 
			
		||||
			lang = $('article select.lang option:selected').attr('id');
 | 
			
		||||
			silhouette = $('article input[name="silhouette"]:checked').attr('id');
 | 
			
		||||
			//console.log("result = 0, " + (REG[1] - 1));
 | 
			
		||||
			//console.log("test = " + pklist.mn[0][lang]);
 | 
			
		||||
			//console.log("test = " + pklist.mn[REG[1] - 1][lang]);
 | 
			
		||||
			console.log("number: " + result);
 | 
			
		||||
 | 
			
		||||
			result = random(0, available_pokemon.length - 1);
 | 
			
		||||
			if (!infinite)
 | 
			
		||||
				available_pokemon.splice(result, 1);
 | 
			
		||||
 | 
			
		||||
			answer = pklist.mn[result][lang];
 | 
			
		||||
			console.log("pokemon: " + answer);
 | 
			
		||||
			if (checkPreviousPokemon(answer)) { // if it's already been selected within a short lapse of time
 | 
			
		||||
				if (whole_set && questions == max_questions) { } // makes the last question of whole set work
 | 
			
		||||
				else choosePokemon(); // recursive call
 | 
			
		||||
			} else {
 | 
			
		||||
				previous_pokemon[previous_pokemon.length] = answer;
 | 
			
		||||
				chooseOptions(result, lang);
 | 
			
		||||
			}
 | 
			
		||||
			chooseOptions(result, lang);
 | 
			
		||||
			imageType(result + 1);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -295,7 +277,7 @@ $(document).ready(function() {
 | 
			
		||||
				}
 | 
			
		||||
				choosePokemon();
 | 
			
		||||
				var wait = max_wait;
 | 
			
		||||
				var clock = setInterval(function() {
 | 
			
		||||
				clock = setInterval(function() {
 | 
			
		||||
					wait--;
 | 
			
		||||
					if (wait === 0) {
 | 
			
		||||
						if (!infinite)	questions < max_questions ? game() : score();
 | 
			
		||||
@@ -310,16 +292,14 @@ $(document).ready(function() {
 | 
			
		||||
					var start_sentence = 'I got ';
 | 
			
		||||
					var end_sentence = 'Can you get a better score than me?';
 | 
			
		||||
					var score_sentence = (Math.floor(good_answers/max_questions*100))+'% Pokémons right!'
 | 
			
		||||
					var questions_sentence = max_questions + " questions"
 | 
			
		||||
 | 
			
		||||
					if (!infinite)	return start_sentence+score_sentence+" "+end_sentence;
 | 
			
		||||
					else			return start_sentence+good_answers+" good Pokémon answers! "+end_sentence;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				function generateSeed() {
 | 
			
		||||
					s_domain = "https://poketest.marchal.dev/index.html";
 | 
			
		||||
					gen = '';
 | 
			
		||||
					if (gen_list.length > 0) {
 | 
			
		||||
					if (gen_list.length < (gen_number + extra_number)) {
 | 
			
		||||
						for (i in gen_list)
 | 
			
		||||
							gen += gen_list[i].substring(0,1);
 | 
			
		||||
					}
 | 
			
		||||
@@ -337,7 +317,7 @@ $(document).ready(function() {
 | 
			
		||||
					localStorage.setItem('questions', (!whole_set? max_questions : "set"));
 | 
			
		||||
					localStorage.setItem('time', max_chrono);
 | 
			
		||||
 | 
			
		||||
					return s_domain + s_gen + s_lang + s_silhouette + s_questions + s_time;
 | 
			
		||||
					return domain + s_gen + s_lang + s_silhouette + s_questions + s_time;
 | 
			
		||||
				}
 | 
			
		||||
				$('article #container #score .pokemon').after('<div id="seed"></div><div id="share"></div>');
 | 
			
		||||
				$('article #container').addClass('diploma');
 | 
			
		||||
@@ -373,6 +353,5 @@ $(document).ready(function() {
 | 
			
		||||
				});
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	});
 | 
			
		||||
});
 | 
			
		||||
@@ -138,8 +138,8 @@ article #container.about ul li a { margin-top: 5px; margin-bottom: 5px; font-wei
 | 
			
		||||
.SM2 { background: #589ac8; color: black; padding: 5px; border-radius: 0px 10px 10px 0px; }
 | 
			
		||||
.SWSH1 { background: #00A1E9; color: black; padding: 5px; border-radius: 10px 0px 0px 10px; }
 | 
			
		||||
.SWSH2 { background: #BF004F; color: white; padding: 5px; border-radius: 0px 10px 10px 0px; }
 | 
			
		||||
.SV1 { background: #e01715; color: white; padding: 5px; border-radius: 10px 0px 0px 10px; }
 | 
			
		||||
.SV2 { background: #741e85; color: white; padding: 5px; border-radius: 0px 10px 10px 0px; }
 | 
			
		||||
.SV1 { background: #F34134; color: black; padding: 5px; border-radius: 10px 0px 0px 10px; }
 | 
			
		||||
.SV2 { background: #8334B7; color: white; padding: 5px; border-radius: 0px 10px 10px 0px; }
 | 
			
		||||
 | 
			
		||||
.RB { border-radius:50%; background: linear-gradient(-45deg, #FF1111 50%, #1111FF 50%); display:block; border: 1px solid white; }
 | 
			
		||||
.GS { border-radius:50%; background: linear-gradient(-45deg, #DAA520 50%, #C0C0C0 50%); display:block; border: 1px solid white; }
 | 
			
		||||
@@ -150,7 +150,7 @@ article #container.about ul li a { margin-top: 5px; margin-bottom: 5px; font-wei
 | 
			
		||||
.SM { border-radius:50%; background: linear-gradient(-45deg, #ef9039 50%, #589ac8 50%); display:block; border: 1px solid white; }
 | 
			
		||||
.SWSH { border-radius:50%; background: linear-gradient(-45deg, #00A1E9 50%, #BF004F 50%); display:block; border: 1px solid white; }
 | 
			
		||||
.LA { border-radius:50%; background: #36597B; display:block; border: 1px solid white; }
 | 
			
		||||
.SV { border-radius:50%; background: linear-gradient(-45deg, #e01715 50%, #741e85 50%); display:block; border: 1px solid white; }
 | 
			
		||||
.SV { border-radius:50%; background: linear-gradient(-45deg, #F34134 50%, #8334B7 50%); display:block; border: 1px solid white; }
 | 
			
		||||
 | 
			
		||||
/**** FOOTER ****/
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user