-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
613 lines (520 loc) · 17.5 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
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
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Brown's Bridge</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.overlay {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: rgb(33,150,243);
background-color: rgba(33,150,243, 1.0);
overflow-x: hidden;
}
</style>
<script type="text/javascript">
var tricksPerHand = [5,4,3,2,1,2,3,4,5,-1];
var handsPerGame = 9;
var players = [];
function GameCtrl() {
this.curHand = 0;
this.curPlayer = 0;
this.dealer = 0;
this.curAction = "BID";
this.curHand = 0;
}
var gameCtrl = new GameCtrl();
// menu control
function openOverlay(id) {
document.getElementById(id).style.width = "100%";
}
function closeOverlay(id) {
document.getElementById(id).style.width = "0%";
}
function openMenu() {
closeOverlay("manage_players");
e = document.getElementById("menuItemNewGame");
if (players.length === 0) {
e.style.display = "none";
} else {
e.style.display = "block";
}
openOverlay("menu");
}
function closeMenu() {
closeOverlay("menu");
closeOverlay("manage_players");
}
function menuClickNewGame() {
closeMenu();
newGame();
updateGameControls();
}
function menuClickPlayers() {
closeMenu();
openOverlay("manage_players");
updateRosterDisplay();
}
function menuClickReset() {
if ( confirm("This will clear all data, are you sure?")) {
closeMenu();
localStorage.clear();
players = [];
gameCtrl = new GameCtrl();
updateGameControls();
newScoreCard();
updateRosterDisplay();
openOverlay("manage_players");
} else {
closeMenu();
}
}
function clickPlayerAdd() {
var e = document.getElementById("new_player_name");
var id = e.value;
e.value = "";
if (id.length > 0) {
players.push({id: id, hands: [], score: 0});
updateRosterDisplay();
}
}
function clickPlayerDel(index) {
var removed = players.splice(index, 1);
updateRosterDisplay();
}
function clickPlayerMoveDown(index) {
if (index < 0 || index > players.length - 2) { return; }
var temp = players[index + 1];
players[index + 1] = players[index];
players[index] = temp;
updateRosterDisplay();
}
function clickPlayerMoveUp(index) {
if (index < 1 || index > players.length - 1) { return; }
var temp = players[index - 1];
players[index - 1] = players[index];
players[index] = temp;
updateRosterDisplay();
}
function updateRosterDisplay() {
var child = document.getElementById("manage_players_roster");
var ih = "";
for (var i = 0; i < players.length; i++) {
ih += '<div class="w3-bar">';
ih += '<div class="w3-bar-item">';
ih += '<button class="w3-button w3-padding-small" onclick="clickPlayerDel(' + i + ');">';
ih += '<i class="fa fa-minus"></i>';
ih += '</button>';
ih += '<button class="w3-button w3-padding-small" onclick="clickPlayerMoveDown(' + i + ');">';
ih += '<i class="fa fa-arrow-down"></i>';
ih += '</button>';
ih += '<button class="w3-button w3-padding-small" onclick="clickPlayerMoveUp(' + i + ');">';
ih += '<i class="fa fa-arrow-up"></i>';
ih += '</button>';
ih += '</div>';
ih += '<div class="w3-bar-item">';
ih += players[i].id;
ih += '</div>';
ih += '</div>';
}
child.innerHTML = ih;
}
function clickPrevHand() {
prevHand();
updateGameControls();
}
function clickNextHand() {
nextHand();
updateGameControls();
}
function clickPrevPlayer() {
prevPlayer();
updateGameControls();
}
function clickNextPlayer() {
nextPlayer();
updateGameControls();
}
function clickNextAction() {
rollAction();
updateGameControls();
}
function clickPrevAction() {
rollAction();
updateGameControls();
}
function clickPlayerName(id) {
setDealer(id);
updateGameControls();
updateScoreCard();
}
function prevHand() {
if(gameCtrl.curHand > 0) {
gameCtrl.curHand--;
} else {
gameCtrl.curHand = handsPerGame - 1;
}
prevDealer();
}
function nextHand() {
if(gameCtrl.curHand === handsPerGame - 1) {
gameCtrl.curHand = 0;
} else {
gameCtrl.curHand++;
}
nextDealer();
}
function prevDealer() {
var index = gameCtrl.dealer - 1;
if(index < 0) {
index = players.length - 1;
}
setDealer(index);
}
function nextDealer() {
var index = gameCtrl.dealer +1;
if(index === players.length) {
index = 0;
}
setDealer(index);
}
function setDealer(index) {
gameCtrl.dealer = index;
gameCtrl.curPlayer = gameCtrl.dealer;
nextPlayer();
}
function nextPlayer() {
if (gameCtrl.curPlayer < players.length - 1) {
gameCtrl.curPlayer++;
} else {
gameCtrl.curPlayer = 0;
}
}
function prevPlayer() {
if(gameCtrl.curPlayer > 0) {
gameCtrl.curPlayer--;
} else {
gameCtrl.curPlayer = players.length - 1;
}
}
function rollAction() {
if(gameCtrl.curAction === "BID") {
gameCtrl.curAction = "TAKE";
} else {
gameCtrl.curAction = "BID";
}
}
function rosterFromCache() {
for (var i = 0; i < localStorage.length; i++) {
players.push({id: localStorage.key(i), hands: [], score: 0});
}
}
function newGame() {
for(var i = 0; i < players.length; i++) {
players[i].hands = [];
players[i].score = 0;
}
gameCtrl.curHand = 0;
gameCtrl.curAction = "BID";
setDealer(0);
newScoreCard();
updateGameControls();
}
function onActionValue(value) {
var h = players[gameCtrl.curPlayer].hands;
if(h.length < gameCtrl.curHand) {
alert("too far ahead");
gameCtrl.curHand = h.length;
updateGameControls();
return;
}
if ( gameCtrl.curAction === "BID") {
if(typeof h[gameCtrl.curHand] === 'undefined' ) {
h[gameCtrl.curHand] = {bid: value};
} else {
h[gameCtrl.curHand].bid = value;
}
} else if (gameCtrl.curAction === "TAKE") {
if(typeof h[gameCtrl.curHand] === 'undefined' ) {
return;
}
h[gameCtrl.curHand].take = value;
calculateCurrentPlayerScore()
}
if(gameCtrl.curPlayer === gameCtrl.dealer) {
if(gameCtrl.curAction === "TAKE") {
nextHand();
} else {
nextPlayer();
}
rollAction();
} else {
nextPlayer();
}
updateScoreCard();
updateGameControls();
}
function calculateCurrentPlayerScore() {
var p = players[gameCtrl.curPlayer];
p.score = 0;
for(var i = 0; i < p.hands.length; i++) {
if(p.hands[i].bid === p.hands[i].take) {
p.score += 10;
p.score += p.hands[i].take;
} else {
p.score -= 10;
p.score -= Math.max(p.hands[i].bid, p.hands[i].take);
}
}
}
var CLASS_CUR_ACTION = " w3-cell-middle w3-padding";
var CLASS_BID_HILITE = " w3-blue-grey ";
var CLASS_TAKE_HILITE = " w3-black ";
function updateGameControls() {
var e;
e = document.getElementById("cur_hand_num");
e.innerHTML = tricksPerHand[gameCtrl.curHand];
var s = "w3-cell-middle";
e = document.getElementById("cur_hand_img");
if ( gameCtrl.curHand < 4) {
s += " fa fa-arrow-circle-down";
} else {
s += " fa fa-arrow-circle-up";
}
e.className = s;
e = document.getElementById("cur_player_name");
s = ""
// may not have any players yet
if (gameCtrl.curPlayer < players.length) {
s = players[gameCtrl.curPlayer].id;
}
e.innerHTML = s;
e = document.getElementById("cur_action");
e.innerHTML = gameCtrl.curAction;
if(gameCtrl.curAction === "BID") {
e.className = CLASS_CUR_ACTION + CLASS_BID_HILITE;
} else if(gameCtrl.curAction === "TAKE") {
e.className = CLASS_CUR_ACTION + CLASS_TAKE_HILITE;
}
e = document.getElementById("cur_available_action");
s = "";
var totalTricks = tricksPerHand[gameCtrl.curHand];
for(var i = 0; i <= totalTricks; i++) {
s += '<button onclick="onActionValue(' +i+ ')" class="w3-button w3-large">' +i+ '</button>';
}
e.innerHTML = s;
highlightCurDetail();
}
const dealerIndicatorClasses = ["fa", "fa-star"];
function highlightCurDetail() {
for (var i = 0; i < players.length; i++) {
e = document.getElementById("player-isDealer-" + i);
if ( i === gameCtrl.dealer) {
e.classList.add(...dealerIndicatorClasses);
} else {
e.classList.remove(...dealerIndicatorClasses);
}
for (var j = 0; j < handsPerGame; j++) {
var s = CLASS_PLAYER_DETAIL_CELL;
if (gameCtrl.curPlayer === i && gameCtrl.curHand === j) {
if(gameCtrl.curAction === "BID") {
s += CLASS_BID_HILITE;
} else {
s += CLASS_TAKE_HILITE;
}
}
var e = document.getElementById("player-" + i + "-hand-" + j);
e.className = s;
}
}
}
function newScoreCard() {
var e = document.getElementById("player_scores");
var ih = "";
for (var i = 0; i < players.length; i++) {
ih +=
'<div id="player-summary-' + i + '" class="w3-container w3-padding">' +
' <div class="w3-cell-row">' +
' <a onclick="clickPlayerName(' + i +')" class="w3-cell w3-cell-middle w3-hover-text-light-blue">' +
players[i].id + ' <i id="player-isDealer-' + i + '"></i>' +
' </a>' +
' <div id="player-score-' + i + '" class="w3-cell w3-cell-middle w3-right">' + players[i].score + '</div>' +
' </div>';
ih +=
' <div class="w3-cell-row w3-text-light-blue">';
for (var j = 0; j < handsPerGame; j++) {
ih += '<div onclick="editDetail('+i+','+j+')" id="player-'+i+'-hand-'+j+'" class="' +CLASS_PLAYER_DETAIL_CELL+ '"> : </div>';
}
ih += ' </div>';
ih += '</div>';
}
e.innerHTML = ih;
}
function editDetail(player, hand) {
console.log("editDetail p:" +player+ " hand:" + hand + " cur:" +gameCtrl.curHand);
var i = gameCtrl.curHand;
if (i < hand) {
while (i < hand) {
nextDealer();
i = i + 1;
}
}
if (i > hand) {
while (i > hand) {
prevDealer();
i = i - 1;
}
}
gameCtrl.curHand = hand;
gameCtrl.curPlayer = player;
updateGameControls();
}
var CLASS_PLAYER_DETAIL_CELL = "w3-cell w3-center w3-border";
function updateScoreCard() {
var e;
for (var i = 0; i < players.length; i++) {
e = document.getElementById("player-score-" + i);
e.innerHTML = players[i].score;
var h = players[i].hands;
if(h.length === 0) {
continue;
}
for (var j = 0; j < handsPerGame; j++) {
e = document.getElementById("player-" +i+ "-hand-" +j);
if(typeof h[j] === "undefined") { continue; }
var s = h[j].bid + ":";
if(h[j].take !== undefined) {
s += h[j].take;
} else {
s += " "
}
e.innerHTML = s;
}
}
}
window.onload = function() {
players = JSON.parse(localStorage.getItem("players"));
if (players === null) {
players = [];
}
if (players.length === 0) {
openOverlay("manage_players");
updateRosterDisplay();
return;
}
gameCtrl = JSON.parse(localStorage.getItem("gameCtrl"));
if (gameCtrl === null) {
newGame();
} else {
newScoreCard();
updateGameControls();
updateScoreCard();
}
}
window.onunload = function() {
localStorage.setItem("gameCtrl", JSON.stringify(gameCtrl));
localStorage.setItem("players", JSON.stringify(players));
}
</script>
<body class="w3-blue">
<div id="menu" class="overlay">
<div class="w3-bar w3-black">
<button class="w3-bar-item w3-button"
onclick="closeMenu();"><i class="fa fa-arrow-left"></i></button>
<div class="w3-bar-item w3-right">Brown's Bridge</div>
</div>
<div class="w3-bar-block">
<div id="menuItemNewGame" onclick="menuClickNewGame();" class="w3-bar-item w3-button">New Game</div>
<div onclick="menuClickPlayers();" class="w3-bar-item w3-button">Players</div>
<div onclick="menuClickReset();" class="w3-bar-item w3-button">Reset</div>
</div>
</div>
<div id="manage_players" class="overlay">
<div class="overlay_content">
<div class="w3-bar w3-black">
<button class="w3-bar-item w3-button"
onclick="openMenu();"><i class="fa fa-arrow-left"></i></button>
<div class="w3-bar-item w3-right">Brown's Bridge</div>
</div>
<div class="w3-bar">
<div class="w3-bar-item">
<button id="add_player_btn" class="w3-button w3-padding-small"
onclick="clickPlayerAdd();"><i class="fa fa-plus"></i></button>
</div>
<div class="w3-bar-item">
<input id="new_player_name" type="text" placeholder="player name"/>
</div>
</div>
<div id="manage_players_roster"></div>
</div>
<script>
var input = document.getElementById("new_player_name");
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function (event) {
// Cancel the default action, if needed
event.preventDefault();
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Trigger the button element with a click
document.getElementById("add_player_btn").click();
}
});
</script>
</div>
<!-- Top container -->
<div class="w3-bar w3-black">
<button class="w3-bar-item w3-button"
onclick="openMenu();"><i class="fa fa-bars"></i></button>
<div class="w3-bar-item w3-right">Brown's Bridge</div>
</div>
<div id="game_controls">
<div class="w3-cell-row">
<div class="w3-cell">
<a onclick="clickPrevHand();" class="w3-button">
<i class="fa fa-arrow-left"></i>
</a>
<span class="w3-cell-middle w3-padding">Hand</span>
<span id="cur_hand_num" class="w3-cell-middle"> 5</span>
<i id="cur_hand_img" class="fa fa-arrow-circle-down w3-cell-middle"></i>
<a onclick="clickNextHand();" class="w3-button">
<i class="fa fa-arrow-right"></i>
</a>
</div>
</div>
<div class="w3-cell-row ">
<div class="w3-cell">
<a onclick="clickPrevPlayer();" class="w3-button">
<i class="fa fa-arrow-left"></i>
</a>
<span id="cur_player_name" class="w3-cell-middle w3-padding"> </span>
<a onclick="clickNextPlayer();" class="w3-button">
<i class="fa fa-arrow-right"></i>
</a>
</div>
</div>
<div class="w3-cell-row ">
<div class="w3-cell">
<a onclick="clickPrevAction();" class="w3-button">
<i class="fa fa-arrow-left"></i>
</a>
<span id="cur_action"> </span>
<a onclick="clickNextAction();" class="w3-button">
<i class="fa fa-arrow-right"></i>
</a>
</div>
</div>
<div class="w3-cell-row ">
<div id="cur_available_action" class="w3-cell"></div>
</div>
</div>
<div id="player_scores">
</div>
</body>
</html>