-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie-2024.html
218 lines (178 loc) · 5.31 KB
/
cookie-2024.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>抽餅乾 2024 Special</title>
<style>
body {
width: 100vw;
min-height: 100vh;
padding: 0;
margin: 0;
position: relative;
font-size: 24px;
font-family: sans-serif;
display: flex;
flex-direction: column;
text-align: center;
}
body > * {
margin-top: 1em;
}
#display {
display: flex;
justify-content: center;
}
#display > div {
--mml-size: min(60vh, 40vw);
width: var(--mml-size);
height: var(--mml-size);
margin-right: 2px;
display: inline;
font-size: calc(var(--mml-size) * 0.8);
line-height: var(--mml-size);
}
button {
border: none;
border-radius: 5px;
padding: 0.5em 1em;
font-size: 1em;
color: white;
}
button#start {
background: green;
}
button#draw {
background: red;
}
form#player {
margin-top: 1em;
}
form#player[mml-status="idle"] button#draw {display: none;}
form#player[mml-status="running"] button#start {display: none;}
div[mml-cookie=A] {
border-radius: 50%;
background-color: steelblue;
border: 2px solid black;
}
div[mml-cookie=B] {
background-color: saddlebrown;
border: 2px solid black;
}
div[mml-cookie=C] {
background-color: wheat;
border: 2px solid black;
}
#record {
display: flex;
justify-content: center;
}
#record > div {
--mml-size: calc(100vw / 24 - 5px);
width: var(--mml-size);
margin-right: 2px;
}
#record > div > div:first-child {
font-size: max(0.8em, calc(var(--mml-size) * 0.6));
text-align: center;
}
#record > div > div:last-child[mml-cookie=A] {
height: var(--mml-size);
}
#record > div > div:last-child[mml-cookie=B] {
height: var(--mml-size);
}
#record > div > div:last-child[mml-cookie=C] {
height: var(--mml-size);
}
footer {
margin-top: auto;
padding: 1em;
text-align: center;
font-size: small;
}
</style>
</head>
<body>
<div id="display">
<div id="student"></div>
<div id="cookie"></div>
</div>
<form id="player" mml-status="running">
<input type="hidden" name="student" />
<input type="hidden" name="cookie" />
<button type="button" id="start">繼續</button>
<button type="submit" id="draw">抽籤</button>
</form>
<div id="record"></div>
<footer>
Developed by <a href="https://katrina.tw" target="_blank">Hao-Yung Chan</a>
</footer>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script type="text/javascript">
const cookieTypes = ["A", "B", "C"];
let students = Array(25).fill().map((_, idx) => idx + 1).filter(i => i !== 10 && i !== 22);
let cookies = Array(24).fill().map((_, idx) => ({cat: cookieTypes[idx % 3], idx: idx}));
let playerForm = document.querySelector("#player");
let record = document.querySelector('#record');
let studentClock;
let studentView = document.querySelector("#student");
let studentInput = document.querySelector("input[name=student]");
let cookieClock;
let cookieView = document.querySelector("#cookie");
let cookieInput = document.querySelector("input[name=cookie]");
function runStudent() {
let choices = _.without(students, Number(studentInput.value));
if (choices.length > 0) {
let choice = _.sample(_.without(students, Number(studentInput.value)));
studentView.innerHTML = choice;
studentInput.value = choice;
}
}
function runCookie() {
let choices = _.filter(cookies, e => e.idx !== Number(cookieInput.value));
if (choices.length > 0) {
let choice = _.sample(_.filter(cookies, e => e.idx !== Number(cookieInput.value)));
cookieView.setAttribute("mml-cookie", choice.cat);
cookieInput.value = choice.idx;
}
}
function createRecord(student, cookie) {
let ele = document.createElement('div');
let studentEle = document.createElement('div');
ele.appendChild(studentEle);
studentEle.innerHTML = student;
let cookieEle = document.createElement('div');
ele.appendChild(cookieEle);
cookieEle.setAttribute("mml-cookie", cookie);
return ele;
}
function startPlay() {
if (Math.min(students.length, cookies.length) === 0) {
window.alert('沒ㄌ');
} else {
playerForm.setAttribute("mml-status", "running");
studentClock = setInterval(runStudent, 100);
cookieClock = setInterval(runCookie, 100);
}
}
playerForm.addEventListener("submit", function(ev) {
ev.preventDefault();
clearInterval(studentClock);
clearInterval(cookieClock);
playerForm.setAttribute("mml-status", "idle");
let data = new FormData(ev.target);
let student = Number(data.get("student"));
let cookieId = Number(data.get("cookie"))
studentView.innerHTML = data.get("student");
let cookie = cookies[_.findIndex(cookies, {idx: cookieId})];
cookieView.setAttribute("mml-cookie", cookie.cat);
record.appendChild(createRecord(student, cookie.cat));
_.remove(students, e => e === student);
_.remove(cookies, e => e.idx === cookieId);
});
document.querySelector("#start").addEventListener("click", startPlay);
window.addEventListener("load", startPlay);
</script>
</body>
</html>