Ubuntu

기능: userId별로 연속 정답 횟수 기록, 오답시 결과 전송

Showing 1 changed file with 41 additions and 3 deletions
...@@ -21,6 +21,8 @@ const dataFileName = 'data.json'; ...@@ -21,6 +21,8 @@ const dataFileName = 'data.json';
21 21
22 var fakerData = {}; 22 var fakerData = {};
23 var quizList = []; 23 var quizList = [];
24 +var isWaitAns = false;
25 +var users = {};
24 26
25 async function checkData() { 27 async function checkData() {
26 try { 28 try {
...@@ -276,15 +278,25 @@ app.post('/hook', async function (req, res) { ...@@ -276,15 +278,25 @@ app.post('/hook', async function (req, res) {
276 console.log('[request source] ', eventObj.source); 278 console.log('[request source] ', eventObj.source);
277 console.log('[request message]', eventObj.message); 279 console.log('[request message]', eventObj.message);
278 280
279 - if (eventObj.message.text == '시작') { 281 + if (!isWaitAns && eventObj.message.text == '시작') {
280 await checkData(); 282 await checkData();
281 - sendQuiz(eventObj.replyToken); 283 + users[eventObj.source.userId] = {
284 + streak: 0,
285 + quizAns: null,
286 + quizInfo: null
287 + };
288 + sendQuiz(eventObj.replyToken, eventObj.source.userId);
289 + } else if (isWaitAns && eventObj.message.text == '정답') {
290 + users[eventObj.source.userId].streak ++;
291 + sendQuiz(eventObj.replyToken, eventObj.source.userId);
292 + } else if (isWaitAns && eventObj.message.text == '오답') {
293 + endQuiz(eventObj.replyToken, eventObj.source.userId);
282 } 294 }
283 295
284 res.sendStatus(200); 296 res.sendStatus(200);
285 }); 297 });
286 298
287 -function sendQuiz(replyToken, message) { 299 +function sendQuiz(replyToken, id) {
288 var randomQuiz = quizList[Math.floor(Math.random()*quizList.length)]; 300 var randomQuiz = quizList[Math.floor(Math.random()*quizList.length)];
289 var quizText = randomQuiz.quiz; 301 var quizText = randomQuiz.quiz;
290 302
...@@ -307,6 +319,32 @@ function sendQuiz(replyToken, message) { ...@@ -307,6 +319,32 @@ function sendQuiz(replyToken, message) {
307 console.log(body) 319 console.log(body)
308 } 320 }
309 ); 321 );
322 +
323 + isWaitAns = true;
324 +}
325 +
326 +function endQuiz(replyToken, id) {
327 + isWaitAns = false;
328 +
329 + request.post(
330 + {
331 + url: TARGET_URL,
332 + headers: {
333 + 'Authorization': `Bearer ${TOKEN}`
334 + },
335 + json: {
336 + "replyToken":replyToken,
337 + "messages":[
338 + {
339 + "type":"text",
340 + "text":`${users[id].streak}문제 연속 정답!`
341 + }
342 + ]
343 + }
344 + },(error, response, body) => {
345 + console.log(body)
346 + }
347 + );
310 } 348 }
311 349
312 try { 350 try {
......