-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKingsCup.html
More file actions
755 lines (656 loc) · 23.9 KB
/
KingsCup.html
File metadata and controls
755 lines (656 loc) · 23.9 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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/de0f812dc0.js" crossorigin="anonymous"></script>
<title>Kings Cup</title>
<style>
body {
background-color: darkgreen;
}
.row>* {
padding: 0;
}
h1 {
text-align: center;
margin: 15px auto;
color: darkorange;
font-weight: bold;
margin: 50px auto 30px;
}
h1 i {
/* font-size: 16px; */
font-weight: normal;
color: #990033;
text-shadow: #fff 1px 1px 1px;
}
.row {
position: relative;
margin: 20px auto;
height: 95px;
width: 295px;
}
.row-button {
flex-direction: column !important;
justify-content: space-evenly;
align-items: center;
}
button[id*="card"] {
position: absolute;
top: 0px;
width: 55px;
height: 95px;
border-radius: 4px;
background: transparent;
border: 0;
}
button[id*="card"].active {
animation-name: light;
animation-duration: 1s;
/* 設定卡片反轉的3D效果 Perspective的值是物件跟熒幕Z軸的距離 */
perspective: 500px;
}
@keyframes light {
from {
border: solid 3px #f0ff33;
box-shadow: 0 0 20px #f0ff33;
}
to {
border: unset;
box-shadow: unset;
}
}
button[id*="card"]:hover {
transform: scale(1.05) translateY(-15px);
}
.flip-inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.8s;
/* perserve-3d specifies the child elements will preserve its 3D position */
transform-style: preserve-3d;
}
button[id*="card"].chosen .flip-inner {
animation-name: lightAndFlip;
animation-duration: 1s;
transform: rotateY(180deg);
transition: 2s;
}
@keyframes lightAndFlip {
0% {
border: solid 3px #f0ff33;
box-shadow: 0 0 20px #f0ff33;
}
80% {
border: unset;
box-shadow: unset;
}
}
button[id*="card"].chosen {
z-index: 1000 !important;
transform: scale(1);
transition: 0.5s;
}
.flip-front,
.flip-back {
position: absolute;
width: 100%;
height: 100%;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.flip-front img {
width: 100%;
height: 100%;
}
.flip-back {
transform: rotateY(180deg);
}
.playcard {
width: 55px;
height: 95px;
display: flex;
flex-direction: column;
justify-content: space-between;
border: gray 1px solid;
border-radius: 4px;
background-color: #fff;
padding: 0;
position: absolute;
top: 0;
left: 0;
}
.playcard p {
width: 100%;
text-align: left;
margin: 0;
padding-left: 4px;
font-size: 16px;
}
.playcard img {
width: 12px;
margin-left: 3px;
}
.pattern-bottom {
display: flex;
flex-direction: column;
}
.pattern-bottom img {
transform: rotate(180deg) translateX(3px);
align-self: flex-end;
margin: 0;
}
.pattern-bottom p {
transform: rotate(180deg);
margin-right: auto;
margin: 0;
}
.pattern-top {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
}
button#auto-chose {
width: 150px !important;
/* margin: auto; */
/* border: 4px #ffc107 solid; */
}
button#again {
width: 150px !important;
/* margin: auto; */
}
.modal-content {
width: 230px;
height: 400px;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.modal-content h5 {
font-size: 16px;
}
.modal-header {
border-bottom: unset;
}
.modal-body {
padding: 2rem;
}
.modal-footer {
border-top: unset;
}
p.number {
margin-bottom: 0;
text-align: center;
margin-bottom: 3px;
}
img.pattern {
width: 12px;
text-align: center;
}
#exampleModal .pattern-bottom,
#exampleModal .pattern-top {
width: 24px;
display: flex;
flex-direction: column;
align-items: center;
}
#exampleModal h5 {
text-align: center;
font-weight: bold;
margin-bottom: 10px;
}
.tooltip {
z-index: 1 !important;
}
h1 {
position: relative;
}
#fak {
width: 100%;
height: 1200px;
background-color: transparent;
position: absolute;
z-index: 500;
top: 0;
left: 0;
}
@media screen and (min-width:768px) {
h1 {
margin: 50px auto 30px;
}
.playcard {
width: 88px;
height: 144px;
}
button[id*="card"] {
width: 88px;
height: 144px;
}
.wrap-lgScreen {
display: flex;
width: 588px;
margin: 0;
margin: auto;
/* transform: translateX(20px); */
}
.wrap-lgScreen .row:first-child {
z-index: 0;
}
.wrap-lgScreen .row:nth-child(2) {
z-index: 1;
transform: translateX(-33px);
}
.row {
height: 160px;
width: 328px;
margin: 25px auto;
}
.row-button {
height: 100px;
margin: 30px auto 10px;
}
.row-button button {
display: block;
/* margin: 30px 30px !important; */
}
button#auto-chose {
bottom: 40px;
}
.modal-content {
width: 264px;
min-height: 432px;
}
p.number {
font-size: 24px;
}
img.pattern {
width: 24px;
text-align: center;
}
}
</style>
</head>
<body>
<h1><i class="fas fa-wine-bottle"></i> King's Cup <i class="fas fa-wine-glass-alt"></i> </h1>
<div class="row row-button">
<button class="btn btn-warning" id="auto-chose" data-bs-toggle="tooltip" data-bs-placement="right"
title="您也可以直接點選卡片">電腦幫我選</button>
<button class="btn btn-danger" id="again" onclick="reset()">重來一局</button>
</div>
<div class="container-md" id="container">
<div class="wrap-lgScreen">
<div class="row">
<button id="card1"></button>
<button id="card2"></button>
<button id="card3"></button>
<button id="card4"></button>
<button id="card5"></button>
<button id="card6"></button>
<button id="card7"></button>
<button id="card8"></button>
<button id="card9"></button>
<button id="card10"></button>
<button id="card11"></button>
<button id="card12"></button>
<button id="card13"></button>
</div>
<div class="row">
<button id="card14"></button>
<button id="card15"></button>
<button id="card16"></button>
<button id="card17"></button>
<button id="card18"></button>
<button id="card19"></button>
<button id="card20"></button>
<button id="card21"></button>
<button id="card22"></button>
<button id="card23"></button>
<button id="card24"></button>
<button id="card25"></button>
<button id="card26"></button>
</div>
</div>
<div class="wrap-lgScreen">
<div class="row">
<button id="card27"></button>
<button id="card28"></button>
<button id="card29"></button>
<button id="card30"></button>
<button id="card31"></button>
<button id="card32"></button>
<button id="card33"></button>
<button id="card34"></button>
<button id="card35"></button>
<button id="card36"></button>
<button id="card37"></button>
<button id="card38"></button>
<button id="card39"></button>
</div>
<div class="row">
<button id="card40"></button>
<button id="card41"></button>
<button id="card42"></button>
<button id="card43"></button>
<button id="card44"></button>
<button id="card45"></button>
<button id="card46"></button>
<button id="card47"></button>
<button id="card48"></button>
<button id="card49"></button>
<button id="card50"></button>
<button id="card51"></button>
<button id="card52"></button>
</div>
</div>
</div>
<template id="card">
<div class="flip-inner">
<div class="flip-front">
<img src="https://opengameart.org/sites/default/files/card%20back%20orange.png" alt="">
</div>
<div class="flip-back playcard">
<div class="pattern-top">
<p></p>
<img src="" alt="">
</div>
<div class="pattern-bottom">
<img src="" alt="">
<p></p>
</div>
</div>
</div>
</template>
<div class="modal" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="pattern-top">
<p class="number"></p>
<img src="" alt="" class="pattern">
</div>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<p class="rule"></p>
</div>
<div class="modal-footer">
<div class="pattern-bottom">
<img src="" alt="" class="pattern">
<p class="number"></p>
</div>
</div>
</div>
</div>
</div>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"
integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"
integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"
crossorigin="anonymous"></script>
<script>
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
})
var myModal = new bootstrap.Modal(document.getElementById('exampleModal'), {
keyboard: false
})
</script>
<script>
let url = "https://raw.githubusercontent.com/chilinglee/FileStorage/main/KingsCup.json"
var deckOfCard = []
var fullDeck;
fetch(url)
.then(response => response.json())
.then(result => {
deckOfCard.push(...result);
fullDeck = deckOfCard;
shuffler(deckOfCard);
creatCard();
window.alert("未成年請勿飲酒,成年人勿飲酒過量\n喝酒不開車,開車不喝酒");
checkKKKK()
})
// //設定卡片的data屬性,以及click事件
// $g("button[id*='card']").forEach(function (card) {
// card.setAttribute("data-bs-toggle", "modal");
// card.setAttribute("data-bs-target", "#exampleModal");
// //click事件發生時加入chosen class 並出現modal
// card.addEventListener("click", function () {
// // card flip
// this.classList.add("chosen");
// //建立model
// createModal(this);
// })
// });
//計算第幾個K
let countK = 0;
//小瑪麗跑起來
let speed = 50;
let step = 0;
let currentSpot = 0;
let allStep = 0;
let fak;
$g("button#auto-chose").addEventListener("click", function () {
fak = document.createElement("div");
fak.setAttribute("id", "fak");
$g("h1").appendChild(fak);
let allcard = $g("button[id*='card']");
allcard.forEach(function (c) { c.classList.remove("active") })
step = Math.floor(Math.random() * deckOfCard.length);
// console.log(step)
// console.log(deckOfCard.length);
allStep = step;
lightMoveToEnd();
//關掉tooltip
let myTooltip = document.querySelector("#auto-chosen");
let tooltip = new bootstrap.Tooltip(myTooltip);
tooltip.toggle();
});
//設定modal關掉時要把DOM節點與deckOfCard中剛選到的牌資料清除
$g("#exampleModal button").addEventListener("click", function () {
let allcard = $g("button[id*='card']");
let modalBtnId = this.getAttribute("ID");
//剔除deckOfCard中的應刪除的資料
deckOfCard = deckOfCard.filter(x => x["ID"] != modalBtnId);
allcard.forEach(function (c) {
if (c.getAttribute("jsonid") == modalBtnId) {
//清除DOM節點中的卡片
c.remove();
}
});
});
//functions
function shuffler(theArray) {
for (let i = theArray.length - 1; i > 0; i--) {
let rnd = Math.floor(Math.random() * i);
let value_i = theArray[i];
let value_rnd = theArray[rnd];
theArray[i] = value_rnd;
theArray[rnd] = value_i;
}
}
function $g(element) {
let found = document.querySelectorAll(element);
return found.length == 1 ? found[0] : found;
}
function creatCard() {
let cardID = 1;
let left_distance = 0;
let z = 1;
deckOfCard.forEach(function (card) {
let tempCard = $g("template#card");
let cloneContent = tempCard.content.cloneNode(true);
let imgs = cloneContent.querySelectorAll(".playcard img");
let ps = cloneContent.querySelectorAll("p");
imgs.forEach(function (i) { i.setAttribute("src", card["img"]); });
ps.forEach(function (p) { p.innerText = card["number"]; });
//調整卡片距離
let theCard = $g(`#card${cardID}`);
theCard.style.setProperty("left", `${left_distance}px`);
theCard.style.setProperty("z-index", `${z}`);
//記錄卡片json資料中的ID
theCard.setAttribute("jsonID", `${card.ID}`);
//記錄卡片的number
theCard.setAttribute("number", `${card.number}`);
//設定卡片的data屬性,以及click事件
theCard.setAttribute("data-bs-toggle", "modal");
theCard.setAttribute("data-bs-target", "#exampleModal");
//click事件發生時加入chosen class 並出現modal
theCard.addEventListener("click", function () {
// card flip
this.classList.add("chosen");
//建立model
createModal(this);
})
theCard.appendChild(cloneContent);
left_distance += 20;
z++;
cardID++;
if (cardID == 27 || cardID == 14 || cardID == 40) {
left_distance = 0;
z = 1;
}
});
}
function createModal(theChosenCard) {
//建立model
let theModal = $g("#exampleModal");
let theNumber = theChosenCard.getAttribute("number");
console.log(theNumber);
let theID = theChosenCard.getAttribute("jsonid");
console.log(theID);
console.log(theNumber);
console.log(`countK${countK}`)
//計算K
if (theNumber == "K") {
countK++;
}
console.log(`countK${countK}`)
if (countK == 4) {
theModal.querySelector("h5").innerText = "THE FOURTH K !!!"
theModal.querySelector("p.rule").innerText = "恭喜你獲得國王杯,要喝光光哦";
theModal.querySelectorAll("p.number").forEach(function (pNum) {
pNum.innerText = theNumber;
})
theModal.querySelectorAll("img").forEach(function (modalImg) {
modalImg.setAttribute("src", fullDeck.find(x => x.ID == theID)["img"]);
})
let theBtn = document.createElement("button");
theBtn.innerText = "再玩一局不醉不歸";
theBtn.classList.add("btn-danger");
theBtn.classList.add("btn");
theBtn.style.setProperty("margin", "15px");
theBtn.style.setProperty("text-align", "center");
theBtn.style.setProperty("font-size", "10px");
theBtn.addEventListener("click", function () {
myModal.hide();
reset();
});
theModal.querySelector(".modal-body").appendChild(theBtn);
} else {
theModal.querySelector("h5").innerText = fullDeck.find(x => x.number == theNumber)["ruleTitle"];
theModal.querySelector("p.rule").innerText = fullDeck.find(x => x.number == theNumber)["rule"];
theModal.querySelectorAll("p.number").forEach(function (pNum) {
pNum.innerText = theNumber;
})
theModal.querySelectorAll("img").forEach(function (modalImg) {
modalImg.setAttribute("src", fullDeck.find(x => x.ID == theID)["img"]);
})
//設定modal button 增加ID屬性來對應json中的資料
let chosenid = theChosenCard.getAttribute("jsonid")
theModal.querySelectorAll("button").forEach(function (b) { b.setAttribute("ID", chosenid) });
$g("body").appendChild(theModal);
}
}
function lightMoveToEnd() {
//關掉所有的button
let allBtn = $g("button");
allBtn.forEach(function (bbb) { bbb.disabled = true });
let allcard = $g("button[id*='card']");
allcard[currentSpot].classList.remove("active");
currentSpot++;
allcard[currentSpot].classList.add("active");
step--;
if (step > 0) {
setTimeout(lightMoveToEnd, speed);
if (step < (allStep / 3)) {
speed += 30;
}
} else {
//被選到的card要反轉並產生modal
let theCard = allcard[currentSpot];
theCard.classList.add("chosen");
createModal(theCard);
//因爲這裏沒有button實現toggle/show因此用bootstrap5的方法來打開modal
// let theModal = new bootstrap.Modal(document.querySelector("#exampleModal"));
// setTimeout(theModal.show(), 2000);
myModal.show();
//打開所有btn
$g("button").forEach(function (b) { b.disabled = false });
//重設
speed = 100;
step = 0;
currentSpot = 0;
//隱藏fak
$g("#fak").remove();
}
}
function reset() {
let cardRows = $g(".wrap-lgScreen .row");
cardRows.forEach(r => { r.innerHTML = "" });
creatDOM(cardRows);
deckOfCard = fullDeck;
shuffler(deckOfCard);
creatCard();
countK = 0;
console.log($g(".modal button.btn-danger"));
if ($g(".modal button.btn-danger").length != 0) {
$g(".modal button.btn-danger").remove();
}
checkKKKK();
}
function checkKKKK() {
let kkkk = deckOfCard.filter(x => x.number == "K");
console.log(deckOfCard.indexOf(kkkk[0]));
console.log(deckOfCard.indexOf(kkkk[1]));
console.log(deckOfCard.indexOf(kkkk[2]));
console.log(deckOfCard.indexOf(kkkk[3]));
}
function creatDOM(rows) {
for (let x = 1; x <= 13; x++) {
let newBtn = document.createElement("button");
newBtn.setAttribute("id", `card${x}`);
rows[0].appendChild(newBtn);
}
for (let x = 14; x <= 26; x++) {
let newBtn = document.createElement("button");
newBtn.setAttribute("id", `card${x}`);
rows[1].appendChild(newBtn);
}
for (let x = 27; x <= 39; x++) {
let newBtn = document.createElement("button");
newBtn.setAttribute("id", `card${x}`);
rows[2].appendChild(newBtn);
}
for (let x = 40; x <= 52; x++) {
let newBtn = document.createElement("button");
newBtn.setAttribute("id", `card${x}`);
rows[3].appendChild(newBtn);
}
}
</script>
</body>
</html>