poketest/flashdex.js

314 lines
8.7 KiB
JavaScript
Raw Permalink Normal View History

2022-03-03 12:30:59 +00:00
$(document).ready(function()
{
2020-04-25 16:29:59 +00:00
/****** Generations ******/
var G1 = [1, 151];
var G2 = [152, 251];
var G3 = [252, 386];
var G4 = [387, 493];
var G5 = [494, 649];
var G6 = [650, 721];
var G7 = [722, 809];
var G8 = [810, 905];
2024-01-29 14:04:29 +00:00
var G9 = [906, 1025];
var MEGA = [1026, 1075];
var GIGA = [1076, 1107];
var REG = [1108, 1162];
var gen_number = 9;
var extra_number = 3; // mega, giga, reg
2020-04-25 16:29:59 +00:00
/************ GAME *************/
/*******************************/
// loading json file
2022-03-03 12:30:59 +00:00
$.getJSON('pokemon.json', function(pk)
{
2020-04-25 16:29:59 +00:00
$('article #container #game #loading').hide();
var pklist = pk;
2022-03-03 12:30:59 +00:00
$('article #container #flashdex_config .all').click(function()
{
if ($(this).hasClass("unselected"))
{
2022-02-28 17:39:21 +00:00
for (i = 1; i < 10; ++i)
2020-04-25 16:29:59 +00:00
$('input[name="'+i+'g"]').prop('checked', true);
$('input[name="mega"]').prop('checked', true);
$('input[name="regional"]').prop('checked', true);
$('input[name="gigantamax"]').prop('checked', true);
$(this).removeClass("unselected").addClass("selected");
$(this).text("Unselect all");
2022-03-03 12:30:59 +00:00
}
else
{
2022-02-28 17:39:21 +00:00
for (i = 1; i < 10; ++i)
2020-04-25 16:29:59 +00:00
$('input[name="'+i+'g"]').prop('checked', false);
$('input[name="mega"]').prop('checked', false);
$('input[name="regional"]').prop('checked', false);
$('input[name="gigantamax"]').prop('checked', false);
$(this).removeClass("selected").addClass("unselected");
$(this).text("Select all");
}
})
2022-03-03 12:30:59 +00:00
$('article #container .button input.start').click(function()
{
2020-04-25 16:29:59 +00:00
var lang = $('article select.lang option:selected').attr('id');
var gen_list = new Array();
var pokemon_generation_list = new Array();
generations();
if (gen_list.length == 0)
return;
setUrlSearch();
$('article #container #game #loading').show();
$('article #container #game #flashdex').empty().hide();
2020-04-25 16:29:59 +00:00
function setUrlSearch()
{
gen = '';
if (gen_list.length < (gen_number + extra_number))
{
for (i in gen_list)
gen += gen_list[i].substring(0,1);
}
else gen += "all";
window.history.replaceState(null, null, "?gen=" + gen + "&lang=" + lang);
localStorage.setItem('lang', lang);
}
2022-03-03 12:30:59 +00:00
function getFormText(pkmn)
{
2020-04-25 16:29:59 +00:00
if (pkmn["origin"] == "Kanto")
return '<span class="form RB"></span>'
else if (pkmn["origin"] == "Johto")
return '<span class="form GS"></span>'
else if (pkmn["origin"] == "Hoenn")
return '<span class="form RS"></span>'
else if (pkmn["origin"] == "Sinnoh")
return '<span class="form DP"></span>'
else if (pkmn["origin"] == "Unova")
return '<span class="form BW"></span>'
else if (pkmn["origin"] == "Kalos")
return '<span class="form XY"></span>'
else if (pkmn["origin"] == "Alola")
return '<span class="form SM"></span>'
else if (pkmn["origin"] == "Galar")
2020-04-25 16:29:59 +00:00
return '<span class="form SWSH"></span>'
else if (pkmn["origin"] == "Hisui")
return '<span class="form LA"></span>'
2023-02-17 23:48:06 +00:00
else if (pkmn["origin"] == "Paldea")
return '<span class="form SV"></span>'
2022-03-02 13:20:50 +00:00
else if (pkmn["form"] == "mega")
2020-04-25 16:29:59 +00:00
return '<span class="form mega"></span>';
2022-03-02 13:20:50 +00:00
else if (pkmn["form"] == "giga")
2020-04-25 16:29:59 +00:00
return '<span class="form giga"></span>';
return '';
}
2022-03-03 12:30:59 +00:00
function getFormattedNumber(num)
{
2020-04-25 16:29:59 +00:00
if (num[0] == 'M' || num[0] == 'R' || num[0] == 'G')
return num.substring(1, num.length);
return num;
}
2022-03-02 13:20:50 +00:00
function getFormattedTypes(num) {
str = '<span class="types"><span class="'+num["type"][0]+'">'+num["type"][0]+'</span>';
if (num["type"][1])
str += '<span class="'+num["type"][1]+'">'+num["type"][1]+'</span>';
str += '</span>';
return str;
}
2022-03-03 12:30:59 +00:00
function getFormattedTypes(num, index)
{
str = '<span class="'+num["type"+index][0]+'">'+num["type"+index][0]+'</span>';
if (num["type"+index][1])
str += '<span class="'+num["type"+index][1]+'">'+num["type"+index][1]+'</span>';
return str;
}
2022-03-02 13:20:50 +00:00
2022-03-03 12:30:59 +00:00
function getFormattedSpriteButton(num)
{
if (num["sprite"] || num["origin"] == "Kanto")
2022-03-04 13:26:36 +00:00
return '<span class="button"><span class="left-arrow">&#9664;&#xFE0E;</span><span class="right-arrow">&#9654;&#xFE0E;</span></span>';
2022-03-03 12:30:59 +00:00
return '';
}
for (i in pokemon_generation_list)
{
2020-04-25 16:29:59 +00:00
$('article #container #game #flashdex').append(
'<div class="pokedex id_'+pklist.mn[pokemon_generation_list[i]]["num"]+'">'
2022-03-03 12:30:59 +00:00
+ '<span class="types">' + getFormattedTypes(pklist.mn[pokemon_generation_list[i]], '') + '</span>'
+ getFormattedSpriteButton(pklist.mn[pokemon_generation_list[i]], '')
2020-04-25 16:29:59 +00:00
+ getFormText(pklist.mn[pokemon_generation_list[i]]) // either gigantamax or mega-evolution
2022-03-03 12:30:59 +00:00
+ '<img src="pokemon/'+ imageType(pklist.mn[pokemon_generation_list[i]]) +'.png"/>'
2020-04-25 16:29:59 +00:00
+ '<span class="name">' + '<strong>' + '#' + getFormattedNumber(pklist.mn[[pokemon_generation_list[i]]]["num"]) + '</strong> '
+ pklist.mn[[pokemon_generation_list[i]]][lang] + '</span>'
+ '</div>'
);
2020-04-25 16:29:59 +00:00
}
2022-03-03 12:30:59 +00:00
$('article #container #game #flashdex').waitForImages(function()
{
2020-04-25 16:29:59 +00:00
$(this).show();
$('article #container #game #loading').hide();
});
2022-03-03 12:30:59 +00:00
function handleArtworks(origin, left)
{
function handleDefault()
{
var form = url.split('-')[1];
if (left)
{
if (form > 0)
form--;
else
2023-04-29 10:57:21 +00:00
form = pklist.mn[pokemon]["sprite"] - 1;
2022-03-03 12:30:59 +00:00
}
else
{
2023-04-29 10:57:21 +00:00
if (form < pklist.mn[pokemon]["sprite"] - 1)
2022-03-03 12:30:59 +00:00
form++;
else
form = 0;
}
2023-04-29 10:57:21 +00:00
var newUrl = 'pokemon/' + pokemonUrl + '-' + form + '.png';
2022-03-03 12:30:59 +00:00
origin.parent().parent().find("img").attr('src', newUrl);
2023-04-29 10:57:21 +00:00
handleChangeType(form, pokemon);
2022-03-03 12:30:59 +00:00
}
function handleKanto()
{
var KantoForm = '';
for (var i = 0; i < originalUrl.length; i++)
{
if (originalUrl[i].match(/[A-Z]/))
KantoForm += originalUrl[i];
}
if (left)
{
if (KantoForm == "RG")
2023-04-29 10:57:21 +00:00
KantoForm = "RB";
2022-03-03 12:30:59 +00:00
else if (KantoForm == "RB")
KantoForm = "";
else
KantoForm = "RG";
}
else
{
if (KantoForm == "RB")
2023-04-29 10:57:21 +00:00
KantoForm = "RG";
2022-03-03 12:30:59 +00:00
else if (KantoForm == "RG")
KantoForm = "";
else
KantoForm = "RB";
}
2023-04-29 10:57:21 +00:00
if (KantoForm)
KantoForm = "-" + KantoForm;
2022-03-03 12:30:59 +00:00
2023-04-29 10:57:21 +00:00
var newUrl = 'pokemon/' + pokemonUrl + KantoForm + '.png';
2022-03-03 12:30:59 +00:00
origin.parent().parent().find("img").attr('src', newUrl);
}
2020-04-25 16:29:59 +00:00
2022-03-03 12:30:59 +00:00
function handleChangeType(form, num)
{
if (form != 0)
{
if (pklist.mn[num]["type"+form])
origin.parent().parent().find(".types").empty().append(getFormattedTypes(pklist.mn[num], form));
}
else
origin.parent().parent().find(".types").empty().append(getFormattedTypes(pklist.mn[num], ''));
}
var url = origin.parent().parent().find("img").attr('src');
var originalUrl = url;
2023-04-29 10:57:21 +00:00
2022-03-03 12:30:59 +00:00
url = url.split('/')[1];
url = url.split('.')[0];
var pokemonUrl = url.split('-')[0];
2023-04-29 10:57:21 +00:00
var pokemon = pklist.mn.findIndex(function(item, i)
2022-03-03 12:30:59 +00:00
{
2023-04-29 10:57:21 +00:00
return item.num == pokemonUrl;
});
2022-03-03 12:30:59 +00:00
2023-04-29 10:57:21 +00:00
if (pklist.mn[pokemon]["origin"] == "Kanto" && !pklist.mn[pokemon]["sprite"])
2022-03-03 12:30:59 +00:00
handleKanto();
2023-04-29 10:57:21 +00:00
else if (pklist.mn[pokemon]["sprite"])
2022-03-03 12:30:59 +00:00
handleDefault();
}
$('.left-arrow').click(function(e)
{
handleArtworks($(this), true);
});
$('.right-arrow').click(function(e)
{
handleArtworks($(this), false);
});
function generations()
{
2020-04-25 16:29:59 +00:00
// create the list of the different generations
2022-03-03 12:30:59 +00:00
function createGenerationList(name)
{
2020-04-25 16:29:59 +00:00
if ( $('article input[name="'+name+'"]').is(':checked') )
gen_list[gen_list.length] = name;
}
// create the list of all possible pokemons for selected options
2022-03-03 12:30:59 +00:00
function generatePokemonList()
{
function evaluateByGeneration(index, name, variable)
{
if (gen_list[index] == name)
{
2020-04-25 16:29:59 +00:00
for (j = (variable[0] - 1); j <= (variable[1] - 1); j++)
pokemon_generation_list[pokemon_generation_list.length] = j;
}
}
2022-03-03 12:30:59 +00:00
if (gen_list.length > 0)
{
for (i in gen_list)
{
2020-04-25 16:29:59 +00:00
evaluateByGeneration(i, "1g", G1); evaluateByGeneration(i, "2g", G2); evaluateByGeneration(i, "3g", G3);
evaluateByGeneration(i, "4g", G4); evaluateByGeneration(i, "5g", G5); evaluateByGeneration(i, "6g", G6);
2022-02-28 17:39:21 +00:00
evaluateByGeneration(i, "7g", G7); evaluateByGeneration(i, "8g", G8); evaluateByGeneration(i, "9g", G9);
2020-04-25 16:29:59 +00:00
evaluateByGeneration(i, "mega", MEGA); evaluateByGeneration(i, "regional", REG); evaluateByGeneration(i, "gigantamax", GIGA);
}
}
}
2022-02-28 17:39:21 +00:00
for (i = 0; i < 10; i++)
2020-04-25 16:29:59 +00:00
createGenerationList((i + "g"));
createGenerationList("regional");
createGenerationList("mega");
2020-04-25 16:29:59 +00:00
createGenerationList("gigantamax");
generatePokemonList();
}
// multiple artworks
2022-03-03 12:30:59 +00:00
function imageType(num)
{
if (num["sprite"])
return num["num"] + "-0";
return num["num"];
2020-04-25 16:29:59 +00:00
}
});
});
});