-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
213 lines (195 loc) · 6.1 KB
/
app.js
File metadata and controls
213 lines (195 loc) · 6.1 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
//List of pop-up screens AKA modals
//instructions pop-up screen
const modalInstructions = document.querySelector('.modal-instructions')
const openInstructions = document.querySelector('.open-instructions');
openInstructions.addEventListener('click', function() {
modalInstructions.showModal();
})
// 1. Watersheds
const modalWatersheds = document.querySelector('.modal-watersheds');
// 2. Water at certain hours
const modalCertainHours = document.querySelector('.modal-certainHours');
// 3. Check for plumbing leaks
const modalLeaks = document.querySelector('.modal-leaks');
// 4. Dedicated bottle
const modalDedicatedBottle = document.querySelector('.modal-dedicatedBottle');
// 5. Shorter showers
const modalShortShowers = document.querySelector('.modal-shortShowers');
// 6. Put a nozzle on garden hoses
const modalNozzle = document.querySelector('.modal-nozzle');
// 7. Don't let the faucet run too long
const modalFaucetOff = document.querySelector('.modal-faucetOff');
// 8. Never use the toilet to throw away trash
const modalToilet = document.querySelector('.modal-toilet');
///////////////////////////////////////////////////////////////////////////////
const game = document.getElementById('game');
let firstPick;
let isPaused = true;
let matches;
// list of images for the conservation tips
// name is essentially the ID to reference the image in the click card function below
// feel free to change tips or images as wanted on the cards
const waterConservationTips = [
{// 1.
image: "watershed.jpg",
name: 'watershed'
},
{// 2.
image: "grassNight.jpg",
name: 'Water certain hours'
},
{// 3.
image: "faucet.jpg",
name: 'Check for plumbing leaks'
},
{// 4.
image: "bottle.jpg",
name: 'dedicated bottle'
},
{// 5.
image: "shower.jpg",
name: 'Take short showers'
},
{// 6.
image: "hose.jpg",
name: 'Put a nozzle on your garden hose'
},
{// 7.
image: "toothBrush.jpg",
name: 'Dont let the Faucet run'
},
{// 8.
image: "toilet.jpg",
name: 'Never use the toilet as a waste basket'
},
]
const resetGame = async() => {
document.getElementById('win').style.display='none';
document.getElementById('game').style.display='grid';
game.innerHTML = '';
isPaused = true;
firstPick = null;
matches = 0;
setTimeout(async () => {
displayTips([...waterConservationTips, ...waterConservationTips]);
isPaused = false;
},200)
}
//randomizes tips in array and turns them into div elements
const displayTips = (tip) => {
tip.sort(_ => Math.random() - 0.5);
const tipHTML = tip.map(tip => {
return `
<div class="card" onclick="clickCard(event)" data-tipname="${tip.name}">
<div class="front ">
</div>
<div class="back rotated">
<img class="tipImage" src="${tip.image}" alt="${tip.name}" />
<!-- <h2>${tip.name}</h2> -->
</div>
</div>
`}).join('');
game.innerHTML = tipHTML;
}
const clickCard = (e) => {
const tipCard = e.currentTarget;
const [front, back] = getFrontAndBackFromCard(tipCard)
if(front.classList.contains("rotated") || isPaused) {
return;
}
isPaused = true;
rotateElements([front, back]);
if(!firstPick){
firstPick = tipCard;
isPaused = false;
}
else {
const secondTipName = tipCard.dataset.tipname;
const firstTipName = firstPick.dataset.tipname;
if(firstTipName !== secondTipName) {
const [firstFront, firstBack] = getFrontAndBackFromCard(firstPick);
setTimeout(() => {
rotateElements([front, back, firstFront, firstBack]);
firstPick = null;
isPaused = false;
}, 500)
}else {
//list of the water conservation tips identified by their id (name)
switch(firstPick.dataset.tipname){
case 'watershed':
modalWatersheds.showModal();
break;
case 'Water certain hours':
modalCertainHours.showModal();
break;
case 'Check for plumbing leaks':
modalLeaks.showModal();
break;
case 'dedicated bottle':
modalDedicatedBottle.showModal();
break;
case 'Take short showers':
modalShortShowers.showModal();
break;
case 'Put a nozzle on your garden hose':
modalNozzle.showModal();
break;
case 'Dont let the Faucet run':
modalFaucetOff.showModal();
break;
case 'Never use the toilet as a waste basket':
modalToilet.showModal();
break;
}
matches++;
//When the game is won, the board is removed and the winning screen is displayed
if(matches === 8) {
document.getElementById('game').style.display='none';
document.getElementById('win').style.display='block';
}
firstPick = null;
isPaused = false;
}
}
}
const getFrontAndBackFromCard = (card) => {
const front = card.querySelector(".front");
const back = card.querySelector(".back");
return [front, back]
}
const rotateElements = (elements) => {
if(typeof elements !== 'object' || !elements.length) return;
elements.forEach(element => element.classList.toggle('rotated'));
}
resetGame();
// This section is the functionality for the close buttons.
//This is a list of the buttons matched up with the modals
const modal = document.querySelectorAll('.modal');
const closeModal = document.querySelectorAll('.close-button');
closeModal[0].addEventListener('click', function() {
modal[0].close();
})
closeModal[1].addEventListener('click', function() {
modal[1].close();
})
closeModal[2].addEventListener('click', function() {
modal[2].close();
})
closeModal[3].addEventListener('click', function() {
modal[3].close();
})
closeModal[4].addEventListener('click', function() {
modal[4].close();
})
closeModal[5].addEventListener('click', function() {
modal[5].close();
})
closeModal[6].addEventListener('click', function() {
modal[6].close();
})
closeModal[7].addEventListener('click', function() {
modal[7].close();
})
closeModal[8].addEventListener('click', function() {
modal[8].close();
})