마수현
Committed by Ma Suhyeon

Implement recommend meta

Showing 1 changed file with 90 additions and 7 deletions
......@@ -28,10 +28,34 @@ function onMessage(message, replyToken) {
if(message.type != 'text')
return;
if(message.text.indexOf('아이템') >= 0)
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;
}
replyMessage(replyToken, [{type: 'text', text: '수행할 수 없는 명령입니다.'}]);
}
function recommendItem(charAlias, replyToken) {
......@@ -43,12 +67,71 @@ function recommendItem(charAlias, replyToken) {
else
reply = `${results[0].alias}의 추천 아이템은 ${results.map(e => e.name).join(', ')} 입니다.`;
axios.post('https://api.line.me/v2/bot/message/reply', {
replyToken: replyToken,
messages: [{type: 'text', text: reply}]
}, {
headers: {'Authorization': channelToken}
replyMessage(replyToken, [{type: 'text', text: 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(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: 'text',
text: traits.join(', ') + '\n' + chars.join(', ') + '\n' + '선택받은 자: ' + results[0].name
});
}
replyMessage(replyToken, messages);
}
function replyMessage(replyToken, messages) {
axios.post('https://api.line.me/v2/bot/message/reply', {
replyToken: replyToken,
messages: messages
}, {
headers: {'Authorization': channelToken}
});
}
......