-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
406 lines (366 loc) · 12.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,minimal-ui">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="no">
<link rel="shortcut icon" href="./favicon.ico">
<title>闷声抽大奖</title>
<script src="js/jquery-3.3.1.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.bundle.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-grid.min.css">
<link rel="stylesheet" href="css/bootstrap-reboot.min.css">
</head>
<body>
<div class="jumbotron text-center">
<h1 class="display-3">闷声抽大奖</h1>
</div>
<div class="card m-3">
<div class="card-header">
<h4 class="card-title">配置</h4>
</div>
<div id="conf_div" class="card-body">
<div class="row">
<div class="col-lg-2 m-auto">
<label for="draw_amount" class="col-form-label">抽选数量</label>
<input id="draw_amount" type="number" step="1" value="10"
class="form-control d-inline" style="width: 5rem;">
</div>
<div class="col-lg-2 m-auto">
<input id="do_not_decrease" type="checkbox">
<label for="do_not_decrease">调试模式(不减库存)</label>
</div>
<div class="col-lg-2 m-auto">
<input id="sort" type="checkbox" checked>
<label for="sort">输出有序结果</label>
</div>
<div class="col-lg-4 m-auto">
<button id="reset_stock_btn"
class="btn btn-danger" onclick="resetStock()">复位库存
</button>
<button id="view_stock_btn" class="btn btn-info" onclick="viewStock()"
data-toggle="modal" data-target="#stockModal">
查看库存
</button>
<button class="btn btn-light"
data-toggle="modal" data-target="#infoModal">
帮助
</button>
</div>
</div>
<button id="draw_button" onclick="draw()"
class="btn btn-lg btn-block btn-primary mt-1">抽选
</button>
</div>
</div>
<div id="on_change_alert" class="alert alert-warning alert-dismissible fade hide m-3">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>注意!</strong> 自变更设置后您还未进行抽选。
</div>
<div class="card bg-dark text-white m-3" id="result_div">
<div class="card-header">
<h4 class="card-title">抽选</h4>
</div>
<div id="draw_result" class="card-body">
请配置抽选选项
<table class="table text-white">
<tbody>
<tr>
<td>此处显示结果</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="card bg-danger text-white m-3">
<div class="card-header">
<h4 class="card-title">调试</h4>
</div>
<div class="card-body">
<div class="form-group">
<input class="form-control d-inline m-auto" style="width: calc(100% - 10rem)"
id="console_input" type="text">
<button onclick="submitConsole()" class="btn btn-warning d-inline m-auto">提交</button>
</div>
<textarea class="form-control" disabled id="console_out" name="console_out" cols="30" rows="1">
</textarea>
</div>
<div class="card-body">
<button class="btn btn-info" onclick="retrieveJson()">获取JSON</button>
<button class="btn btn-danger" onclick="saveJson()">保存JSON</button>
<br>
<textarea class="form-control" id="json" name="console_out" cols="30" rows="10">
</textarea>
</div>
</div>
<div class="modal fade" id="stockModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header">
<h4 class="modal-title">奖品信息</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div id="prize_info" class="modal-body">
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="infoModal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header">
<h4 class="modal-title">闷声抽大奖(v2.7.1)帮助</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div class="modal-body">
<p>第一次打开本工具时,这个帮助信息会自动弹出</p>
<h3>基本描述</h3>
<p>本工具为静态页面,初始的奖品信息保存在
<span class="text-primary">explanations.js</span>,您可以根据实际情况修改此文件</p>
<h3>自定义奖品(仅开发人员)</h3>
<p>使用在线托管服务时,您可能无法直接更改此文件,此时您可以使用浏览器调试台的
<span class="text-primary">localStorage</span>中的
<span class="text-primary">explanations</span>项(您可自行创建),以JSON数组格式保存奖品信息</p>
<p>例:</p>
<blockquote>
<pre>[
{
description:"奖品描述",
stock: 0, //初始库存
}
]</pre>
</blockquote>
<p>您也可以使用本工具末尾自带的调试工具进行奖品编辑</p>
<p>请留意
<button class="btn btn-danger">复位库存</button>
按钮将会使您本地存储的商品信息清空,从而重新使用
<span class="text-primary">explanations.js</span>,因此上述情况
<span class="text-danger">请不要使用</span>"复位库存"功能
</p>
<h3>本地存储</h3>
<p>每一次抽奖造成的库存变化都会保存在
<span class="text-primary">localStorage</span>中的
<span class="text-primary">explanations</span>项。点击
<button class="btn btn-danger">复位库存</button>
按钮将会清除保存的数据,从而恢复原本在
<span class="text-primary">explanations.js</span>
的奖品信息
</p>
<h3>小功能</h3>
<p><strong>调试模式</strong>勾选时,抽奖并不会影响库存,可用于测试</p>
<p><strong>输出有序结果</strong>勾选时,同一个奖品将会被排序在一起</p>
<p><strong>调试台</strong>您可以在此使用简易的console,以及获取和保存奖品的JSON(自动保存至localStorage)</p>
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<script src="explanations.js"></script>
<script>
String.prototype.replaceAll = function (s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
};
let result_set = [];
let do_not_decrease = false;
let do_sort;
//从本地读取保存的库存信息
let t_explanations = localStorage.getItem("explanations");
if (t_explanations != null && t_explanations !== "") {
explanations = JSON.parse(t_explanations);
}
//弹出帮助
if (localStorage.getItem("first_2.7.0") != "false") {
$("#infoModal").modal("show");
localStorage.setItem("first_2.7.0", "false");
}
function draw() {
//初始化设置
result_set = [];
let draw_amt = parseInt($("#draw_amount").val());
if (isNaN(draw_amt) || draw_amt < 1) {
alert("抽选数量无效");
return;
}
let do_sort = $("#sort").is(":checked");
do_not_decrease = $("#do_not_decrease").is(":checked");
//检查库存
let total_stock = getTotalStock();
if (total_stock < draw_amt) {
alert("库存为" + total_stock + ",不足本次抽卡数量");
return;
}
//抽数进结果集(随机数在0至[库存-1]区间)
for (let i = 0; i < draw_amt; i++) {
let random = parseInt(Math.random() * total_stock);
//查重 重新生成直至不重复
while (checkRepetition(random)) {
random = parseInt(Math.random() * total_stock);
}
result_set.push(random);
}
//排序
if (do_sort)
result_set.sort(function (a, b) {
return a - b;
});
//生成表格,处理库存
let html = generateResultHtmlAndDecreaseStock(result_set);
saveExplanationsToLS();
$("#draw_result").html(html);
confChanged(false);
$("html, body").animate({
scrollTop: $("#result_div").offset().top,
}, 300);
}
function saveExplanationsToLS() {
localStorage.setItem("explanations", JSON.stringify(explanations));
}
function generateResultHtmlAndDecreaseStock(items) {
let indexes = [];
let html = "<table class=\"table text-white\">";
items.forEach(number => {
let index = getPrizeIndex(number);
indexes.push(index);
html += "<tr>" +
"<td>" + explanations[index].description + "</td>"
+ "</tr>"
});
if (!do_not_decrease)
decreaseStocks(indexes);
return html;
}
/**
* returns true if already exists
* @param number
*/
function checkRepetition(number) {
var flag = false;
result_set.some(function (element) {
if (element == number) {
flag = true;
return true;
}
});
return flag;
}
function getPrizeIndex(number) {
let result;
explanations.some(function (item, index) {
//边界条件: number==0 为第一个库存 则stock必须>number
if (item.stock > number) {
result = index;
return true;
}
//落在本商品的库存区间以外,减去目前数值,向下一个区间寻找
number -= item.stock;
});
return result;
}
function decreaseStocks(indexes) {
//先保存各index再降库存,否则getPrizeIndex(计算受影响)
indexes.forEach(function (i) {
explanations[i].stock--;
});
}
function getTotalStock() {
let total = 0;
explanations.forEach(function (item) {
total += item.stock;
});
return total;
}
function resetStock() {
localStorage.removeItem("explanations");
window.location.reload();
}
function viewStock() {
let total_stock = getTotalStock();
let html = "<table class='table table-striped table-responsive'>" +
"<thead class='w-100'>" +
"<tr>" +
"<th>品名</th><th>剩余库存</th><th>概率</th>" +
"</tr>" +
"</thead><tbody class='w-100'>";
explanations.forEach(function (item) {
html += "<tr><td>" +
item.description +
"</td>";
html += "<td>" + item.stock + "</td>";
html += "<td>" + ((item.stock * 100) / total_stock).toFixed(2) + "% </td>";
html += "</tr>";
});
html += "</tbody></table>";
$("#prize_info").html(html);
}
function confChanged(flag) {
let alert = $("#on_change_alert");
if (flag) {
alert.removeClass("hide");
alert.addClass("show");
} else {
alert.removeClass("show");
alert.addClass("hide");
}
}
function submitConsole() {
let script = $("#console_input").val();
$("#console_input").val("");
let out = $("#console_out");
if (script === "clear") {
out.val("");
out.attr("rows", 1);
return;
}
if (script === "larger") {
out.attr("rows", (parseInt(out.attr("rows")) + 5));
return;
}
try {
out.val(
out.val() + "\n" + eval(script)
);
} catch (err) {
out.val(
out.val() + "\n" + err
);
}
out.attr("rows", (parseInt(out.attr("rows")) + 1));
}
function retrieveJson() {
$("#json").val(JSON.stringify(explanations)
.replaceAll("\\},", "},\n")
.replaceAll("\\[", "[\n")
.replaceAll("\\]", "\n]"));
}
function saveJson() {
let json = $("#json").val();
explanations = JSON.parse(json.replaceAll("\n", ""));
saveExplanationsToLS();
alert("保存成功");
}
$(document).ready(function () {
$("#conf_div input").on("change", function () {
confChanged(true);
});
$("#console_input").keyup(function (e) {
if (e.keyCode === 13)
submitConsole();
});
});
</script>
</body>
</html>