Minkyung Ko

Add result & show percent

Showing 1 changed file with 102 additions and 0 deletions
...@@ -14,6 +14,7 @@ app.use(bodyParser.urlencoded({ ...@@ -14,6 +14,7 @@ app.use(bodyParser.urlencoded({
14 app.use('/api', apiRouter); 14 app.use('/api', apiRouter);
15 15
16 let userDB = new Array(); 16 let userDB = new Array();
17 +let mbtiper = new Array();
17 18
18 apiRouter.post('/test', (req, res) => { 19 apiRouter.post('/test', (req, res) => {
19 const responseBody = { 20 const responseBody = {
...@@ -324,6 +325,107 @@ apiRouter.post('/question14', (req, res) => { ...@@ -324,6 +325,107 @@ apiRouter.post('/question14', (req, res) => {
324 res.status(200).send(responseBody); 325 res.status(200).send(responseBody);
325 }); 326 });
326 327
328 +apiRouter.post('/result', (req, res) => {
329 + var mesg = req.body.userRequest.utterance;
330 + var userId = req.body.userRequest.user.id;
331 + var mbti = '';
332 + if (mesg == "네"){
333 + mbti = 'N';
334 + }
335 + else if (mesg == "아니오") {
336 + mbti = 'S';
337 + }
338 + userDB[userId][1] += mbti;
339 + console.log(userDB[userId]);
340 + console.log(mbtiper);
341 + var e = calc(userDB[userId], 'E');
342 + var i = 2-e;
343 + var n = calc(userDB[userId], 'N');
344 + var s = 2-n;
345 + var f = calc(userDB[userId], 'F');
346 + var t = 1-f;
347 + var j = calc(userDB[userId], 'J');
348 + var p = 2-j;
349 + mbtiper.push(20 * e);
350 + mbtiper.push(20 * i);
351 + mbtiper.push(20 * n);
352 + mbtiper.push(20 * s);
353 + mbtiper.push(20 * f);
354 + mbtiper.push(20 * t);
355 + mbtiper.push(20 * j);
356 + mbtiper.push(20 * p);
357 + if(mbtiper[0]>mbtiper[1]){
358 + userDB[userId][4] += 'E';
359 + } else {
360 + userDB[userId][4] += 'I';
361 + }
362 + if(mbtiper[2]>mbtiper[3]){
363 + userDB[userId][4] += 'N';
364 + } else {
365 + userDB[userId][4] += 'S';
366 + }
367 + if(mbtiper[4]>mbtiper[5]){
368 + userDB[userId][4] += 'T';
369 + } else {
370 + userDB[userId][4] += 'F';
371 + }
372 + if(mbtiper[6]>mbtiper[7]){
373 + userDB[userId][4] += 'J';
374 + } else {
375 + userDB[userId][4] += 'P';
376 + }
377 + const responseBody = {
378 + version: "2.0",
379 + template: {
380 + outputs: [
381 + {
382 + simpleText: {
383 + text: "당신의 MBTI는 : "+userDB[userId][4]
384 + }
385 + }
386 + ],
387 + quickReplies: [{
388 + action: "block",
389 + label: "MBTI 테스트 다시하기",
390 + message: "MBTI 테스트 다시하기",
391 + blockId : "62977ff05ceed96c385449b9"
392 + },
393 + {
394 + action: "block",
395 + label: "결과 상세보기",
396 + message: "결과 상세보기",
397 + blockId: "62987b78e7a0253c7662dcd9"
398 + }]
399 + }
400 + }
401 + res.status(200).send(responseBody);
402 + });
403 +
404 + function calc(array, mbti_cap) {
405 + var result = 0;
406 + for (var k = 0; k < 5; k++) {
407 + if(array[k] == 'mbti_cap') {
408 + result += 1;
409 + }
410 + }
411 + return result;
412 +}
413 +
414 +apiRouter.post('/percent', (req, res) => {
415 + const responseBody = {
416 + version: "2.0",
417 + template: {
418 + outputs: [
419 + {
420 + simpleText: {
421 + text: 'E: '+mbtiper[0]+'%\nI: '+mbtiper[1]+'%\nN: '+mbtiper[2]+'%\nS: '+mbtiper[3]+'%\nT: '+mbtiper[4]+'%\nF: '+mbtiper[5]+'%\nJ: '+mbtiper[6]+'%\nP: '+mbtiper[7]+'%'
422 + }
423 + }
424 + ]
425 + }
426 + }
427 + res.status(200).send(responseBody);
428 + });
327 app.listen((process.env.PORT || 3000), function() { 429 app.listen((process.env.PORT || 3000), function() {
328 console.log('Example skill server listening on port 3000!'); 430 console.log('Example skill server listening on port 3000!');
329 }); 431 });
...\ No newline at end of file ...\ No newline at end of file
......