forked from IceCang/RandomSeat
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrenderer.js
452 lines (427 loc) · 15.6 KB
/
renderer.js
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
let rowCount, columnCount, K, last_row_pos_can_be_chosen, person, zz, defaultspl, export_table;
const tbody = "<tbody>";
const tr = "<tr>";
const btr = "</tr>";
const btbody = "</tbody>";
const td = "<td>";
const btd = "</td>";
const nothing = "-";
const logBox = document.getElementsByClassName("log-box")[0];
function shuffle(arr) {
let random;
let tmp;
for (let i = arr.length; i > 0; i--) {
random = Math.floor(Math.random() * arr.length)
tmp = arr[i - 1];
arr[i - 1] = arr[random]
arr[random] = tmp;
}
return arr
}
function upload() {
const inputObj = document.createElement("input");
inputObj.setAttribute("id", "file");
inputObj.setAttribute("type", "file");
inputObj.setAttribute("name", "file");
inputObj.setAttribute("style", "visibility:hidden");
document.body.appendChild(inputObj);
inputObj.value;
inputObj.click();
// console.log(inputObj);
}
function read(json) {
try {
rowCount = json.rows; //行数
columnCount = json.columns; //列数
K = json.random_between_rows; //每多少行之间打乱
last_row_pos_can_be_chosen = json.last_row_pos_can_be_chosen.split(" "); //最后一行可选位置
person = json.person_sort_by_height.split(" ");
zz = json.zz.split(" ");
defaultspl = json.separate;
for (const idx in last_row_pos_can_be_chosen) {
last_row_pos_can_be_chosen[idx] = parseInt(last_row_pos_can_be_chosen[idx]);
}
for (const idx in zz) {
zz[idx] = parseInt(zz[idx]);
}
for (const idx in person) {
person[idx] = parseInt(person[idx]);
}
} catch (error) {
console.log(error);
log("config文件格式错误,无法读取!");
}
}
function log(msg) {
logBox.value += msg + "\n";
logBox.scrollTop = logBox.scrollheight;
}
function resetTable() {
let result = document.createElement("span");
result.id = "tb"
console.log(result);
let node = document.getElementById("res");
console.log(node);
node.parentNode.insertBefore(result, node);
node.remove();
}
function initTable() {
let node;
let result = document.createElement("table");
result.className = "tc mt";
result.id = "res"
let insideHtml = tbody;
for (let _ = 0; _ < rowCount; _++) {
insideHtml += tr;
for (let __ = 0; __ < columnCount; __++) {
insideHtml += td + nothing + btd;
}
insideHtml += btr;
}
insideHtml += btbody;
result.innerHTML = insideHtml;
node = document.getElementById("tb");
let special = document.createElement("span");
let special1 = document.createElement("br");
special.setAttribute("style", "width: 100%;");
special.innerHTML = `<div style="text-align: center">幸运观众: <span id="special">-</span></div>`
node.parentNode.insertBefore(result, node);
node.parentNode.insertBefore(special1, node);
node.parentNode.insertBefore(special, node);
node.remove();
document.getElementById("generate").addEventListener("click", generate);
document.getElementById("generate").addEventListener("click", () => {
const gen = document.getElementById("generate");
gen.className = "am-btn am-btn-default am-round am-disabled";
gen.innerHTML = "生成中 <i class=\"am-icon-circle-o-notch am-icon-spin\"></i>";
})
}
function initLog() {
log(`Hello Random Seat by EDP2021C1 EEC`);
}
function resetButton() {
const gen = document.getElementById("generate");
gen.className = "am-btn am-btn-primary am-round";
gen.innerHTML = "开始生成!";
}
function failed() {
const gen = document.getElementById("generate");
gen.className = "am-btn am-btn-danger am-round";
gen.innerHTML = "生成失败!";
setTimeout(resetButton, 1000);
}
function success() {
const gen = document.getElementById("generate");
gen.className = "am-btn am-btn-success am-round";
gen.innerHTML = "生成成功!";
setTimeout(resetButton, 1000);
}
function setTable(dat) {
let result = document.getElementById("res");
const ztd = "<td class=\"zz\">";
let insideHtml = tbody;
const zz = dat.zz;
const seat = dat.seat;
const special = dat.special;
// console.log("seat in setTable function const");
// console.log(seat);
for (let i = 0; i < rowCount; i++) {
insideHtml += tr;
for (let j = 0; j < columnCount; j++) {
if (zz[j] === i) {
export_table[i + 1][j] = "*" + seat[i][j] + "*";
insideHtml += ztd;
} else {
export_table[i + 1][j] = seat[i][j];
insideHtml += td;
}
insideHtml += seat[i][j];
insideHtml += btd;
}
insideHtml += btr;
}
for (let j = 0; j < columnCount; j++) export_table[0][j] = "第" + (j + 1) + "列";
insideHtml += btbody;
result.innerHTML = insideHtml;
document.getElementById("special").innerText = special;
}
function checkSpl(a, b, seat) {
for (let i = 0; i < rowCount; i++) {
for (let j = 0; j < columnCount; j++) {
if (seat[i][j] === a || seat[i][j] === b) {
if (i !== 0) {
if (seat[i - 1][j] === a || seat[i - 1][j] === b) return false;
if (j !== 0) {
if (seat[i - 1][j - 1] === a || seat[i - 1][j - 1] === b) return false;
}
if (j !== columnCount - 1) {
if (seat[i - 1][j + 1] === a || seat[i - 1][j + 1] === b) return false;
}
}
if (i !== rowCount - 1) {
if (seat[i + 1][j] === a || seat[i + 1][j] === b) return false;
if (j !== 0) {
if (seat[i + 1][j - 1] === a || seat[i + 1][j - 1] === b) return false;
}
if (j !== columnCount - 1) {
if (seat[i + 1][j + 1] === a || seat[i + 1][j + 1] === b) return false;
}
}
if (j !== 0) {
if (seat[i][j - 1] === a || seat[i][j - 1] === b) return false;
}
if (j !== columnCount - 1) {
if (seat[i][j + 1] === a || seat[i][j + 1] === b) return false;
}
}
}
}
return true;
}
/*
const checkCom = (a, b, seat) => {
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (seat[i][j] === a || seat[i][j] === b) {
if (i !== 0) {
if (seat[i - 1][j] === a || seat[i - 1][j] === b) return true;
}
if (i !== n - 1) {
if (seat[i + 1][j] === a || seat[i + 1][j] === b) return true;
}
if (j !== 0) {
if (seat[i][j - 1] === a || seat[i][j - 1] === b) return true;
}
if (j !== m - 1) {
if (seat[i][j + 1] === a || seat[i][j + 1] === b) return true;
}
}
}
}
// To disable This, edit this to "return true;"
return true;
}
*/
function checkValid(dat) {
const seat = dat.seat;
const zz = dat.zz;
// const com = dat.com;
const spl = dat.spl;
let finalzz = new Array(columnCount).fill(0);
for (let j = 0; j < columnCount; j++) {
let havezz = false;
let currZzList = new Array(0);
for (let i = 0; i < rowCount; i++) {
for (let k = 0; k < zz.length; k++) {
if (seat[i][j] === zz[k]) {
currZzList.push(i);
havezz = true;
break;
}
}
}
if (!havezz) return [false];
finalzz[j] = currZzList[Math.floor(Math.random() * currZzList.length)];
}
for (let i = 0; i < spl.length; i++) {
if (!checkSpl(spl[i][0], spl[i][1], seat)) return [false];
}
// for (let i = 0; i < com.length; i++) {
// if (!checkCom(com[i][0], com[i][1], seat)) return [false];
// }
return [true, finalzz];
}
async function generate() {
let generation = 0;
log("Checking format...");
let res;
try {
// let com = document.getElementById("com").value.split("\n");
let spl = document.getElementById("spl").value.split("\n");
let seed = document.getElementById("seed").value;
Math.seedrandom(seed);
if (spl[0] !== "") {
for (const idx in spl) {
spl[idx] = spl[idx].split(" ");
spl[idx][0] = parseInt(spl[idx][0]);
spl[idx][1] = parseInt(spl[idx][1]);
}
} else spl = [];
// if (com[0] != "") {
// for (const idx in com) {
// com[idx] = com[idx].split(" ");
// com[idx][0] = parseInt(com[idx][0]);
// com[idx][1] = parseInt(com[idx][1]);
// }
// } else com = [];
res = {
"status": 1,
"spl": spl,
// "com": com
}
} catch {
res = {
"status": 0
}
}
if (res.status === 0) {
log("Invalid format!");
failed();
return;
}
let rs;
let ans = [];
let tmp = [];
let last_row = [];
let pos = [];
const resspl = res.spl;
// var rescom = res.com;
let vis = [];
let seat = new Array(rowCount).fill().map(() => new Array(columnCount).fill("-"));
// console.log(person);
while (generation >= 0) {
generation++;
for (let now_row = 0; now_row < rowCount; now_row += K) {
tmp.length = 0;
if (rowCount - now_row - K === 0) {
for (let i = 0; i < person.length - now_row * columnCount; i++) {
tmp[i] = person[i + now_row * columnCount];
}
shuffle(tmp);
for (let i = 0; i < (K - 1) * columnCount; i++) {
ans[i + now_row * columnCount] = tmp[i];
}
for (let i = 0; i < person.length - (now_row + K - 1) * columnCount; i++) {
last_row[i] = tmp[i + (K - 1) * columnCount];
}
break;
} else if (rowCount - now_row - K === 1) {
for (let i = 0; i < person.length - now_row * columnCount; i++) {
tmp[i] = person[i + now_row * columnCount];
}
shuffle(tmp);
for (let i = 0; i < K * columnCount; i++) {
ans[i + now_row * columnCount] = tmp[i];
}
for (let i = 0; i < person.length - (now_row + K) * columnCount; i++) {
last_row[i] = tmp[i + K * columnCount];
}
break;
}
for (let i = 0; i < K * columnCount; i++) {
tmp[i] = person[i + now_row * columnCount];
}
shuffle(tmp);
for (let i = 0; i < K * columnCount; i++) {
ans[i + now_row * columnCount] = tmp[i];
}
}
for (let i = 0; i < rowCount; i++)
for (let j = 0; j < columnCount; j++)
seat[i][j] = "-";
for (let i = 0; i < rowCount - 1; i++)
for (let j = 0; j < columnCount; j++)
seat[i][j] = ans[i * columnCount + j];
if (last_row.length - 1 > last_row_pos_can_be_chosen.length) {
log("生成失败,最后一行人数大于可选择位置数!");
failed();
return;
}
for (let i = 0; i < columnCount; i++) {
vis[i] = 0;
pos[i] = 0;
}
for (let i = 0; i < last_row.length - 1; i++) {
let rand_num = Math.floor(Math.random() * last_row_pos_can_be_chosen.length);
while (vis[rand_num]) {
rand_num = Math.floor(Math.random() * last_row_pos_can_be_chosen.length)
}
vis[rand_num] = 1;
pos[i] = last_row_pos_can_be_chosen[rand_num] - 1;
}
for (let i = 0; i < last_row.length - 1; i++) {
seat[rowCount - 1][pos[i]] = last_row[i];
}
rs = checkValid({
"seat": seat,
"zz": zz,
// "com": rescom,
"spl": resspl
});
if (rs[0]) {
await setTable({
"seat": seat,
"zz": rs[1],
"special": last_row[last_row.length - 1]
})
log("Success Generation " + generation + "!");
success();
return;
}
}
}
initLog()
document.getElementById("export").addEventListener("click", () => {
// console.log(export_table);
const newList = export_table.map(res => res.join(","))
let data = newList.join("\n")
data += "\n";
data += "seed,";
data += document.getElementById("seed").value;
data += ",special,";
data += document.getElementById("special").innerText;
console.log(data);
const uri = "data:text/csv;charset=utf-8,\ufeff" + encodeURIComponent(data);
const downloadLink = document.createElement("a");
downloadLink.href = uri;
const d = new Date();
const year = d.getFullYear() - 100 + 2000;
const month = d.getMonth() + 1;
const date = d.getDate();
downloadLink.download = (year + "-" + month + "-" + date + ".csv") || "temp.csv";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
})
document.getElementById("chooseFile").addEventListener("click", () => {
upload();
document.querySelector("#file").addEventListener("change", e => {
for (let entry of e.target.files) {
document.getElementById("fileNameInput").value = entry.name;
}
let file = e.target.files[0];
let file_reader = new FileReader();
file_reader.onload = () => {
try {
let fc = file_reader.result;
let fc_json = JSON.parse(fc);
read(fc_json);
resetTable();
initTable(); //读完 n,m 才有值
export_table = new Array(rowCount + 1).fill(0).map(_ => new Array(columnCount));
} catch (error) {
console.log(error);
log("config文件格式错误,无法读取!");
}
};
file_reader.readAsText(file, "UTF-8");
});
});
fetch("./config.json")
.then((response) => response.json())
.then((json) => {
read(json);
initTable(); //读完 n,m 才有值
export_table = new Array(rowCount + 1).fill(0).map(_ => new Array(columnCount));
})
document.getElementById("random-seed").addEventListener("click", () => {
let tmp = Math.floor(Math.random() * 1e9);
Math.seedrandom(tmp);
document.getElementById("seed").value = tmp;
})
document.getElementById("set-seed-use-time").addEventListener("click", () => {
document.getElementById("seed").value = new Date(+new Date() + 8 * 3600 * 1000).toISOString().split(".")[0].replaceAll(/T|Z/gi, " ");
})
document.getElementById("fill5").addEventListener("click", () => {
document.getElementById("spl").value = defaultspl;
})