app.js
7.71 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
223
224
225
226
227
228
229
const express = require('express');
const app = express();
const mysql = require('mysql2/promise');
const axios = require('axios').default;
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use('/images', express.static('images'));
const channelToken = 'Bearer ZVI571ynUAVT0U44734ziyjVtStYiUi2UA4txCgVUvugv5YQwToMDgE991Sd4UbsZtJpBhXEtQKX/YkvQVx0PgH9F7qy75eUR0iKMwz33RXD5BG8JuFZ67FqR/GXZQNSk+kXHMBVnCQPeeNqTNn5MgdB04t89/1O/w1cDnyilFU=';
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '0712',
database: 'tft'
});
app.post('/messages', function(req, res) {
for(let event of req.body.events) {
if(event.type == 'message')
onMessage(event.message, event.replyToken);
}
res.sendStatus(200);
});
function onMessage(message, replyToken) {
if(message.type != 'text')
return;
if(message.text.indexOf('아이템') >= 0) {
recommendItem(message.text.split(' ')[0], replyToken);
return;
}
let keywords = [
'추천덱',
'추천메타',
'덱추천',
'메타추천',
'메타덱',
'추천시너지',
'메타시너지',
'시너지추천'
];
let isMeta = false;
let textOnly = message.text.replace(' ', '');
for(let k of keywords) {
if(textOnly.indexOf(k) >= 0)
isMeta = true;
}
if(isMeta) {
recommendMeta(replyToken);
return;
}
if(message.text.indexOf('확률') >= 0) {
sendProbabilities(replyToken);
return;
}
replyMessage(replyToken, [{type: 'text', text: '수행할 수 없는 명령입니다.'}]);
}
function recommendItem(charAlias, replyToken) {
let sql = 'SELECT n.`alias`, i.`id`, i.`name` FROM `character_aliases` a JOIN `character_items` c ON a.`character_id`=c.`character_id` JOIN `character_aliases` n ON c.`character_id`=n.`character_id` JOIN `items` i ON c.`item`=i.`id` WHERE a.`alias`=? AND n.`is_name`=1 ORDER BY c.`count` DESC LIMIT 0,5';
pool.query(sql, [charAlias]).then(function([results]) {
let reply;
if(results.length == 0)
reply = {type: 'text', text: '데이터가 없습니다.'};
else
reply = {
type: 'flex',
altText: `${results[0].alias}의 추천 아이템은 ${results.map(e => e.name).join(', ')} 입니다.`,
contents: {
type: 'bubble',
size: 'kilo',
body: {
type: 'box',
layout: 'vertical',
alignItems: 'flex-start',
contents: [
{type: 'text', text: `${results[0].alias}의 추천 아이템`}
].concat(results.map(e => ({
type: 'box',
layout: 'horizontal',
alignItems: 'center',
justifyContent: 'flex-start',
spacing: 'md',
contents: [
{
type: 'image',
url: `https://jongro3.ga/images/items/${e.id.toString().padStart(2, '0')}.png`,
size: 'xxs',
flex: 0
},
{
type: 'text',
text: e.name,
flex: 0
}
]
})))
}
}
};
replyMessage(replyToken, [reply]);
});
}
async function recommendMeta(replyToken) {
let sql = 'SELECT * FROM decks ORDER BY `count` DESC LIMIT 0,3';
let [results] = await pool.query(sql);
let messages = [];
for(let row of results) {
let chars = [];
sql = 'SELECT `alias` FROM character_aliases WHERE character_id=? AND is_name=1';
for(let i = 1; i <= 8; i++) {
[results] = await pool.query(sql, [row[`character_${i}`]]);
chars.push({id: row[`character_${i}`], name: results[0].alias});
}
let traitCnt = new Map();
traitCnt.set(row.chosen, 1);
let checked = new Set();
sql = 'SELECT trait FROM character_traits WHERE character_id=?';
for(let i = 0; i <= 8; i++) {
if(checked.has(row[`character_${i}`]))
continue;
[results] = await pool.query(sql, [row[`character_${i}`]]);
for(let t of results) {
if(traitCnt.has(t.trait))
traitCnt.set(t.trait, traitCnt.get(t.trait) + 1);
else
traitCnt.set(t.trait, 1);
}
checked.add(row[`character_${i}`]);
}
let traits = [];
sql = 'SELECT t.name, l.count FROM traits t JOIN trait_levels l ON t.id=l.trait_id WHERE t.id=? AND l.count <= ? ORDER BY l.count DESC LIMIT 1';
for(let [key, value] of traitCnt.entries()) {
[results] = await pool.query(sql, [key, value]);
if(results.length == 0)
continue;
traits.push(`${results[0].count}${results[0].name}`);
};
[results] = await pool.query('SELECT `name` FROM traits WHERE `id`=?', [row.chosen]);
messages.push({
type: 'flex',
altText: traits.join(', ') + '\n' + chars.join(', ') + '\n' + '선택받은 자: ' + results[0].name,
contents: {
type: 'bubble',
size: 'giga',
body: {
type: 'box',
layout: 'vertical',
contents: [
{type: 'text', text: '시너지'},
{
type: 'text',
text: traits.join(', '),
wrap: true
/*layout: 'horizontal',
contents: traits.map(e => ({
type: 'text',
text: e
}))*/
},
{type: 'text', text: '캐릭터'},
{
type: 'box',
layout: 'horizontal',
contents: chars.map(e => ({
type: 'image',
url: `https://jongro3.ga/images/champions/${e.id}.png`,
size: 'xxs'
}))
},
{type: 'text', text: chars.map(e => e.name).join(', '), wrap: true},
]
}
}
});
}
replyMessage(replyToken, messages);
}
function sendProbabilities(replyToken) {
let probs = [
[100, 0, 0, 0, 0],
[100, 0, 0, 0, 0],
[75, 25, 0, 0, 0],
[55, 30, 15, 0, 0],
[45, 33, 20, 2, 0],
[35, 35, 25, 5, 0],
[22, 35, 30, 12, 1],
[15, 25, 35, 20, 5],
[10, 15, 30, 30, 15]
];
replyMessage(replyToken, [{
type: 'text',
text: probs.map((arr, idx) => `Lv.${idx + 1}: ${arr.map(e => `${e}%`).join(' ')}`).join('\n')
}])
}
function replyMessage(replyToken, messages) {
axios.post('https://api.line.me/v2/bot/message/reply', {
replyToken: replyToken,
messages: messages
}, {
headers: {'Authorization': channelToken}
});
}
app.listen(3000, () => console.log('TFT Chatbot started'));