From a5d790ef5812bb50f4a80538741caf285f8b9510 Mon Sep 17 00:00:00 2001 From: IgorL123 Date: Tue, 22 Aug 2023 12:15:57 +0300 Subject: [PATCH 1/4] init db --- db.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 db.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..2e604e4 --- /dev/null +++ b/db.py @@ -0,0 +1,28 @@ +import psycopg2 + + +db_params = { + 'host': '51.250.68.42', + 'database': 'test', + 'user': 'curator', + 'password': '4oumQk' +} + + +try: + connection = psycopg2.connect(**db_params) + cursor = connection.cursor() + + # Ваш код выполнения запросов + + cursor.execute("SELECT * FROM chats") + rows = cursor.fetchall() + + for row in rows: + print(row) + + cursor.close() + connection.close() + +except Exception as e: + print("Ошибка:", e) From 74c563a96addb66c335c4b658352c71e3af05349 Mon Sep 17 00:00:00 2001 From: Anastasia Viktorova Date: Tue, 22 Aug 2023 16:10:24 +0300 Subject: [PATCH 2/4] action --- app.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- db.py | 6 ++++-- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 43d35e8..3db457c 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,67 @@ -from flask import Flask +from flask import Flask, request, jsonify +import db app = Flask(__name__) -@app.route('/') -def hello_world(): # put application's code here - return 'Hello World!' +def isEnd(cur, trueChatID): + # cur.execute(f"SELECT ChatID FROM Chats WHERE TrueChatID = '{trueChatID}'") + # row = cur.fetchall() + # chatID = row[0] + # + # cur.execute(f"SELECT UserID FROM UsersChats WHERE ChatID = '{chatID}'") + # usersIDs = cur.fetchall() + # + # allAlive = True + # for id in usersIDs: + # cur.execute(f"SELECT isAlive FROM Users WHERE UserID = '{id}', role = ") + # row = cur.fetchall() + # if row[0] == False: + # allAlive = False + + cur.execute(f"Select role from chats join userschats uc on uc.chatid = chats.chatid join " + f"users on users.userid = uc.userid where truechatid = '{trueChatID}' and isAlive = true") + roles = cur.fetchall() + + m = roles.count(4) + if m == 0: + return "citizens" + elif m >= len(roles) - m: + return "mafia" + + return "" + + +@app.route('/action', methods=["POST"]) +def action(): # put application's code here + data = request.json + trueChatID = data['chatID'] + killedUserID = data['KilledUserID'] + healedUserID = data['HealedUserID'] + checkedUserID = data['CheckedUserID'] + + response = { + "killedUserId": "", + "isEndGame": "", + "checkedRole": False + } + + cur = db.cursor + + if killedUserID != healedUserID: + cur.execute(f"UPDATE Users SET isAlive = false WHERE TrueUserID = '{killedUserID}'") + response["killedUserId"] = killedUserID + + cur.execute(f"SELECT role FROM users WHERE TrueUserID ='{checkedUserID}';") + row = cur.fetchall() + role = row[0] + + if role == 4: + response["checkedRole"] = True + + response["isEndGame"] = isEnd(cur, trueChatID) + + return jsonify(response), 200 if __name__ == '__main__': diff --git a/db.py b/db.py index 2e604e4..4ece3e4 100644 --- a/db.py +++ b/db.py @@ -3,7 +3,7 @@ import psycopg2 db_params = { 'host': '51.250.68.42', - 'database': 'test', + 'database': 'postgres', 'user': 'curator', 'password': '4oumQk' } @@ -15,7 +15,9 @@ try: # Ваш код выполнения запросов - cursor.execute("SELECT * FROM chats") + cursor.execute(f"Select role from chats join userschats uc on uc.chatid = " + f"chats.chatid join users on users.userid = uc.userid where truechatid = '{7656}' and isAlive = true") + rows = cursor.fetchall() for row in rows: From 24adab1e1964ad58a0bbc75163667cba3b569663 Mon Sep 17 00:00:00 2001 From: Anastasia Viktorova Date: Tue, 22 Aug 2023 16:37:51 +0300 Subject: [PATCH 3/4] kill by userID --- app.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 3db457c..739a77c 100644 --- a/app.py +++ b/app.py @@ -49,12 +49,14 @@ def action(): # put application's code here cur = db.cursor if killedUserID != healedUserID: - cur.execute(f"UPDATE Users SET isAlive = false WHERE TrueUserID = '{killedUserID}'") + cur.execute(f"Select UserID from chats join userschats uc on uc.chatid = chats.chatid join " + f"users on users.userid = uc.userid where truechatid = '{trueChatID}' and TrueUserID = '{killedUserID}'") + id = cur.fetchall()[0] + cur.execute(f"UPDATE Users SET isAlive = false WHERE UserID = '{id}'") response["killedUserId"] = killedUserID cur.execute(f"SELECT role FROM users WHERE TrueUserID ='{checkedUserID}';") - row = cur.fetchall() - role = row[0] + role = cur.fetchall()[0] if role == 4: response["checkedRole"] = True From 4a1a93792dae1e00d25e7b5a06d0bbe325a0c614 Mon Sep 17 00:00:00 2001 From: Anastasia Viktorova Date: Tue, 22 Aug 2023 16:41:36 +0300 Subject: [PATCH 4/4] new file: api functions --- api_functions.py | 26 ++++++++++++++++++++++++++ app.py | 31 ++----------------------------- 2 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 api_functions.py diff --git a/api_functions.py b/api_functions.py new file mode 100644 index 0000000..285d3ff --- /dev/null +++ b/api_functions.py @@ -0,0 +1,26 @@ +def isEnd(cur, trueChatID): + # cur.execute(f"SELECT ChatID FROM Chats WHERE TrueChatID = '{trueChatID}'") + # row = cur.fetchall() + # chatID = row[0] + # + # cur.execute(f"SELECT UserID FROM UsersChats WHERE ChatID = '{chatID}'") + # usersIDs = cur.fetchall() + # + # allAlive = True + # for id in usersIDs: + # cur.execute(f"SELECT isAlive FROM Users WHERE UserID = '{id}', role = ") + # row = cur.fetchall() + # if row[0] == False: + # allAlive = False + + cur.execute(f"Select role from chats join userschats uc on uc.chatid = chats.chatid join " + f"users on users.userid = uc.userid where truechatid = '{trueChatID}' and isAlive = true") + roles = cur.fetchall() + + m = roles.count(4) + if m == 0: + return "citizens" + elif m >= len(roles) - m: + return "mafia" + + return "" diff --git a/app.py b/app.py index 739a77c..58031fc 100644 --- a/app.py +++ b/app.py @@ -1,37 +1,10 @@ from flask import Flask, request, jsonify import db +import api_functions app = Flask(__name__) -def isEnd(cur, trueChatID): - # cur.execute(f"SELECT ChatID FROM Chats WHERE TrueChatID = '{trueChatID}'") - # row = cur.fetchall() - # chatID = row[0] - # - # cur.execute(f"SELECT UserID FROM UsersChats WHERE ChatID = '{chatID}'") - # usersIDs = cur.fetchall() - # - # allAlive = True - # for id in usersIDs: - # cur.execute(f"SELECT isAlive FROM Users WHERE UserID = '{id}', role = ") - # row = cur.fetchall() - # if row[0] == False: - # allAlive = False - - cur.execute(f"Select role from chats join userschats uc on uc.chatid = chats.chatid join " - f"users on users.userid = uc.userid where truechatid = '{trueChatID}' and isAlive = true") - roles = cur.fetchall() - - m = roles.count(4) - if m == 0: - return "citizens" - elif m >= len(roles) - m: - return "mafia" - - return "" - - @app.route('/action', methods=["POST"]) def action(): # put application's code here data = request.json @@ -61,7 +34,7 @@ def action(): # put application's code here if role == 4: response["checkedRole"] = True - response["isEndGame"] = isEnd(cur, trueChatID) + response["isEndGame"] = api_functions.isEnd(cur, trueChatID) return jsonify(response), 200