-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
133 lines (120 loc) · 3.57 KB
/
scripts.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
// query selectors
const rock = document.querySelector("#rock");
const paper = document.querySelector("#paper");
const scissor = document.querySelector("#scissor");
const userScore = document.querySelector("#user-score");
const botScore = document.querySelector("#bot-score");
const userScoreFinal = document.querySelector("#user-score-final");
const botScoreFinal = document.querySelector("#bot-score-final");
const userImage = document.querySelector("#user-image");
const botImage = document.querySelector("#bot-image");
const userWinnerTag = document.querySelector("#winner-user");
const botWinnerTag = document.querySelector("#winner-bot");
const next = document.querySelector("#next");
const reset = document.querySelector("#reset");
const message = document.querySelector("#message");
//global variables
const scores = {
user: 0,
bot: 0,
};
let currentPage = 1;
let round = 1;
//fetch bot move from API
const fetchResult = async () => {
try {
const response = await fetch("https://random.darkjoy.ca/random/GetPick");
return await response.text();
} catch (error) {
console.log("Error:", error);
}
};
window.addEventListener("DOMContentLoaded", () => {
fetchResult().then((data) => {
botMove = data.toLowerCase();
});
rock.addEventListener("click", onButtonClick);
paper.addEventListener("click", onButtonClick);
scissor.addEventListener("click", onButtonClick);
next.addEventListener("click", onNextRound);
reset.addEventListener("click", onReset);
});
const navigateToPage = (num) => {
const nextPage = document.querySelector(`#page-${num}`);
nextPage.style.display = "flex";
const current = document.querySelector(`#page-${currentPage}`);
current.style.display = "none";
currentPage = num;
};
//button callback function
const onButtonClick = (e) => {
const userMove = e.target.id.toLowerCase();
userImage.src = `./assets/images/${userMove}.png`;
botImage.src = `./assets/images/${botMove}.png`;
const winner = getWinner(userMove, botMove);
updateScore(winner);
updateWinnerTag(winner);
round++;
if (round > 3) {
renderMessage();
navigateToPage(3);
} else {
navigateToPage(2);
}
};
const updateWinnerTag = (winner) => {
userWinnerTag.style.opacity = 0;
botWinnerTag.style.opacity = 0;
if (winner) {
document.querySelector(`#winner-${winner}`).style.opacity = 1;
}
};
//winning conditions
const getWinner = (userMove, botMove) => {
if (userMove === botMove) {
return null;
} else if (botMove === "rock") {
return userMove === "scissor" ? "bot" : "user";
} else if (botMove === "paper") {
return userMove === "rock" ? "bot" : "user";
} else {
return userMove === "paper" ? "bot" : "user";
}
};
const updateScore = (winner) => {
if (winner === "user") {
scores.user++;
userScore.innerText = scores.user;
userScoreFinal.innerText = scores.user;
} else if (winner === "bot") {
scores.bot++;
botScore.innerText = scores.bot;
botScoreFinal.innerText = scores.bot;
}
};
const renderMessage = () => {
if (scores.user > scores.bot) {
message.innerText = "Congrats! You win.";
} else if (scores.user < scores.bot) {
message.innerText = "Sorry. You lose.";
} else {
message.innerText = "Tie. Please try again.";
}
};
const onNextRound = () => {
fetchResult().then((data) => {
botMove = data.toLowerCase();
});
navigateToPage(1);
};
const onReset = () => {
scores.user = 0;
scores.bot = 0;
userScore.innerText = 0;
botScore.innerText = 0;
userScoreFinal.innerText = 0;
botScoreFinal.innerText = 0;
round = 1;
navigateToPage(1);
};
//button actions/events