-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
426 lines (355 loc) · 12.8 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//function to get raw JSON data from URL
async function getData(url){
let request = await fetch(url);
if(request.status !== 404){
return await request.json()
}else{
return false;
}
}
//let's list all Pokémons for name search
let allPokemons;
const typeColor = {
"normal": "#F5F5DC",
"fire": "#F08030",
"water": "#87CEEB",
"electric": "#F8D030",
"grass": "#90EE90",
"ice": "#E0FFFF",
"fighting": "#F08080",
"poison": "#9370DB",
"ground": "#D2B48C",
"flying": "#E6E6FA",
"psychic": "#FF69B4",
"bug": "#90EE90",
"rock": "#B0C4DE",
"ghost": "#9370DB",
"dragon": "#9932CC",
"dark": "#A9A9A9",
"steel": "#B0C4DE",
"fairy": "#FFB6C1"
};
//search
async function isType(pokemon, type){
let data = await getData("https://pokeapi.co/api/v2/type/"+type);
let isType = false;
data['pokemon'].forEach(poke => {
if(poke['pokemon']['name'] === pokemon){
isType = true;
}
})
return isType;
}
let searchBar = document.querySelector('#search')
let amount = document.querySelector('.amount')
let type = document.querySelector('#type')
type.addEventListener('change', async () => {
stockReset();
if(searchBar.value === ''){
getDefaultPokemon();
}else{
await addPokemonIfQuery();
}
})
//async filter by Tamás Sallai
async function filter(array, condition){
const results = await Promise.all(array.map(condition));
return array.filter((_v, index) => results[index]);
}
//find pokemon based on various conditions
async function findPokemon(query) {
return await filter(allPokemons, async (entry) => {
if (entry.name.includes(query)) {
if (type.value !== "") {
return await isType(entry.name, type.value);
}else{
return true;
}
}else{
return false;
}
})
}
async function addPokemonIfQuery(){
let result = await findPokemon(searchBar.value); //ducoup on await
amount.innerHTML = 'Résultats : '+result.length;
let i = 0;
result.forEach((pokemon) => {
if(i<10){
addToDeck(pokemon['name']);
i++;
}
})
}
searchBar.addEventListener('keyup', async ()=>{ //on fait une fonction async
stockReset();
if(searchBar.value === ''){
// vérifier que
getDefaultPokemon();
amount.innerHTML = ''
}else{
await addPokemonIfQuery();
}
})
let deckContainers = document.querySelectorAll('div.deck');
//let's generate the big deck
deckContainers.forEach((deck, index) => {
let cell = document.createElement('div');
cell.classList.add('cell');
for(let i = 0; i<6; i++){
deck.appendChild(cell.cloneNode());
}
})
//put the first as active
deckContainers[0].classList.add('active');
//elements
let allCell = document.querySelectorAll('div.cell');
let pokemonStock = document.querySelector(".poke-" + "stock");
let trash = document.querySelector("#trash");
let deckSelector = document.querySelector('#slots');
deckSelector.addEventListener('change', function () {
deckContainers.forEach((deck) => {
deck.classList.remove('active');
})
deckContainers[parseInt(this.value) - 1].classList.add('active')
})
//drag drop functions :
function allowDrop(e){
if((e.target.classList.contains('cell') && e.target.childElementCount === 0)
|| e.target.id === 'trash'
|| e.target.classList.contains('poke-stock')){
e.preventDefault();
}else{
}
}
function dragPokemon(e){
e.dataTransfer.setData('pokemon', e.target.id);
if(e.target.parentNode.classList.contains('poke-stock')){
e.dataTransfer.setData('toCopy', 'true');
console.log('à copier')
}
}
let aaaAAAH = document.querySelector('#aaaAAAH');
aaaAAAH.volume = 0.2;
function dropPokemon(e){
if((e.target.classList.contains('cell') && e.target.childElementCount === 0)
|| e.target.id === 'trash'
|| e.target.classList.contains('poke-stock')){
let pokemonId = e.dataTransfer.getData('pokemon');
if(e.target.id === 'trash'){
document.querySelector('#'+pokemonId).remove();
if(!aaaAAAH.paused){
aaaAAAH.paused = true;
aaaAAAH.currentTime=0;
}
aaaAAAH.play();
}else{
if(e.dataTransfer.getData('toCopy') === 'true' && !e.target.classList.contains('poke-stock')){
let pokeID = pokemonId.split('_')[2];
getPokemonData(pokeID).then(data => {
e.target.appendChild(generateCard(data));
})
}else{
if(e.target.classList.contains('poke-stock') && e.dataTransfer.getData('toCopy') !== 'true'){
document.querySelector('#'+pokemonId).remove();
}else{
e.target.appendChild(document.querySelector('#'+pokemonId));
}
}
}
}
}
let vanishSound = document.querySelector('#vanish');
vanishSound.volume = 0.5;
trash.addEventListener('dblclick', () => {
let storage = JSON.parse(localStorage.deck).slice(0,6);
if(!storage.every(cell => cell === 0)){
if(!vanishSound.paused){
vanishSound.paused = true;
vanishSound.currentTime=0;
}
vanishSound.play();
let offset = 0; //TODO pimper la fonction pour qu'elle puisse supprimer n'importe quelle team
for(let i=offset; i<offset+6; i++){
setTimeout(()=>{
allCell[i].innerHTML = '';
}, 100*(i%6))
}
refreshStats();
}
})
//deck and visual
async function getPokemonData(name){
let pokemon = await getData("https://pokeapi.co/api/v2/pokemon/"+name);
if(pokemon !== false){
return pokemon;
}
}
let amountCard = 0;
function generateCard(pokemon){
let img = document.createElement('img');
let p = document.createElement('p');
let imgContainer = document.createElement('div');
let imginfo = document.createElement('img');
imginfo.src = "media/information.png";
imginfo.classList.add('info');
imginfo.style.width = "20px";
imginfo.style.height = "20px";
imginfo.style.position = "absolute";
imginfo.addEventListener('click', () => {
let modal
if(document.querySelector('#modal') === null) {
modal = document.createElement('div');
modal.id = 'modal';
modal.classList.add('modal');
modal.style.position = "absolute";
modal.style.top = "0";
modal.style.left = "0";
modal.style.width = "100%";
modal.style.height = "100%";
modal.style.backgroundColor = "rgba(0,0,0,0.5)";
modal.style.display = "flex";
modal.style.justifyContent = "center";
modal.style.alignItems = "center";
modal.style.zIndex = "100";
document.body.appendChild(modal);
let modalContent = document.createElement('div');
modalContent.classList.add('modal-content');
modalContent.style.width = "50%";
modalContent.style.height = "60%";
modalContent.style.backgroundColor = "black";
modalContent.style.borderRadius = "10px";
modalContent.style.display = "flex";
modalContent.style.flexDirection = "column";
modalContent.style.justifyContent = "center";
modalContent.style.alignItems = "center";
modal.appendChild(modalContent);
let modalTitle = document.createElement('h1');
modalTitle.innerHTML = pokemon['name'];
modalTitle.style.color = "white";
modalContent.appendChild(modalTitle);
let modalImg = document.createElement('img');
modalImg.src = pokemon['sprites']['front_default'];
modalImg.style.width = "20rem";
modalContent.appendChild(modalImg);
//bouton pour passer l'img en shiny
let shiny = document.createElement('img');
shiny.src = "media/shiny.png";
shiny.classList.add('shiny');
shiny.style.width = "40px";
shiny.style.height = "40px";
shiny.style.top = "0";
shiny.style.right = "0";
shiny.style.zIndex = "100";
modalContent.appendChild(shiny);
shiny.addEventListener('click', () => {
if(modalImg.src === pokemon['sprites']['front_default']){
modalImg.src = pokemon['sprites']['front_shiny'];
shiny.style.filter = "invert(1)";
}else{
modalImg.src = pokemon['sprites']['front_default'];
shiny.style.filter = "invert(0)";
}
})
let modalClose = document.createElement('button');
modalClose.innerHTML = "Close";
modalClose.style.width = "100px";
modalClose.style.height = "50px";
modalClose.style.backgroundColor = "red";
modalClose.style.borderRadius = "10px";
modalClose.style.color = "white";
modalClose.style.fontSize = "20px";
modalClose.style.cursor = "pointer";
modalClose.addEventListener('click', () => {
modal.remove();
})
modalContent.appendChild(modalClose);
//bonton faire evoluer
let modalEvolve = document.createElement('button');
modalEvolve.innerHTML = "Evolve";
modalEvolve.style.width = "100px";
modalEvolve.style.height = "50px";
modalEvolve.style.backgroundColor = "green";
modalEvolve.style.borderRadius = "10px";
modalEvolve.style.color = "white";
modalEvolve.style.fontSize = "30px";
modalEvolve.style.cursor = "pointer";
modalEvolve.addEventListener('click', () => {
if(pokemon['species']['url'] !== pokemon['forms'][0]['url']){
getData(pokemon['species']['url']).then(data => {
getData(data['evolution_chain']['url']).then(data => {
let evolution = data['chain'];
while(evolution['evolves_to'].length > 0){
if(evolution['species']['url'] === pokemon['forms'][0]['url']){
getData(evolution['evolves_to'][0]['species']['url']).then(data => {
modalImg.src = data['sprites']['front_default'];
modalTitle.innerHTML = data['name'];
pokemon = data;
})
break;
}else{
evolution = evolution['evolves_to'][0];
}
}
})
})
}
})
modalContent.appendChild(modalEvolve);
}
})
imgContainer.classList.add('img-container');
img.src = pokemon['sprites']['front_default'];
img.draggable = false;
p.innerHTML = pokemon['name'].replace('-', ' ');
let card = document.createElement('div');
card.classList.add('poke-card');
card.appendChild(p);
imgContainer.appendChild(img);
card.appendChild(imgContainer);
card.appendChild(imginfo);
card.style.borderColor = typeColor[pokemon['types'][0]['type']['name']]
card.id = 'card_' + amountCard + '_'+pokemon['id'];
amountCard++;
card.draggable = true;
card.ondragstart = dragPokemon;
return card;
}
function addToDeck(name){
getPokemonData(name).then(pokemon => {
pokemonStock.appendChild(generateCard(pokemon));
})
}
function getDefaultPokemon(){
if(type.value === ''){
for(let i = 1; i<31; i+=3){
addToDeck(i);
}
}else{
getData("https://pokeapi.co/api/v2/type/"+type.value).then(data =>{
for(let i = 0; i<10; i++){
addToDeck(data['pokemon'][i]['pokemon']["name"]);
}
})
}
}
function stockReset(){
pokemonStock.innerHTML = '';
}
//events
allCell.forEach(cell => cell.ondragover = allowDrop)
allCell.forEach(cell => cell.ondrop = dropPokemon)
pokemonStock.ondragover = allowDrop;
pokemonStock.ondrop = dropPokemon;
trash.ondragover = allowDrop;
trash.ondrop = dropPokemon;
window.onload = async () => {
await getData("https://pokeapi.co/api/v2/pokemon?limit=100000&offset=0").then((data)=>{
allPokemons = data['results'];
});
getDefaultPokemon();
//storage
restoreDeck();
}
// info pokemon
const popinfo = document.querySelector('.iconeinfo');