Ubuntu

기능: 오답시 관련 정보 소개, 정답 안내 메시지 추가

Showing 1 changed file with 42 additions and 12 deletions
...@@ -23,6 +23,8 @@ var fakerData = {}; ...@@ -23,6 +23,8 @@ var fakerData = {};
23 var quizList = []; 23 var quizList = [];
24 var isWaitAns = false; 24 var isWaitAns = false;
25 var users = {}; 25 var users = {};
26 +var sumOfStreak = 0;
27 +var sumOfTry = 0;
26 28
27 async function checkData() { 29 async function checkData() {
28 try { 30 try {
...@@ -285,11 +287,11 @@ app.post('/hook', async function (req, res) { ...@@ -285,11 +287,11 @@ app.post('/hook', async function (req, res) {
285 quizAns: null, 287 quizAns: null,
286 quizInfo: null 288 quizInfo: null
287 }; 289 };
288 - sendQuiz(eventObj.replyToken, eventObj.source.userId); 290 + sendQuiz(eventObj.replyToken, eventObj.source.userId, true);
289 } else if (isWaitAns) { 291 } else if (isWaitAns) {
290 if (checkAns(eventObj.source.userId, eventObj.message.text)) { 292 if (checkAns(eventObj.source.userId, eventObj.message.text)) {
291 users[eventObj.source.userId].streak++; 293 users[eventObj.source.userId].streak++;
292 - sendQuiz(eventObj.replyToken, eventObj.source.userId); 294 + sendQuiz(eventObj.replyToken, eventObj.source.userId, false);
293 } else { 295 } else {
294 endQuiz(eventObj.replyToken, eventObj.source.userId); 296 endQuiz(eventObj.replyToken, eventObj.source.userId);
295 } 297 }
...@@ -298,13 +300,32 @@ app.post('/hook', async function (req, res) { ...@@ -298,13 +300,32 @@ app.post('/hook', async function (req, res) {
298 res.sendStatus(200); 300 res.sendStatus(200);
299 }); 301 });
300 302
301 -function sendQuiz(replyToken, id) { 303 +function sendQuiz(replyToken, id, isInit) {
302 var randomQuiz = quizList[Math.floor(Math.random()*quizList.length)]; 304 var randomQuiz = quizList[Math.floor(Math.random()*quizList.length)];
303 var quizText = randomQuiz.quiz; 305 var quizText = randomQuiz.quiz;
304 - 306 + var messages;
305 users[id].quizAns = randomQuiz.ans; 307 users[id].quizAns = randomQuiz.ans;
306 users[id].quizInfo = randomQuiz.info; 308 users[id].quizInfo = randomQuiz.info;
307 309
310 + if (isInit) {
311 + messages = [
312 + {
313 + "type":"text",
314 + "text":quizText
315 + }
316 + ];
317 + } else {
318 + messages = [
319 + {
320 + "type":"text",
321 + "text":'정답입니다.'
322 + },
323 + {
324 + "type":"text",
325 + "text":quizText
326 + }
327 + ];
328 + }
308 request.post( 329 request.post(
309 { 330 {
310 url: TARGET_URL, 331 url: TARGET_URL,
...@@ -313,12 +334,7 @@ function sendQuiz(replyToken, id) { ...@@ -313,12 +334,7 @@ function sendQuiz(replyToken, id) {
313 }, 334 },
314 json: { 335 json: {
315 "replyToken":replyToken, 336 "replyToken":replyToken,
316 - "messages":[ 337 + "messages": messages
317 - {
318 - "type":"text",
319 - "text":quizText
320 - }
321 - ]
322 } 338 }
323 },(error, response, body) => { 339 },(error, response, body) => {
324 console.log(body) 340 console.log(body)
...@@ -338,7 +354,9 @@ function checkAns(id, ans) { ...@@ -338,7 +354,9 @@ function checkAns(id, ans) {
338 354
339 function endQuiz(replyToken, id) { 355 function endQuiz(replyToken, id) {
340 isWaitAns = false; 356 isWaitAns = false;
341 - 357 + sumOfTry++;
358 + sumOfStreak += users[id].streak;
359 + var averageStreak = sumOfStreak/sumOfTry;
342 request.post( 360 request.post(
343 { 361 {
344 url: TARGET_URL, 362 url: TARGET_URL,
...@@ -350,7 +368,19 @@ function endQuiz(replyToken, id) { ...@@ -350,7 +368,19 @@ function endQuiz(replyToken, id) {
350 "messages":[ 368 "messages":[
351 { 369 {
352 "type":"text", 370 "type":"text",
353 - "text":`${users[id].streak}문제 연속 정답!` 371 + "text":"오답입니다."
372 + },
373 + {
374 + "type":"text",
375 + "text":users[id].quizInfo
376 + },
377 + {
378 + "type":"text",
379 + "text":`${users[id].streak}문제 연속 정답!\n(유저 평균 : ${averageStreak.toFixed(1)}문제)`
380 + },
381 + {
382 + "type":"text",
383 + "text":"퀴즈를 다시 시작하려면 '시작'을 입력해주세요."
354 } 384 }
355 ] 385 ]
356 } 386 }
......