2018-10-24 21:21:48 +02:00
|
|
|
class base_quest():
|
|
|
|
TABLE = None
|
|
|
|
|
|
|
|
def __init__(self, db, chat_id, qid, name=None, imp=None, diff=None,
|
2018-11-01 13:57:01 +01:00
|
|
|
state=None):
|
2018-10-24 21:21:48 +02:00
|
|
|
self.DB = db
|
|
|
|
self.CHAT_ID = chat_id
|
|
|
|
self.name = name
|
|
|
|
self.QID = qid
|
|
|
|
self.imp = imp
|
|
|
|
self.diff = diff
|
|
|
|
self.state = state
|
|
|
|
|
2018-10-27 13:07:24 +02:00
|
|
|
def add_to_db(self):
|
|
|
|
cursor = self.DB.cursor()
|
|
|
|
query = (f'INSERT INTO {self.TABLE}(chat_id, qid, name, importance'
|
2018-11-01 13:57:01 +01:00
|
|
|
', difficulty, state) values(?, ?, ?, ?, ?, ?)')
|
2018-10-27 13:07:24 +02:00
|
|
|
cursor.execute(query, (self.CHAT_ID, self.QID, self.name, self.imp,
|
2018-11-01 13:57:01 +01:00
|
|
|
self.diff, self.state))
|
2018-10-27 13:07:24 +02:00
|
|
|
self.DB.commit()
|
|
|
|
|
|
|
|
def update_db(self):
|
|
|
|
cursor = self.DB.cursor()
|
|
|
|
query = (f'UPDATE {self.TABLE} SET name=?, importance=?, difficulty=?,'
|
2018-11-01 13:57:01 +01:00
|
|
|
' state=? WHERE chat_id=? AND qid=?')
|
|
|
|
cursor.execute(query, (self.name, self.imp, self.diff,
|
2018-10-27 13:07:24 +02:00
|
|
|
self.state, self.CHAT_ID, self.QID))
|
|
|
|
self.DB.commit()
|
|
|
|
|
2018-10-27 13:29:01 +02:00
|
|
|
def get_from_db(self):
|
|
|
|
cursor = self.DB.cursor()
|
2018-11-01 13:57:01 +01:00
|
|
|
query = (f'SELECT name, importance, difficulty, state FROM '
|
2018-10-30 23:00:43 +01:00
|
|
|
f'{self.TABLE} where chat_id=? AND qid=?')
|
|
|
|
cursor.execute(query, (self.CHAT_ID, self.QID))
|
2018-10-27 13:29:01 +02:00
|
|
|
output = cursor.fetchone()
|
2018-11-01 13:57:01 +01:00
|
|
|
self.name, self.imp, self.diff, self.state = output
|
2018-10-27 13:29:01 +02:00
|
|
|
|
2018-10-24 21:21:48 +02:00
|
|
|
|
|
|
|
class quest(base_quest):
|
|
|
|
TABLE = "quests"
|
|
|
|
|
|
|
|
|
|
|
|
class side_quest(base_quest):
|
|
|
|
TABLE = "side_quests"
|
2018-10-27 13:29:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_quest(db, chat_id, qid):
|
|
|
|
q = quest(db, chat_id, qid)
|
|
|
|
q.get_from_db()
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
2018-10-30 23:00:43 +01:00
|
|
|
def add_quest(db, chat_id, qid, name=None, imp=None, diff=None,
|
2018-11-01 13:57:01 +01:00
|
|
|
state=None):
|
|
|
|
q = quest(db, chat_id, qid, name, imp, diff, state)
|
2018-10-27 13:29:01 +02:00
|
|
|
q.add_to_db()
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
|
|
|
def get_side_quest(db, chat_id, qid):
|
|
|
|
q = side_quest(db, chat_id, qid)
|
|
|
|
q.get_from_db()
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
2018-10-30 23:00:43 +01:00
|
|
|
def add_side_quest(db, chat_id, qid, name=None, imp=None, diff=None,
|
2018-11-01 13:57:01 +01:00
|
|
|
state=None):
|
|
|
|
q = side_quest(db, chat_id, qid, name, imp, diff, state)
|
2018-10-27 13:29:01 +02:00
|
|
|
q.add_to_db()
|
|
|
|
return q
|
2018-10-28 11:34:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
class player():
|
|
|
|
def __init__(self, db, chat_id):
|
|
|
|
self.DB = db
|
|
|
|
self.CHAT_ID = chat_id
|
|
|
|
cursor = self.DB.cursor()
|
2018-10-30 23:00:43 +01:00
|
|
|
cursor.execute('SELECT * FROM state WHERE chat_id = ?', (chat_id,))
|
2018-10-28 11:34:52 +01:00
|
|
|
row = cursor.fetchone()
|
|
|
|
if not row:
|
2018-10-31 13:46:26 +01:00
|
|
|
cursor.execute('INSERT INTO state(chat_id, state, extra) '
|
2018-10-31 23:34:01 +01:00
|
|
|
'VALUES(?,?,?)', (chat_id, 'none', 0))
|
2018-10-28 11:34:52 +01:00
|
|
|
db.commit()
|
2018-10-30 23:00:43 +01:00
|
|
|
cursor.execute('SELECT * FROM points WHERE chat_id = ?', (chat_id,))
|
2018-10-28 11:34:52 +01:00
|
|
|
row = cursor.fetchone()
|
|
|
|
if not row:
|
2018-10-30 23:00:43 +01:00
|
|
|
cursor.execute('INSERT INTO points(chat_id, points) VALUES(?,?)',
|
2018-10-28 11:34:52 +01:00
|
|
|
(chat_id, 0))
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
cursor = self.DB.cursor()
|
2018-10-31 13:46:26 +01:00
|
|
|
query = 'SELECT state, extra FROM state WHERE chat_id=?'
|
2018-10-30 23:00:43 +01:00
|
|
|
cursor.execute(query, (self.CHAT_ID,))
|
2018-10-28 11:34:52 +01:00
|
|
|
output = cursor.fetchone()
|
2018-10-31 13:46:26 +01:00
|
|
|
return {"state": output[0], "extra": output[1]}
|
2018-10-28 11:34:52 +01:00
|
|
|
|
2018-10-31 13:46:26 +01:00
|
|
|
def set_state(self, state, extra=0):
|
2018-10-28 11:34:52 +01:00
|
|
|
cursor = self.DB.cursor()
|
2018-10-31 13:46:26 +01:00
|
|
|
query = 'UPDATE state SET state=?, extra=? WHERE chat_id=?'
|
|
|
|
cursor.execute(query, (state, extra, self.CHAT_ID))
|
2018-10-28 11:34:52 +01:00
|
|
|
self.DB.commit()
|
2018-10-30 04:20:46 +01:00
|
|
|
|
|
|
|
def get_points(self):
|
|
|
|
cursor = self.DB.cursor()
|
|
|
|
query = 'SELECT points FROM points WHERE chat_id=?'
|
2018-10-30 23:00:43 +01:00
|
|
|
cursor.execute(query, (self.CHAT_ID,))
|
2018-10-30 04:20:46 +01:00
|
|
|
output = cursor.fetchone()
|
|
|
|
return int(output[0])
|
|
|
|
|
|
|
|
def add_points(self, points):
|
|
|
|
new_points = self.get_points() + points
|
|
|
|
cursor = self.DB.cursor()
|
|
|
|
query = 'UPDATE points SET points=? WHERE chat_id=?'
|
|
|
|
cursor.execute(query, (new_points, self.CHAT_ID))
|
|
|
|
self.DB.commit()
|
2018-10-30 15:33:43 +01:00
|
|
|
|
2018-10-30 23:00:43 +01:00
|
|
|
def get_quests(self, state=0):
|
2018-10-30 15:33:43 +01:00
|
|
|
cursor = self.DB.cursor()
|
2018-11-01 13:57:01 +01:00
|
|
|
query = ('SELECT chat_id, qid, name, importance, difficulty, '
|
2018-10-30 15:33:43 +01:00
|
|
|
'state FROM quests WHERE chat_id = ?')
|
|
|
|
if state is not None:
|
|
|
|
query += ' AND state = ?'
|
2018-10-30 23:00:43 +01:00
|
|
|
cursor.execute(query, (self.CHAT_ID, state))
|
2018-10-30 15:33:43 +01:00
|
|
|
else:
|
2018-10-30 23:00:43 +01:00
|
|
|
cursor.execute(query, (self.CHAT_ID,))
|
2018-10-30 15:33:43 +01:00
|
|
|
quests = []
|
|
|
|
for row in cursor:
|
|
|
|
q = quest(self.DB, *row)
|
|
|
|
quests.append(q)
|
|
|
|
return quests
|
2018-10-30 23:00:43 +01:00
|
|
|
|
|
|
|
def get_side_quests(self, state=0):
|
|
|
|
cursor = self.DB.cursor()
|
2018-11-01 13:57:01 +01:00
|
|
|
query = ('SELECT chat_id, qid, name, importance, difficulty, '
|
2018-10-30 23:00:43 +01:00
|
|
|
'state FROM side_quests WHERE chat_id = ?')
|
|
|
|
if state is not None:
|
|
|
|
query += ' AND state = ?'
|
|
|
|
cursor.execute(query, (self.CHAT_ID, state))
|
|
|
|
else:
|
|
|
|
cursor.execute(query, (self.CHAT_ID,))
|
|
|
|
quests = []
|
|
|
|
for row in cursor:
|
|
|
|
q = side_quest(self.DB, *row)
|
|
|
|
quests.append(q)
|
|
|
|
return quests
|