-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
277 lines (198 loc) · 7.13 KB
/
Copy pathmain.js
File metadata and controls
277 lines (198 loc) · 7.13 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
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
// Initializing the buttons
const spinButton = document.getElementById("start");
const stopButton = document.getElementById("stop");
const resetButton = document.getElementById("reset");
const values = document.getElementById("values");
const topScoreList = document.getElementById("top");
const latestScoreList = document.getElementById("latestScoreList");
// initializes the slots
const slot1 = document.getElementById("box1");
const slot2 = document.getElementById("box2");
const slot3 = document.getElementById("box3");
// initializes the fruits and their values, default values of emojis are put in incase the api breaks for whatever reason.
let fruits = ["🍋", "🍊", "🍒", "🍇", "7"];
let fruitValue = [50, 100, 200, 300, 500];
// Lemon 🍋
// Orange 🍊
// Cherry 🍒
// Grape 🍇
// Seven ️
// Moneybag 💰
let slotPos = [0, 0, 0];
let stopped = ["", "", ""];
let spinCount = 0;
let latestScore = "";
// Checks to see if there are existing values for top and latest leaderboards, if not makes them blank
let leaderTop = JSON.parse(localStorage.getItem("leaderTop")) || [0, 0, 0, 0, 0];
let leaderLatest = JSON.parse(localStorage.getItem("leaderLatest")) || [0, 0, 0, 0, 0];
// calls the berries from the api
getBerry("sitrus-berry", 0);
getBerry("pomeg-berry", 1);
getBerry("cheri-berry", 2);
getBerry("bluk-berry", 3);
getBerry("fire-gem", 4);
// displays the leaderboards and the berry values
valuesList();
displayTop();
displayLatest();
stopButton.disabled = true;
// starts the spinning
spinButton.addEventListener("click", () => {
// any button enabling/disabling is done to make sure the game doesnt break
spinButton.disabled = true;
stopButton.disabled = false;
// uses setinterval to step up the stop function
stopped[0] = setInterval(spin, 100, slot1, 0);
stopped[1] = setInterval(spin, 75, slot2, 1);
stopped[2] = setInterval(spin, 50, slot3, 2);
spinCount = 0;
})
stopButton.addEventListener("click", () => {
// spin count tracks which slot you are on, once you stop all the slots the leaderboards are updated
stop(spinCount);
spinCount++;
if (spinCount >= 3) {
stopButton.disabled = true;
updateLatest();
checkTop();
}
})
// calls the reset function
resetButton.addEventListener("click", reset);
function spin(slot, id) {
// increments the slot value by 1
slotPos[id] += 1;
// wraps the slot back around to the first value if it goes over
if (slotPos[id] == fruits.length) {
slotPos[id] = 0
}
// and displays the slot
slot.innerHTML = fruits[slotPos[id]];
}
function stop(id) {
clearInterval(stopped[id]);
// clear interval stops the spinning
}
function reset() {
// stops all the slots and resets their value
for (let index = 0; index < stopped.length; index++) {
stop(index);
slotPos[index] = 0;
}
// clears out the slot box
slot1.innerHTML = "";
slot2.innerHTML = "";
slot3.innerHTML = "";
// resets spin count which tracks which slot your are on
spinCount = 0;
spinButton.disabled = false;
stopButton.disabled = true;
}
function score() {
let points = 0;
let bonus = 0;
console.log(`
slot 1 = ${slotPos[0]}
slot 2 = ${slotPos[1]}
slot 3 = ${slotPos[2]}
`)
// goes through the slots and adds the fruit value based on the slotposition
for (let index = 0; index < slotPos.length; index++) {
points += fruitValue[slotPos[index]];
}
// checks to see if slot 2 or 3 match with slot 1
if (slotPos[0] == slotPos[1] || slotPos[0] == slotPos[2]) {
bonus += 1;
}
// checks to see if slot 2 matches with slot 3
if (slotPos[1] == slotPos[2]) {
bonus += 1;
// console.log(`bonus ${bonus}`)
}
// if either of the above if statements is true, that means there is a pair so the bonus value of 1 is applied (1.5x)
// if both of the above if statements are true, that means all 3 slots are the same so the bonus value of 2 is applied (2x)
if (bonus == 1) {
points = 1.5 * points;
}
else if (bonus == 2) {
points = 2 * points;
}
console.log(points);
return points;
}
function updateLatest() {
// pulls the latest score from the score calculator
latestScore = score();
// pushes the latest score to the leaderboard array
leaderLatest.push(latestScore);
// checks if the array is longer than 5 and shortens it
if (leaderLatest.length > 5) {
leaderLatest.shift();
}
// saves leaderboard to local storage
localStorage.setItem("leaderLatest", JSON.stringify(leaderLatest));
// updates display
displayLatest();
}
function checkTop(score) {
// addes the number to the top score array
leaderTop.push(Number(latestScore));
// sorts using a function which checks which number is bigger
leaderTop.sort(function (a, b) { return a - b });
// if the array is longer than 5 it cuts the first item which should be the smallest number
if (leaderTop.length > 5) {
leaderTop.shift();
}
// saves to local storage and displays on the page
localStorage.setItem("leaderTop", JSON.stringify(leaderTop));
displayTop();
}
// Updates the Top Score leaderboard
function displayTop() {
// clears innerhtml
topScoreList.innerHTML = "";
// goes from highest to lowest and creates a list item for each score
for (let index = leaderTop.length - 1; index >= 0; index--) {
topScoreList.innerHTML += `<li> ${leaderTop[index]} </li>`;
}
}
// Updates the latest score leaderboard
function displayLatest() {
latestScoreList.innerHTML = "";
// goes from most recent to the oldest and creates a list item for each
for (let index = leaderLatest.length - 1; index >= 0; index--) {
latestScoreList.innerHTML += `<li> ${leaderLatest[index]} </li>`;
}
}
// In the rules section, displays the fruits next to their score values
function valuesList() {
values.innerHTML = "";
for (let index = fruits.length - 1; index >= 0; index--) {
values.innerHTML += `<li>${fruits[index]} = ${fruitValue[index]} </li>`;
}
}
// Gets the berry sprite from the pokeapi and adds it to the fruits list
async function getBerry(id, index) {
let response = await fetch(`https://pokeapi.co/api/v2/item/${id}/`);
let berry = await response.json();
// console.log(berry.name);
fruits[index] = ` <img src="${berry.sprites.default}"> `;
valuesList();
}
// async function getPokemon(id) {
// let response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}/`);
// pokemon = await response.json()
// // testers.innerHTML = `<img src="${pokemon.sprites.front_default}" alt="">`;
// }
// getPokemon(500);
// function createPokemonCard(id) {
// testOutput.innerHTML = "";
// const card = document.createElement("div");
// const pickButton = document.createElement("button");
// pickButton.innerText = "Pick!"
// pickButton.addEventListener("click", () => {
// console.log("test");
// })
// card.appendChild(pickButton);
// testOutput.appendChild(card);
// }