index.js
4.99 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const Question = require('../models/questions');
const Job = require('../models/jobs');
const bodyParser = require('body-parser');
bodyParser.json();
async function jobRecommend(fight, detail, traffic, control, support, activity){
const jobs = await Job.find().exec();
let scores = [fight, detail, traffic, control, support];
const bar = 50;
// 내림차순 정렬
scores.sort(function(a, b) {
return b-a;
});
if(activity >= bar){
// 동적
if(scores[0] === fight){
// 전투성향 높음 : 보병, 기갑
if(detail >= bar){
return "포병";
}
else if(traffic >= bar){
// 기갑
return "기갑";
}
return "보병";
}
else if(scores[0] === detail){
return "포병";
}
else if(scores[0] === traffic){
if(fight >= bar){
return "기갑";
}
else{
return "수송";
}
}
else if(scores[0] === control){
return "헌병";
}
else{
return "의무";
}
}
else{
//정적
if(scores[0] === fight){
// 전투성향 높음
if(detail >= bar){
return "방공";
}
return "화생방";
}
else if(scores[0] === detail){
if(fight >= bar){
return "방공";
}
return "공병";
}
else if(scores[0] === traffic){
return "보급";
}
else if(scores[0] === control){
if(support >= bar){
return "정보통신";
}
return "인사";
}
else{
if(control>=bar){
return "정보통신";
}
else if(fight >= bar){
return "화생방";
}
else if(traffic >= bar){
return "보급";
}
else{
return "정보";
}
}
}
return "보병";
}
module.exports = function(app)
{
app.get('/', (req, res, next) => {
res.send('hello world!');
});
app.get('/getQuestions', async (req, res, next) => {
/**
* 질문 폼
* 번호 질문내용 답1 답2 가중치1, 가중치2
*/
const questions = await Question.find().exec();
let questionNums = [];
const numOfQuestions = 25;
for(let i=0; i<numOfQuestions; i++){
questionNums[i] = i;
}
// shuffle(questionNums);
questionNums.sort(function () {
return Math.round(Math.random()) - 0.5
});
questionNums = questionNums.slice(0, 10);
let sendQuestions = [];
for(let i=0; i<10; i++){
sendQuestions.push(questions[questionNums[i]]);
}
res.send(sendQuestions);
});
app.post('/submit', async (req, res, next) => {
const answers = req.body;
// console.log(answers);
let fight = 50;
let detail = 50;
let traffic = 50;
let control = 50;
let support = 50;
let activity = 50;
/**
* 정답 폼
* 문항 번호 정답 고른것
*/
for(let i=0; i<answers.length; i++){
const question = await Question.findOne({num: answers[i].num}).exec();
if(answers.choice === 0){
fight += question.fight;
detail += question.detail;
traffic += question.traffic;
control += question.control;
support += question.support;
activity += question.activity;
}
else{
fight -= question.fight;
detail -= question.detail;
traffic -= question.traffic;
control -= question.control;
support -= question.support;
activity -= question.activity;
}
}
// console.log(fight, detail, traffic, control, support, activity);
const recommandedJob = await jobRecommend(fight, detail, traffic, control, support, activity);
// TODO : recommand job
const jobList = await Job.find({high: recommandedJob}).exec();
const rand = Math.floor(Math.random() * jobList.length);
const result = jobList[rand];
// res.send(recommandedJob);
res.send(result);
});
app.post('/addJobs', async(req, res, next) => {
// 보직 추가하는 api
const datas = req.body;
for(let i=0; i<datas.length; i++){
const data = new Job();
data.num = datas[i].num;
data.high = datas[i].high;
data.low = datas[i].low;
data.description = datas[i].description;
data.image = datas[i].image;
await data.save();
}
res.send(datas[0]);
});
app.post('/addQuestions', async(req, res, next) => {
// 질문 추가하는 api
const datas = req.body;
// console.log(datas);
console.log(typeof(datas));
for(let i=0; i<datas.length; i++){
const data = new Question();
data.num = datas[i].num;
data.question = datas[i].question;
data.answer1 = datas[i].answer1;
data.answer2 = datas[i].answer2;
data.fight = datas[i].fight;
data.detail = datas[i].detail;
data.traffic = datas[i].traffic;
data.control = datas[i].control;
data.support = datas[i].support;
data.activity = datas[i].activity;
// console.log(data);
await data.save();
}
res.send(req.body);
});
}