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 Telegraf from 'telegraf'
import TelegrafExtra from 'telegraf/extra' import TelegrafExtra from 'telegraf/extra'
import { saveChat, getChats, saveLogoutTime, getLastLogoutTime, saveKillCount, getKillCount, import { saveChat, getChats, saveLogoutTime, getLastLogoutTime, saveKillCount, getKillCount,
saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData, getAllKills
} from './database' } from './database'
import { import {
EVENT_TYPE_JOIN, EVENT_TYPE_DEATH, EVENT_TYPE_QUIT, EVENT_TYPE_PLAYER_LEVEL_CHANGE, 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 bot = new Telegraf('643297173:AAGuqfZx3GhiiARwvY7AtWTTFw1T-2FiwCM')
const markdown = TelegrafExtra.markdown() 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.start(({ message: { chat: { id } } }) => saveChat(id))
bot.command( bot.command(
'players', 'players',
({ replyWithMarkdown }) => getPlayersData() ({ replyWithMarkdown }) => getPlayersData()
.then(getPlayersData => replyWithMarkdown( .then(playersData => replyWithMarkdown(
stringTable.create( formattedStringTable(
getPlayersData playersData.map(({ displayName, level, online }) => ({
.map(({ displayName, level, online }) => ({ 'имя': `👤 ${displayName}`,
'имя': `👤 ${displayName}`, 'уровень': level,
'уровень': level, 'статус': `${online ? '✅ онлайн' : '❔ оффлайн'}`
'статус': `${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() bot.launch()
const sendMessageToAll = text => getChats() const sendMessageToAll = text => getChats()

View File

@ -93,6 +93,7 @@ export const getKillCount = (displayName, entityName) => get(
'killCount', 'killCount',
0 0
) )
export const getAllKills = () => all('SELECT * FROM playerKills ORDER BY displayName ASC')
export const saveLevelUp = (displayName, levelUpCount) => run( export const saveLevelUp = (displayName, levelUpCount) => run(
`INSERT OR REPLACE INTO levelUps (displayName, levelUpCount) VALUES (?, ?)`, `INSERT OR REPLACE INTO levelUps (displayName, levelUpCount) VALUES (?, ?)`,