level up fixes, first bot command
This commit is contained in:
parent
71186ac223
commit
6cc8b71ae2
50
bot.js
50
bot.js
@ -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
|
||||
|
51
database.js
51
database.js
@ -23,6 +23,22 @@ db.run(
|
||||
UNIQUE(displayName, entityName)
|
||||
)`)
|
||||
|
||||
db.run(
|
||||
`CREATE TABLE IF NOT EXISTS levelUps (
|
||||
displayName STRING,
|
||||
levelUpCount INTEGER DEFAULT 0,
|
||||
UNIQUE(displayName)
|
||||
)`)
|
||||
|
||||
db.run(
|
||||
`CREATE TABLE IF NOT EXISTS playerData (
|
||||
displayName STRING,
|
||||
level INTEGER DEFAULT 0,
|
||||
online INTEGER DEFAULT 0,
|
||||
UNIQUE(displayName)
|
||||
)`
|
||||
)
|
||||
|
||||
const run = (sql, params) => new Promise((resolve, reject) => {
|
||||
db.run(sql, params, (err) => {
|
||||
if (err) {
|
||||
@ -43,9 +59,8 @@ const get = (sql, params, column, defaultParam = null) => new Promise((resolve,
|
||||
})
|
||||
})
|
||||
|
||||
export const saveChat = id => run(`INSERT OR IGNORE INTO chats (chatId) VALUES (?)`, [ id ])
|
||||
export const getChats = () => new Promise((resolve, reject) => {
|
||||
db.all('SELECT * FROM chats', [], (err, rows) => {
|
||||
const all = (sql, params = []) => new Promise((resolve, reject) => {
|
||||
db.all(sql, params, (err, rows) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
@ -54,7 +69,10 @@ export const getChats = () => new Promise((resolve, reject) => {
|
||||
})
|
||||
})
|
||||
|
||||
export const saveLogout = displayName => run(
|
||||
export const saveChat = id => run(`INSERT OR IGNORE INTO chats (chatId) VALUES (?)`, [ id ])
|
||||
export const getChats = () => all('SELECT * FROM chats')
|
||||
|
||||
export const saveLogoutTime = displayName => run(
|
||||
`INSERT OR REPLACE INTO playerLogouts (displayName, lastLogoutTime) VALUES (?, ?)`,
|
||||
[ displayName, currentTime() ]
|
||||
)
|
||||
@ -75,3 +93,28 @@ export const getKillCount = (displayName, entityName) => get(
|
||||
'killCount',
|
||||
0
|
||||
)
|
||||
|
||||
export const saveLevelUp = (displayName, levelUpCount) => run(
|
||||
`INSERT OR REPLACE INTO levelUps (displayName, levelUpCount) VALUES (?, ?)`,
|
||||
[ displayName, levelUpCount ]
|
||||
)
|
||||
export const getLevelUpCount = displayName => get(
|
||||
`SELECT levelUpCount FROM levelUps WHERE displayName = ?`,
|
||||
[ displayName ],
|
||||
'levelUpCount',
|
||||
0
|
||||
)
|
||||
export const saveLevel = (displayName, newLevel) => run(
|
||||
`INSERT OR REPLACE INTO playerData (displayName, level, online) VALUES (?, ?, 1)`,
|
||||
[ displayName, newLevel ]
|
||||
)
|
||||
|
||||
export const setOnlineState = displayName => run(
|
||||
`INSERT OR REPLACE INTO playerData (displayName, online) VALUES (?, 1)`,
|
||||
[ displayName ]
|
||||
)
|
||||
export const setOfflineState = displayName => run(
|
||||
`INSERT OR REPLACE INTO playerData (displayName, online) VALUES (?, ?, 0)`,
|
||||
[ displayName ]
|
||||
)
|
||||
export const getPlayersData = () => all('SELECT * FROM playerData ORDER BY online DESC, displayName ASC')
|
||||
|
Loading…
x
Reference in New Issue
Block a user