-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.html
More file actions
418 lines (357 loc) · 12.9 KB
/
draw.html
File metadata and controls
418 lines (357 loc) · 12.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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>抽签程序网页版</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; max-width: 600px; margin: auto; background-image: url('background.png'); background-size: cover; background-position: center; background-attachment: fixed; }
h1 { text-align: center; }
h3 { margin: 15px 0 10px 0; }
#orderList, #candidateList {
border: 1px solid #ccc; padding: 8px; height: 160px; overflow-y: auto;
background: #f9f9f9; white-space: pre-wrap;
}
#drawArea { margin-top: 16px; text-align: center; }
#currentDraw { line-height: 36px; font-size: 30px; margin: 10px 0 4px 0; min-height: 30px; }
button { margin: 10px 5px; padding: 8px 12px; font-size: 16px; }
table { width: 100%; border-collapse: collapse; margin-top: 10px; }
th, td { border: 1px solid #000; padding: 8px; text-align: center; }
</style>
</head>
<body>
<h1 id="pageTitle">抽签程序</h1>
<label>选择名单文件 (.txt):</label>
<input type="file" id="fileInput" accept=".txt" />
<br />
<button id="loadBtn">加载名单文件</button>
<button id="nextBatchBtn" disabled>下一批</button>
<button id="exportBtn" disabled>导出抽签结果</button>
<label><input type="checkbox" id="allowRepeatsCheckbox" />允许重复抽取</label>
<br />
<label style="margin-top:15px;">选择背景音乐 (可选):</label>
<input type="file" id="bgmInput" accept="audio/*" />
<audio id="bgmPlayer" loop></audio>
<div style="display: flex; gap: 20px;">
<div style="flex: 1;">
<h3>待抽顺序</h3>
<div id="orderList"></div>
</div>
<div style="flex: 1;">
<h3>待抽名单</h3>
<div id="candidateList"></div>
</div>
</div>
<div id="drawArea" style="display:none;">
<div>当前抽取:<span id="currentOrder"></span></div>
<div id="currentDraw"></div>
<button id="startStopBtn">开始抽签</button>
<button id="undoBtn" disabled>撤销</button>
</div>
<table id="resultTable" style="display:none;">
<thead>
<tr><th>序号</th><th>抽签项</th><th>抽中人员</th></tr>
</thead>
<tbody></tbody>
</table>
<script>
const fileInput = document.getElementById('fileInput');
const musicInput = document.getElementById('musicInput');
const loadBtn = document.getElementById('loadBtn');
const nextBatchBtn = document.getElementById('nextBatchBtn');
const exportBtn = document.getElementById('exportBtn');
const orderListDiv = document.getElementById('orderList');
const candidateListDiv = document.getElementById('candidateList');
const drawArea = document.getElementById('drawArea');
const currentOrderSpan = document.getElementById('currentOrder');
const currentDrawDiv = document.getElementById('currentDraw');
const startStopBtn = document.getElementById('startStopBtn');
const undoBtn = document.getElementById('undoBtn');
const resultTable = document.getElementById('resultTable');
const tbody = resultTable.querySelector('tbody');
const pageTitle = document.getElementById('pageTitle');
const bgmInput = document.getElementById('bgmInput');
const bgmPlayer = document.getElementById('bgmPlayer');
const allowRepeatsCheckbox = document.getElementById('allowRepeatsCheckbox');
let batches = [];
let baseTitle = '';
let currentBatchIndex = 0;
let orderList = [];
let candidateList = [];
let candidatePool = [];
let assigned = [];
let allAssigned = [];
let drawIndex = 0;
let rolling = false;
let intervalId = null;
let hasLoadedFile = false;
window.onload = () => {
nextBatchBtn.disabled = true;
exportBtn.disabled = true;
};
bgmInput.onchange = () => {
const bgmFile = bgmInput.files[0];
if (bgmFile) {
const url = URL.createObjectURL(bgmFile);
bgmPlayer.src = url;
} else {
bgmPlayer.src = '';
}
};
// 加载名单文件
loadBtn.onclick = () => {
if (hasLoadedFile) {
const confirmReload = confirm('确定要加载名单吗?这将清空当前抽签数据(包括未导出的抽签结果)!');
if (!confirmReload) return;
}
const file = fileInput.files[0];
if (!file) {
alert('请选择名单文件');
return;
}
const reader = new FileReader();
reader.onload = () => {
parseFile(reader.result);
if (batches.length === 0) {
alert('未找到有效批次');
return;
}
allAssigned.length = 0; // 清空所有记录
assigned.length = 0;
drawIndex = 0;
currentBatchIndex = 0;
loadBatch(currentBatchIndex);
nextBatchBtn.disabled = batches.length <= 1;
exportBtn.disabled = allAssigned.length === 0;
tbody.innerHTML = '';
resultTable.style.display = 'none';
};
reader.readAsText(file, 'utf-8');
hasLoadedFile = true;
};
// 解析名单文本
function parseFile(text) {
batches = [];
const lines = text.split(/\r?\n/).map(l => l.trim()).filter(l => l.length > 0);
if (lines.length === 0) {
alert('文件为空');
return;
}
baseTitle = lines[0];
document.title = baseTitle;
let batchIndexes = [];
for(let i = 1; i < lines.length; i++) {
if (/^==[^=]+==$/.test(lines[i])) {
batchIndexes.push(i);
}
}
if (batchIndexes.length === 0) {
alert('未找到批次标题(格式: == 批次标题 ==)');
return;
}
for(let i = 0; i < batchIndexes.length; i++) {
const startIdx = batchIndexes[i];
const endIdx = i + 1 < batchIndexes.length ? batchIndexes[i + 1] : lines.length;
const batchLines = lines.slice(startIdx, endIdx);
const title = batchLines[0].replace(/^==\s*|\s*==$/g, '');
const orderIdx = batchLines.findIndex(l => l.toLowerCase() === 'order');
const idIdx = batchLines.findIndex(l => l.toLowerCase() === 'id');
if (orderIdx === -1 || idIdx === -1 || orderIdx >= idIdx) {
continue;
}
const orderListBatch = batchLines.slice(orderIdx + 1, idIdx);
const candidateListBatch = batchLines.slice(idIdx + 1);
if (orderListBatch.length === 0 || candidateListBatch.length === 0) continue;
batches.push({title, orderList: orderListBatch, candidateList: candidateListBatch});
}
}
// 加载某批次数据
function loadBatch(index) {
if (index < 0 || index >= batches.length) return;
const batch = batches[index];
orderList = batch.orderList.slice();
candidateList = batch.candidateList.slice();
candidatePool = candidateList.slice();
// 如果不允许重复抽取,且待抽人数不足 orderList,则补齐 `[empty]`
if (!allowRepeatsCheckbox.checked && candidatePool.length < orderList.length) {
const diff = orderList.length - candidatePool.length;
for (let i = 0; i < diff; i++) {
candidatePool.push('[empty]');
}
}
assigned = [];
drawIndex = 0;
rolling = false;
clearInterval(intervalId);
intervalId = null;
pageTitle.textContent = `${baseTitle} - ${batch.title}`;
orderListDiv.textContent = orderList.join('\n');
candidateListDiv.textContent = candidatePool.join('\n');
currentDrawDiv.textContent = '';
updateCurrentOrder();
drawArea.style.display = 'block';
startStopBtn.disabled = false;
startStopBtn.textContent = '开始抽签';
undoBtn.disabled = true;
allowRepeatsCheckbox.disabled = false;
tbody.innerHTML = '';
resultTable.style.display = 'none';
}
function updateCurrentOrder() {
currentOrderSpan.textContent = drawIndex < orderList.length ? orderList[drawIndex] : '本批抽完';
}
startStopBtn.onclick = () => {
if (!rolling) {
if (drawIndex >= orderList.length) {
alert('本批已抽完');
return;
}
if (candidatePool.length === 0) {
alert('本批待抽名单为空,无法继续抽签');
return;
}
rolling = true;
startStopBtn.textContent = '停止抽签';
intervalId = setInterval(() => {
const idx = Math.floor(Math.random() * candidatePool.length);
currentDrawDiv.textContent = candidatePool[idx];
}, 30);
// 禁用“允许重复抽取”复选框
allowRepeatsCheckbox.disabled = true;
// 播放背景音乐
if (bgmPlayer.src) {
bgmPlayer.currentTime = 0;
bgmPlayer.play().catch(err => console.warn("BGM 播放失败:", err));
}
} else {
rolling = false;
startStopBtn.textContent = '开始抽签';
clearInterval(intervalId);
const selected = currentDrawDiv.textContent;
assigned.push({order: orderList[drawIndex], candidate: selected});
allAssigned.push({order: orderList[drawIndex], candidate: selected});
// 根据是否允许重复来决定是否移除待抽项
if (!allowRepeatsCheckbox.checked) {
const removeIdx = candidatePool.indexOf(selected);
if (removeIdx !== -1) candidatePool.splice(removeIdx, 1);
}
orderListDiv.textContent = orderList.slice(drawIndex + 1).join('\n');
candidateListDiv.textContent = candidatePool.join('\n');
const tr = document.createElement('tr');
tr.innerHTML = `<td>${tbody.children.length + 1}</td><td>${orderList[drawIndex]}</td><td>${selected}</td>`;
tbody.appendChild(tr);
resultTable.style.display = 'table';
drawIndex++;
updateCurrentOrder();
undoBtn.disabled = false;
exportBtn.disabled = false;
// 暂停音乐
if (!bgmPlayer.paused) {
bgmPlayer.pause();
}
// 检查是否只剩最后一项
if (drawIndex === orderList.length - 1 && candidatePool.length === 1) {
const finalOrder = orderList[drawIndex];
const finalCandidate = candidatePool[0];
assigned.push({order: finalOrder, candidate: finalCandidate});
allAssigned.push({order: finalOrder, candidate: finalCandidate});
const tr = document.createElement('tr');
tr.innerHTML = `<td>${drawIndex + 1}</td><td>${finalOrder}</td><td>${finalCandidate}</td>`;
tbody.appendChild(tr);
resultTable.style.display = 'table';
currentDrawDiv.textContent = finalCandidate; // 让界面上显示出最后一个被自动分配的抽签结果
candidatePool = [];
drawIndex++;
updateCurrentOrder();
orderListDiv.textContent = '';
candidateListDiv.textContent = '';
startStopBtn.disabled = true;
undoBtn.disabled = false;
exportBtn.disabled = false;
}
if (drawIndex >= orderList.length) {
startStopBtn.disabled = true;
undoBtn.disabled = false;
exportBtn.disabled = false;
}
}
};
undoBtn.onclick = () => {
if (rolling) {
alert('正在抽签中,无法撤销');
return;
}
if (assigned.length === 0) {
alert('没有可以撤销的抽签');
return;
}
// 撤销最后一次抽签
const last = assigned.pop();
// 从 allAssigned 中移除对应项
for (let i = allAssigned.length - 1; i >= 0; i--) {
if (
allAssigned[i].order === last.order &&
allAssigned[i].candidate === last.candidate
) {
allAssigned.splice(i, 1);
break;
}
}
// 将待抽项放回待抽名单(末尾即可)
if (!allowRepeatsCheckbox.checked) {
candidatePool.push(last.candidate);
}
// 减少当前抽签索引
drawIndex--;
// 删除结果表最后一行
const lastTr = tbody.querySelector('tr:last-child');
if (lastTr) tbody.removeChild(lastTr);
// 更新待抽列表显示(不再更改 orderList,只用 slice 动态显示)
orderListDiv.textContent = orderList.slice(drawIndex).join('\n');
candidateListDiv.textContent = candidatePool.join('\n');
updateCurrentOrder();
startStopBtn.disabled = false;
allowRepeatsCheckbox.disabled = assigned.length !== 0;
undoBtn.disabled = assigned.length === 0;
exportBtn.disabled = allAssigned.length === 0;
};
nextBatchBtn.onclick = () => {
const currentBatchUnfinished = drawIndex < orderList.length;
if (currentBatchUnfinished) {
const confirmNext = confirm('当前批次尚未抽完,确定要加载下一批吗?');
if (!confirmNext) return;
}
if (rolling) {
alert('正在抽签中,无法切换批次');
return;
}
if (currentBatchIndex + 1 >= batches.length) {
alert('已经是最后一批');
return;
}
currentBatchIndex++;
loadBatch(currentBatchIndex);
};
exportBtn.onclick = () => {
if (allAssigned.length === 0) {
alert('无抽签结果可导出');
return;
}
let content = '';
allAssigned.forEach(pair => {
content += `${pair.order}: ${pair.candidate}\n`;
});
const blob = new Blob([content], {type: 'text/plain;charset=utf-8'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${baseTitle}_抽签结果.txt`;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
};
</script>
</body>
</html>