-
Notifications
You must be signed in to change notification settings - Fork 0
/
molkky.php
696 lines (660 loc) · 16.8 KB
/
molkky.php
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<?php
$timestart = microtime(true);
//error_reporting(E_ALL);
//ini_set('display_errors', 'On');
//ini_set('log_errors', 'On');
define("WIDTH", 350);
define("HEIGHT", 530);
define("PLAYER", 50);
define("BLANK_WIDTH", 8);
define("BLANK_MAX", 3);
define("GROUND", HEIGHT - ((PLAYER + 1) * 3));
define("PLAYING", PLAYER - 10);
define("BITS", min(78, floor((GROUND - PLAYING) / 4)));
define("TARGET", BITS * 4);
define("TARGET_MARGIN", (GROUND - PLAYING - TARGET) / 2);
define("BUTTON_PLAY", floor(WIDTH / 5) - 4);
define("MAX_PLAYERS", 6);
define("BITS_NUMBER", 12);
if (stripos($_SERVER['HTTP_USER_AGENT'], "google") !== false) {
?>
<html>
<head>
<meta name="robots" content="noindex,nofollow">
<meta name="googlebot" content="noindex,nofollow,noarchive,nosnippet">
<title>EMPTY</title>
</head>
<body>EMPTY</body>
</html>
<?php
exit(0);
}
function bit($nb) {
printf("<span id=\"bit_%d\" onclick=\"bit(%d);\">%d</span>", $nb, $nb, $nb);
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MÖLKKY</title>
<script src="jquery.js"></script>
<script>
var bits = [];
var players = [];
var scores = [];
var blank = [];
var currentplayer = -1;
var winner = -1;
var nbplayer = 0;
var mode = 0; //0: adding player. 1: gaming. 2: done.
function firstload() {
for (var i = 0; i < <?php print(MAX_PLAYERS); ?>; i++) {
players[i] = "";
scores[i] = 0;
blank[i] = 0;
}
currentplayer = -1;
winner = -1;
nbplayer = 0;
mode = 0;
clearDiv();
initExit();
$("#play").hide();
refresh();
}
function sameGame() {
for (var i = 0; i < nbplayer; i++) {
scores[i] = 0;
blank[i] = 0;
}
winner = -1;
clearDiv();
readyToPlay();
}
function winGame() {
players.sort(function(a, b) {
var n, m = 0;
if (a == "") return 1;
if (b == "") return -1;
for (var i = 0; i < nbplayer; i++) {
if (players[i] == a) n = i;
if (players[i] == b) m = i;
}
if (n == winner) return -1;
if (m == winner) return 1;
var v = scores[m] - scores[n];
if (v != 0)
return v;
return blank[n] - blank[m];
});
sameGame();
}
function loseGame() {
players.sort(function(a, b) {
var n, m = 0;
if (a == "") return 1;
if (b == "") return -1;
for (var i = 0; i < nbplayer; i++) {
if (players[i] == a) n = i;
if (players[i] == b) m = i;
}
if (n == winner) return 1;
if (m == winner) return -1;
var v = scores[n] - scores[m];
if (v != 0)
return v;
return blank[m] - blank[n];
});
sameGame();
}
function rndGame() {
players.sort(function(a, b) {
if (a == "") return 1;
if (b == "") return -1;
return 0.5 - Math.random();
});
sameGame();
}
function exit1() {
$("#exit1").hide();
$("#exit2").show();
}
function initExit() {
$("#exit1").show();
$("#exit2").hide();
}
function bit(nb) {
if (mode != 1)
return;
initExit();
bits[nb - 1] = ! bits[nb - 1];
turnBits();
}
function clearDiv() {
document.getElementById("current").innerHTML = "";
document.getElementById("winner").innerHTML = "";
for (var i = 0; i < <?php print(MAX_PLAYERS); ?>; i++) {
$("#player_" + i).css("background-color", "transparent");
$("#player_" + i).css("color", "#EEEEEE");
$("#player_" + i).css("opacity", "1");
if (nbplayer == 0)
$("#player_" + i).css("width", "100%");
$("#player_" + i).hide();
document.getElementById("name_" + i).innerHTML = players[i];
document.getElementById("score_" + i).innerHTML = 0;
for (var b = 0; b < <?php print(BLANK_MAX); ?>; b++)
$("#blank_" + i + "_" + b).hide();
}
}
function turnBits() {
var bg = "";
var v = false;
for(var i = 0; i < bits.length; i++) {
if (bits[i]) {
bg = "859900";
v = true;
} else
bg = "BBBBBB";
$("#bit_" + (i+1)).css("background-color", "#" + bg);
}
if (v) {
$("#go").show();
$("#ko").hide();
} else {
$("#go").hide();
$("#ko").show();
}
var n = 50 - scores[currentplayer];
if (n <= 12)
if (! bits[n - 1])
$("#bit_" + n).css("background-color", "#B58900");
}
function updatePlayer() {
initExit();
var s = getScore();
if (s < 0) {
blank[currentplayer]++;
} else {
blank[currentplayer] = 0;
scores[currentplayer] += s;
if (scores[currentplayer] > 50) {
scores[currentplayer] = 25;
} else {
if (scores[currentplayer] == 50) {
winner = currentplayer;
mode = 2;
}
}
}
nextPlayer();
refresh();
}
function validPlayer(n) {
if (n >= nbplayer)
return false;
if (blank[n] >= <?php print(BLANK_MAX); ?>)
return false;
return true;
}
function next(nb) {
var p = nb;
while (true) {
p = (p + 1) % nbplayer;
if (validPlayer(p))
break;
if (p == nb)
break;
}
return p;
}
function nextPlayer() {
if (mode != 1)
return;
currentplayer = next(currentplayer);
}
function lastPlayer() {
return currentplayer == next(currentplayer);
}
function refresh() {
if (mode == 0) {
$("#go").hide();
$("#ko").hide();
$("#newplayer").show();
$("#target").hide();
$("#vicotory").hide();
}
if ((mode == 1) && (lastPlayer())) {
winner = currentplayer;
mode = 2;
}
if (mode == 1) {
$("#newplayer").hide();
$("#target").show();
$("#vicotory").hide();
clearBits();
turnBits();
document.getElementById("current").innerHTML = players[currentplayer];
}
if (mode == 2) {
$("#go").hide();
$("#ko").hide();
$("#newplayer").hide();
$("#target").hide();
$("#vicotory").show();
document.getElementById("winner").innerHTML = players[currentplayer];
$("#player_" + winner).css("background-color", "#859900");
$("#player_" + winner).css("color", "#268BD2");
}
for (var i = 0; i < nbplayer; i++) {
$("#player_" + i).show();
document.getElementById("name_" + i).innerHTML = players[i];
document.getElementById("score_" + i).innerHTML = scores[i];
var l = "";
for (var b = 0; b < <?php print(BLANK_MAX); ?>; b++) {
l = "#blank_" + i + "_" + b;
if ((blank[i] > 0) && (b <= (blank[i] - 1)))
$(l).show();
else
$(l).hide();
}
if (blank[i] >= <?php print(BLANK_MAX); ?>)
$("#player_" + i).css("opacity", "0.5");
}
}
function getScore() {
var bit = 0;
var nb = 0;
for(var i = 0; i < bits.length; i++)
if (bits[i]) {
nb++;
bit = i+1;
}
if (nb == 0)
return -1;
if (nb > 1)
return nb;
return bit;
}
function clearBits() {
for (var i = 0; i < <?php print(BITS_NUMBER); ?>; i++)
bits[i] = false;
}
function readyToPlay() {
mode = 1;
currentplayer = 0;
refresh();
}
function addPlayer() {
var n = document.getElementById("fname").value.trim();
document.getElementById("fname").value = "";
if (n.length <= 0)
return;
players[nbplayer] = n;
nbplayer++;
if (nbplayer == ((<?php print(MAX_PLAYERS); ?> / 2) + 1))
for (var i = 0; i < nbplayer; i++)
$("#player_" + i).css("width", "50%");
if (nbplayer > ((<?php print(MAX_PLAYERS); ?> / 2) + 1))
$("#player_" + (nbplayer - 1)).css("width", "50%");
if (nbplayer > 1)
$("#play").show();
if (nbplayer >= <?php print(MAX_PLAYERS); ?>)
readyToPlay();
refresh();
}
</script>
<style>
@font-face {
font-family: "mymenlo";
src: url(menlo.ttf) format("truetype");
}
body {
font-family: mymenlo;
background-color: #002b36;
color: #839496;
}
p {
text-align: center;
margin-bottom: 0px;
margin-top: 3px;
}
a {
text-decoration: none;
color: #ABBCBE;
}
.centered {
display: table;
position: relative;
left: 50%;
/* bring your own prefixes */
transform: translate(-50%, 0%);
}
#square {
width: <?php print(WIDTH); ?>px;
height: <?php print(HEIGHT); ?>px;
margin: 0px;
padding: 0px;
border: 0px;
position: relative;
}
#target, #vicotory, #newplayer {
width: 100%;
height: <?php print(GROUND); ?>px;
margin: inherit;
padding: inherit;
border: inherit;
position: absolute;
left: 0px;
bottom: 0px;
display: none;
}
#target #playing, #vicotory #txt, #vicotory #winner {
width: 100%;
height: <?php print(PLAYING); ?>px;
line-height: <?php print(PLAYING); ?>px;
max-height: <?php print(PLAYING); ?>px;
vertical-align: middle;
text-align: center;
font-size: 35px;
text-transform: uppercase;
padding: inherit;
margin: inherit;
border: inherit;
position: relative;
float: left;
}
#target #playing span {
color: #EEEEEE;
}
#target #bits {
width: 100%;
height: <?php print(TARGET); ?>px;
margin: <?php print(TARGET_MARGIN); ?>px 0px 0px 0px;
padding: inherit;
border: inherit;
position: relative;
float: left;
}
#target #bits div {
width: 100%;
height: <?php print(BITS); ?>px;
margin: 0px;
padding: inherit;
border: inherit;
position: relative;
float: left;
text-align: center;
}
#target #bits div span {
width: <?php print(BITS); ?>px;
height: <?php print(BITS); ?>px;
line-height: <?php print(BITS); ?>px;
max-height: <?php print(BITS); ?>px;
vertical-align: middle;
text-align: center;
font-size: 30px;
background-color: #BBBBBB;
color: #555555;
border-radius: 50%;
margin: 0px 4px 0px 4px;
padding: inherit;
display: inline-block;
cursor: pointer;
}
#target #go, #target #ko, #target #exit1, #target #exit2 {
cursor: pointer;
display: inline-block;
margin: inherit;
padding: inherit;
position: absolute;
bottom: 0px;
display: none;
}
#target #go, #target #ko {
width: 50px;
height: 50px;
border-radius: 100% 5px 5px 5px;
right: 0px;
}
#target #go {
background-color: #859900;
}
#target #ko, #target #exit2 {
background-color: #DC322F;
}
#target #exit1, #target #exit2 {
left: 0px;
border-radius: 3px 100% 3px 3px;
}
#target #exit1 {
width: 20px;
height: 20px;
background-color: #CB4B16;
}
#target #exit2 {
width: 50px;
height: 50px;
line-height: 50px;
max-height: 50px;
vertical-align: middle;
text-align: left;
font-size: 30px;
}
#target #exit2 span {
color: #EEEEEE;
text-transform: uppercase;
font-weight: bold;
margin-left: 10px;
}
#vicotory #txt {
margin-top: 60px;
height: 60px;
line-height: 60px;
max-height: 60px;
font-size: 50px;
font-weight: bold;
color: #859900;
}
#vicotory #winner {
margin-top: 20px;
font-size: 40px;
color: #EEEEEE;
}
#vicotory #buttons {
float: left;
width: <?php print(BUTTON_PLAY * 3 + 20); ?>px;
padding: inherit;
border: inherit;
}
.button, .buttons, .buttonBs {
vertical-align: middle;
text-align: center;
border-radius: 50%;
cursor: pointer;
display: inline-block;
padding: inherit;
background-color: #859900;
color: #EEEEEE;
text-transform: uppercase;
}
.button {
width: 110px;
height: 110px;
line-height: 110px;
max-height: 110px;
margin-bottom: 30px;
position: absolute;
bottom: 0px;
font-size: 35px;
}
.buttons, .buttonBs {
margin: 2px;
float: left;
}
.buttons {
width: <?php print(BUTTON_PLAY); ?>px;
height: <?php print(BUTTON_PLAY); ?>px;
line-height: <?php print(BUTTON_PLAY); ?>px;
max-height: <?php print(BUTTON_PLAY); ?>px;
font-size: 25px;
}
.buttonBs {
width: <?php print(BUTTON_PLAY * 1.5); ?>px;
height: <?php print(BUTTON_PLAY * 1.5); ?>px;
line-height: <?php print(BUTTON_PLAY * 1.5); ?>px;
max-height: <?php print(BUTTON_PLAY * 1.5); ?>px;
font-size: 50px;
}
#newplayer #takeit {
width: 100%;
height: <?php print(PLAYER); ?>px;
line-height: <?php print(PLAYER); ?>px;
max-height: <?php print(PLAYER); ?>px;
margin: inherit;
padding: inherit;
border: inherit;
vertical-align: middle;
font-size: 25px;
}
input {
width: 95%;
height: <?php print(PLAYER); ?>px;
line-height: <?php print(PLAYER); ?>px;
max-height: <?php print(PLAYER); ?>px;
font-size: 25px;
}
#newplayer #add {
width: 100%;
height: <?php print(PLAYER); ?>px;
line-height: <?php print(PLAYER); ?>px;
max-height: <?php print(PLAYER); ?>px;
margin-top: 10px;
padding: inherit;
border: inherit;
vertical-align: middle;
}
#newplayer #add div {
width: <?php print(PLAYER); ?>px;
height: <?php print(PLAYER); ?>px;
line-height: <?php print(PLAYER); ?>px;
max-height: <?php print(PLAYER); ?>px;
vertical-align: middle;
text-align: center;
border-radius: 50%;
cursor: pointer;
display: inline-block;
margin: 0px;
padding: inherit;
position: absolute;
background-color: #859900;
color: #EEEEEE;
text-transform: uppercase;
font-size: <?php print(PLAYER); ?>px;
font-weight: bold;
}
.player {
width: 100%;
height: <?php print(PLAYER); ?>px;
line-height: <?php print(PLAYER); ?>px;
max-height: <?php print(PLAYER); ?>px;
margin: inherit;
padding: inherit;
border-bottom: 1px solid #BABABA;
position: relative;
text-align: left;
color: #EEEEEE;
display: none;
float: left;
}
.player span {
margin-left: 10px;
font-size: 25px;
font-variant: small-caps;
text-overflow: ellipsis;
}
.player div.score {
width: <?php print(PLAYER); ?>px;
height: inherit;
line-height: inherit;
max-height: inherit;
vertical-align: middle;
text-align: center;
font-size: 30px;
margin: inherit;
padding: inherit;
border: 0px;
position: absolute;
right: 0px;
bottom: 0px;
}
.player div.blank {
width: <?php print((BLANK_WIDTH + 4) * BLANK_MAX); ?>px;
height: inherit;
margin: inherit;
padding: inherit;
border: 0px;
position: absolute;
right: <?php print(PLAYER); ?>px;
bottom: 0px;
}
.player div.blank div {
width: <?php print(BLANK_WIDTH); ?>px;
height: <?php print(PLAYER - 12); ?>px;
margin: 6px 2px;
padding: inherit;
border: inherit;
background-color: #DC322F;
float: left;
display: none;
}
</style>
</head>
<body onload="firstload()">
<div id="square" class="centered">
<?php
for ($i = 0; $i < MAX_PLAYERS; $i++) {
printf("<div class=\"player\" id=\"player_%d\"><span id=\"name_%d\"></span><div class=\"blank\">", $i, $i);
for ($b = 0; $b < BLANK_MAX; $b++)
printf("<div id=\"blank_%d_%d\"></div>", $i, $b);
printf("</div><div class=\"score\" id=\"score_%d\"></div></div>", $i);
}
?>
<div id="newplayer">
<div id="takeit"><input class="centered" type="text" id="fname" name="fname" placeholder="Player..."></div>
<div id="add"><div class="centered" onclick="addPlayer();">+</div></div>
<div id="play" class="button centered" onclick="readyToPlay();">Play</div>
</div>
<div id="target">
<div id="playing">>>> <span id="current"></span> <<<</div>
<div id="bits">
<div><?php bit(7); bit(9); bit(8); ?></div>
<div><?php bit(5); bit(11); bit(12); bit(6); ?></div>
<div><?php bit(3); bit(10); bit(4); ?></div>
<div><?php bit(1); bit(2); ?></div>
</div>
<div id="go" onclick="updatePlayer();"></div>
<div id="ko" onclick="updatePlayer();"></div>
<div id="exit1" onclick="exit1();"></div>
<div id="exit2" onclick="firstload();"><span>X</span></div>
</div>
<div id="vicotory">
<div id="txt">VICOTORY !</div>
<div id="winner"></div>
<div class="centered" style="position:absolute;bottom:0px;">
<div id="buttons" class="centered">
<div class="buttonBs" onclick="winGame();">W</div>
<div class="buttonBs" onclick="loseGame();">L</div>
</div>
<div id="buttons" class="centered">
<div class="buttons" onclick="rndGame();">R</div>
<div class="buttons" onclick="sameGame();">S</div>
<div class="buttons" onclick="firstload();">N</div>
</div>
</div>
</div>
</div>
<p style="font-size:45%;">Generated in <?php print(microtime(true) - $timestart); ?> seconds::::<a href="https://github.com/er-1/molkky">GitHub</a></p>
</body>
</html>