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 9958ace..5361a26 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,43 @@ -from flask import Flask, request +from flask import Flask, request, jsonify import db +import api_functions + app = Flask(__name__) -@app.route('/') -def hello_world(): # put application's code here - return 'Hello World!' +@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"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}';") + role = cur.fetchall()[0] + + if role == 4: + response["checkedRole"] = True + + response["isEndGame"] = api_functions.isEnd(cur, trueChatID) + + return jsonify(response), 200 @app.route('/create_game', methods=["POST"]) diff --git a/db.py b/db.py index e8c021a..43d3b4f 100644 --- a/db.py +++ b/db.py @@ -8,6 +8,7 @@ db_params = { 'password': '4oumQk' } + connection = psycopg2.connect(**db_params) # запускаем один раз @@ -50,3 +51,4 @@ def query_change(x): def close(cur): cursor.close() +