-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
550 lines (513 loc) · 17.8 KB
/
index.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
const fetch = require('node-fetch');
const { promisify } = require('util');
const writeFile = promisify(require('fs').writeFile);
const readFile = promisify(require('fs').readFile);
const config = require('./config');
const token = config.botToken;
const url = `https://api.telegram.org/bot${token}/`;
const startBotTime = new Date(config.startBotTime);
const endTime = new Date(config.endBotTime);
let lstOffset = 0;
const idToUsers = new Map();
process.on('unhandledRejection', (up) => { throw up; });
function div(t, x) {
return Math.floor(t / x);
}
const secret = {
password: config.adminPassword,
};
const constants = {
messages: {
welcome: msg => `سلام ${msg}. به سامانه آزمون گیر شاز خوش آمدید.`,
error: '?این چه پیامی است',
},
};
async function saveBackup() {
let data = { users: [] };
for (const user of idToUsers.values()) {
data.users.push(user);
}
data = JSON.stringify(data);
await writeFile(`backups/b${(new Date()).valueOf()}.json`, data);
await writeFile('dat.json', data);
}
function milisecondToFarsi(time) {
let t = time;
t = div(t, 1000);
let s = '';
if (t <= 0) return s;
s = `${t % 60} ثانیه ${s}`;
t = div(t, 60);
if (t <= 0) return s;
s = `${t % 60} دقیقه و ${s}`;
t = div(t, 60);
if (t <= 0) return s;
s = `${t % 24} ساعت و ${s}`;
t = div(t, 24);
if (t <= 0) return s;
s = `${t % 30} روز و ${s}`;
t = div(t, 30);
return s;
}
async function sendMessage(id, text, { keyboard, hideKeyboard = false }) {
let replyMarkup;
if (keyboard != null) replyMarkup = { keyboard };
if (hideKeyboard) replyMarkup = { hide_keyboard: true };
let res = await fetch(`${url}sendMessage`, {
method: 'POST',
body: JSON.stringify({
chat_id: id,
text,
reply_markup: replyMarkup,
}),
headers: { 'Content-Type': 'application/json' },
});
res = await res.json();
if (res.ok !== true) {
console.log(res);
}
return res;
}
async function forwardMessage(id, msg) {
console.log(msg);
let res = await fetch(`${url}forwardMessage`, {
method: 'POST',
body: JSON.stringify({
chat_id: id,
from_chat_id: msg.chat.id,
message_id: msg.message_id,
}),
headers: { 'Content-Type': 'application/json' },
});
res = await res.json();
if (res.ok !== true) {
console.log(res);
}
return res;
}
const { contest } = config;
const initUserContest = () => contest.map(c => ({
problem: c.problem.map(() => ({
answer: (c.type === 'option' ? 0 : undefined),
score: 0,
sahihState: 'noAnswer',
})),
}));
class User {
constructor({
state = 'init', nowContest = 0, realName, id, isAdmin = false, contest = initUserContest(), startTime = startBotTime,
}) {
this.state = state;
this.id = id;
this.isAdmin = isAdmin;
this.picker = { on: false };
this.contest = contest;
this.realName = realName;
this.nowContest = nowContest;
this.startTime = new Date(startTime);
}
reset() {
this.state = 'init';
this.contest = initUserContest();
this.nowContest = 0;
}
getFinalScore(id) {
let res = 0;
if (contest[id].type === 'option') {
if (contest[id].answer === '-') {
return 'هنوز معلوم نیست.';
}
this.contest[id].problem.forEach((p, i) => {
const score = (ans, jdg, sc) => {
if (ans === 0) return 0;
if (String(ans) === jdg) return sc;
return -sc / 4;
};
res += score(p.answer, contest[id].answer[i], contest[id].problem[i].score);
});
return res;
}
for (const p of this.contest[id].problem) {
if (p.sahihState === 'notDone' || p.sahihState === 'doing') {
return 'هنوز معلوم نیست.';
}
res += p.score;
}
return res;
}
getAllScore() {
return this.contest.map((x, i) => this.getFinalScore(i)).reduce((a, b) => (a === 'هنوز معلوم نیست.' || b === 'هنوز معلوم نیست.' ? 'هنوز معلوم نیست.' : a + b));
}
renderNatije() {
const finalScore = this.getAllScore();
const fhand = (x, i) => `نمره آزمون ${contest[i].nam}:${this.getFinalScore(i)}\n${x.problem.map((y, j) => {
if (y.sahihState === 'noAnswer') {
return `سوال ${contest[i].problem[j].nam} : ${y.score} از ${contest[i].problem[j].score} ( سفید )\n`;
}
if (y.sahihState === 'notDone') {
return `سوال ${contest[i].problem[j].nam} : صحیح نشده\n`;
}
if (y.sahihState === 'doing') {
return `سوال ${contest[i].problem[j].nam} : در حال تصحیح\n`;
}
return `سوال ${contest[i].problem[j].nam} : ${y.score} از ${contest[i].problem[j].score}\n`;
})}`;
const foption = (x, i) => {
let res = `نمره آزمون ${contest[i].nam}:${this.getFinalScore(i)}\n`;
if (contest[i].answer !== '-') {
res += x.problem.map((y, j) => {
if (y.answer === 0) {
return `سوال ${contest[i].problem[j].nam} : ◻️ پاسخ درست ${contest[i].answer[j]}`;
}
if (`${y.answer}` === contest[i].answer[j]) {
return `سوال ${contest[i].problem[j].nam} : ✅ پاسخ شما ${contest[i].answer[j]}`;
}
return `سوال ${contest[i].problem[j].nam} : ❌ پاسخ شما ${y.answer} پاسخ درست ${contest[i].answer[j]}`;
}).join('\n');
}
return res;
};
return `نتیجه شما:\nنمره نهایی شما: ${finalScore}\n${this.contest.map((x, i) => {
if (contest[i].type === 'option') return foption(x, i);
return fhand(x, i);
})}`;
}
inAzmoonKeyboard() {
const baze = (l, r) => new Array(r - l)
.fill()
.map((_, i) => ({ text: contest[this.nowContest].problem[i + l].nam }));
const jadval = n => new Array(div(n + 4, 5))
.fill()
.map((_, i) => baze(i * 5, Math.min(n, i * 5 + 5)));
return [...jadval(contest[this.nowContest].problem.length), [{ text: 'وضعیت آزمون' }]];
}
async finishContest() {
this.state = 'before_azmoon';
this.nowContest += 1;
const keyboard = (this.nowContest === contest.length
? [[{ text: 'نتیجه را بگو' }]]
: [[{ text: 'چقدر مونده؟' }]]
);
await this.sendMessage('وقت شما به پایان رسید', { keyboard });
}
async forwardMessage(msg) {
return forwardMessage(this.id, msg);
}
async sendMessage(text, options) {
if (options == null) {
return sendMessage(this.id, text, {});
}
return sendMessage(this.id, text, options);
}
async handleInit() {
this.state = 'start';
await this.sendMessage('خوش آمدید. نام و نام خانوادگی خود را وارد کنید ( برای گرفتن پیتزا باید شناسنامه بیاورید)');
}
async handleStart(msg) {
const farsiNameChecker = x => (x.match(/[a-z,A-Z]/) == null && x.match(/ /) != null);
if (!farsiNameChecker(msg)) {
await this.sendMessage('لطفا نام و نام خانوادگی خود را به صورت فارسی و کامل وارد نمایید');
return;
}
this.realName = msg;
this.state = 'waiting';
const keyboard = [[{ text: 'چقدر مونده؟' }]];
await this.sendMessage(constants.messages.welcome(msg), { keyboard });
}
async handleWaiting() {
if (startBotTime - (new Date()) < 0) {
this.state = 'before_azmoon';
return this.handleBeforeAzmoon('چقدر مونده؟');
}
this.state = 'waiting';
const keyboard = [[{ text: 'چقدر مونده؟' }]];
await this.sendMessage(`${milisecondToFarsi(startBotTime - (new Date()))} به زمان آزمون مانده است.`, { keyboard });
}
async handleBeforeAzmoon(msg) {
if (this.nowContest === contest.length) {
await this.sendMessage(this.renderNatije());
return;
}
if (msg === 'آزمون را شروع کن.') {
this.startTime = new Date();
this.state = 'in_azmoon';
await Promise.all([
this.sendMessage('آزمون شروع شد', { keyboard: this.inAzmoonKeyboard() }),
(contest[this.nowContest].soorat == null)
? this.sendMessage('صورت سوالات در دسترس نیست.')
: this.forwardMessage(contest[this.nowContest].soorat),
]);
return;
}
const keyboard = [[{ text: 'آزمون را شروع کن.' }, { text: 'چقدر مونده؟' }]];
const matn = contest.map((x, i) => {
if (i < this.nowContest) return '';
return `آزمون ${x.nam} ${x.duration} ساعته ،\n`;
}).reduce((a, b) => a + b);
await this.sendMessage(
`${milisecondToFarsi(endTime - (new Date()))} به پایان مهلت آزمون مانده است.
:آزمون های پیش رو
${matn}در هر بازه ${contest[this.nowContest].duration} ساعته می توانید آزمون را شروع کنید.`, { keyboard },
);
}
async handleInAzmoon(msg) {
const rmTime = () => contest[this.nowContest].duration * 60 * 60 * 1000
- (new Date() - this.startTime);
if (rmTime() < 0) {
await this.finishContest();
return;
}
if (msg === 'وضعیت آزمون') {
let answers = '';
if (contest[this.nowContest].type === 'option') { answers = this.contest[this.nowContest].problem.map((x, i) => `سوال ${contest[this.nowContest].problem[i].nam}: پاسخ شما ${(x.answer === 0 ? 'نزده' : x.answer)}\n`); }
await this.sendMessage(`${milisecondToFarsi(rmTime())}از وقت شما باقی مانده است.\n${answers}`);
return;
}
const id = contest[this.nowContest].problem.findIndex(x => x.nam === msg);
if (id !== -1) {
const optionKeyboard = (contest[this.nowContest].type === 'hand'
? { hideKeyboard: true }
: {
keyboard: [
[{ text: '1' }, { text: '2' }, { text: '3' }, { text: '4' }, { text: '5' }],
[{ text: 'نزده' }],
],
}
);
await this.sendMessage(`پاسخ خود را در قالب یک پیام برای سوال ${msg} ارسال کنید.`, optionKeyboard);
this.picker.on = true;
this.picker.send = async (m) => {
if (rmTime() < 0) {
await this.finishContest();
return;
}
if (contest[this.nowContest].type === 'option') {
let msgText = m.text;
if (msgText === 'نزده') msgText = '0';
if (!(msgText).match(/^\d+$/)) {
await this.sendMessage(`پاسخ شما به کلی نادیده گرفته شد. پاسخ باید عددی بین 0 تا 5 باشد اما ${msgText} است.`, { keyboard: this.inAzmoonKeyboard() });
return;
}
const jv = Number(msgText);
if (jv < 0 || jv > 5) {
await this.sendMessage(`پاسخ شما به کلی نادیده گرفته شد. پاسخ باید بین 0 تا 5 باشد اما ${msgText} است.`, { keyboard: this.inAzmoonKeyboard() });
return;
}
this.contest[this.nowContest].problem[id].answer = jv;
await this.sendMessage('پاسخ شما ثبت شد.', { keyboard: this.inAzmoonKeyboard() });
return;
}
this.contest[this.nowContest].problem[id].answer = m;
this.contest[this.nowContest].problem[id].sahihState = 'notDone';
await this.sendMessage('پاسخ شما ثبت شد.', { keyboard: this.inAzmoonKeyboard() });
};
return;
}
if (contest[this.nowContest].type === 'option') {
let prs = msg.match(/\d+:\d+/g);
if (prs != null) {
prs = prs.map(x => x.split(':'));
for (const q of prs) {
const ind = contest[this.nowContest].problem.findIndex(x => x.nam === q[0]);
if (ind === -1) {
await this.sendMessage(`پاسخ شما به کلی نادیده گرفته شد. سوال ${q[0]} وجود ندارد.`);
return;
}
if (!(q[1].match(/^\d+$/))) {
await this.sendMessage(`پاسخ شما به کلی نادیده گرفته شد. پاسخ باید بین 0 تا 5 باشد اما ${q[1]} است.`);
return;
}
const jv = Number(q[1]);
if (jv < 0 || jv > 5) {
await this.sendMessage(`پاسخ شما به کلی نادیده گرفته شد. پاسخ باید بین 0 تا 5 باشد اما ${q[1]} است.`);
return;
}
}
for (const q of prs) {
const ind = contest[this.nowContest].problem.findIndex(x => x.nam === q[0]);
const jv = Number(q[1]);
this.contest[this.nowContest].problem[ind].answer = jv;
}
await this.sendMessage(`پاسخ ${prs.length} سوال با موفقیت تغییر یافت`);
return;
}
prs = msg.split('');
if (prs.length === contest[this.nowContest].problem.length) {
for (const x of prs) {
if (!(x.match(/^\d+$/))) {
await this.sendMessage(`پاسخ شما به کلی نادیده گرفته شد. پاسخ باید بین 0 تا 5 باشد اما ${x} است.`);
return;
}
const jv = Number(x);
if (jv < 0 || jv > 5) {
await this.sendMessage(`پاسخ شما به کلی نادیده گرفته شد. پاسخ باید بین 0 تا 5 باشد اما ${x} است.`);
return;
}
}
prs.forEach((x, i) => {
this.contest[this.nowContest].problem[i].answer = x;
});
await this.sendMessage('پاسخ تمامی سوالات با موفقیت تغییر یافت');
return;
}
}
await this.sendMessage(constants.messages.error);
}
async handleAdmin(message) {
const msg = message.split(' ');
if (msg[0] === 'resetMe') {
this.reset();
await this.sendMessage('done');
return;
}
if (msg[0] === 'eval') {
const txt = msg.slice(1).join(' ');
// eslint-disable-next-line no-eval
eval(txt);
return;
}
if (msg[0] === 'cat') {
await this.sendMessage(msg[1]);
return;
}
if (msg[0] === 'backup') {
await saveBackup();
await this.sendMessage('done');
return;
}
if (msg[0] === 'setSoorat') {
await this.sendMessage('soorato bede');
this.picker.on = true;
this.picker.send = async (m) => {
contest[msg[1]].soorat = m;
await this.sendMessage('soorat tanzim shod');
};
return;
}
if (msg[0] === 'scoreboard') {
const pm = [...idToUsers.values()].map(x => ({
nam: x.realName,
score: x.getAllScore(),
})).sort((a, b) => {
if (a.score === b.score) return 0;
if (a.score === 'هنوز معلوم نیست') return -1;
if (b.score === 'هنوز معلوم نیست') return 1;
return b.score - a.score;
}).map((a, i) => `${i + 1}. ${a.nam} --- ${a.score}`).join('\n');
await this.sendMessage(pm);
return;
}
if (msg[0] === 'pick') {
for (const u of idToUsers.values()) {
const p = u.contest[msg[1]].problem[msg[2]];
if (p.answer != null && p.sahihState === 'notDone') {
p.sahihState = 'doing';
await this.forwardMessage(p.answer);
this.picker.on = true;
this.picker.send = async (m) => {
const mt = m.text;
if (!(mt.match(/^\d+$/))) {
p.sahihState = 'notDone';
await this.sendMessage('cancel shod');
return;
}
p.sahihState = 'Done';
p.score = Number(mt);
await this.sendMessage('nomre tanzim shod');
};
return;
}
}
await this.sendMessage('tashihe in soal kamel shode :)');
return;
}
await this.sendMessage('error');
}
async handleMessage(msg) {
if (this.picker.on) {
this.picker.on = false;
return this.picker.send(msg);
}
let msgText = msg.text;
if (typeof msgText !== 'string') {
msgText = '';
}
if (msgText[0] !== '' && msgText[0] === '$') {
msgText = msgText.substr(1);
if (this.isAdmin) {
return this.handleAdmin(msgText);
}
if (msgText === secret.password) {
await this.sendMessage('تبریک میگم شما ادمین شدید.');
this.isAdmin = true;
return;
}
await this.sendMessage(constants.messages.error);
return;
}
if (this.state === 'init') {
return this.handleInit();
}
if (this.state === 'start') {
return this.handleStart(msgText);
}
if (this.state === 'waiting') {
return this.handleWaiting();
}
if (this.state === 'before_azmoon') {
return this.handleBeforeAzmoon(msgText);
}
if (this.state === 'in_azmoon') {
return this.handleInAzmoon(msgText);
}
await this.sendMessage('Not Implemented Yet');
}
}
async function loadBackup() {
let data = await readFile('dat.json');
data = JSON.parse(data);
data.users.forEach((element) => {
const u = new User(element);
idToUsers.set(u.id, u);
});
}
function handleError(e) {
console.log(e);
for (const u of idToUsers.values()) {
if (u.isAdmin) {
u.sendMessage(`${e}`);
}
}
}
async function loop() {
let res = await fetch(`${url}getUpdates`, {
method: 'POST',
body: JSON.stringify({
offset: lstOffset + 1,
}),
headers: { 'Content-Type': 'application/json' },
});
res = await res.json();
res = res.result;
await Promise.all(res.map(async (element) => {
lstOffset = element.update_id;
console.log(element);
const { id } = element.message.chat;
if (!idToUsers.has(id)) {
idToUsers.set(id, new User({ id }));
}
const user = idToUsers.get(id);
await user.handleMessage(element.message);
}));
}
async function main() {
await loadBackup();
for (;;) {
try {
await loop();
} catch (e) {
handleError(e);
}
}
}
main();