-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (127 loc) · 4.42 KB
/
index.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
//Variables
const gameChoicesArray = ["Paper", "Scissors", "Rock"];
const rulesElement = document.querySelector(".container__rules");
const scoreNumberElement = document.querySelector(".header__scoreNumber");
const modalElement = document.querySelector(".modal");
const modalOverlayElement = document.querySelector(".modal__overlay");
const closeElement = document.querySelector(".modal__closeIcon");
const gameContentElement = document.querySelector(".gameContent");
const gameChoiceElements = document.querySelectorAll(
".gameContent__gameChoice"
);
const gameChoiceImageElement = document.querySelector(
".gameContent__gameChoiceImage"
);
const gameChoiceComputerElement = document.querySelector(
".gameContent__gameChoice--isComputer"
);
const countdownTextElement = document.querySelector(
".gameContent__countdownText"
);
const resultButtonElement = document.querySelector(
".gameContent__resultButton"
);
const resultTextElement = document.querySelector(".gameContent__resultText");
const player = document.querySelector("lottie-player"); //animation
let countdown = 4;
let randomResult;
const storedScore = localStorage.getItem("gameScore");
if (storedScore !== null) {
scoreNumberElement.textContent = storedScore;
}
//FUNCTIONS
const rulesModalEvent = () => modalElement.classList.toggle("modal--isActive");
const getRandomNumber = () => Math.floor(Math.random() * 3);
const showResult = (userChoice, computerChoice) => {
const score = parseInt(scoreNumberElement.textContent);
if (userChoice === computerChoice) {
//draw condition
resultTextElement.textContent = "Draw";
} else if (
(userChoice === gameChoicesArray[0] &&
computerChoice === gameChoicesArray[1]) ||
(userChoice === gameChoicesArray[1] &&
computerChoice === gameChoicesArray[2]) ||
(userChoice === gameChoicesArray[2] &&
computerChoice === gameChoicesArray[0])
) {
//lose conditions
resultTextElement.textContent = "You lose";
gameContentElement.classList.add("gameContent--isLost");
if (score > 0) {
scoreNumberElement.textContent = score - 1;
saveScoreToLocalStorage(score - 1);
}
} else {
//win condition
resultTextElement.textContent = "You win";
setTimeout(
() =>
player.load(
"https://assets10.lottiefiles.com/packages/lf20_aEFaHc.json"
),
900
);
scoreNumberElement.textContent = score + 1;
saveScoreToLocalStorage(score + 1);
}
};
const startCountdown = () => {
countdownTextElement.textContent = countdown - 1;
countdown -= 1;
if (countdown) {
setTimeout(() => startCountdown(), 600);
} else {
const selectedGameChoiceElement = document.querySelector(
".gameContent__gameChoice--isActive"
);
const selectedChoice = selectedGameChoiceElement.dataset.choice;
randomResult = gameChoicesArray[getRandomNumber()];
showResult(selectedChoice, randomResult);
setTimeout(
() => gameContentElement.classList.add(`gameContent--revealResult`),
500
);
countdownTextElement.textContent = "";
gameChoiceComputerElement.classList.add(
`gameContent__gameChoice--is${randomResult}`
);
gameChoiceImageElement.setAttribute(
"src",
`./assets/icon-${randomResult.toLowerCase()}.svg`
);
countdown = 4;
}
};
const gameChoiceEvent = (event) => {
gameContentElement.classList.add("gameContent--isActive");
event.target.classList.add("gameContent__gameChoice--isActive");
startCountdown();
};
const playAgainEvent = () => {
const activeChoiceElement = document.querySelector(
".gameContent__gameChoice--isActive"
);
gameContentElement.classList.remove(`gameContent--revealResult`);
gameChoiceComputerElement.classList.remove(
`gameContent__gameChoice--is${randomResult}`
);
gameChoiceImageElement.setAttribute("src", "");
gameContentElement.classList.remove(
"gameContent--isActive",
"gameContent--isLost"
);
activeChoiceElement.classList.remove("gameContent__gameChoice--isActive");
};
const saveScoreToLocalStorage = (score) => {
localStorage.setItem("gameScore", score);
};
//MODAL EVENTS
rulesElement.addEventListener("click", rulesModalEvent);
closeElement.addEventListener("click", rulesModalEvent);
modalOverlayElement.addEventListener("click", rulesModalEvent);
//GAME CHOICES EVENTS
gameChoiceElements.forEach((item) =>
item.addEventListener("click", gameChoiceEvent)
);
resultButtonElement.addEventListener("click", playAgainEvent);