From ac755538f764f52bdec9498f86176cdc3a4c652a Mon Sep 17 00:00:00 2001 From: smsteel Date: Mon, 11 Feb 2019 16:18:50 +0300 Subject: [PATCH] kill count --- bot.js | 31 +++++++++++++++---------------- database.js | 2 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/bot.js b/bot.js index e7a9540..43f57fd 100644 --- a/bot.js +++ b/bot.js @@ -32,23 +32,22 @@ bot.command( ) )) ) -bot.command( - 'kills', - ({ replyWithMarkdown }) => getAllKills() +bot.hears( + /^\/kills (.*)$/, + ctx => getAllKills(ctx.match[1]) .then(allKills => { - let groupedKills = [{}] - let currentGroupId = 0 - allKills.forEach(kills => { - if (groupedKills[currentGroupId].displayName && groupedKills[currentGroupId].displayName !== kills.displayName) { - currentGroupId++ - } - groupedKills[currentGroupId] = { - displayName: kills.displayName, - ...groupedKills[currentGroupId], - [kills.entityName]: kills.killCount - } - }) - replyWithMarkdown(formattedStringTable(groupedKills)) + if (!allKills) { + ctx.reply('Не найдено') + return + } + ctx.replyWithMarkdown( + formattedStringTable( + allKills.map(kills => ({ + 'монстр': getEntityName(kills.entityName), + 'убийств': kills.killCount + })) + ) + ) }) ) bot.launch() diff --git a/database.js b/database.js index 90dd6cf..9780d62 100644 --- a/database.js +++ b/database.js @@ -93,7 +93,7 @@ export const getKillCount = (displayName, entityName) => get( 'killCount', 0 ) -export const getAllKills = () => all('SELECT * FROM playerKills ORDER BY displayName ASC') +export const getAllKills = displayName => all('SELECT * FROM playerKills WHERE displayName = ? ORDER BY displayName ASC', [ displayName ]) export const saveLevelUp = (displayName, levelUpCount) => run( `INSERT OR REPLACE INTO levelUps (displayName, levelUpCount) VALUES (?, ?)`,