-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
367 lines (332 loc) · 11.1 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>
<!--
********************************************************************
* Copyright (c) 2018, Douglas Campbell
* Licensed under the MIT License
********************************************************************
-->
<html lang="en" class="viewer">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Image match game</title>
<link rel="shortcut icon" href="/image-match-game/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Assistant">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<style>
/* after specific fonts, revert to Bootstrap defaults */
body {
background-color: #DCD0C0;
font-family: 'Assistant', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
h2, h3, h4, h5, h6 {
color: DarkOliveGreen;
font-weight: bold;
}
/* add back link underlining where Bootstrap removes it */
a:link,
a:visited {
color: #444422;
text-decoration: underline;
}
a:hover,
a:active {
color: DarkSlateGrey;
}
.caption {
font-size: 0.9em;
background-color: #F4F4F4;
padding: 0.5em;
}
.header {
background-color: #C0B283;
padding-top: 0.5em;
padding-bottom: 0.3em;
}
.footer {
background-color: #C0B283;
padding-top: 1.0em;
padding-bottom: 0.4em;
font-size: 0.9em;
}
</style>
<script>
console.log("gtag('config', 'UA-XXX');")
</script>
</head>
<body>
<!-- header -->
<div class="header container-fluid">
<div class="col-12 col-md-8 col-xl-6 mx-auto">
<div class="row">
<div class="col-10">
<h4 class="main-heading"><a href="/image-match-game/" style="text-decoration: none;">Image match game</a></h4>
</div>
<div class="col text-right">
<a href="about.html" style="text-decoration: none;">About</a>
</div>
</div>
</div>
</div>
<!-- my-2 = bootstrap code for: margin top/bottom = 2rem -->
<div class="container-fluid my-2">
<div class="row">
<div class="col-12 col-md-8 col-xl-6 mx-auto">
<div class="row">
<div id="counter" class="col-10"></div>
<div id="high-score" class="col text-right">High score: 0</div>
</div>
<div id="target-image" class="row">
<p>Loading image...</p>
</div>
<div class="row">
<h4 style="margin: 0.4em;">Choose the caption for this image</h4>
</div>
<div id="options" class="row">
<p>Loading options...</p>
</div>
</div>
</div>
</div>
<!-- footer -->
<div class="footer container-fluid">
<div class="col-12 col-md-8 col-xl-6 mx-auto">
<p>Over 100,000 downloadable images in <a href="https://collections.tepapa.govt.nz/">Te Papa's Collections Online</a>.</p>
</div>
</div>
<!-- popup gameplay modal (right/wrong) -->
<div id="playAlert" title="Play update" class="modal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 id="playAlertTitle" class="modal-title"></h5>
</div>
<div id="playAlertMessage" class="modal-body"></div>
<div class="modal-footer">
<div id="playAlertFooter"></div>
<button id="play-alert-close-button" type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- popup game-over modal (score/next game) -->
<div id="gameAlert" title="Game update" class="modal fade">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 id="gameAlertTitle" class="modal-title"></h5>
</div>
<div id="gameAlertMessage" class="modal-body"></div>
<div class="modal-footer">
<button id="game-alert-close-button" type="button" class="btn btn-secondary" data-dismiss="modal">New game</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
// globals
var API_BASE = 'https://data.tepapa.govt.nz/collection'
var API_KEY = 'P7ufPoVJ1n9qcmrbvfYdx3Z2opdexYIm3eVBSboa'
var VIEW_BASE = 'https://collections.tepapa.govt.nz/object/'
// maximum results expected from canned query (for random generator base)
var MAX_POSSIBLE = 13000
// how many answer options we want to show for each card
var MAX_OPTIONS = 3
// how many card plays in each game
var MAX_CARDS = 10
var card_counter = 0
var game_counter = 0
var game_score = 0
var high_score = 0
var target = void 0
var options = []
/**
* Once the page is ready, start the first game
*/
document.addEventListener('DOMContentLoaded', function() {
// set up modal close on card play alert to move to next card/game over
$('#playAlert').on('hidden.bs.modal', function (e) {
if (card_counter < MAX_CARDS) {
nextCard()
} else {
gameOver()
}
})
// set up modal close on game-over alert to move to next game
$('#gameAlert').on('hidden.bs.modal', function (e) {
startNewGame()
})
// start the game
startNewGame()
})
function startNewGame() {
// reset counters and play new card
game_counter++
card_counter = 0
game_score = 0
nextCard()
}
function gameOver() {
console.log("GAME OVER")
// start building message - player's score
let html = 'End of game ' + game_counter
html += ' -- Your score: ' + game_score
// update high score
if (game_score > high_score) {
html += '<br />New high score!! (Up from ' + high_score + ')'
high_score = game_score
$('#high-score').html('High score: ' + high_score)
}
// display game-over message
$('#gameAlertTitle').html('<p style="color: #3366cc;">Game over</p>')
$('#gameAlertMessage').html(html)
$('#gameAlert').modal()
}
function nextCard() {
// display current card number
card_counter++
$('#counter').html('<p>Game ' + game_counter + ' - Card ' + card_counter + ' of ' + MAX_CARDS + '</p>')
// reset and load/display next random card and options
target = void 0
options = []
let randomNo = Math.floor(Math.random() * MAX_POSSIBLE)
// retrieve(randomNo, 1, addToOptions)
retrieve(randomNo, 5, processResults)
}
/**
* Queries for multiple resources from the Te Papa Collections API.
*
* @param {Integer} - position in search results to start from
* @param {Integer} - size of search results to request
* @param {Function} - Callback function to pass the JSON response to
*/
function retrieve(from, size, callback) {
let url = API_BASE + '/search/?q=_exists_:hasRepresentation+AND+collection:Art&fields=id,title,caption,hasRepresentation&from=' + from + '&size=' + size
console.log("API: " + url)
try {
$.ajax({
url: url,
type: "GET",
headers: {
"x-api-key": API_KEY
},
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function(response, textStatus, jqXHR) {
callback(response)
},
error: function(error, textStatus, errorThrown) {
console.log("Retrieve failed: ", error)
}
})
} catch (e) {
console.log("Retrieve failed: ", e)
}
}
function processResults(responseJson) {
console.log("Response (processResults): ", responseJson)
if (responseJson.results) {
// choose the target randomly from the first four results
let randomNo = Math.floor(Math.random() * 4)
target = responseJson.results[randomNo]
displayImage(target)
// the first four results are the options
options.push(responseJson.results[0])
options.push(responseJson.results[1])
options.push(responseJson.results[2])
options.push(responseJson.results[3])
displayOptions(options)
$('html, body').animate({scrollTop: 0}, 1);
}
}
/*
function addToOptions(responseJson) {
console.log("Response (addToOptions): ", responseJson)
if (responseJson.results) {
responseJson.results.forEach(function(rec) {
if (target == null) {
target = rec
displayImage(target)
}
options.push(rec)
})
// display when we've retrieved enough
if (options.length >= MAX_OPTIONS) {
displayOptions(options)
} else {
// otherwise retrieve another
let randomNo = Math.floor(Math.random() * MAX_POSSIBLE)
retrieve(randomNo, addToOptions)
}
}
}
*/
/**
*
*
* @param {Array}
*/
function displayOptions(options) {
//console.log("Options: ", options)
if (options) {
// clear existing options
$('#options').html('')
options.forEach(function(rec) {
let html = '<p id="' + rec.id + '" class="caption">' + rec.caption + '</p>'
$('#options').append(html)
// bind right/wrong play handler to each card
if (rec.id === target.id) {
$('#' + rec.id).on('click', function() {
handlePlay(true, rec.id, rec.title)
})
} else {
$('#' + rec.id).on('click', function() {
handlePlay(false, rec.id, rec.title)
})
}
})
}
}
/**
*
*
* @param {Object} - API single resource JSON
*/
function displayImage(rec) {
//console.log("Rec (image): ", rec)
if (rec) {
// clear existing image
$('#target-image').html('')
if (rec.hasRepresentation) {
let html = '<img id="target-' + rec.id + '" src="' + rec.hasRepresentation[0].thumbnailUrl + '" class="img-fluid" alt="Target image" />'
$('#target-image').append(html)
}
}
}
function handlePlay(isCorrect, id, title) {
if (isCorrect) {
game_score++
$('#playAlertTitle').html('<p style="color:green;">Right</p>')
} else {
$('#playAlertTitle').html('<p style="color:red;">Wrong</p>')
}
let html = target.title + '<br /><a href="' + VIEW_BASE + target.id + '" target="_blank" style="font-size:0.8em;">View this image on Collections Online</a>'
$('#playAlertMessage').html(html)
$('#playAlertFooter').html('Your progress: ' + game_score + ' of ' + card_counter)
$('#playAlert').modal()
}
// extract querystring parameter from page URL
function _getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
</script>
</body>
</html>