kill count

This commit is contained in:
smsteel
2019-02-11 16:06:29 +03:00
parent 77aa977897
commit 93e1500e98
2 changed files with 32 additions and 12 deletions

43
bot.js
View File

@ -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()

View File

@ -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 (?, ?)`,