diff --git a/bot.js b/bot.js index ac4bb9e..0b47a1a 100644 --- a/bot.js +++ b/bot.js @@ -1,7 +1,7 @@ import Telegraf from 'telegraf' import TelegrafExtra from 'telegraf/extra' import { saveChat, getChats, saveLogoutTime, getLastLogoutTime, saveKillCount, getKillCount, - saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData + saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData, getAllKills } from './database' import { EVENT_TYPE_JOIN, EVENT_TYPE_DEATH, EVENT_TYPE_QUIT, EVENT_TYPE_PLAYER_LEVEL_CHANGE, @@ -13,25 +13,44 @@ import stringTable from 'string-table' const bot = new Telegraf('643297173:AAGuqfZx3GhiiARwvY7AtWTTFw1T-2FiwCM') const markdown = TelegrafExtra.markdown() +const formattedStringTable = data => stringTable.create(data) + .split('\n') + .map(line => `\`\`\`${line.slice(0, -1)}\`\`\``) + .join('\n') bot.start(({ message: { chat: { id } } }) => saveChat(id)) bot.command( 'players', ({ replyWithMarkdown }) => getPlayersData() - .then(getPlayersData => replyWithMarkdown( - stringTable.create( - getPlayersData - .map(({ displayName, level, online }) => ({ - 'имя': `👤 ${displayName}`, - 'уровень': level, - 'статус': `${online ? '✅ онлайн' : '❔ оффлайн'}` - })) + .then(playersData => replyWithMarkdown( + formattedStringTable( + playersData.map(({ displayName, level, online }) => ({ + 'имя': `👤 ${displayName}`, + 'уровень': level, + 'статус': `${online ? '✅ онлайн' : '❔ оффлайн'}` + })) ) - .split('\n') - .map(line => `\`\`\`${line.slice(0, -1)}\`\`\``) - .join('\n') )) ) +bot.command( + 'kills', + ({ replyWithMarkdown }) => getAllKills() + .then(allKills => { + let groupedKills = [] + let currentGroupId = 0 + allKills.forEach(kills => { + if (currentGroupId !== 0 && groupedKills[currentGroupId].displayName !== kills.displayName) { + currentGroupId++ + } + groupedKills[currentGroupId] = { + displayName: kills.displayName, + ...groupedKills[currentGroupId], + [kills.entityName]: kills.killCount + } + }) + replyWithMarkdown(formattedStringTable(groupedKills)) + }) +) bot.launch() const sendMessageToAll = text => getChats() diff --git a/database.js b/database.js index 9aea9a7..90dd6cf 100644 --- a/database.js +++ b/database.js @@ -93,6 +93,7 @@ export const getKillCount = (displayName, entityName) => get( 'killCount', 0 ) +export const getAllKills = () => all('SELECT * FROM playerKills ORDER BY displayName ASC') export const saveLevelUp = (displayName, levelUpCount) => run( `INSERT OR REPLACE INTO levelUps (displayName, levelUpCount) VALUES (?, ?)`,