From 37612ee218acc58fdf956f7ec48e775e7b854452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Marchal?= Date: Thu, 3 Mar 2022 13:14:56 +0100 Subject: [PATCH] Fix generation selection not working --- poketest.js | 132 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 47 deletions(-) diff --git a/poketest.js b/poketest.js index 66d4ec4..4359738 100644 --- a/poketest.js +++ b/poketest.js @@ -40,11 +40,13 @@ $(document).ready(function() { /************ UTIL *************/ /*******************************/ - function random(min, max) { + function random(min, max) + { return Math.floor(Math.random()*(max-min+1)+min); } - function shuffleArray(array) { + function shuffleArray(array) + { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; @@ -54,7 +56,8 @@ $(document).ready(function() { return array; } - function findInArray(array, elem, size) { + function findInArray(array, elem) + { for (var j = 0; j < array.length; j++) { if (array[j] === elem) return true; @@ -67,14 +70,16 @@ $(document).ready(function() { /*******************************/ // loading json file - var json = $.getJSON('pokemon.json', function(pk) { - + $.getJSON('pokemon.json', function(pk) + { var pklist = pk; var pokemon_generation_list = new Array(); // starting the game for the first time and creating the layout - function firstGame() { - $('article #game').fadeToggle('quick', function() { + function firstGame() + { + $('article #game').fadeToggle('quick', function() + { document.removeEventListener('keydown', funcListener); $('article #container').append('

'); $('article #container #score').append('

'); @@ -82,51 +87,57 @@ $(document).ready(function() { max_chrono = $('article select.time option:selected').attr('id'); max_questions = $('article select.questions option:selected').attr('id'); if (max_questions == "infinite") infinite = true; - if (max_questions == "set") { + if (max_questions == "set") + { whole_set = true; - // set max_questions depending on generation - // if it's not all generation, we check list of available pokemons 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] + 1; } - choosePokemon(); game(); }); } // allows to start the game when pressing enter - document.addEventListener('keydown', funcListener = function(event) { + document.addEventListener('keydown', funcListener = function(event) + { if (event.keyCode == 13) // press enter firstGame(); }); + // start the game when clicking on start game - $('article #container .button input.start').click(function() { + $('article #container .button input.start').click(function() + { firstGame(); }); - function generations() { - + function generations() + { // create the list of the different generations - function createGenerationList(name) { + function createGenerationList(name) + { if ( $('article input[name="'+name+'"]').is(':checked') ) gen_list[gen_list.length] = name; } // create the list of all possible pokemons for selected options - function generatePokemonList() { - - function evaluateByGeneration(index, name, variable) { - if (gen_list[index] == name) { + function generatePokemonList() + { + function evaluateByGeneration(index, name, variable) + { + if (gen_list[index] == name) + { for (j = (variable[0] - 1); j <= (variable[1] - 1); j++) pokemon_generation_list[pokemon_generation_list.length] = j; } } - if (gen_list.length > 0) { - for (i in gen_list) { + if (gen_list.length > 0) + { + 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, "9g", G9); @@ -157,14 +168,18 @@ $(document).ready(function() { } // multiple artworks - function imageType(result) { + function imageType(result) + { + // TODO + var four_types = [351, 386, 585, 586, 676, 741, 800]; var three_types = [249, 250, 384, 412, 413, 718, 745, 898]; var two_types = [251, 252, 255, 258, 374, 387, 390, 393, 421, 422, 423, 487, 492, 495, 498, 501, 521, 550, 555, 592, 593, 641, 642, 643, 644, 645, 647, 648, 668, 678, 681, 720, 746, 774, 791, 792, 802, 849, 876, 877, 888, 889, 892, 905, "G892"]; - function randomizer() { + function randomizer() + { if (result >= MEGA[0]) return (pklist.mn[result-1]["num"]) if (result === 25) return (result+'-'+random(0, 16)); if (result === 666) return (result+'-'+random(0, 20)); @@ -190,7 +205,8 @@ $(document).ready(function() { return image_name; } - function recursiveOptions(options, lang) { + function recursiveOptions(options, lang) + { tmp = pokemon_generation_list[random(0, pokemon_generation_list.length - 1)]; // checking if it's already there, if it is, recursion @@ -201,7 +217,8 @@ $(document).ready(function() { } // choosing the four options - function chooseOptions(result, lang) { + function chooseOptions(result, lang) + { options = new Array(); options[0] = pklist.mn[result][lang]; for (var i = 1; i < 4; i++) @@ -209,20 +226,25 @@ $(document).ready(function() { options = shuffleArray(options); } - function choosePokemon() { + function choosePokemon() + { lang = $('article select.lang option:selected').attr('id'); silhouette = $('article input[name="silhouette"]:checked').attr('id'); - result = random(0, available_pokemon.length - 1); + resultIndex = Math.floor((available_pokemon.length - 1) * Math.random()); + result = available_pokemon[resultIndex]; + if (!infinite) - available_pokemon.splice(result, 1); + available_pokemon.splice(resultIndex, 1); answer = pklist.mn[result][lang]; chooseOptions(result, lang); imageType(result + 1); } - function game() { + function game() + { + choosePokemon(); questions++; chrono = max_chrono; $('article #container').addClass('game_active'); @@ -238,9 +260,11 @@ $(document).ready(function() { $('article #container #answers').append('') // timer setting - var clock = setInterval(function() { + var clock = setInterval(function() + { chrono--; - if (chrono === 0) { + if (chrono === 0) + { clearInterval(clock); showAnswer(false); // timer has ended, so it's a bad answer } @@ -248,12 +272,16 @@ $(document).ready(function() { }, 1000); // checking if the answer is good or not - $('article .option').click(function() { + $('article .option').click(function() + { clearInterval(clock); ($(this).attr('value') == answer) ? showAnswer(true) : showAnswer(false); }); - document.addEventListener('keydown', funcListener = function(event) { - function checkAnswer(v) { + + document.addEventListener('keydown', funcListener = function(event) + { + function checkAnswer(v) + { clearInterval(clock); ($('#answers input.a'+v).attr('value') == answer) ? showAnswer(true) : showAnswer(false); } @@ -263,21 +291,25 @@ $(document).ready(function() { else if (event.keyCode == 52 || event.keyCode == 76) checkAnswer(4); }); - function showAnswer(ok) { + function showAnswer(ok) + { document.removeEventListener('keydown', funcListener); $('article .pokemon img').removeClass('silhouette'); - if (ok) { + if (ok) + { good_answers++; $('article #answers').empty().append("

That's right! \ The answer is "+ answer +"!

"); - } else { + } + else + { bad_answers++; $('article #answers').empty().append("

Too bad... \ The answer was "+ answer +"!

"); } - choosePokemon(); var wait = max_wait; - clock = setInterval(function() { + clock = setInterval(function() + { wait--; if (wait === 0) { if (!infinite) questions < max_questions ? game() : score(); @@ -286,9 +318,10 @@ $(document).ready(function() { }, 1000); } - function score() { - - function generateTwitterText() { + function score() + { + function generateTwitterText() + { 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!' @@ -297,9 +330,11 @@ $(document).ready(function() { else return start_sentence+good_answers+" good Pokémon answers! "+end_sentence; } - function generateSeed() { + function generateSeed() + { gen = ''; - if (gen_list.length < (gen_number + extra_number)) { + if (gen_list.length < (gen_number + extra_number)) + { for (i in gen_list) gen += gen_list[i].substring(0,1); } @@ -319,6 +354,7 @@ $(document).ready(function() { return domain + s_gen + s_lang + s_silhouette + s_questions + s_time; } + $('article #container #score .pokemon').after('
'); $('article #container').addClass('diploma'); $('article #container').removeClass('game_active'); @@ -339,12 +375,14 @@ $(document).ready(function() { // class="start_again" name="start_again" value="Play again!">

'); // copy seed in clipboard - $("article #container #copyseed").click(function() { + $("article #container #copyseed").click(function() + { $('article #container #seed input').select(); document.execCommand("copy"); }); - $('article #container .button input.start_again').click(function() { + $('article #container .button input.start_again').click(function() + { score = questions = good_answers = bad_answers = 0; chrono = max_chrono; $('article #container .chrono').show();