Showing
2 changed files
with
19 additions
and
14 deletions
... | @@ -20,7 +20,6 @@ const dataFileName = 'data.json'; | ... | @@ -20,7 +20,6 @@ const dataFileName = 'data.json'; |
20 | 20 | ||
21 | var fakerData = {}; | 21 | var fakerData = {}; |
22 | var quizList = []; | 22 | var quizList = []; |
23 | -var isWaitAns = false; | ||
24 | var users = {}; | 23 | var users = {}; |
25 | var sumOfStreak = 0; | 24 | var sumOfStreak = 0; |
26 | var sumOfTry = 0; | 25 | var sumOfTry = 0; |
... | @@ -421,23 +420,28 @@ app.post('/hook', async function (req, res) { | ... | @@ -421,23 +420,28 @@ app.post('/hook', async function (req, res) { |
421 | // request log | 420 | // request log |
422 | console.log('======================', new Date(), '======================'); | 421 | console.log('======================', new Date(), '======================'); |
423 | console.log('[request]', req.body); | 422 | console.log('[request]', req.body); |
424 | - console.log('[request source] ', eventObj.source); | 423 | + console.log('[request source] ', source); |
425 | - console.log('[request message]', eventObj.message); | 424 | + console.log('[request message]', message); |
426 | 425 | ||
427 | - if (!isWaitAns && eventObj.message.text == '시작') { | 426 | + if (!users.hasOwnProperty(source.userId)) { |
428 | - await checkData(); | 427 | + users[source.userId] = { |
429 | - users[eventObj.source.userId] = { | 428 | + isWaitingAns: false, |
430 | streak: 0, | 429 | streak: 0, |
431 | quizAns: null, | 430 | quizAns: null, |
432 | quizInfo: null | 431 | quizInfo: null |
433 | }; | 432 | }; |
434 | - sendQuiz(eventObj.replyToken, eventObj.source.userId, true); | 433 | + } |
435 | - } else if (isWaitAns) { | 434 | + |
436 | - if (checkAns(eventObj.source.userId, eventObj.message.text)) { | 435 | + if (!users[source.userId].isWaitingAns && message.text == '시작') { |
437 | - users[eventObj.source.userId].streak++; | 436 | + await checkData(); |
438 | - sendQuiz(eventObj.replyToken, eventObj.source.userId, false); | 437 | + |
438 | + sendQuiz(eventObj.replyToken, source.userId, true); | ||
439 | + } else if (users[source.userId].isWaitingAns) { | ||
440 | + if (checkAns(source.userId, message.text)) { | ||
441 | + users[source.userId].streak++; | ||
442 | + sendQuiz(eventObj.replyToken, source.userId, false); | ||
439 | } else { | 443 | } else { |
440 | - endQuiz(eventObj.replyToken, eventObj.source.userId); | 444 | + endQuiz(eventObj.replyToken, source.userId); |
441 | } | 445 | } |
442 | } | 446 | } |
443 | 447 | ||
... | @@ -485,7 +489,7 @@ function sendQuiz(replyToken, id, isInit) { | ... | @@ -485,7 +489,7 @@ function sendQuiz(replyToken, id, isInit) { |
485 | } | 489 | } |
486 | ); | 490 | ); |
487 | 491 | ||
488 | - isWaitAns = true; | 492 | + users[id].isWaitingAns = true; |
489 | } | 493 | } |
490 | 494 | ||
491 | function checkAns(id, ans) { | 495 | function checkAns(id, ans) { |
... | @@ -497,7 +501,7 @@ function checkAns(id, ans) { | ... | @@ -497,7 +501,7 @@ function checkAns(id, ans) { |
497 | } | 501 | } |
498 | 502 | ||
499 | function endQuiz(replyToken, id) { | 503 | function endQuiz(replyToken, id) { |
500 | - isWaitAns = false; | 504 | + users[id].isWaitingAns = false; |
501 | sumOfTry++; | 505 | sumOfTry++; |
502 | sumOfStreak += users[id].streak; | 506 | sumOfStreak += users[id].streak; |
503 | var averageStreak = sumOfStreak / sumOfTry; | 507 | var averageStreak = sumOfStreak / sumOfTry; | ... | ... |
-
Please register or login to post a comment