Compare commits

..

No commits in common. "0b19f848147464782222b097836e924ff41de791" and "8eeb0dee35acba42383c951c8b6288a76829dae7" have entirely different histories.

View File

@ -40,13 +40,11 @@ $(document).ready(function() {
/************ UTIL *************/ /************ UTIL *************/
/*******************************/ /*******************************/
function random(min, max) function random(min, max) {
{
return Math.floor(Math.random()*(max-min+1)+min); return Math.floor(Math.random()*(max-min+1)+min);
} }
function shuffleArray(array) function shuffleArray(array) {
{
for (var i = array.length - 1; i > 0; i--) { for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1)); var j = Math.floor(Math.random() * (i + 1));
var temp = array[i]; var temp = array[i];
@ -56,8 +54,7 @@ $(document).ready(function() {
return array; return array;
} }
function findInArray(array, elem) function findInArray(array, elem, size) {
{
for (var j = 0; j < array.length; j++) { for (var j = 0; j < array.length; j++) {
if (array[j] === elem) if (array[j] === elem)
return true; return true;
@ -70,16 +67,14 @@ $(document).ready(function() {
/*******************************/ /*******************************/
// loading json file // loading json file
$.getJSON('pokemon.json', function(pk) var json = $.getJSON('pokemon.json', function(pk) {
{
var pklist = pk; var pklist = pk;
var pokemon_generation_list = new Array(); var pokemon_generation_list = new Array();
// starting the game for the first time and creating the layout // starting the game for the first time and creating the layout
function firstGame() function firstGame() {
{ $('article #game').fadeToggle('quick', function() {
$('article #game').fadeToggle('quick', function()
{
document.removeEventListener('keydown', funcListener); document.removeEventListener('keydown', funcListener);
$('article #container').append('<div id="score"><div id="top_game"><h1 class="whois"></h1><h1 class="chrono"></h1></div></div>'); $('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>'); $('article #container #score').append('<p class="pokemon"></p><div id="answers"></div>');
@ -87,57 +82,51 @@ $(document).ready(function() {
max_chrono = $('article select.time option:selected').attr('id'); max_chrono = $('article select.time option:selected').attr('id');
max_questions = $('article select.questions option:selected').attr('id'); max_questions = $('article select.questions option:selected').attr('id');
if (max_questions == "infinite") infinite = true; if (max_questions == "infinite") infinite = true;
if (max_questions == "set") if (max_questions == "set") {
{
whole_set = true; whole_set = true;
// set max_questions depending on generation
// if it's not all generation, we check list of available pokemons // if it's not all generation, we check list of available pokemons
if (pokemon_generation_list.length > 0) if (pokemon_generation_list.length > 0)
max_questions = pokemon_generation_list.length; max_questions = pokemon_generation_list.length;
else // else, it's all the generation, from the first one to the regional forms else // else, it's all the generation, from the first one to the regional forms
max_questions = REG[1] + 1; max_questions = REG[1] + 1;
} }
choosePokemon();
game(); game();
}); });
} }
// allows to start the game when pressing enter // 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 if (event.keyCode == 13) // press enter
firstGame(); firstGame();
}); });
// start the game when clicking on start game // start the game when clicking on start game
$('article #container .button input.start').click(function() $('article #container .button input.start').click(function() {
{
firstGame(); firstGame();
}); });
function generations() function generations() {
{
// create the list of the different generations // create the list of the different generations
function createGenerationList(name) function createGenerationList(name) {
{
if ( $('article input[name="'+name+'"]').is(':checked') ) if ( $('article input[name="'+name+'"]').is(':checked') )
gen_list[gen_list.length] = name; gen_list[gen_list.length] = name;
} }
// create the list of all possible pokemons for selected options // create the list of all possible pokemons for selected options
function generatePokemonList() function generatePokemonList() {
{
function evaluateByGeneration(index, name, variable) function evaluateByGeneration(index, name, variable) {
{ if (gen_list[index] == name) {
if (gen_list[index] == name)
{
for (j = (variable[0] - 1); j <= (variable[1] - 1); j++) for (j = (variable[0] - 1); j <= (variable[1] - 1); j++)
pokemon_generation_list[pokemon_generation_list.length] = j; pokemon_generation_list[pokemon_generation_list.length] = j;
} }
} }
if (gen_list.length > 0) if (gen_list.length > 0) {
{ for (i in gen_list) {
for (i in gen_list)
{
evaluateByGeneration(i, "1g", G1); evaluateByGeneration(i, "2g", G2); evaluateByGeneration(i, "3g", G3); 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, "4g", G4); evaluateByGeneration(i, "5g", G5); evaluateByGeneration(i, "6g", G6);
evaluateByGeneration(i, "7g", G7); evaluateByGeneration(i, "8g", G8); evaluateByGeneration(i, "9g", G9); evaluateByGeneration(i, "7g", G7); evaluateByGeneration(i, "8g", G8); evaluateByGeneration(i, "9g", G9);
@ -168,35 +157,40 @@ $(document).ready(function() {
} }
// multiple artworks // multiple artworks
function imageType(result) function imageType(result) {
{ var four_types = [351, 386, 585, 586, 676, 741, 800];
function randomizer(result) 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,
if (pk.mn[result]["sprite"]) 592, 593, 641, 642, 643, 644, 645, 647, 648, 668, 678, 681, 720, 746, 774, 791, 792, 802, 849,
return pklist.mn[result]["num"] + '-' + random(0, pk.mn[result]["sprite"] - 1); 876, 877, 888, 889, 892, 905, "G892"];
else
{ function randomizer() {
if (pk.mn[result]["origin"] == "Kanto") if (result >= MEGA[0]) return (pklist.mn[result-1]["num"])
{ if (result === 25) return (result+'-'+random(0, 16));
randomValue = random(0, 2); if (result === 666) return (result+'-'+random(0, 20));
if (randomValue == 0) if (result === 646) return (result+'-'+random(0, 6));
return pklist.mn[result]["num"]; if (result === 479) return (result+'-'+random(0, 5));
else if (randomValue == 1) for (i in four_types) {
return pklist.mn[result]["num"]+'RB'; if (four_types[i] == result) return (result+'-'+random(0, 3));
else if (randomValue == 2)
return pklist.mn[result]["num"]+'RG';
} }
return pklist.mn[result]["num"]; for (i in three_types) {
if (three_types[i] == result) return (result+'-'+random(0, 2));
} }
for (i in two_types) {
if (two_types[i] == result) return (result+'-'+random(0, 1));
} }
image_name = randomizer(result); if (result > 151) return result;
if ((tmp = random(0, 2)) == 0) return result;
else if (tmp == 1) return (result+'RB');
else return (result+'RG');
}
image_name = randomizer(result+1);
image_memory = new Image(); image_memory = new Image();
image_memory.src = 'pokemon/' + image_name + '.png'; image_memory.src = 'pokemon/' + image_name + '.png';
return image_name; return image_name;
} }
function recursiveOptions(options, lang) function recursiveOptions(options, lang) {
{
tmp = pokemon_generation_list[random(0, pokemon_generation_list.length - 1)]; tmp = pokemon_generation_list[random(0, pokemon_generation_list.length - 1)];
// checking if it's already there, if it is, recursion // checking if it's already there, if it is, recursion
@ -207,8 +201,7 @@ $(document).ready(function() {
} }
// choosing the four options // choosing the four options
function chooseOptions(result, lang) function chooseOptions(result, lang) {
{
options = new Array(); options = new Array();
options[0] = pklist.mn[result][lang]; options[0] = pklist.mn[result][lang];
for (var i = 1; i < 4; i++) for (var i = 1; i < 4; i++)
@ -216,25 +209,20 @@ $(document).ready(function() {
options = shuffleArray(options); options = shuffleArray(options);
} }
function choosePokemon() function choosePokemon() {
{
lang = $('article select.lang option:selected').attr('id'); lang = $('article select.lang option:selected').attr('id');
silhouette = $('article input[name="silhouette"]:checked').attr('id'); silhouette = $('article input[name="silhouette"]:checked').attr('id');
resultIndex = Math.floor((available_pokemon.length - 1) * Math.random()); result = random(0, available_pokemon.length - 1);
result = available_pokemon[resultIndex];
if (!infinite) if (!infinite)
available_pokemon.splice(resultIndex, 1); available_pokemon.splice(result, 1);
answer = pklist.mn[result][lang]; answer = pklist.mn[result][lang];
chooseOptions(result, lang); chooseOptions(result, lang);
imageType(result); imageType(result + 1);
} }
function game() function game() {
{
choosePokemon();
questions++; questions++;
chrono = max_chrono; chrono = max_chrono;
$('article #container').addClass('game_active'); $('article #container').addClass('game_active');
@ -250,11 +238,9 @@ $(document).ready(function() {
$('article #container #answers').append('<span><input type="submit" class="option a4" name="a4" value="'+options[3]+'"></span>') $('article #container #answers').append('<span><input type="submit" class="option a4" name="a4" value="'+options[3]+'"></span>')
// timer setting // timer setting
var clock = setInterval(function() var clock = setInterval(function() {
{
chrono--; chrono--;
if (chrono === 0) if (chrono === 0) {
{
clearInterval(clock); clearInterval(clock);
showAnswer(false); // timer has ended, so it's a bad answer showAnswer(false); // timer has ended, so it's a bad answer
} }
@ -262,16 +248,12 @@ $(document).ready(function() {
}, 1000); }, 1000);
// checking if the answer is good or not // checking if the answer is good or not
$('article .option').click(function() $('article .option').click(function() {
{
clearInterval(clock); clearInterval(clock);
($(this).attr('value') == answer) ? showAnswer(true) : showAnswer(false); ($(this).attr('value') == answer) ? showAnswer(true) : showAnswer(false);
}); });
document.addEventListener('keydown', funcListener = function(event) {
document.addEventListener('keydown', funcListener = function(event) function checkAnswer(v) {
{
function checkAnswer(v)
{
clearInterval(clock); clearInterval(clock);
($('#answers input.a'+v).attr('value') == answer) ? showAnswer(true) : showAnswer(false); ($('#answers input.a'+v).attr('value') == answer) ? showAnswer(true) : showAnswer(false);
} }
@ -281,25 +263,21 @@ $(document).ready(function() {
else if (event.keyCode == 52 || event.keyCode == 76) checkAnswer(4); else if (event.keyCode == 52 || event.keyCode == 76) checkAnswer(4);
}); });
function showAnswer(ok) function showAnswer(ok) {
{
document.removeEventListener('keydown', funcListener); document.removeEventListener('keydown', funcListener);
$('article .pokemon img').removeClass('silhouette'); $('article .pokemon img').removeClass('silhouette');
if (ok) if (ok) {
{
good_answers++; good_answers++;
$('article #answers').empty().append("<p class=\"final_answer\"><span class=\"good\">That's right!</span> \ $('article #answers').empty().append("<p class=\"final_answer\"><span class=\"good\">That's right!</span> \
The answer is <strong>"+ answer +"</strong>!</p>"); The answer is <strong>"+ answer +"</strong>!</p>");
} } else {
else
{
bad_answers++; bad_answers++;
$('article #answers').empty().append("<p class=\"final_answer\"><span class=\"bad\">Too bad...</span> \ $('article #answers').empty().append("<p class=\"final_answer\"><span class=\"bad\">Too bad...</span> \
The answer was <strong>"+ answer +"</strong>!</p>"); The answer was <strong>"+ answer +"</strong>!</p>");
} }
choosePokemon();
var wait = max_wait; var wait = max_wait;
clock = setInterval(function() clock = setInterval(function() {
{
wait--; wait--;
if (wait === 0) { if (wait === 0) {
if (!infinite) questions < max_questions ? game() : score(); if (!infinite) questions < max_questions ? game() : score();
@ -308,10 +286,9 @@ $(document).ready(function() {
}, 1000); }, 1000);
} }
function score() function score() {
{
function generateTwitterText() function generateTwitterText() {
{
var start_sentence = 'I got '; var start_sentence = 'I got ';
var end_sentence = 'Can you get a better score than me?'; 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 score_sentence = (Math.floor(good_answers/max_questions*100))+'% Pokémons right!'
@ -320,11 +297,9 @@ $(document).ready(function() {
else return start_sentence+good_answers+" good Pokémon answers! "+end_sentence; else return start_sentence+good_answers+" good Pokémon answers! "+end_sentence;
} }
function generateSeed() function generateSeed() {
{
gen = ''; gen = '';
if (gen_list.length < (gen_number + extra_number)) if (gen_list.length < (gen_number + extra_number)) {
{
for (i in gen_list) for (i in gen_list)
gen += gen_list[i].substring(0,1); gen += gen_list[i].substring(0,1);
} }
@ -344,7 +319,6 @@ $(document).ready(function() {
return 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 #score .pokemon').after('<div id="seed"></div><div id="share"></div>');
$('article #container').addClass('diploma'); $('article #container').addClass('diploma');
$('article #container').removeClass('game_active'); $('article #container').removeClass('game_active');
@ -365,14 +339,12 @@ $(document).ready(function() {
// class="start_again" name="start_again" value="Play again!"></p>'); // class="start_again" name="start_again" value="Play again!"></p>');
// copy seed in clipboard // copy seed in clipboard
$("article #container #copyseed").click(function() $("article #container #copyseed").click(function() {
{
$('article #container #seed input').select(); $('article #container #seed input').select();
document.execCommand("copy"); 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; score = questions = good_answers = bad_answers = 0;
chrono = max_chrono; chrono = max_chrono;
$('article #container .chrono').show(); $('article #container .chrono').show();