Skip to content

Commit bf73859

Browse files
committed
TASK: Implement method to get playing users
1 parent 13cf730 commit bf73859

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

database/db_wrapper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import os
33
import sqlite3
4+
from time import time
45

56
__author__ = 'Rico'
67

@@ -60,6 +61,13 @@ def get_user(self, user_id: int) -> tuple:
6061
else:
6162
return ()
6263

64+
def get_recent_players(self):
65+
oneDayInSecs = 60 * 60 * 24
66+
currentTime = int(time())
67+
self.cursor.execute("SELECT userID FROM users WHERE lastPlayed>=?;", [currentTime - oneDayInSecs])
68+
69+
return self.cursor.fetchall()
70+
6371
def get_played_games(self, user_id: int) -> int:
6472
self.cursor.execute("SELECT gamesPlayed FROM users WHERE userID=?;", [str(user_id)])
6573

main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ def answer(bot, update):
242242
bot.sendMessage(chat_id=sender_id, text="Message sent!")
243243

244244

245+
def users(bot, update):
246+
sender_id = update.message.from_user.id
247+
db = DBwrapper.get_instance()
248+
players = db.get_recent_players()
249+
250+
text = "Last 24 hours: {}".format(len(players))
251+
252+
if sender_is_admin(sender_id):
253+
bot.sendMessage(chat_id=sender_id, text=text)
254+
# TODO get users of e.g. last 24 hours
255+
256+
245257
def sender_is_admin(user_id: int) -> bool:
246258
db = DBwrapper.get_instance()
247259
return user_id in db.get_admins()
@@ -347,6 +359,7 @@ def get_translations_of_string(string):
347359
language_handler = CommandHandler('language', language_cmd)
348360
comment_handler = CommandHandler('comment', comment_cmd)
349361
callback_handler = CallbackQueryHandler(callback_eval)
362+
users_handler = CommandHandler('users', users)
350363
answer_handler = CommandHandler('answer', answer)
351364

352365
game_command_handler = MessageHandler(Filters.text, game_commands)
@@ -361,6 +374,7 @@ def get_translations_of_string(string):
361374
dispatcher.add_handler(language_handler)
362375
dispatcher.add_handler(comment_handler)
363376
dispatcher.add_handler(callback_handler)
377+
dispatcher.add_handler(users_handler)
364378
dispatcher.add_handler(answer_handler)
365379

366380
dispatcher.add_handler(mp_handler)

0 commit comments

Comments
 (0)