-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcheck-in.js
159 lines (143 loc) · 3.67 KB
/
check-in.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
const fs = require("fs");
const path = require("path");
const { getDay } = require("../utils/day");
const us = require("../static/users/index");
const meta = require("../static/meta.json");
const mySolutions = require("../static/my/solutions.json");
const { whitelist } = require("../config/index");
const allUsers = JSON.parse(JSON.stringify(us));
// 清空补签卡
function resetCards() {
for (const name in allUsers) {
allUsers[name].card = 0;
}
fs.writeFileSync(
path.resolve(__dirname, "../static/users/index.json"),
JSON.stringify(allUsers),
);
}
function run(n) {
// 返回目前为止满勤的人(连续七天可获取补签卡)
function fullCheckIn(from = 1, to = getDay()) {
const users = [];
const DAYS_TO_GET_CARD = 7;
for (const login in mySolutions) {
const solutions = mySolutions[login];
let i = from - 1;
let card = 0; // 补签卡数量
let noCheckDays = 0;
let continuousDays = 0; // 连续打卡的天数
while (i < to) {
if (!solutions || !solutions[i] || !solutions[i].onTime) {
continuousDays = 0;
noCheckDays++;
} else {
continuousDays++;
if (continuousDays == DAYS_TO_GET_CARD) {
card++;
continuousDays = 0;
}
}
i++;
}
if (card >= noCheckDays) {
users.push({ login, card: card - noCheckDays });
}
}
return users;
}
// 返回7天内(不包括今天)所有已经打卡的
function checkWithinNDays(n) {
const users = {};
const day = getDay();
if (day <= n) {
return us; // 至少第 n + 1 天才能开始统计前 n 天
}
for (const name in us) {
if (whitelist.includes(name)) {
users[name] = true;
continue;
}
us[name].noCheck = false;
us[name].allCheck = false;
const solutions = mySolutions[name];
if (!us[name].createTime) {
us[name].createTime = new Date().getTime();
}
// 如果注册时间不满 n 天,则算打过卡。
if ((day - getDay(us[name].createTime) + 1) <= n) {
users[name] = true;
continue;
}
// [day - n - 2, day - 2]
let i = day - n - 1;
let count = 0;
while (i < (day - 1)) {
if (!solutions || !solutions[i] || !solutions[i].onTime) {
count += 1;
} else {
count = 0;
}
i++;
}
if (count < n) {
users[name] = true;
}
}
return users;
}
function diff(A, B) {
for (const name in B) {
delete A[name.toLocaleLowerCase()];
}
return Object.keys(A);
}
const graplist = checkWithinNDays(n);
const blacklist = diff(allUsers, graplist).filter(
(name) => !/[A-Z]/.test(name),
);
const redlist = fullCheckIn();
console.log(`no check within ${n} days`, JSON.stringify(blacklist));
console.log(`full check`, redlist);
for (const login of Object.keys(graplist)) {
// 更新 users 信息。 如果七天没有打过卡,则直接不会更新到 users
if (!us[login]) {
us[login] = {};
}
us[login].noCheck = false;
}
for (const red of redlist) {
if (!us[red.login]) {
us[red.login] = {};
}
us[red.login].allCheck = true;
us[red.login].noCheck = false;
us[red.login].card = red.card;
}
for (const login of blacklist) {
if (!us[login]) {
us[login] = {};
}
us[login].noCheck = true;
}
fs.writeFileSync(
path.resolve(__dirname, "../static/users/index.json"),
JSON.stringify(us),
);
if (!meta.checkIn) {
meta.checkIn = {};
}
meta.checkIn.lastUpdateTime = new Date().getTime();
fs.writeFileSync(
path.resolve(__dirname, "../static/meta.json"),
JSON.stringify(meta),
);
}
const lastUpdateTime = meta.checkIn ? -1 : -1;
const MS_PER_HOUR = 1 * 60 * 60 * 1000;
const TODAY = getDay(new Date().getTime() - MS_PER_HOUR);
// 一天仅检查一次
if ((TODAY - getDay(lastUpdateTime)) > 0) {
run(7);
}
// resetCards();