-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
266 lines (217 loc) · 8.18 KB
/
backend.js
File metadata and controls
266 lines (217 loc) · 8.18 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
var points = 0;
var lifes = 3;
var postCount = 0;
var redditData = [];
var element = {};
var highscore = 0;
var streak = 0;
function contains_all(str, search) {
ret = 0;
search.forEach(word => ret += str.includes(word));
return (ret === search.length);
}
function contains_one(str, search) {
for (const word of search) {
if (str.includes(word)) {
return true;
}
}
return false;
}
function getPost(debug_index = null) {
document.getElementById("form").reset();
document.getElementById("placeholder").innerHTML = "";
document.getElementById("title").innerHTML = "";
document.getElementById("description").innerHTML = "";
document.getElementById("link").innerHTML = "";
element = redditData.pop()
// document.getElementById("postCount").textContent = postCount;
document.getElementById("points").textContent = points;
document.getElementById("streak").textContent = streak;
document.getElementById("title").textContent = element.title;
if (element.desc != "") {
document.getElementById("description").textContent = element.desc;
}
if (contains_one(element.vid, ["v.redd.it", ".gifv"])) {
let video_url = element.url.includes("v.redd.it") ? element.vid + ".gif" : element.url.replace(".gifv", ".mp4");
let video = document.createElement('video');
video.src = element.vid;
video.type = "video/mp4";
video.autoplay = true;
video.muted = true;
video.loop = true;
document.getElementById("placeholder").appendChild(video);
} else if (contains_one(element.url, ["i.redd.it", "imgur"])) {
let x = document.createElement("img");
x.setAttribute("src", element.url);
x.setAttribute("onerror", "this.style.display='none'");
document.getElementById("placeholder").appendChild(x);
} else if (element.url.includes("twitter")) {
let iframe_url = "https://twitframe.com/show?url=" + encodeURI(element.url);
var hyperlink = document.createElement('a');
hyperlink.href = element.url;
hyperlink.innerText = element.url;
hyperlink.target = "_blank";
document.getElementById("link").appendChild(hyperlink);
let ifrm = document.createElement('iframe');
ifrm.setAttribute("src", iframe_url);
document.getElementById("placeholder").appendChild(ifrm);
} else if (element.url.includes("gfycat") || contains_all(element.url, ["gallery", "reddit"])) {
document.getElementById("placeholder").textContent = "We can not display the media at this time.";
var hyperlink = document.createElement('a');
hyperlink.href = element.url;
hyperlink.innerText = element.url;
hyperlink.target = "_blank";
document.getElementById("link").appendChild(hyperlink);
} else if (element.url != "" && !element.url.includes("reddit")) {
var hyperlink = document.createElement('a');
hyperlink.href = element.url;
hyperlink.innerText = element.url;
hyperlink.target = "_blank";
document.getElementById("link").appendChild(hyperlink);
} else {
document.getElementById("description").textContent = "";
document.getElementById("placeholder").textContent = element.desc;
}
document.getElementById("answer").disabled = false;
}
async function highlight(obj, color) {
var orig = obj.style.background;
obj.style.background = color;
return new Promise(resolve => setTimeout(
function() {
obj.style.background = orig;
document.querySelector("#answer").value = "";
resolve();
},
600,
));
}
async function enter() {
document.querySelector("#show").disabled = true;
document.getElementById("streak").style.background = "";
userInput = document.getElementById("answer").value;
document.querySelector("#answer").value = element.sub;
document.querySelector("#answer").disabled = true;
if (userInput.toLowerCase() == element.sub.toLowerCase()) {
await highlight(document.body, "#0f0");
points++;
streak++;
if (streak % 5 === 0 && streak > 0) {
document.getElementById("streak").style.background = "#0f0";
lifes++;
let life_div = document.createElement("div");
life_div.innerHTML = '<div class="heart-shape"></div>';
life_div.className = "inline pad";
document.getElementById("lifes").appendChild(life_div);
}
} else {
await highlight(document.body, "#f00");
streak = 0;
document.getElementById("lifes").children[0].remove();
if (lifes === 1) {
nh = ""
if (points > highscore) {
highscore = points;
nh = " \nNew Highscore!";
}
alert("The game is lost. You earned " + points + " points." + nh);
lifes = 3;
const life_div = document.createElement("div");
life_div.innerHTML = '<div class="heart-shape"></div>';
life_div.className = "inline pad";
const life_container = document.getElementById("lifes");
life_container.appendChild(life_div.cloneNode(true));
life_container.appendChild(life_div.cloneNode(true));
life_container.appendChild(life_div.cloneNode(true));
points = 0;
streak = 0;
} else {
lifes--;
}
}
document.getElementById("points").textContent = points;
document.getElementById("streak").textContent = streak;
updateSuggestions();
getPost();
document.querySelector("#show").disabled = false;
document.querySelector("#answer").disabled = false;
document.querySelector("#answer").focus();
if (redditData.length <= 25) {
getNextFrontpage(1);
}
}
function updateSuggestions() {
var subList = [];
for (var i = 0, len = redditData.length; i < len; i++) {
if (!subList.includes(redditData[i].sub)) {
subList.push(redditData[i].sub);
}
}
document.querySelector("#suggestions").innerHTML = "";
for (const sub of subList) {
var option = document.createElement('option');
option.value = sub;
document.getElementById("suggestions").appendChild(option);
}
}
var after = ''
async function getNextFrontpage(pages_to_load, ordering = "") {
if (!["hot", "new", "top", "rising"].includes(ordering)) {
ordering = "";
} else {
ordering += "/";
}
for (let j = 0; j < pages_to_load; j++) {
await fetch('https://www.reddit.com/' + ordering + '.json?callback=foo&after=' + after)
.then(response => response.json())
.then(function (json) {
for (let i = 0; i < 25; i++) {
postCount += 1;
try {
redditData.push(
{
url : json.data.children[i].data.url,
sub : json.data.children[i].data.subreddit,
title : json.data.children[i].data.title,
desc : json.data.children[i].data.selftext,
vid : json.data.children[i].data.media.reddit_video.fallback_url
}
);
} catch(err) {
redditData.push(
{
url : json.data.children[i].data.url,
sub : json.data.children[i].data.subreddit,
title : json.data.children[i].data.title,
desc : json.data.children[i].data.selftext,
vid : ''
}
);
}
}
after = json.data.after;
}
);
}
redditData.sort(() => Math.random() - 0.5)
}
// TODO:
// - Keep game state in local storage and continue from saved state
// --> Start from top posts after certain time
// - Keep better track of highscore
function initializeGame() {
points = 0;
lifes = 3;
streak = 0;
getNextFrontpage(2)
.then(function () {
updateSuggestions();
getPost();
document.querySelector("#answer").focus();
})
.catch(alert);
}
document.addEventListener("DOMContentLoaded", function(event) {
initializeGame();
})