-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.js
146 lines (119 loc) · 4.64 KB
/
app2.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
//! Selectors
const selectionArticle = document.querySelector(".selection");
//Secilen elemanlarin tasiyicilari
const yourChoiceDiv = document.getElementById("your-choice");
const pcChoiceDiv = document.getElementById("pc-choice");
//Message
const messagePar = document.querySelector(".message");
//Score
const scoreCardSection = document.querySelector(".score-card");
const pcScoreSpan = document.querySelector("#pc-score");
const yourScoreSpan = document.querySelector("#your-score");
const domTopScore = document.querySelector("#top-score")
//Modal
const modalCardSection = document.querySelector(".modal-card")
const finalMessagePar = document.querySelector("#final-message")
const playAgainButton = document.querySelector("#play-again")
//!Variables
let userSelectImg = document.createElement("img");
let pcSelectImg = document.createElement("img");
let pcArr;
let pcRandom;
let imgRandom;
// Colors
const YELLOW = "#ffc538";
const RED = "#fb778b";
const GREEN = "#5ab7ac";
//Image
const bgImg = document.querySelector("#bgImg");
//!Event Listeners
domTopScore.textContent = localStorage.getItem("topScoreText") || null;
selectionArticle.addEventListener("click", (e) => {
console.log(e.target.id);
if (e.target.id) {
userSelectImg.src = `./assets/${e.target.id}.png`;
userSelectImg.alt = e.target.id;
yourChoiceDiv.appendChild(userSelectImg);
createPcSelection();
}
});
const imgChange = () => {
imgRandom = Math.trunc(Math.random() * 1000);
bgImg.src = `https://picsum.photos/id/${imgRandom}/1200/800`;
};
imgChange();
playAgainButton.addEventListener("click",() => {
modalCardSection.style.display = "none";
imgChange();
window.location.reload()
})
//!Functions
const createPcSelection = () => {
pcArr = ["rock", "paper", "scissor"];
pcRandom = pcArr[Math.floor(Math.random() * 3)];
pcSelectImg.src = `./assets/${pcRandom}.png`;
pcSelectImg.alt = pcRandom;
pcChoiceDiv.appendChild(pcSelectImg);
calculteResult();
};
//Its a draw"
const calculteResult = () => {
if (userSelectImg.alt === pcRandom) {
result(1);
}
else {
result(userSelectImg.alt === "rock" ? (pcRandom === "paper" ? 2 : 3) : userSelectImg.alt === "scissor" ? (pcRandom === "rock" ? 2 : 3): userSelectImg.alt === "paper" ? (pcRandom === "scissor" ? 2 : 3):null);
}
};
const result = (x) => {
const mes = {t1: "Its a draw", s1: YELLOW, t2: "You lost", s2: RED, t3: "You win", s3: GREEN,};
messagePar.textContent = mes[`t${x}`];
scoreCardSection.style.color = mes[`s${x}`];
messagePar.style.backgroundColor = mes[`s${x}`];
x === 2 ? pcScoreSpan.textContent++ : x === 3 ? yourScoreSpan.textContent++ : null
if (pcScoreSpan.textContent === "10" || yourScoreSpan.textContent === "10") {
// domTopScore.textContent = `${yourScoreSpan.textContent}:${pcScoreSpan.textContent}`
openModal()
topScore()
}
};
const topScore = () => {
let topScoreText = `${yourScoreSpan.textContent}:${pcScoreSpan.textContent}`;
let storedScore = localStorage.getItem("topScoreText");
if (!storedScore || compareScores(topScoreText, storedScore)) {
localStorage.setItem("topScoreText", topScoreText);
domTopScore.textContent = topScoreText;
}
};
const compareScores = (newScore, storedScore) => {
const [newYourScore, newPcScore] = newScore.split(":");
const [storedYourScore, storedPcScore] = storedScore.split(":");
return (
parseInt(newYourScore) > parseInt(storedYourScore) ||
parseInt(newPcScore) < parseInt(storedPcScore)
);
};
// const topScore = () => {
// let topScoreText = domTopScore.textContent;
// localStorage.setItem("topScoreText",topScoreText)
// let localScore = localStorage.getItem("topScoreText")
// console.log(localScore);
// }
const openModal = () => {
modalCardSection.classList.add("show")
if (yourScoreSpan.textContent === "10") {
finalMessagePar.textContent = "💃 You win"
document.querySelector(".modal").style.backgroundColor = GREEN
playAgainButton.style.color = GREEN
}
else {
finalMessagePar.textContent = "☹️ You Lost"
document.querySelector(".modal").style.backgroundColor = RED
playAgainButton.style.color = RED
}
}
// const imgChange = () => {
// imgRandom = Math.trunc(Math.random()*1000)
// bgImg.src = `https://picsum.photos/id/${imgRandom}/1200/800`
// console.log(bgImg.src);
// }