From 39ab2842619f68097e0667dda66e82b147cd9182 Mon Sep 17 00:00:00 2001 From: Anastasia Viktorova Date: Tue, 22 Aug 2023 16:41:36 +0300 Subject: [PATCH] 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