Add send_status, /me. Drop date from tables.

This commit is contained in:
Ceda EI 2018-11-01 18:27:01 +05:30
parent 7fca63a2ab
commit 0b74dcabf0
2 changed files with 47 additions and 28 deletions

39
bot.py
View File

@ -138,12 +138,38 @@ def add_imp(bot, update, player, type, qid):
x.update_db() x.update_db()
text = "Quest Added!" text = "Quest Added!"
bot.send_message(chat_id=chat_id, text=text)
send_status(bot, update, player)
def send_status(bot, update, player):
name = str(update.message.from_user.first_name)
if update.message.from_user.last_name:
name += " " + str(update.message.from_user.last_name)
points = player.get_points()
total_quests = len(player.get_quests(None))
completed_quests = len(player.get_quests(1))
total_side_quests = len(player.get_side_quests(None))
completed_side_quests = len(player.get_side_quests(1))
text = (f'<b>{name}</b>\n\n'
f'🔥XP: {points}\n'
f'Quests: {completed_quests}/{total_quests}\n'
f'💠Side Quests: {completed_side_quests}/{total_side_quests}\n')
chat_id = update.message.chat_id
custom_keyboard = [ custom_keyboard = [
['Add Quest', 'Add Side Quest'], ['Add Quest', 'Add Side Quest'],
['List Quests', 'List Side Quests'] ['List Quests', 'List Side Quests']
] ]
reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup) bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup,
parse_mode="HTML")
def me_handler(bot, update, db):
chat_id = update.message.chat_id
player = questable.player(db, chat_id)
send_status(bot, update, player)
def message_handling(bot, update, db): def message_handling(bot, update, db):
@ -183,12 +209,12 @@ cursor = db.cursor()
# Set up tables # Set up tables
queries = [ queries = [
("CREATE TABLE IF NOT EXISTS quests(chat_id int NOT NULL, qid int NOT" ("CREATE TABLE IF NOT EXISTS quests(chat_id int NOT NULL, qid int NOT"
" NULL, name varchar(255), difficulty int, importance int, date int" " NULL, name varchar(255), difficulty int, importance int, "
", state int DEFAULT 0, UNIQUE(chat_id, qid));"), "state int DEFAULT 0, UNIQUE(chat_id, qid));"),
("CREATE TABLE IF NOT EXISTS side_quests(chat_id int NOT NULL, qid int " ("CREATE TABLE IF NOT EXISTS side_quests(chat_id int NOT NULL, qid int "
"NOT NULL, name varchar(255), difficulty int, importance int, date " "NOT NULL, name varchar(255), difficulty int, importance int, "
"int, state int DEFAULT 0, UNIQUE(chat_id, qid));"), "state int DEFAULT 0, UNIQUE(chat_id, qid));"),
("CREATE TABLE IF NOT EXISTS points(chat_id int PRIMARY KEY, points " ("CREATE TABLE IF NOT EXISTS points(chat_id int PRIMARY KEY, points "
"int);"), "int);"),
@ -206,6 +232,9 @@ dispatcher = updater.dispatcher
start_handler = CommandHandler('start', start) start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler) dispatcher.add_handler(start_handler)
start_handler = CommandHandler('me', lambda x, y: me_handler(x, y, db))
dispatcher.add_handler(start_handler)
handler = MessageHandler(Filters.text, lambda x, y: message_handling(x, y, db)) handler = MessageHandler(Filters.text, lambda x, y: message_handling(x, y, db))
dispatcher.add_handler(handler) dispatcher.add_handler(handler)

View File

@ -1,11 +1,8 @@
from datetime import datetime
class base_quest(): class base_quest():
TABLE = None TABLE = None
def __init__(self, db, chat_id, qid, name=None, imp=None, diff=None, def __init__(self, db, chat_id, qid, name=None, imp=None, diff=None,
state=None, date=None): state=None):
self.DB = db self.DB = db
self.CHAT_ID = chat_id self.CHAT_ID = chat_id
self.name = name self.name = name
@ -13,37 +10,30 @@ class base_quest():
self.imp = imp self.imp = imp
self.diff = diff self.diff = diff
self.state = state self.state = state
if date:
if isinstance(date, datetime):
self.date = date
else:
self.date = datetime.fromtimestamp(date)
else:
self.date = None
def add_to_db(self): def add_to_db(self):
cursor = self.DB.cursor() cursor = self.DB.cursor()
query = (f'INSERT INTO {self.TABLE}(chat_id, qid, name, importance' query = (f'INSERT INTO {self.TABLE}(chat_id, qid, name, importance'
', difficulty, date, state) values(?, ?, ?, ?, ?, ?, ?)') ', difficulty, state) values(?, ?, ?, ?, ?, ?)')
cursor.execute(query, (self.CHAT_ID, self.QID, self.name, self.imp, cursor.execute(query, (self.CHAT_ID, self.QID, self.name, self.imp,
self.diff, self.date, self.state)) self.diff, self.state))
self.DB.commit() self.DB.commit()
def update_db(self): def update_db(self):
cursor = self.DB.cursor() cursor = self.DB.cursor()
query = (f'UPDATE {self.TABLE} SET name=?, importance=?, difficulty=?,' query = (f'UPDATE {self.TABLE} SET name=?, importance=?, difficulty=?,'
' date=?, state=? WHERE chat_id=? AND qid=?') ' state=? WHERE chat_id=? AND qid=?')
cursor.execute(query, (self.name, self.imp, self.diff, self.date, cursor.execute(query, (self.name, self.imp, self.diff,
self.state, self.CHAT_ID, self.QID)) self.state, self.CHAT_ID, self.QID))
self.DB.commit() self.DB.commit()
def get_from_db(self): def get_from_db(self):
cursor = self.DB.cursor() cursor = self.DB.cursor()
query = (f'SELECT name, importance, difficulty, date, state FROM ' query = (f'SELECT name, importance, difficulty, state FROM '
f'{self.TABLE} where chat_id=? AND qid=?') f'{self.TABLE} where chat_id=? AND qid=?')
cursor.execute(query, (self.CHAT_ID, self.QID)) cursor.execute(query, (self.CHAT_ID, self.QID))
output = cursor.fetchone() output = cursor.fetchone()
self.name, self.imp, self.diff, self.date, self.state = output self.name, self.imp, self.diff, self.state = output
class quest(base_quest): class quest(base_quest):
@ -61,8 +51,8 @@ def get_quest(db, chat_id, qid):
def add_quest(db, chat_id, qid, name=None, imp=None, diff=None, def add_quest(db, chat_id, qid, name=None, imp=None, diff=None,
state=None, date=None): state=None):
q = quest(db, chat_id, qid, name, imp, diff, state, date) q = quest(db, chat_id, qid, name, imp, diff, state)
q.add_to_db() q.add_to_db()
return q return q
@ -74,8 +64,8 @@ def get_side_quest(db, chat_id, qid):
def add_side_quest(db, chat_id, qid, name=None, imp=None, diff=None, def add_side_quest(db, chat_id, qid, name=None, imp=None, diff=None,
state=None, date=None): state=None):
q = side_quest(db, chat_id, qid, name, imp, diff, state, date) q = side_quest(db, chat_id, qid, name, imp, diff, state)
q.add_to_db() q.add_to_db()
return q return q
@ -127,7 +117,7 @@ class player():
def get_quests(self, state=0): def get_quests(self, state=0):
cursor = self.DB.cursor() cursor = self.DB.cursor()
query = ('SELECT chat_id, qid, name, importance, difficulty, date, ' query = ('SELECT chat_id, qid, name, importance, difficulty, '
'state FROM quests WHERE chat_id = ?') 'state FROM quests WHERE chat_id = ?')
if state is not None: if state is not None:
query += ' AND state = ?' query += ' AND state = ?'
@ -142,7 +132,7 @@ class player():
def get_side_quests(self, state=0): def get_side_quests(self, state=0):
cursor = self.DB.cursor() cursor = self.DB.cursor()
query = ('SELECT chat_id, qid, name, importance, difficulty, date, ' query = ('SELECT chat_id, qid, name, importance, difficulty, '
'state FROM side_quests WHERE chat_id = ?') 'state FROM side_quests WHERE chat_id = ?')
if state is not None: if state is not None:
query += ' AND state = ?' query += ' AND state = ?'