forked from calebrob6/countries-of-the-world-quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
367 lines (318 loc) · 11 KB
/
index.html
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="description" content="Countries of the World Quiz" />
<meta name="author" content="Caleb Robinson" />
<title>Countries of the World Quiz</title>
<!-- Core CSS -->
<link href="css/leaflet.css" rel="stylesheet" />
<style type="text/css">
html,
body {
height: 100%;
height: 100%;
}
body {
padding: 0;
margin: 0;
}
#map {
width: 100%;
height: 100%;
min-height: 100%;
}
.logo-area {
background-color: #19715e;
color: white;
align-items: center;
display: block;
font-family: "Segoe UI Web (West European)", Segoe UI, -apple-system,
BlinkMacSystemFont, Roboto, Helvetica Neue, sans-serif;
}
.logo-content {
padding-right: 2rem;
padding-left: 2rem;
padding-top: 1rem;
padding-bottom: 1rem;
}
.logo-header {
padding-right: 2rem;
padding-left: 2rem;
padding-top: 1rem;
padding-bottom: 0.5rem;
}
.logo-heading {
font-size: 1.7em;
letter-spacing: 0.1rem;
font-weight: 400;
line-height: 1.5;
color: white;
}
.logo-link {
font-size: 100%;
font-weight: 400;
color: white;
padding-right: 1.5rem;
padding-left: 1.5rem;
}
.leaflet-container .logo-area a {
color: rgb(219, 219, 219);
}
.leaflet-container .logo-area a:hover {
color: rgb(255, 255, 255);
}
.game-area {
align-items: center;
display: block;
}
.game-row {
padding-bottom: 0.5rem;
}
#game-timer {
margin-left: 2rem;
font-size: 140%;
font-weight: 600;
width: 100px;
text-align: center;
border: 1px solid black;
padding: 0.25rem;
}
#game-start:hover {
background-color: rgb(235, 235, 235);
}
#game-stop:hover {
background-color: rgb(235, 235, 235);
}
.game-label {
font-size: 120%;
}
.game-bad-label {
font-size: 140%;
color: red;
}
.form-control {
padding: 0.375rem 0.75rem;
font-size: 0.8rem;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
#game-input {
display: inline-block;
width: 15rem;
}
#game-score {
margin-left: 2rem;
font-size: 140%;
font-weight: 600;
}
</style>
</head>
<body>
<div id="map"></div>
<!-- Core JavaScript
================================================== -->
<script src="js/jquery-3.3.1.min.js"></script>
<!-- Leaflet JavaScript
================================================== -->
<script src="js/leaflet.js" type="text/javascript"></script>
<!-- Main code
================================================== -->
<script src="countries.js" type="text/javascript"></script>
<script src="https://unpkg.com/reconnecting-websocket@4.4.0/dist/reconnecting-websocket-iife.min.js"></script>
<script type="text/javascript">
const ws = new ReconnectingWebSocket("wss://data.elektron.art");
var START_CENTER = [20, -50]; // lat, lon
var START_ZOOM = 3;
var countryMarkers = [];
var countryLatLons = [];
var countryNames = [];
var foundCountries = Object();
var allCountries = Object();
var foundCount = 0;
var totalCount = countries.length;
var timeLeft = 15 * 60;
var running = false;
function updateScore() {
$("#game-score").text(foundCount + "/" + totalCount);
}
function doTimer() {
if (timeLeft > 0) {
timeLeft -= 1;
var minutes = Math.floor(timeLeft / 60);
var seconds = timeLeft % 60;
var pad = "00";
minutes = minutes.toString();
seconds = seconds.toString();
minutes = ("00" + minutes).substring(minutes.length);
seconds = ("00" + seconds).substring(seconds.length);
$("#game-timer").text(minutes + ":" + seconds);
setTimeout(doTimer, 1000);
} else {
if (running) {
running = false;
endGame();
}
}
}
function endGame() {
$("#game-timer").text("00:00");
for (var i = 0; i < totalCount; i++) {
if (!(i in foundCountries)) {
var name = countryNames[i];
countryMarkers[i]._icon.innerHTML =
'<span class="game-bad-label">' + name + "</span>";
}
}
}
$(document).ready(function () {
//----------------------------------------------------------------------
// Setup map layers
//----------------------------------------------------------------------
var CartoDB_VoyagerNoLabels = L.tileLayer(
"https://{s}.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}{r}.png",
{
attribution:
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="https://carto.com/attributions">CARTO</a>',
maxZoom: 15,
minZoom: 2,
}
);
var map = L.map("map", {
zoomControl: false,
center: START_CENTER,
zoom: START_ZOOM,
layers: [CartoDB_VoyagerNoLabels],
});
//---------------------------------------------------------------------
// Setup custom controls
//----------------------------------------------------------------------
var logoControl = $("<div class='leaflet-control logo-area'></div>");
var logoContent = $("<div class='logo-content'></div>");
logoContent.append(
"<div class='logo-container'><span class='logo-heading'> Countries of the World Quiz </span></div>"
);
logoContent.append(
"<div class='logo-container'>Based on the Sporcle <a href='https://www.sporcle.com/games/g/world'>Countries of the World Map Quiz</a>.</div>"
);
logoContent.append(
"<div class='logo-container'>Open source on <a href='https://github.com/calebrob6/countries-of-the-world-quiz'>github</a>.</div>"
);
logoContent.append(
"<div class='logo-container'>Made by <a href='http://calebrob.com'>Caleb Robinson</a>.</div>"
);
logoControl.append(logoContent);
$(".leaflet-top.leaflet-left").append(logoControl);
var gameControl = $("<div class='leaflet-control game-area'></div>");
var gameContent = $("<div class='game-content'></div>");
gameContent.append(
"<div class='game-row'><input type='button' class='form-control' value='Start' id='game-start'><input type='button' class='form-control' value='Stop' id='game-stop'><span id='game-timer'>15:00</span></div>"
);
gameContent.append(
"<div class='game-row'><input type='text' id='game-input' class='form-control' placeholder='Press start, enter country names, beat the timer!'><span id='game-score'>" +
foundCount +
"/" +
totalCount +
"</span></div>"
);
gameControl.append(gameContent);
$(".leaflet-top.leaflet-left").append(gameControl);
//----------------------------------------------------------------------
// Custom initialization of the map zoom controls so that we can
// position it where we want
//----------------------------------------------------------------------
L.control
.zoom({
position: "topleft",
})
.addTo(map);
//----------------------------------------------------------------------
// Create markers for all the countries and populate the country globals
//----------------------------------------------------------------------
for (var i = 0; i < totalCount; i++) {
var lat = countries[i][0];
var lon = countries[i][1];
var names = [];
for (var j = 2; j < countries[i].length; j++) {
names.push(countries[i][j]);
}
var marker = new L.Marker([lat, lon], {
icon: new L.DivIcon({
className: "my-div-icon",
html: '<span class="game-label">____</span>',
}),
});
marker.addTo(map);
countryNames.push(names[0]);
countryMarkers.push(marker);
countryLatLons.push([lat, lon]);
for (var j = 0; j < names.length; j++) {
allCountries[names[j].toLowerCase()] = i;
}
}
//----------------------------------------------------------------------
// Text listener (all game logic)
//----------------------------------------------------------------------
$("#game-input").keyup(function () {
if (running) {
var query = this.value.toLowerCase();
if (query in allCountries) {
var idx = allCountries[query];
if (!(idx in foundCountries)) {
var name = countryNames[idx];
var lat = countryLatLons[idx][0];
var lon = countryLatLons[idx][1];
countryMarkers[idx]._icon.innerHTML =
'<span class="game-label">' + name + "</span>";
foundCount += 1;
foundCountries[idx] = true;
map.flyTo([lat, lon], map.getZoom());
$(this).val("");
updateScore();
// Not sure this is the right place but doing it anyway
// Edit the message as needed
const message = {
channel: "countries",
type: "CHAT",
value: "Found " + name,
};
ws.send(JSON.stringify(message));
} else {
console.debug("Already found");
}
}
} else {
$(this).val("");
}
});
$("#game-start").click(function () {
$(this).prop("disabled", true);
$("#game-stop").prop("disabled", false);
$(this).css("background-color", "#2bf259");
$(this).val("Started");
running = true;
doTimer();
});
$("#game-stop").click(function () {
if (running) {
$(this).prop("disabled", true);
$(this).css("background-color", "#ad0800");
$(this).val("Stopped");
running = false;
timeLeft = 0;
endGame();
}
});
});
</script>
</body>
</html>