-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
204 lines (153 loc) · 6.01 KB
/
script.js
File metadata and controls
204 lines (153 loc) · 6.01 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
const text = document.getElementById(`advise`);
const button = document.getElementById(`startButton`);
let points = 0;
const title = document.getElementById(`title`);
let found = [];
button.addEventListener(`click`, () => {
start();
shuffle();
});
const cards = document.getElementsByClassName(`card`);
let lastClick = undefined;
let clicked = false;
let foundAI = false;
for (let i = 0; i < cards.length; i++) {
cards[i].addEventListener(`click`, e => {
let divs = document.getElementsByClassName(`card`);
divs = Array.from(divs)
if (divs.find(x => x.style.border)) {
for (let i = 0; i < 18; i++) {
if (divs[i].style.border) divs[i].style.border = ``;
}
}
if (lastClick == e.target) return getAdvise(`non puoi abbinare la stessa carta`);
if (e.target.style.background == `transparent`) return;
if (!e.target.id) {
getAdvise(`devi prima mescolare le carte`);
window.scrollTo(0, 0);
return;
}
e.target.style.transform = `rotateY(180deg)`;
setTimeout(() => {
e.target.style.transform = ``;
e.target.style.backgroundImage = `url('imgs/minigame/${e.target.id}.png')`;
}, 125);
setTimeout(() => {
if (lastClick == e.target) return getAdvise(`non puoi abbinare la stessa carta`);
if (clicked) {
if (lastClick == e.target) return getAdvise(`non puoi abbinare la stessa carta`);
if (lastClick.id == e.target.id) {
points += 20;
getAdvise(`abbinamento corretto`);
lastClick.style.background = `transparent`;
e.target.style.background = `transparent`;
lastClick.style.boxShadow = `0px 0px 0px`;
e.target.style.boxShadow = `0px 0px 0px`;
lastClick.style.cursor = `default`;
e.target.style.cursor = `default`;
found.push(lastClick.id);
found.push(e.target.id);
if (lastClick.id == 4 && e.target.id == 4 && found.length < 16) {
popup(`Le carte sono state mescolate!`);
shuffle();
}
if (lastClick.id == 2 && e.target.id == 2) {
popup(`Alla prossima carta riceverai un aiuto ;)`)
foundAI = true;
}
if (found.length >= 18) {
title.style.fontSize = `50px`;
title.textContent = `HAI ${points > 0 ? `VINTO` : `PERSO`} CON ${points} PUNTI!`;
window.scrollTo(0, 0);
}
} else {
points -= 5;
getAdvise(`abbinamento sbagliato`);
lastClick.style.backgroundImage = `url('imgs/minigame/behind.png')`;
e.target.style.backgroundImage = `url('imgs/minigame/behind.png')`;
}
lastClick = undefined;
clicked = false;
return;
}
if (!clicked) {
lastClick = e.target;
clicked = true;
if (foundAI) {
let id = e.target.id;
let divs = document.getElementsByClassName(`card`);
divs = Array.from(divs);
const doneDivs = [];
divs.find(x => x.id == id && x != e.target).style.border = `gold ridge 5px`;
for (let i = 0; i < divs.length; i++) {
if (divs[i].style.background == `transparent` || divs[i].id == id) continue;
if (doneDivs.length == 2) break;
if (Math.round(Math.random()) == 1) {
divs[i].style.border = `gold ridge 5px`;
doneDivs.push(divs[i]);
}
}
foundAI = false;
}
}
}, 700);
});
}
setInterval(() => {
if (found.length < 18) title.textContent = `PUNTI: ${points}`;
}, 1000);
function getAdvise(content) {
text.textContent = content;
setTimeout(() => {
text.textContent = ` `;
}, 2000);
}
function shuffle() {
const divs = document.getElementsByClassName(`card`);
const doneDivs = [];
let i = 1;
let increment = true;
do {
increment = true;
for (let j = 0; j < 2; j++) {
if (found.includes(i.toString())) {
i++;
increment = false;
break;
}
let divToUse = undefined;
do {
divToUse = divs[Math.floor(Math.random() * divs.length)];
} while (doneDivs.includes(divToUse) || divToUse.style.background == `transparent`);
divToUse.id = i;
doneDivs.push(divToUse);
}
if (increment) i++;
} while (doneDivs.length != 18 - found.length);
getAdvise(`le carte sono state mescolate`);
}
function start() {
found = [];
points = 0;
lastClick = undefined;
clicked = false;
title.style.fontSize = ``;
const divs = document.getElementsByClassName(`card`);
for (let i = 0; i < divs.length; i++) {
divs[i].style.backgroundImage = `url('imgs/minigame/behind.png')`;
divs[i].style.boxShadow = `10px 10px 10px black`;
divs[i].style.cursor = `pointer`;
}
}
function popup(description) {
const box = document.getElementById(`popup`);
console.log(box)
const descriptionText = document.getElementById(`description`);
box.style.visibility = `visible`;
box.style.opacity = `1`;
descriptionText.textContent = description;
}
document.getElementById(`close`).addEventListener(`click`, () => {
document.getElementById(`popup`).style.visibility = `hidden`;
document.getElementById(`popup`).style.opacity = `0`;
});