forked from N-Shar-ma/FOSS-Weekend-Emoji-Card-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
301 lines (262 loc) · 7.2 KB
/
script.js
File metadata and controls
301 lines (262 loc) · 7.2 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { getInstructions, getShuffledDeck } from "./decks.js"
let cardObjectsDeck = []
let currentCardObjectsDeck = [];
let wrongCardObjectsDeck = [];
const landingPage = document.getElementById("landing-page");
const instructions = document.getElementById("instructions");
const category = document.getElementById("category");
const difficultyMode = document.getElementById("difficulty-mode");
const gameArea = document.getElementById("game-area");
const playButton = document.getElementById("play-btn");
const nameShower = document.getElementById("name-shower");
const nameToShow = document.querySelector("#name-shower span");
const cardDropPosition = document.getElementById("card-drop-position");
const source = document.getElementById("source");
const destination = document.getElementById("destination");
const score = document.getElementById("score");
const hint = document.getElementById("hint");
const maxCardsPlayable = 6;
const maxCardsWrong = 3;
let deckLength;
let matcher;
let showName;
let scoreCount = 0;
let hintCount = 0;
chooseDeck()
category.addEventListener("change", chooseDeck)
playButton.addEventListener("click", setUpGame)
function setUpGame()
{
setDifficultyMode();
landingPage.remove();
gameArea.style.display = "flex";
addCardsOneByOne(300)
updateCardDropPosition();
setUpCardDropPositionEventListeners();
}
function chooseDeck ()
{
cardObjectsDeck = getShuffledDeck(category.value);
deckLength = cardObjectsDeck.length
instructions.innerText = getInstructions(category.value)
}
function setDifficultyMode()
{
const mode = difficultyMode.value;
switch(mode)
{
case "Easy":
matcher = "name";
showName = false;
break;
case "Challenging":
matcher = "matchValue";
showName = true;
break;
}
}
function addCardsOneByOne (timeInterval) {
addCard();
let i = 1;
let repeater = setInterval(()=>{
if(i === maxCardsPlayable)
clearInterval(repeater);
else
{
addCard();
i++;
}
}, timeInterval);
}
function addCard()
{
const cardDocumentFragment = cardTemplate.content.cloneNode(true);
const card = cardDocumentFragment.querySelector(".card");
const cardObject = cardObjectsDeck.pop();
currentCardObjectsDeck.push(cardObject);
fillInCardDetails(card, cardObject);
card.addEventListener("click", flip);
const draggable = cardDocumentFragment.querySelector("[draggable='true']");
setUpCardDragEventListeners(draggable);
source.append(cardDocumentFragment);
}
function fillInCardDetails(card, cardObject)
{
const primary = card.querySelector(".primary");
const secondary = card.querySelector(".secondary");
const hint = card.querySelector(".hint");
primary.innerText = cardObject.content.primary;
secondary.innerText = cardObject.content.secondary;
if(!cardObject.content.primary) primary.remove()
if(!cardObject.content.secondary) secondary.remove()
hint.innerText = cardObject.hint;
card.dataset.name = cardObject.name;
}
function setUpCardDragEventListeners(draggable)
{
draggable.addEventListener("dragstart", (e)=>{
draggable.id = "dragging";
setUpDragStart(e);
});
draggable.addEventListener("dragend", ()=>{
draggable.removeAttribute("id");
});
}
function flip()
{
const cardObject = getCorrespondingCardObject(this.dataset.name);
if(cardObject.seenHint || wrongCardObjectsDeck.includes(cardObject))
{
this.classList.toggle("flipped");
}
else if(!cardObject.seenHint)
{
updateHintElement(++hintCount);
cardObject.seenHint = true;
this.classList.toggle("flipped");
}
}
function getCorrespondingCardObject(name)
{
return currentCardObjectsDeck.find(cardObject=>(cardObject.name === name)) || wrongCardObjectsDeck.find(cardObject=>(cardObject.name === name));
}
function setUpDragStart(e)
{
e.dataTransfer.effectAllowed = "move";
setGhostImage(e);
}
function setGhostImage(e)
{
const cardToGhost = document.getElementById("dragging").getElementsByClassName("card")[0];
let cardFace;
if(cardToGhost.classList.contains("flipped"))
cardFace = cardToGhost.getElementsByClassName("back-face")[0];
else
cardFace = cardToGhost.getElementsByClassName("front-face")[0];
e.dataTransfer.setDragImage(cardFace, 90, 120);
}
function setUpCardDropPositionEventListeners()
{
cardDropPosition.addEventListener("dragenter", (e)=>{
cardDropPosition.classList.add("dragged-over");
e.dataTransfer.dropEffect = "move";
});
cardDropPosition.addEventListener("dragover", (e)=>{
e.preventDefault();
});
cardDropPosition.addEventListener("dragleave", ()=>{
cardDropPosition.classList.remove("dragged-over");
});
cardDropPosition.addEventListener("drop", moveCard);
}
function moveCard()
{
const cardToMove = document.getElementById("dragging");
destination.insertBefore(cardToMove, cardDropPosition);
cardToMove.removeAttribute("draggable");
handleCardInDestination(cardToMove);
removeFromCurrentDeck(cardToMove.querySelector(".card"));
cardDropPosition.style.display = "none";
if(cardObjectsDeck[0])
setTimeout(addCard, 100);
if(currentCardObjectsDeck[0])
updateCardDropPosition();
}
function handleCardInDestination(card)
{
const innerCard = card.querySelector(".card");
if(showName)
{
nameToShow.innerText = innerCard.dataset.name;
nameShower.classList.add("show");
setTimeout(()=>{nameShower.classList.remove("show");}, 3200);
}
if(isCorrectMove(innerCard))
{
updateScoreElement(++scoreCount);
card.classList.add("correct");
setTimeout(()=>{card.remove();}, 2500);
setTimeout(checkWin, 2500);
}
else
{
card.classList.add("wrong");
wrongCardObjectsDeck.push(getCorrespondingCardObject(innerCard.dataset.name));
checkLoss();
}
}
function isCorrectMove(card) // temp body !!!
{
const cardObject = getCorrespondingCardObject(card.dataset.name);
if(cardObject[matcher] === cardDropPosition.innerText)
return true;
return false;
}
function updateScoreElement(scoreCount)
{
score.innerText = scoreCount;
}
function updateHintElement(hintCount)
{
hint.innerText = hintCount;
}
function checkWin()
{
if(currentCardObjectsDeck.length === 0)
{
cardDropPosition.remove();
showGameOver(true);
}
}
function checkLoss()
{
if(isDestinationFull())
{
cardDropPosition.remove();
setTimeout(()=>{showGameOver(false)}, 1800);
}
}
function isDestinationFull()
{
if(destination.getElementsByClassName("card-wrapper")[maxCardsWrong-1])
{
return true;
}
return false;
}
function showGameOver(won)
{
const msg = won? `🏆 You won! 🏆`: `😭 You lost! 😭`;
if(confirm(`${msg}
Your score is ${scoreCount} and you took ${hintCount} hint${hintCount===1?"":"s"}.
${won? getAchievement() : ""}Select ok to play again!`))
{
location.reload();
}
}
function getAchievement()
{
if(scoreCount===deckLength && hintCount===0)
return `You played a PERFECT GAME!
`;
else
return``;
}
function updateCardDropPosition()
{
setTimeout(()=>{
cardDropPosition.style.display = "flex";
cardDropPosition.classList.remove("dragged-over");
cardDropPosition.querySelector("#matcher").innerText = getRandomMatcher();
}, 2500);
}
function getRandomMatcher()
{
const i = Math.floor(Math.random() * currentCardObjectsDeck.length);
return currentCardObjectsDeck[i][matcher];
}
function removeFromCurrentDeck(card)
{
const index = currentCardObjectsDeck.findIndex(cardObject=>card.dataset.name === cardObject.name);
currentCardObjectsDeck.splice(index, 1);
}