-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame_Functionality.js
More file actions
124 lines (109 loc) · 3.4 KB
/
Game_Functionality.js
File metadata and controls
124 lines (109 loc) · 3.4 KB
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
const gameSetup = async () => {
await setupNewCard();
await populateDropdown();
$(document).ready(function() {
const $select = $('#search-box').selectize({
sortField: 'text',
maxOptions: 5, // Set a higher initial maxOptions
closeAfterSelect: true,
onItemAdd: function(value, $item) {
guessHandling(value);
const selectizeInstance = $select[0].selectize;
selectizeInstance.clear();
}
});
});
}
const setupNewCard = async()=>{
let pokemon;
try{
pokemon = await getRandomPokemon();
createPokemonCard(pokemon)
typesToMatch = []
for (let data of pokemon.types){
typesToMatch.push(data.type.name)
}
}
catch (error){
console.log(`Error in game setup ${error}`)
pokemonCardContainer.innerText = `Sorry Ash, the pokémon is in another castle :( \nplease refresh!`
}
}
const guessHandling = async(guess)=>{
console.log(`Submitted ${guess}`)
try{``
const pokemon = await getPokemonById(guess)
if(pokemon === null){
console.log(`Pokémon not found`)
return false
}
//Update card with guessed Pokémon or create new card if it doesn't exist
//Create versus text
if(pokemonCardContainer.querySelectorAll('#versus-text').length === 0){
showVersusText()
}
if(pokemonCardContainer.querySelectorAll('#pokemon-card').length >1){
updatePokemonCard(pokemon,1)
}
else{
createPokemonCard(pokemon)
}
let effectiveness = 0;
for(let data of pokemon.types){
const url = data.type.url;
typeData = await getTypeData(url)
effectiveness = Math.max(effectiveness,checkEffectiveness(typeData.damage_relations,typesToMatch))
}
resultHandling(effectiveness)
}
catch(error){
console.log(`Pokémon not found ${error}`)
resultHandling(-1)
}
}
const checkEffectiveness = (guessedTypeRelations,typeToBeat) =>{
let multiplier = 1; // Initialize multiplier to 1
filt = (t) => typeToBeat.includes(t.name)
let ne = guessedTypeRelations.no_damage_to.filter(filt).length;
let he = guessedTypeRelations.half_damage_to.filter(filt).length;
let se = guessedTypeRelations.double_damage_to.filter(filt).length;
if(ne > 0){
multiplier *= 0; // No damage
}
else if(he > 0){
multiplier *= 0.5*he; // Half damage
}
else if(se > 0){
multiplier *= 2*se; // Double damage
}
console.log(`Multiplier: ${multiplier}`)
return multiplier;
}
const resultHandling = (result) =>{
if(result>=2){
shiny_chance += 0.03
}
else{
shiny_chance =0.03
}
showResultText(result)
}
//Form Handling
const form = document.querySelector('form')
form.addEventListener('submit',async(event)=>{
event.preventDefault()
const searchBox = document.querySelector('#search-box')
console.log(searchBox.value.length)
if(searchBox.value.length >0){
const result = await guessHandling(searchBox.value.toLowerCase());
searchBox.value=""
}
})
//Restart button handling
const restartButton = document.querySelector('#restart')
restartButton.addEventListener('click',(event)=>{
typesToMatch = null
resetScreen()
setupNewCard()
})
let shiny_chance = 0.03