-
Notifications
You must be signed in to change notification settings - Fork 4
/
pepper.js
188 lines (168 loc) · 5.65 KB
/
pepper.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
const Duration = require("duration-js");
function pluralize(amount, unit) {
if (amount > 1) {
return amount + " " + unit + "s";
}
return amount + " " + unit;
}
function formatDuration(dur) {
const year = 365 * Duration.day;
const month = 30 * Duration.day;
const out = [];
let d = dur;
if (d > year) {
const years = Math.floor(d / year);
out.push(pluralize(years, "year"));
d = Duration.subtract(d, years * year);
}
if (d > month) {
const months = Math.floor(d / month)
out.push(pluralize(months, "month"));
d = Duration.subtract(d, months * month);
}
if (d.weeks() > 0) {
out.push(pluralize(d.weeks(), "week"));
d = Duration.subtract(d, d.weeks() * Duration.week);
}
if (d.days() > 0) {
out.push(pluralize(d.days(), "day"));
d = Duration.subtract(d, d.days() * Duration.day);
}
if (d.hours() > 0) {
out.push(pluralize(d.hours(), "hour"));
d = Duration.subtract(d, d.hours() * Duration.hour);
}
if (d.minutes() > 0) {
out.push(pluralize(d.minutes(), "minute"));
d = Duration.subtract(d, d.minutes() * Duration.minute);
}
return out.join(' ');
}
function formatSmallDuration(dur) {
const out = [];
let d = dur;
if (d.hours() > 0) {
out.push(pluralize(d.hours(), "hour"));
d = Duration.subtract(d, d.hours() * Duration.hour);
}
if (d.minutes() > 0) {
out.push(pluralize(d.minutes(), "minute"));
d = Duration.subtract(d, d.minutes() * Duration.minute);
}
if (d.seconds() > 0) {
out.push(pluralize(d.seconds(), "second"));
}
return out.join(' ');
}
function report(chatClient, channel, user, minutes) {
const duration = formatDuration(new Duration(minutes * Duration.minute));
chatClient.say(channel, `${user} has redeemed ${duration.toString()} of pepper time in total.`);
}
async function command(chatClient, db, channel, user, minutes) {
const { rows } = await db.query('SELECT pepper_minutes FROM peppertime WHERE user_name = $1', [user]);
let pepper = 0;
if (rows.length > 0) {
pepper = rows[0].pepper_minutes;
}
pepper += minutes;
report(chatClient, channel, user, pepper);
if (rows.length == 0) {
await db.query('INSERT INTO peppertime (user_name, pepper_minutes) VALUES($1, $2)', [user, pepper]);
} else {
await db.query('UPDATE peppertime SET pepper_minutes = $1 WHERE user_name = $2', [pepper, user]);
}
}
async function leaders(chatClient, db, channel) {
const { rows } = await db.query('SELECT user_name, pepper_minutes FROM peppertime ORDER BY pepper_minutes DESC LIMIT 3');
chatClient.say(channel, 'Your super shaker pepper cam leaders are:');
if (rows.length == 0) {
chatClient.say(channel, 'No one. For shame.');
}
for (const row of rows) {
report(chatClient, channel, row.user_name, row.pepper_minutes);
}
}
async function leadersResults(db) {
const { rows } = await db.query('SELECT user_name, pepper_minutes FROM peppertime ORDER BY pepper_minutes DESC LIMIT 3');
return rows.map(row => ({
'name': row.user_name,
'time': formatDuration(new Duration(row.pepper_minutes * Duration.minute))
}));
}
async function claimedLeaders(db) {
const { rows } = await db.query('SELECT user_name, pepper_seconds FROM claimedtime ORDER BY pepper_seconds DESC LIMIT 3');
return rows.map(row => ({
'name': row.user_name,
'time': formatSmallDuration(new Duration(row.pepper_seconds * Duration.second))
}));
}
// Set lastPing to epoch
let lastPing = 0;
let timer = 0;
let claimant = {};
function start() {
console.log("Pepper cam activated");
// Set lastPing to now
lastPing = Date.now();
timer = 0;
claimant = {};
}
function ping(secs) {
timer = Number(secs);
lastPing = Date.now();
return claimant;
}
function stop() {
console.log("Pepper cam maxed out");
lastPing = 0;
timer = 0;
claimant = {};
}
async function claim(chatClient, apiClient, db, channel, user) {
const since = Date.now() - lastPing;
if (since > 11 * 1000) {
chatClient.say(channel, `Pepper cam isn't active, ${user}. You goofball.`);
return;
}
const adjusted = timer + Math.floor(since / 1000);
console.log(`${adjusted}s is the adjusted time`);
const excess = adjusted - 15 * 60;
console.log(`${excess}s is the excess time`);
if (excess < 0) {
chatClient.say(channel, `Pepper cam isn't expired, ${user}. You silly.`);
return;
}
if (claimant.claimed) {
chatClient.say(channel, `Sorry ${user}, pepper cam time has already been reclaimed by ${claimant.user}.`);
return;
}
const target = await apiClient.users.getUserByName(user);
claimant = {
claimed: true,
profileURL: target.profilePictureUrl,
secs: adjusted,
user
};
chatClient.say(channel, `${user} has reclaimed ${formatSmallDuration(new Duration(excess * Duration.second))} of the pepper cam time.`);
const { rows } = await db.query('SELECT pepper_seconds FROM claimedtime WHERE user_name = $1', [user]);
let pepper = 0;
if (rows.length > 0) {
pepper = rows[0].pepper_seconds;
}
pepper += excess;
if (rows.length == 0) {
await db.query('INSERT INTO claimedtime (user_name, pepper_seconds) VALUES($1, $2)', [user, pepper]);
} else {
await db.query('UPDATE claimedtime SET pepper_seconds = $1 WHERE user_name = $2', [pepper, user]);
}
}
module.exports = {
claim,
claimedLeaders,
command,
leaders,
leadersResults,
ping,
start,
stop
};