-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArtGuessr.html
367 lines (310 loc) · 12.1 KB
/
ArtGuessr.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
<!-- Script Imports -->
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script src="https://unpkg.com/jwt-decode/build/jwt-decode.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<title>
ArtGuessr
</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="google-signin-client_id" content="872248582990-q1itv7mi6i6up5ivp2cike2nh6c1umg5.apps.googleusercontent.com">
<!-- CSS -->
<style>
body {
background-color: #555;
}
img {
display: inline-block;
margin-left: auto;
height: 100%;
max-width: 50%;
border: 5px solid #555;
}
h1 {
font-size: 6vh;
text-align: center;
color: "white";
font-weight: bold;
font-family: 'Trebuchet MS', sans-serif;
}
p {
font-size: 16;
margin-top: 20px;
margin-bottom: 20px;
font-family: 'Trebuchet MS', sans-serif;
}
input[type=text] {
padding: 12px 20px;
font-family: 'Trebuchet MS', sans-serif;
}
label {
font-family: 'Trebuchet MS', sans-serif;
}
button {
background-color: #4CAF50;
padding: 14px 20px;
margin: 0px;
border: none;
border-radius: 4px;
cursor: pointer;
font-family: 'Trebuchet MS', sans-serif;
}
button:hover {
background-color: #45a049;
}
#gameBox {
display: flex;
border-radius: 8px;
padding: 20px;
background-color: #f2f2f2;
border: 4px solid #555;
width: 60%;
height: 45%;
margin: auto;
}
#map {
border-radius: 8px;
padding: 20px;
background-color: #f2f2f2;
border: 4px solid #555;
width: 60%;
height: 25%;
margin: auto;
}
#OAuth {
padding: 14px 20px;
margin: 0px;
border: none;
border-radius: 4px;
cursor: pointer;
font-family: 'Trebuchet MS', sans-serif;
}
</style>
</head>
<body>
<div id="g_id_onload" data-client_id="872248582990-q1itv7mi6i6up5ivp2cike2nh6c1umg5.apps.googleusercontent.com"
data-callback="handleCredentialResponse">
</div>
<!-- User Data Storage -->
<h3 id = "userId" hidden></h3>
<h3 id = "games" hidden></h3>
<h3 id = "wins" hidden></h3>
<h3 id = "uName" hidden></h3>
<!-- Banner -->
<h1 id="bannerText" style="color:white">ArtGuessr</h1>
<!-- Formatting Box for Game -->
<div id="gameBox">
<div>
<!-- Login Instruction Text -->
<p id='welcomeText'>Log in with your Google account before continuing.</p>
<p id="infoText"></p>
<p id="scoring"></p>
<!-- New Game Button -->
<div id="OAuthButton" class="g_id_signin" data-type="standard" data-size="large" data-theme="outline" data-text="sign_in_with"
data-shape="rectangular" data-logo_alignment="left" style="display:block">
</div>
<button id="newGame" type="button" style="color:#f2f2f2; display:none">New Round</button>
</div>
<!-- Art -->
<img src="https://i.imgur.com/elo4jqy.jpeg" id="art" src="" alt="Art" />
</div>
<!-- Interactive Google Map -->
<div id="map"></div>
</body>
</html>
<script type="module">
// Firebase SQL database initialization
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js"
const firebaseConfig = {
apiKey: "AIzaSyA3lvQv4Y1dRm7kXLhS3EUl9DWcrv37i08",
authDomain: "artguessr-581b8.firebaseapp.com",
databaseURL: "https://artguessr-581b8-default-rtdb.firebaseio.com",
projectId: "artguessr-581b8",
storageBucket: "artguessr-581b8.appspot.com",
messagingSenderId: "722133732238",
appId: "1:722133732238:web:e0122f33d06945ed9ef2df",
measurementId: "G-FYPYSESQ21"
};
const app = initializeApp(firebaseConfig);
import {getDatabase, set, get, update, remove, ref, child} from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js"
const db = getDatabase();
// Storing user data
var welcome = document.querySelector("#welcomeText");
var scoring = document.querySelector("#scoring");
var hiddenWins = document.querySelector("#wins");
var hiddenGames = document.querySelector("#games");
var hiddenId = document.querySelector("#userId");
var uName = "Guest";
window.handleCredentialResponse = (response) => {
const responsePayload = jwt_decode(response.credential);
findUserData(responsePayload.sub, responsePayload.name)
uName = responsePayload.name;
document.getElementById("uName").innerHTML = uName;
}
function insertUserData(id, name) {
set(ref(db, "User/" + id), {
Id: id,
Name: name,
Wins: 0,
Games: 0
})
.then(() => {
findUserData(id)
})
.catch((error) => {
alert("insertUserData " + error)
});
}
function findUserData(id, name) {
console.log(id)
const dbref = ref(db);
var valid;
get(child(dbref, "User/" + id))
.then ((snapshot) => {
if (snapshot.exists()) {
welcome.innerHTML = "Welcome " + snapshot.val().Name;
document.getElementById("OAuthButton").style.display = "none"
document.getElementById("newGame").style.display = "block"
scoring.innerHTML = "You have played " + snapshot.val().Games + " rounds and won " + snapshot.val().Wins + " of them";
document.getElementById("")
hiddenGames.innerHTML = snapshot.val().Games;
hiddenWins.innerHTML = snapshot.val().Wins;
hiddenId.innerHTML = id;
} else {
insertUserData(id, name);
}
})
.catch((error) => {
alert("findUserData " + error);
valid = false;
})
console.log(valid)
return valid;
}
window.updateScore = (correct) => {
const dbref = ref(db);
if (correct) {
update(ref(db, "User/" + hiddenId.innerHTML), {
Wins: parseInt(hiddenWins.innerHTML) + 1,
Games: parseInt(hiddenGames.innerHTML) + 1
})
.then(()=>{
})
.catch((error)=>{
alert("updateScoreCorrect " + error);
});
} else {
update(ref(db, "User/" + hiddenId.innerHTML),{
Games: parseInt(hiddenGames.innerHTML) + 1
})
.then(()=>{
})
.catch((error)=>{
alert("updateScoreFail " + error);
});
}
}
</script>
<script>
// URL of backend
var backend_url = 'https://artguessr.onrender.com'
// Initializing image and answer with default values
var image_url = "https://media0.giphy.com/media/l1K9Dcy7ww0CW3JHq/giphy.gif"
var answer = "Algeria"
// Resets various UI elements and starts a new round
function startGame() {
setColors("#555");
document.getElementById("infoText").innerHTML = "Loading new art, this might take a moment...";
document.getElementById("art").src = "https://acegif.com/wp-content/uploads/loading-78.gif";
get_game(uName);
return uName;
}
var subButton = document.getElementById('newGame');
subButton.addEventListener('click', startGame, false);
// Function for sending user data to backend and getting MET API data
async function get_game(user) {
console.log("Sending with name: " + document.getElementById("uName").innerHTML);
const response = await fetch(backend_url, {
method: "post",
contentType: "application/json; charset=utf-8",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: document.getElementById("uName").innerHTML })
});
const data = await response.json();
new_game = data;
image_url = new_game.image;
answer = new_game.solution;
var new_art_image = new_game.image;
var new_art_answer = new_game.solution;
document.getElementById("art").src = new_art_image;
answer = new_art_answer;
document.getElementById("infoText").innerHTML = "Where do you think this piece originated?"
}
// Updates colors of HTML elements to communicate information, currently just changes gameBox borders but kept as separate function in case we want to change how wins/losses are displayed later
function setColors(color) {
document.getElementById("gameBox").style.borderColor = color;
}
// Checks user guesses, displaying correctness and updating scores accordingly
function check_guess(guess) {
if (guess == answer) {
setColors("LimeGreen");
document.getElementById("infoText").innerHTML = "Correct!"
document.getElementById("games").innerHTML = parseInt(document.getElementById("games").innerHTML) + 1;
document.getElementById("wins").innerHTML = parseInt(document.getElementById("wins").innerHTML) + 1;
scoring.innerHTML = "You have played " + document.getElementById("games").innerHTML + " rounds and won " + document.getElementById("wins").innerHTML + " of them";
updateScore(true);
} else {
setColors("red");
document.getElementById("games").innerHTML = parseInt(document.getElementById("games").innerHTML) + 1;
scoring.innerHTML = "You have played " + document.getElementById("games").innerHTML + " rounds and won " + document.getElementById("wins").innerHTML + " of them";
document.getElementById("infoText").innerHTML = "Incorrect! The correct answer was ".concat(answer);
updateScore(false);
};
}
// Code for getting user guesses and reverse geocoding
function initMap() {
var myLatlng = { lat: 0, lng: 0 };
const geocoder = new google.maps.Geocoder();
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 2,
center: myLatlng,
});
let infoWindow = new google.maps.InfoWindow({
content: "Click a country to submit a guess!",
position: myLatlng,
});
infoWindow.open(map);
map.addListener("click", (mapsMouseEvent) => {
// Close the current InfoWindow.
infoWindow.close();
// Create a new InfoWindow.
infoWindow = new google.maps.InfoWindow({
position: mapsMouseEvent.latLng,
});
infoWindow.setContent(
JSON.stringify(mapsMouseEvent.latLng.toJSON(), null, 2)
);
myLatLng = mapsMouseEvent.latLng.toJSON()
geocoder
.geocode({ location: myLatLng })
.then((response) => {
console.log(response)
if (response.results[response.results.length - 1]) {
infoWindow.setContent(response.results[response.results.length - 1].formatted_address);
var guess = response.results[response.results.length - 1].formatted_address;
check_guess(guess);
} else {
window.alert("No results found");
}
})
.catch((e) => window.alert("Geocoder failed due to: " + e));
infoWindow.open(map);
});
}
window.initMap = initMap;
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAFU_P2bNacsOeQ-RQsRgyOdmnUDDxSHLA&callback=initMap">
</script>