Skip to content
Permalink
Browse files

Added lederbords a little

  • Loading branch information...
YogurtTheHorse committed Sep 4, 2016
1 parent 2096167 commit 1e9a981fcd32dc1fc4c0eeefaec20cb253c7d7ec
Showing with 83 additions and 7 deletions.
  1. +1 −0 .gitignore
  2. +49 −0 databasemanager.py
  3. +25 −5 main.py
  4. +2 −1 requirements.txt
  5. +4 −0 user.py
  6. +2 −1 usermanager.py
@@ -91,6 +91,7 @@ ENV/

# Project trash
users/
data/
config.py
vk_config.json
rogue_bot.sublime-workspace
@@ -0,0 +1,49 @@
import config
from tinydb import TinyDB, Query

VAR_TABLE = 'vars'
ROOMS_TABLE = 'rooms'
KILLS_TABLE = 'kills'

db = TinyDB(config.DATABASE_PATH)

def get_variabe(name, def_val=None):
global db
Variable = Query()

table = db.table(VAR_TABLE)
ans = table.get(Variable.name == name)
if ans:
return ans['value']
else:
return def_val

def set_variable(name, value):
global db

table = db.table(VAR_TABLE)
return table.insert({'name':name,'value':value})

def add_to_lederboard(user, score, lederboard_name='rooms'):
global db

table = db.table(lederboard_name)
doc = {
'uid': user.uid,
'name': user.name,
'score': score
}
table.insert(doc)

def get_lederboard(lederboard_name='rooms', count=10):
global db

def sort_by_score(doc):
return doc['score']

table = db.table(lederboard_name)

res = table.all()
res.sort(key=sort_by_score)

return res[:count]
30 main.py
@@ -4,6 +4,7 @@
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram.ext import Job

import databasemanager
import usermanager
import telegram
import logging
@@ -209,6 +210,24 @@ def rep(txt, btns=None, photo=None):
context=(c_id, bot, msg, buttons, image))
reply(c_id, bot, msg, buttons, image)

def lederboard(bot, update):
c_id = update.message.chat_id

res = databasemanager.get_lederboard()

msg = 'По открытым комнатам побеждают:\n\n'

for i, r in enumerate(res):
uid = r['uid']
name = r['name']
score = r['score']

table_name = "{0} ({1})".format(name, bot.getChat(uid)['username'])
msg += '{0}. {1}: {2}'.format(i + 1, table_name, score)

bot.sendMessage(update.message.chat_id, text=msg)


def error_callback(bot, update, error):
error_msg = 'User "%s" had error "%s"' % (update.message.chat_id, error)
if '429' in str(error):
@@ -221,17 +240,18 @@ def error_callback(bot, update, error):
text='```text\n{0}\n```'.format(error_msg),
parse_mode=telegram.ParseMode.MARKDOWN)

if not os.path.isdir('users'):
if not os.path.isdir(config.USERS_PATH):
logger.info('Creating users directory')
os.makedirs('users')
os.makedirs(config.USERS_PATH)

logger.info('Creating Updater...')
updater = Updater(config.TELEGRAM_TOKEN)

updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('debug', debug_print))
updater.dispatcher.add_handler(CommandHandler('notify', notify))
updater.dispatcher.add_handler(CommandHandler('lederboard', lederboard))
updater.dispatcher.add_handler(CommandHandler('setname', setname))
updater.dispatcher.add_handler(CommandHandler('notify', notify))
updater.dispatcher.add_handler(CommandHandler('debug', debug_print))
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('room', room))
updater.dispatcher.add_handler(CommandHandler('give', give))

@@ -1,2 +1,3 @@
python-telegram-bot
vk_api
vk_api
tinydb
@@ -13,6 +13,7 @@
from collections import Counter

from localizations import locale_manager
import databasemanager as dbmanager

logger = logging.getLogger('rg')

@@ -291,6 +292,9 @@ def death(self, reply):
self.dead = True
self.state = ''

dbmanager.add_to_lederboard(self, self.rooms_count)
dbmanager.add_to_lederboard(self, self.monsters_killed, dbmanager.KILLS_TABLE)

reply(locale_manager.get('DEAD_MESSAGE').format(self.monsters_killed, self.rooms_count), [ '/start' ])

def open_corridor(self, reply):
@@ -2,10 +2,11 @@
import glob
import pickle
import random
import config
from user import User

def get_fname(uid):
return 'users/{0}.usr'.format(uid)
return config.USERS_PATH + '/{0}.usr'.format(uid)

def save_user(usr):
with open(get_fname(usr.uid), 'wb') as outfile:

0 comments on commit 1e9a981

Please sign in to comment.
You can’t perform that action at this time.