-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (153 loc) · 3.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
const express = require('express')
const app = express()
const fetch = require('node-fetch')
const session = require('express-session')
const bodyParser = require("body-parser");//handle post request
const questions = require('./questions').questions
app.use(express.static('public'));
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 } }))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const hello = (name_p) => {
let str = 'hello human, what is your name?';
if (name_p) {
str = `nice to meet you ${name_p}, I'm mister quizzy`
}
return {
message: {
loading: true,
delay: 1000,
content: str
}
};
}
const ask_name = () => {
return {
input: {
action: {
placeholder: "type your name here"
}
}
};
}
const question = (index) => {
return {
message: {
loading: true,
delay: 1000,
content: questions[index].question
}
};
}
const choices = (index) => {
return {
choices: {
delay: 1000,
action: [
{ // show only one button
text: questions[index].choices[0],
value: questions[index].choices[0]
},
{ // show only one button
text: questions[index].choices[1],
value: questions[index].choices[1]
},
{ // show only one button
text: questions[index].choices[2],
value: questions[index].choices[2]
}
]
}
};
}
const show_answer = (success, solution) => {
let text = ` boooo its false; good answer is ${solution}`
if (success) {
text = "YES!";
}
return {
message: {
loading: true,
delay: 3000,
content: text
}
};
}
const score = (name, score, nb_question) => {
return {
message: {
loading: true,
delay: 1000,
content: `Thank you for playing${name}, your score is ${score}/${nb_question}`
}
};
};
const replay = ()=>{
return {
message: {
loading: true,
delay: 1000,
content: `replay...`
}
};
}
app.post('/next', (req, res, next) => {
let data;
let { state } = req.session
console.log(`post /next :enter state is ${state}`);
if (state == undefined) {
data = hello()
state = "hello"
} else if (state == "hello") {
data = ask_name()
state = "ask"
} else if (state == "ask") {
const name = req.body.value
req.session.name = name
data = hello(name)
state = "quiz"
} else if (state == "quiz") {
if (req.session.question_number == undefined) {
req.session.question_number = 0;
} else {
req.session.question_number++;
}
const question_number = req.session.question_number;
if (question_number < questions.length) {
data = question(question_number)
state = "choices"
} else {
data = score(req.session.name, req.session.score, questions.length)
state = 'finish'
}
} else if (state == "choices") {
const question_number = req.session.question_number;
data = choices(question_number);
state = "answer"
} else if (state == "answer") {
const question_number = req.session.question_number;
const answer = req.body.value;
const solution = questions[question_number].answer;
const success = answer == solution;
if (req.session.score == undefined) {
req.session.score = 0;
}
if (success) {
req.session.score++;
}
data = show_answer(success, solution);
state = "quiz";
}else if (state == 'finish'){
req.session.score = 0;
req.session.question_number = undefined;
data = replay();
state = 'quiz'
}
console.log(`post /next :exit state is ${state}`);
req.session.state = state;
const str = JSON.stringify(data)
res.end(str);
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})