This commit is contained in:
Anastasia Viktorova
2023-08-22 16:10:24 +03:00
parent a5d790ef58
commit 7781a1184c
2 changed files with 64 additions and 6 deletions
+60 -4
View File
@@ -1,11 +1,67 @@
from flask import Flask from flask import Flask, request, jsonify
import db
app = Flask(__name__) app = Flask(__name__)
@app.route('/') def isEnd(cur, trueChatID):
def hello_world(): # put application's code here # cur.execute(f"SELECT ChatID FROM Chats WHERE TrueChatID = '{trueChatID}'")
return 'Hello World!' # 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__': if __name__ == '__main__':
+4 -2
View File
@@ -3,7 +3,7 @@ import psycopg2
db_params = { db_params = {
'host': '51.250.68.42', 'host': '51.250.68.42',
'database': 'test', 'database': 'postgres',
'user': 'curator', 'user': 'curator',
'password': '4oumQk' '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() rows = cursor.fetchall()
for row in rows: for row in rows: