-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpredictCooldown.js
114 lines (95 loc) · 2.88 KB
/
predictCooldown.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
import fs from "fs";
// XXX: PLEASE DO NOT MODIFY
const cooldownFile = 'cooldown.json';
export function addCooldown(email) {
let cooldownData = {};
try {
cooldownData = JSON.parse(fs.readFileSync(cooldownFile));
}
catch (err) {
return {
errorMsg: `Function (addCooldown) Error: ${err.message}`
}
}
if (!(email in cooldownData) || isCooldownExpired(cooldownData[email])) {
cooldownData[email] = Date.now();
fs.writeFileSync(cooldownFile, JSON.stringify(cooldownData, null, 2));
const nextSubmitTimestamp = cooldownData[email] + (31 * 60 * 1000);
const nextSubmitDate = new Date(nextSubmitTimestamp);
const nextSubmitTime = nextSubmitDate.toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true });
return {
hr: nextSubmitTime,
added: true
};
}
else {
return checkCooldown(email);
}
}
export function checkCooldown(email) {
let cooldownData = {};
try {
cooldownData = JSON.parse(fs.readFileSync(cooldownFile));
}
catch (err) {
return {
errorMsg: `Function (checkCooldown) Error: ${err.message}`
}
}
if (email in cooldownData) {
const nextSubmitTimestamp = cooldownData[email] + (31 * 60 * 1000);
const nextSubmitDate = new Date(nextSubmitTimestamp);
const nextSubmitTime = nextSubmitDate.toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true });
return {
min: getCooldown(email) === 0 ? deleteCooldown(email) : getCooldown(email) + "min",
hr: nextSubmitTime
};
}
else {
return {
errorMsg: `Function (checkCooldown) Error: Can't Find Cooldown for ${email}`
}
}
}
function deleteCooldown(email) {
let cooldownData = {};
try {
cooldownData = JSON.parse(fs.readFileSync(cooldownFile));
}
catch (err) {
return {
errorMsg: `Function (deleteCooldown) Error: ${err.message}`
}
}
if (email in cooldownData) {
delete cooldownData[email];
fs.writeFileSync(cooldownFile, JSON.stringify(cooldownData, null, 2));
//console.log(`Deleted cooldown for ${email}`);
return true
}
else {
return {
errorMsg: `Function (deleteCooldown) Error: Can't Find Cooldown for ${email}`
}
}
}
function getCooldown(email) {
const cooldownData = JSON.parse(fs.readFileSync(cooldownFile));
if (email in cooldownData) {
const timestamp = cooldownData[email];
const now = Date.now();
const cooldownDuration = 31 * 60 * 1000; // 30 minutes in milliseconds
const remainingTime = cooldownDuration - (now - timestamp);
const remainingMinutes = Math.floor(remainingTime / (60 * 1000));
return remainingMinutes;
}
else {
return null;
}
}
function isCooldownExpired(timestamp) {
const now = Date.now();
const cooldownDuration = 31 * 60 * 1000;
return now - timestamp >= cooldownDuration;
}
//console.log(addCooldown("kyle"))