diff --git a/__pycache__/db.cpython-311.pyc b/__pycache__/db.cpython-311.pyc deleted file mode 100644 index bf498f9..0000000 Binary files a/__pycache__/db.cpython-311.pyc and /dev/null differ diff --git a/api_functions.py b/api_functions.py index 826800c..67571e1 100644 --- a/api_functions.py +++ b/api_functions.py @@ -1,3 +1,6 @@ +import random + + def isEnd(cur, trueChatID): # cur.execute(f"SELECT ChatID FROM Chats WHERE TrueChatID = '{trueChatID}'") # row = cur.fetchall() @@ -23,4 +26,25 @@ def isEnd(cur, trueChatID): elif m >= len(roles) - m: return "mafia" - return None + return "" + + +def role_distribution(userIDs): # Распределение ролей + user_count = len(userIDs) + # Сначала нам необходимо понять, сколько каких ролей будет в зависимости от кол-ва игроков + userIDs = list(userIDs) + resp = {} # Инициализируем будущий ответ(в формате словаря {ID : Роль}) + ls = [0, 0, 1, 1, 0] + # Массив с ролями, где индекс - роль (0 - inactive, 1 - civil, 2 - doc, 3 - comm, 4 - maf), док и ком всегда один + ls[4] = user_count//4 + # Мафии вычисляются, как кол-во игроков//4, остальные мирные + ls[1] = user_count - ls[4]-2 + # Перемешиваем массив чтоб постоянно одни и те же роли не выпадали + random.shuffle(userIDs) + for i in range(5): + for j in range(ls[i]): + # Ну и, пробегая по каждой роли, убираем из массива один ID, + resp[userIDs.pop()] = str(i) + # присваивая ему роль, добавляя в словарь вывода + + return resp diff --git a/app.py b/app.py index e912eec..02d8b7a 100644 --- a/app.py +++ b/app.py @@ -24,7 +24,7 @@ def action(): # put application's code here 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}'") + 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 @@ -50,7 +50,15 @@ def create(): @app.route('/start_game', methods=["POST"]) def start(): # Команда принимает на вход - return '', 501 + TCI = request.json['chatID'] + userlist = request.json['userIDs'] + cursor = db.connection.cursor() + for idi in userlist: + db.query_ins_user(id, cursor) + db.close(cursor) + resp = api_functions.role_distribution(userlist) + return "", resp + if __name__ == '__main__': diff --git a/test.py b/test.py new file mode 100644 index 0000000..ad3103c --- /dev/null +++ b/test.py @@ -0,0 +1,24 @@ +# for i in range(0): +# print(i) + +# d = dict +# print(d) +import random + +def role_distribution(userIDs): + user_count = len(userIDs) + userIDs = list(userIDs) + resp = {} # resp = {userIDs[i]: 0 for i in range(len(userIDs))} + ls = [0, 0, 1, 1, 0] + ls[4] = user_count//4 + ls[1] = user_count - ls[4]-2 + random.shuffle(userIDs) + for i in range(5): + for j in range(ls[i]): + print(type(userIDs)) + resp[userIDs.pop()] = str(i) + + return resp +a = ["a", "b", "c", "d", "e", "f", "g"] + +print(role_distribution(a))