mirror of
https://github.com/IgorL123/mafia_api.git
synced 2026-09-23 23:20:19 +00:00
added comments and fix er
This commit is contained in:
+8
-5
@@ -29,19 +29,22 @@ def isEnd(cur, trueChatID):
|
||||
return ""
|
||||
|
||||
|
||||
def role_distribution(userIDs):
|
||||
def role_distribution(userIDs): # Распределение ролей
|
||||
user_count = len(userIDs)
|
||||
# Сначала нам необходимо понять, сколько каких ролей будет в зависимости от кол-ва игроков
|
||||
userIDs = list(userIDs)
|
||||
resp = {}
|
||||
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]):
|
||||
print(type(userIDs))
|
||||
# Ну и, пробегая по каждой роли, убираем из массива один ID,
|
||||
resp[userIDs.pop()] = str(i)
|
||||
# присваивая ему роль, добавляя в словарь вывода
|
||||
|
||||
return resp
|
||||
|
||||
# [0, 4, 1, 1, 2]
|
||||
|
||||
@@ -54,12 +54,11 @@ def start(): # Команда принимает на вход
|
||||
userlist = request.json['userIDs']
|
||||
cursor = db.connection.cursor()
|
||||
for idi in userlist:
|
||||
db.query_ins_user(id, curs)
|
||||
db.close()
|
||||
db.query_ins_user(id, cursor)
|
||||
db.close(cursor)
|
||||
resp = api_functions.role_distribution(userlist)
|
||||
return "", resp
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
@@ -50,4 +50,4 @@ def query_change(x):
|
||||
|
||||
|
||||
def close(cur):
|
||||
cursor.close()
|
||||
cur.close()
|
||||
|
||||
@@ -5,7 +5,20 @@
|
||||
# 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))
|
||||
|
||||
Reference in New Issue
Block a user