-
Notifications
You must be signed in to change notification settings - Fork 8
/
quiplibre.html
499 lines (457 loc) · 24.4 KB
/
quiplibre.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480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4ZtMtkh/V1zi1QEjU2xRmOqBFexKnWrsxmrC7PVZeu6P9u3hJDfXfv99nvX+14f8AI8DzyeB++3yWA+yTDEQ35Fw+3BJIWKrTXWgCnMl6w1lfVXH3yYoSDPbQAMnFqzMb8WuUoP113ofPg6HKO5GvE3L6mhEP+M07Vhe/mB6pPeFp/P5+/4pe3ns7neU7Vv56aj1Gd6D9Kzx6mhFLKyfaE+kWKk5aaWCkirizqFcgxkNB2CtE5Qr4FcpLY8Si3VkFYBdQvm6f4QwgN5DVC34WKksH+Gmqogrzrq963GAi07JqltL+S1IkkDejUWfkttjCnsgsT6aETiZtEf5bsDTGkLJHaMxkzf9xT+Pk6UtYaYhgsSc4zRsPHA3c47j16NMr2EDTJroWjDkFpulIL1Q27XKNh5yK0oQbEOQnJ+ipUPyeVGKNIIpNdIkdohPccIBdoG+ZXNUJiwFQq4QmHOQQWOEAWJr4QSNn+nGNehiCMUIrkaqjgraAYqw3qP5hvNhjqyemi6SqjE3keT+YH/ugP9C6EY2yWaaHAp1OOJ0yzBAqioLExzPFsGNeXcohl6nFDW7o+cq6h3ARS2pHWKczJQAsXldSRo2FitFeorvBihIePNi5EZnKeC1O3FcQcyyLoz76jD6OVSZJySht4IZyEe8G20IDNZ19feePqVKcXedHm32pHpCtyeprbuJ++HPk1Mkp8nwkNvH3e3n64pX2XFrP0AoAjG/bXZdakAAAAASUVORK5CYII=" type="image/png">
<style>input, button {font-size: 10vh;}</style>
</head>
<body>
<h1 id="roomname">Quiplibre</h1>
<div id="attrDiv"><i>Quiplibre is free software, under <a href="license.txt">AGPL v3+</a>. Its source code can be found <a href="https://github.com/wyattscarpenter/quiplibre">here</a>.</i></div>
<div id="nameDiv"></div><div id="pointsDiv"></div>
<div id="prevDiv"></div>
<div id="playDiv" style="font-size: 10vh;">
Enter a room name <input id="roomBox"></input> then <button onclick="join(roomBox.value)">Join</button> or <button onClick='host(roomBox.value)'>Host</button>
</div>
<form id="form" onSubmit="sendAnswer()" target="playDiv" formaction="#" style="display: none;">
<input id="inputBox"></input><input type="submit" value="Submit"></input>
</form>
<div id="buttonDiv" style="display: none;">
<button id="startButton" onClick="nextRound()">start the game</button>
</div>
<script src="https://unpkg.com/peerjs@1.3.0/dist/peerjs.min.js"></script>
<script>
form.addEventListener("submit", function(event){event.preventDefault();}, false);
//join if we have a fragment, for sharing purposes.
if(window.location.hash){join(window.location.hash.substring(1))};
function toQuiplibreSlug(s){return s.toLowerCase().replace(/[^a-z0-9]/g, "");}
function toQuiplibreRoomId(s){return "quiplibre_"+toQuiplibreSlug(s);}
function errorAlert(error){
alert("There has been a networking error; if you find yourself unable to play, please refresh the page.\n"+error);
console.log(error);
}
function join(name){
roomid = toQuiplibreRoomId(name);
var peer = new Peer({debug: 3});
peer.on('error', errorAlert);
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
conn = peer.connect(roomid);
conn.on('open', function() {
// Receive messages
conn.on('data', function(data) {
console.log('Received', data);
if(data.constructor === Array){handle(data[0], data[1]);}
});
conn.send('Hello!');
});
});
becomeSmallScreen(name);
}
function host(name){
//connect
roomid = toQuiplibreRoomId(name);
var peer = new Peer(roomid,{debug: 3});
peer.on('error', errorAlert);
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
peer.on('connection', function(conn){
console.log("receiving a connection");
conn.on('open', function() {
// A new player has arrived.
if (!gameBegun){
players.push(new Player(conn, "player" + playerId++));
conn.on('data', function(data) {
console.log(conn, " sent ", data);
if(data && data.constructor === Array){handlePlayer(conn,data[0],data[1]);}
});
}
});
});
});
becomeBigScreen(name);
}
//big screen stuff
function becomeBigScreen(name){
roomname.innerHTML += ": "+name
playDiv.innerHTML = "Hi, welcome to Quiplibre! Visit https://wyattscarpenter.github.io/quiplibre#"+toQuiplibreSlug(name)+" to join! Room code: "+name+"<br>"+
"No players established.<br>"+
"(You need at least two people to connect in order to play Quiplibre.)";
buttonDiv.style = ""; //show, by removing display: none;
}
//big screen globals
var gameBegun = false;
var playerId = 0;
var players = [];
var playerPairs = [];
var pp = []; //the current playerPair
var round = 0;
const maxRounds = 3; //a nice number of rounds
var choices = [];
var votes = [0,0];
function randInt(min, max) {
if (max === undefined) {
max = min;
min = 0;
}
return Math.random() * (max - min) + min | 0;
}
class Player {
constructor(conn, name) {
this.conn = conn;
this.name = name;
this.state = "nameless";
this.prompts = [];
this.answers = [];
this.score = 0;
this.send(['displayPrompt', "What is your name?"]);
}
send(data){
this.conn.send(data);
}
disconnect() {
const ndx = players.indexOf(this);
if (ndx >= 0) {
players.splice(ndx, 1);
}
}
receiveAnswer(cmd){
//the idea is to check if the response is to the right question
//to prevent errors
console.log(this);
console.log(cmd.prompt+" "+cmd.answer);
if(cmd.answer==""){console.log("ignoring empty response")}
else if(this.state=="rest"){console.log("player is at rest!");}
else if(this.state=="nameless"&&cmd.prompt=="What is your name?"){
this.send(["updateName", cmd.answer]);
this.send(["updateScore", 0]);
this.name = cmd.answer;
this.state = "rest";
this.send(["display", "<p>Your game should begin shortly! Look at the big screen!</p>"]);
display("<p>Last player to join: "+cmd.answer+"<br>total players: "+playerId+"</p>");
} else if(this.state == "prompt0"){
if (cmd.prompt == this.prompts[0]){
this.answers[0] = cmd.answer;
this.send(["displayPrompt", this.prompts[1]]);
this.state = "prompt1";
}
} else if(this.state == "prompt1"){
if (cmd.prompt == this.prompts[1]){
this.answers[1] = cmd.answer;
this.send(["display",
"<p>Excellent! Now you just have to wait for everyone else to answer.</p>"]
);
this.state = "rest";
}
if(allPlayersHaveAnswered()){
progressJudgement();
}
}
}
receiveChoice(cmd){
console.log(this, " handling choice ", cmd);
console.log(cmd.choices[0]!=choices[0]||cmd.choices[1]!=choices[1]);
if(cmd.choices[0]!=choices[0]||cmd.choices[1]!=choices[1]){//remember, == does not work on arrays
console.log("ignoring choice as it's between incorrect options");
} else if(this.state!="choosing"){
console.log("ignoring choice as player state is "+this.state);
} else {
votes[cmd.index]++;
this.send(["display","<p>You voted for option "+(cmd.index+1)+".</p>"])
this.state = "rest";
if(votes[0]+votes[1] == players.length){
awardPoints();
progressJudgement();
}
}
}
}
Player.prototype.toString = function(){return this.name};
// big screen game logic fns
function nextRound(){
if (players.length <= 1){
console.log(players.length, "players, that's not enough");
} else {
gameBegun = true;
round++;
if (round <= maxRounds){
allPlayers(function f(p){p.prompts = []; p.answers = []});
var len = players.length
for (var i=0; i<len; ++i) { //todo: could refactor this to use allPlayers
playerPairs.push([players[i], pullRandomPlayer(players[i]), pullPrompt()]);
playerPairs[i][0].prompts[0] = playerPairs[i][2];
playerPairs[i][1].prompts[1] = playerPairs[i][2];
}
for (var i=0; i<len; ++i) {
players[i].send(["displayPrompt", players[i].prompts[0]]);
players[i].state = "prompt0";
}
display("<p>Time for a nice round of Quiplibre! Round "+round+", to be exact!</p>");
buttonDiv.innerHTML = "";
}else{
endGame();
}
}
}
function progressJudgement(){
votes = [0,0];
if (playerPairs.length != 0){
pp = playerPairs.splice(Math.floor(Math.random()*playerPairs.length), 1)[0];
display("<p><b>"+pp[2]+"</b></p>"+"<p>"+pp[0].answers[0]+"<br>OR<br>"+pp[1].answers[1]+"</p>");
choices = [pp[0].answers[0],pp[1].answers[1], pp[2]];
allPlayers(function f(p){
p.send(["displayChoice", choices]);
p.state = "choosing";
});
} else {
nextRound();
}
}
function awardPoints(){
var scoreStr = "Previous result:<br>";
for(var i = 0; i<votes.length;i++){
scoreStr = scoreStr.concat(pp[i].name+" won "+votes[i]+" votes and thus "+computeScore(i)+" points!<br>");
pp[i].score += computeScore(i);
pp[i].send(["updateScore", pp[i].score]);
}
prevDiv.innerHTML = scoreStr;
}
function endGame(){
sortPlayers();
display("<p>End of the game! Here's a scoreboard:</p>"+
players.reduce(function f(acc, p){
return acc+p.name+": "+p.score+"<br>";
}, "")+
"<br><button onclick=\"newGame()\" value>New Game with Same Players</button>"+
"<br><button onclick=\"location.reload()\" value>New Game with New Players</button>"+
"<br><a href=\"https://github.com/wyattscarpenter/quiplibre\">"+
"Submit prompts or improvements at Github"+
"</a>"
)
attrDiv.style.visibility = ""; //show, by removing visibility: hidden;
//todo: could send something to players
}
function newGame(){
prevDiv.innerHTML = "Previous winner: "+players[0];
round = 0;
allPlayers(function f(p){
p.score=0;
p.send(["updateScore", 0]);
});
nextRound();
}
// big screen helper & utility fns
function display(html){playDiv.innerHTML = html;}
function computeScore(i){return votes[i] * round}
function pullPrompt(){
if (prompts.length == 0){
return "What's a good prompt for a round of Quiplibre?"+
" (Please send us the winning answer of this round, "+
"as you have exhausted our list of prompts)";}
return prompts.splice(Math.floor(Math.random()*prompts.length), 1)[0];
}
randomPlayers = [];
function pullRandomPlayer(excludedPlayer){
//remember that excludedPlayer is just a reference to a player
//use slice to shallow-copy array
if (randomPlayers.length == 0){randomPlayers = players.slice()};
tmpPlayers = randomPlayers.filter(function f(p){return (p != excludedPlayer)});
var i = Math.floor(Math.random()*(tmpPlayers.length-1));
randomPlayers = randomPlayers.filter(function f(p){return (p != tmpPlayers[i])})
return tmpPlayers[i];
}
function allPlayers(pred){ //query all players or apply a fn to them
ret = true;
var len = players.length
for (var i=0; i<len; ++i) {
ret = (pred(players[i])) && ret; //must avoid short-circuit eval
}
return ret;
}
function allPlayersHaveAnswered(){
return allPlayers(function f(p){return p.answers.length==2;});
}
function sortPlayers(){//descending. In-place and returns arr
return players.sort(function f(a, b){return (b.score - a.score)});
}
//small screen stuff
function becomeSmallScreen(name){
const attrDiv = document.querySelector('#attrDiv');
const nameDiv = document.querySelector('#nameDiv');
const pointsDiv = document.querySelector('#pointsDiv');
const playDiv = document.querySelector('#playDiv');
const form = document.querySelector('#form');
const inputBox = document.querySelector('#inputBox');
roomname.innerHTML += ": "+name
playDiv.innerHTML = "Connecting to quiplibre host...";
attrDiv.style.visibility="hidden"; //hide so the player can't accidentally click the links
}
function showForm(){
form.style = ""; //show, by removing display: none;
}
function hideForm(){
form.style = "display: none;";
}
function clearInputBox(){
inputBox.value = "";
}
function connect() {
display("<p>Connected to host...</p>");
}
function disconnect() {
display("<p>Disconnected.</p>");
}
function displayPrompt(prompt) {
console.log("displaying "+prompt);
display("<p><b id=\"prompt\">"+prompt+"</b></p>");
showForm();
inputBox.focus();
form.addEventListener("submit", function(event){
event.preventDefault();
}, false);
}
function displayChoice(choices){
console.log(choices);
hideForm();
display(choices[2]+"<br><button id=\"choice0\" onClick=\"sendChoice(0)\">"+choices[0]+"</button>"+
"<br>OR<br><button id=\"choice1\" onClick=\"sendChoice(1)\">"+choices[1]+"</button>");
}
function updateName(name) {
nameDiv.innerHTML = "Name: " + name;
}
function updateScore(score) {
pointsDiv.innerHTML = "Points: " + score;
}
function sendAnswer() {
conn.send(['receiveAnswer', {
prompt: document.querySelector('#prompt').innerHTML,
answer: document.querySelector('#inputBox').value
}]);
if(inputBox.value){clearInputBox();hideForm();}
};
function sendChoice(i) {
conn.send(['receiveChoice', {
choices: [document.querySelector('#choice0').innerHTML,document.querySelector('#choice1').innerHTML],
index: i
}]);
};
//prompts //There are 100 prompts here, some of them even good. Probably, more prompts would be even better. (I am unlikely to go to this trouble myself.) If you need more prompts, I think the question sections from jokes usually suffice, so just check an old joke book maybe. You can probably even find a public domain joke book with modern verbiage if you simply find one (from the USA) from before 1964 that failed to renew its copyright, or from before 1988 that failed to have a copyright notice.
prompts = [
"___ was my nickname in high school.",
"___ was my band name in high school.",
"That's why they call me Johnny ___.",
"What's a bad name for a child?",
"What's the best superpower?",
"On his deathbed, Otto von Bismarck apocryphally predicted that ___ would cause World War One.",
"What is Garrison Keillor's personal hell?",
"What's a fun game to play during a road trip?",
"It was weird that grandpa kept ___",
"What's a good name for a superhero dog?",
"Critics are raving about the new children's cartoon about the Holocaust, ___",
"What's something you DON'T want to hear your doctor say?",
"What's something you DON'T want to hear your surgeon say?",
"What's something you DON'T want to hear your spouse say?",
"What's something you DON'T want to hear your gynocologist say?",
"What's something you DON'T want to hear your obstetrician say?",
"What's something you DON'T want to hear your veternarian say?",
"What's something you DON'T want to hear your optometrists say?",
"What's something you DON'T want to hear your dentist say?",
"What's something you DON'T want to hear your son say?",
"What's something you DON'T want to hear your daughter say?",
"What's something you DON'T want to hear your dad say?",
"What's something you DON'T want to hear your mom say?",
"If they renamed Washington state, what would they rename it too?",
"The hot new dance craze sweeping the nation: The __",
"Worst reason to be called to the principal's office in high school.",
"Best reason to be called to the principal's office in high school.",
"Name something you can use in place of a taco shell, in a pinch.",
"Type the funniest word you can think of.",
"Write something that you shouldn't ask at a funeral.",
"What's a bad name for a band?",
"What's a bad name for a law firm?",
"Birds of a feather flock together, fish of a fin __",
"Complete the analogy: a puddle is to the ocean as __",
"By what method would you like your death to be avenged?",
"What's a film that should never be made?",
"Oh, just write something obscene.",
"What's the most important trait you look for in a partner?",
"What's a good name for a water park?",
"What's a good name for a city populated entirely by birds?",
"What's your social security number? You can trust me... I'm Quiplibre...",
"What's a group of people it will always be ok to make fun of?",
"Who REALLY shot JFK?",
"Not only is Bigfoot real, he also has an adorable pet panda named __",
"My name is Ozymandias, king of kings: Look on my works, ye Mighty, and __!",
"What's a good idea for a mad scientist's high school science fair project?",
"What's a weird way to find out your spouse is cheating on you?",
"What's the first sign that your dog is actually a wild animal?",
"Make up a name for a new breed of dog.",
"'The Holy Ghost' is a translation error. The real holy trinity is comprised of the Father, the Son, and __",
"The murderer is in this very room! The murderer is one of you! THE MURDERER IS ___",
"Soup of the day: Cream of __.",
"Bad superhero name: __man.",
"The only emperor is the emperor of __.",
"Jet fuel can't melt __.",
"What's your first order when you become president?",
"What's a name you're glad you avoided having your parents pick for you?",
"Beware the Jabberwock, my son! The __ that bite, the claws that __!",
"A vast image out of Spiritus Mundi troubles my sight: somewhere in sands of the desert a shape with lion body and the head of __.",
"Send not to know for whom the bell tolls, it tolls for __.",
"But I like it, because it is bitter, and because it is my __.",
"Aliens are real and they are going to mine our planet for precious __.",
"Worst way to make a chair.",
"Why doesn't Uncle Greg smoke any more?",
"Four score and seven years ago, our fathers brought forth on this continent __.",
"The real quiplibre was the friends we made along the way, but the real friends we made along the way were __.",
"What's the lowest price at which you would betray your friends?",
"What's a bear's favorite song?",
"Beer before liquor, __",
"You know the old saying: red touching black, you're ok Jack, red touching yellow, __.",
"A stitch in time may save time, but you'd rather make a stitch in __ instead.",
"What's the funniest thing for a pirate to say?",
"What's a holiday that doesn't exist, but should?",
"What would you name the EIGHTH day of the week?",
"Make up a name for a number between 2 and 3.",
"You just found a mountain in your backyard. What will you call it?",
"The true breakfast of champions is __, __, and __, mixed with __.",
"Truth or dare?",
"The person who names the higher number wins this round.",
"What's something people don't take seriously that they should?",
"What's something people take seriously that they shouldn't?",
"What's something you refuse to learn about?",
"No Mr Bond, I expect you to __!",
"What's a bad present to give at a wedding?",
"The little-known founding father, __.",
"What's your favorite color?",
"April is the cruellest month, __.",
"How can you tell if your doctor is fresh out of medical school?",
"What's a discouraging thing to hear on an airplane?",
"What's the most depressing color of paint you can think of?",
"When do beavers celebrate hanukkah?",
"Why did the programmer confuse Halloween and Christmas?",
"If you're *really* bad, instead of coal Santa will give you __.",
"What did one pirate say to the other, while playing chess?",
"A good name for an Aesop's fable would be:",
"The pot may call the kettle black, but the kettle calls the pot, __.",
"It's like they say: out of the frying pan, into the fire; out of the fire, into the __.",
"A good name for a 19th century president of the United States of America would be:",
"A good name for a president of the United States of America, who is also a cat, would be:",
"Historical first! Cats have finally won a class-action lawsuit, in a case titled __.", //cat-action lawsuit?!
]
//communication redirection fns
function handle(instruction, details){
console.log("handling", "return "+instruction+"("+details+");");
return eval(instruction)(details);
} //polytropos
function handlePlayer(conn, instruction, details){
//this seems circuitous but other ways didn't work
return allPlayers(function(player){
if(player.conn==conn){
return player[instruction](details);
}
});
}
</script>
</body>
</html>