diff --git a/assets/main.db.sql b/assets/main.db.sql index 1e2fcf1..db36ee6 100755 --- a/assets/main.db.sql +++ b/assets/main.db.sql @@ -2,7 +2,6 @@ BEGIN TRANSACTION; -- DROP TABLE IF EXISTS `word`; CREATE TABLE IF NOT EXISTS `word` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `is_russian` INTEGER NOT NULL, `word` TEXT UNIQUE ); -- DROP TABLE IF EXISTS `user`; @@ -14,6 +13,15 @@ CREATE TABLE IF NOT EXISTS `user` ( `date` INTEGER NOT NULL, PRIMARY KEY(`id`) ); +-- DROP TABLE IF EXISTS `reset`; +CREATE TABLE IF NOT EXISTS `reset` ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT, + `user_id` INTEGER, + `conf_id` INTEGER, + `date` INTEGER, + `relation_id` INTEGER, + FOREIGN KEY(`user_id`) REFERENCES `user`(`id`) +); -- DROP TABLE IF EXISTS `relations`; CREATE TABLE IF NOT EXISTS `relations` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, diff --git a/database.py b/database.py index fb453f3..f13475c 100755 --- a/database.py +++ b/database.py @@ -79,13 +79,15 @@ class DataBase: LEFT JOIN word w ON w.id = r.word_id LEFT JOIN `user` u ON u.id = r.user_id WHERE u.id = '%s' AND - r.conf_id = '%s' + r.conf_id = '%s' AND + r.id > (SELECT IFNULL(MAX(relation_id), 0) FROM reset WHERE user_id = '%s') GROUP BY w.word ORDER BY count DESC LIMIT %s """ % ( user_id, conf_id, + user_id, limit ) result = self.execute(sql) @@ -93,11 +95,12 @@ class DataBase: def here(self, user_id, conf_id): sql= """ - select distinct(u.username) from relations r - left join user u - on u.id = r.user_id - left join conf c on r.conf_id = c.id - where c.id = '%s' and + SELECT DISTINCT(u.username) FROM relations r + LEFT JOIN user u + ON u.id = r.user_id + LEFT JOIN conf c + ON r.conf_id = c.id + WHERE c.id = '%s' and u.id != '%s' """ % ( conf_id, @@ -106,6 +109,10 @@ class DataBase: result = self.execute(sql) return(result) +# def reset(self, user_id, conf_id): +# date = int(dt.datetime.now().strftime("%s")) +# sql + def close(self): self.conn.close() \ No newline at end of file diff --git a/worker.py b/worker.py index 99e175f..1d95f64 100755 --- a/worker.py +++ b/worker.py @@ -32,6 +32,20 @@ class MessageWorker: message += '*%s*: %s\n' % (word[1], word[0]) self.send(id=conf_id, msg=message) return True + + if msg['message']['text'] == '/reset': + conf_id = msg['message']['chat']['id'] + user_id = msg['message']['from']['id'] + chat_title = msg['message']['chat']['title'] + self.db.add_conf(conf_id, chat_title) + + message = """Your stat has been resetted.""" + self.db.reset( + conf_id=conf_id, + user_id=user_id) + + return True + if msg['message']['text'] == '@here': conf_id = msg['message']['chat']['id'] user_id = msg['message']['from']['id']