level up fixes, first bot command

This commit is contained in:
smsteel
2019-02-10 18:45:25 +03:00
parent 71186ac223
commit 6cc8b71ae2
3 changed files with 86 additions and 19 deletions

50
bot.js
View File

@ -1,7 +1,10 @@
import Telegraf from 'telegraf'
import { saveChat, getChats, saveLogout, getLastLogoutTime, saveKillCount, getKillCount } from './database'
import { saveChat, getChats, saveLogoutTime, getLastLogoutTime, saveKillCount, getKillCount,
saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData
} from './database'
import {
EVENT_TYPE_JOIN, EVENT_TYPE_DEATH, EVENT_TYPE_QUIT, EVENT_TYPE_PLAYER_LEVEL_CHANGE, EVENT_TYPE_PLAYER_KILLED_ENTITY
EVENT_TYPE_JOIN, EVENT_TYPE_DEATH, EVENT_TYPE_QUIT, EVENT_TYPE_PLAYER_LEVEL_CHANGE,
EVENT_TYPE_PLAYER_KILLED_ENTITY
} from './types'
import { currentTime } from './utility'
import { getEntityDeathMessage, getEntityName } from './ru'
@ -9,6 +12,15 @@ import { getEntityDeathMessage, getEntityName } from './ru'
const bot = new Telegraf('643297173:AAGuqfZx3GhiiARwvY7AtWTTFw1T-2FiwCM')
bot.start(({ message: { chat: { id } } }) => saveChat(id))
bot.command(
'players',
({ reply }) => getPlayersData()
.then(getPlayersData => reply(
getPlayersData
.map(({ displayName, level, online }) => `${displayName} уровень ${level} ${online ? 'онлайн' : 'оффлайн'}`)
.join('\n')
))
)
bot.launch()
const sendMessageToAll = text => getChats()
@ -20,32 +32,44 @@ const JOIN_NOTIFICATION_TIME_DELTA = 60 * 60 // 1h
const LEVEL_NOFIFICATION_EACH_LEVELS = 5
const KILL_NOTIFICATION_EACH_KILLS = 50
export const sendEvent = ({ type, ...payload }) => {
export const sendEvent = ({ type, displayName, ...payload }) => {
switch (type) {
case EVENT_TYPE_JOIN:
getLastLogoutTime(payload.displayName)
setOnlineState(displayName)
getLastLogoutTime(displayName)
.then(lastLogoutTime => lastLogoutTime < currentTime() - JOIN_NOTIFICATION_TIME_DELTA &&
sendMessageToAll(`Игрок ${payload.displayName} присоединился!`))
sendMessageToAll(`Игрок ${displayName} присоединился!`))
break
case EVENT_TYPE_QUIT:
saveLogout(payload.displayName)
setOfflineState(displayName)
saveLogoutTime(displayName)
break
case EVENT_TYPE_DEATH:
sendMessageToAll(`Игрок ${payload.displayName} ${getEntityDeathMessage(payload)} :(`)
sendMessageToAll(`Игрок ${displayName} ${getEntityDeathMessage(payload)} :(`)
break
case EVENT_TYPE_PLAYER_LEVEL_CHANGE:
const { newLevel } = payload
if (parseInt(newLevel) % LEVEL_NOFIFICATION_EACH_LEVELS === 0) {
sendMessageToAll(`Игрок ${payload.displayName} прокачался до уровня ${newLevel}!`)
const { newLevel, oldLevel } = payload
saveLevel(displayName, newLevel)
if (oldLevel && newLevel < oldLevel) {
return
}
const levelUpCount = getLevelUpCount(displayName)
if (newLevel % LEVEL_NOFIFICATION_EACH_LEVELS === 0 &&
levelUpCount >= LEVEL_NOFIFICATION_EACH_LEVELS
) {
sendMessageToAll(`Игрок ${displayName} прокачался до уровня ${newLevel}!`)
saveLevelUp(displayName, 0)
} else {
saveLevelUp(displayName, levelUpCount + 1)
}
break
case EVENT_TYPE_PLAYER_KILLED_ENTITY:
getKillCount(payload.displayName, payload.entityName)
getKillCount(displayName, payload.entityName)
.then(killCount => {
const newKillCount = killCount + 1
saveKillCount(payload.displayName, payload.entityName, newKillCount)
saveKillCount(displayName, payload.entityName, newKillCount)
if (newKillCount % KILL_NOTIFICATION_EACH_KILLS === 0) {
sendMessageToAll(`${getEntityName(payload)} повержен(а) игроком ${payload.displayName}. Он убил ещё ${KILL_NOTIFICATION_EACH_KILLS}! Всего: ${newKillCount}`)
sendMessageToAll(`${getEntityName(payload)} повержен(а) игроком ${displayName}. Убито ещё ${KILL_NOTIFICATION_EACH_KILLS}! Всего: ${newKillCount}`)
}
})
break