From 5cc1433ab412a59285e0905974b9949f046fa4f4 Mon Sep 17 00:00:00 2001 From: ada-dmitry Date: Tue, 22 Aug 2023 17:14:34 +0300 Subject: [PATCH 1/7] add start_game --- app.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 5361a26..c5abe65 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 @@ -48,15 +48,14 @@ def create(): db.close(cursor) -@app.route('/start_game') +@app.route('/start_game', methods = ["POST"]) def start(): # Команда принимает на вход - pass - - -@app.route('/action') -def action(): - pass - + TCI = request.json['chatID'] + userlist = request.json['usderIDs'] + cursor = db.connection.cursor() + for id in userlist: + db.query_ins_user(id, curs) + db.close() if __name__ == '__main__': app.run() From 0b2580592a12ac9c2901b54d0ed9a002ba8bc461 Mon Sep 17 00:00:00 2001 From: ada-dmitry Date: Tue, 22 Aug 2023 23:55:00 +0300 Subject: [PATCH 2/7] add func --- api_functions.py | 14 ++++++++++++++ app.py | 3 ++- db.py | 1 - 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/api_functions.py b/api_functions.py index 285d3ff..ac0411f 100644 --- a/api_functions.py +++ b/api_functions.py @@ -1,3 +1,6 @@ +from random import shuffle + + def isEnd(cur, trueChatID): # cur.execute(f"SELECT ChatID FROM Chats WHERE TrueChatID = '{trueChatID}'") # row = cur.fetchall() @@ -24,3 +27,14 @@ def isEnd(cur, trueChatID): return "mafia" return "" + + +def role_cnt(user_count): + ls = [0, 0, 1, 1, 0] + ls[4] = user_count//4 + ls[1] = user_count - ls[4] + return ls + + +def role_distribution(userIDs, count_r): + pass diff --git a/app.py b/app.py index c5abe65..97087c2 100644 --- a/app.py +++ b/app.py @@ -48,7 +48,7 @@ def create(): db.close(cursor) -@app.route('/start_game', methods = ["POST"]) +@app.route('/start_game', methods=["POST"]) def start(): # Команда принимает на вход TCI = request.json['chatID'] userlist = request.json['usderIDs'] @@ -57,5 +57,6 @@ def start(): # Команда принимает на вход db.query_ins_user(id, curs) db.close() + if __name__ == '__main__': app.run() diff --git a/db.py b/db.py index 43d3b4f..72cf6d7 100644 --- a/db.py +++ b/db.py @@ -51,4 +51,3 @@ def query_change(x): def close(cur): cursor.close() - From 8101ab3c7cbd1e65bafa6921741f7813ed1bc3dd Mon Sep 17 00:00:00 2001 From: ada-dmitry Date: Wed, 23 Aug 2023 16:02:18 +0300 Subject: [PATCH 3/7] added /start_game --- api_functions.py | 19 +++++++++++++------ app.py | 7 +++++-- test.py | 11 +++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 test.py diff --git a/api_functions.py b/api_functions.py index ac0411f..5ecd831 100644 --- a/api_functions.py +++ b/api_functions.py @@ -1,4 +1,4 @@ -from random import shuffle +import random def isEnd(cur, trueChatID): @@ -29,12 +29,19 @@ def isEnd(cur, trueChatID): return "" -def role_cnt(user_count): +def role_distribution(userIDs): + user_count = len(userIDs) + userIDs = list(userIDs) + resp = {} ls = [0, 0, 1, 1, 0] ls[4] = user_count//4 - ls[1] = user_count - ls[4] - return ls + 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 -def role_distribution(userIDs, count_r): - pass + # [0, 4, 1, 1, 2] diff --git a/app.py b/app.py index 97087c2..e80ab22 100644 --- a/app.py +++ b/app.py @@ -51,11 +51,14 @@ def create(): @app.route('/start_game', methods=["POST"]) def start(): # Команда принимает на вход TCI = request.json['chatID'] - userlist = request.json['usderIDs'] + userlist = request.json['userIDs'] cursor = db.connection.cursor() - for id in userlist: + for idi in userlist: db.query_ins_user(id, curs) db.close() + 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..5ac60fe --- /dev/null +++ b/test.py @@ -0,0 +1,11 @@ +# for i in range(0): +# print(i) + +# d = dict +# print(d) +import random + + +a = ["a", "b", "c", "d", "e", "f", "g"] + +print(role_distribution(a)) From c7e00ec3617f8924b00ff042889311b1c5a8b318 Mon Sep 17 00:00:00 2001 From: ada-dmitry Date: Wed, 23 Aug 2023 16:05:31 +0300 Subject: [PATCH 4/7] mod gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6b638aa..35153a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ venv setiings.json -.vscode \ No newline at end of file +.vscode +__pycache__ \ No newline at end of file From 37ed8a4a1a9faa3f28ac91ba9a4c3fd170aae047 Mon Sep 17 00:00:00 2001 From: Dmitry <124861781+ada-dmitry@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:06:42 +0300 Subject: [PATCH 5/7] Delete __pycache__ directory --- __pycache__/db.cpython-311.pyc | Bin 2413 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 __pycache__/db.cpython-311.pyc diff --git a/__pycache__/db.cpython-311.pyc b/__pycache__/db.cpython-311.pyc deleted file mode 100644 index bf498f9881cf8a13b628e8791bbf850a6396b622..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2413 zcmds2-%A`v9G}^pJ9p1p<8Q4=>_*czml#n=LJ6TDYC*8qCTE*^2xh$-6pKv@O&qOq@VbWJ;!N64Ph;KPOQvA&Ra` zyI&$R7)KO;g&*LkOyxCFTCW_}Ve+`V5G9mMVY6xpM|~br0Qo(n0;)0tODL*PLggvR ztcDfU#1RzIeistL^TD&d-F;_!y3f7ceWuSPHs!Wk6-zV2wwUWbW2L7r-UtzwT(f|o zn1*dk8l1W$%_(!0l)?Fo#mrb)0T3$Gmf_w00mv#+(E?rr^lCHl0{RTh@rAsOK_z&L z4T~M+S!q>_`&Gwa(g^BAc7>UaBX)REva9xXneu|P4(EGZJ|jkBuyawmEs6a_G=pc* ztUQKhaEOHGFARqxLzhPNc+$4?RC+QIk9B_t8uJsQEKLO(9_-SYm7raEocAZnC24F1;u8LW3hXUuKt`pPVPo&(8PfH;7wBXK5^LQ`aA}rl;e!s}9Y^Xv&UT zN!OQR@VgD!#j`E}4{#S#F6^l5`pX{k{P<;y-54LR%y{zZ_$8XMc-$VJHm2gngprC@ zAak-iHRsmcOj9;D5l`|7;Vaexl4{`Pvp_PaAo+Y9TT1X@a3$z8O*kK27S!umu&-l}Y%1Ea_2_0+IhIq7IS2I}XayNI!MNQBKfimr z7d;8|H_N}&_SeZz>wEzJZvk}ffB+h}X4u655=aD4#5Qa?Jh&G+tQzdYf`W+fVf8>t zF~FK(Qt8$hNMUWTC|p~1EAe#*zjsK|R*B}6sG~&xd$+PyIKja#{02CG zT?77I?k{?|dqD2X`*L4ekPpm#`L?{9`+zEPUk=YVM21F&21fOh`n#9je@_=y=AKJJ zgoy-ll__e&Y9hgEVan=&0INY@5#BCg)(mG&0J$hkkiL4+cWN;uIZd#3!4O|0frxBW zE3)xqRymbZPC3e{{Go=2iz|!w?%cn#OvJPEB7lNTd+Fz)f0l#X%fZlT^j)C8UjDJR zUz49|K7g^xk}I%R#Qgxo-4#I_IJ|*E*wd5?{;pcVqFP{* Date: Thu, 24 Aug 2023 12:21:02 +0300 Subject: [PATCH 6/7] 0.4 --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index e80ab22..2b84a0b 100644 --- a/app.py +++ b/app.py @@ -57,7 +57,7 @@ def start(): # Команда принимает на вход db.query_ins_user(id, curs) db.close() resp = api_functions.role_distribution(userlist) - return ("", resp) + return "", resp From 7227efa00ee86bd23cca14ca74b5a0306c85eb35 Mon Sep 17 00:00:00 2001 From: ada-dmitry Date: Thu, 24 Aug 2023 12:34:10 +0300 Subject: [PATCH 7/7] added comments and fix er --- api_functions.py | 13 ++++++++----- app.py | 5 ++--- db.py | 2 +- test.py | 13 +++++++++++++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/api_functions.py b/api_functions.py index 5ecd831..67571e1 100644 --- a/api_functions.py +++ b/api_functions.py @@ -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] diff --git a/app.py b/app.py index 2b84a0b..91d8dc1 100644 --- a/app.py +++ b/app.py @@ -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() diff --git a/db.py b/db.py index 72cf6d7..15d0a02 100644 --- a/db.py +++ b/db.py @@ -50,4 +50,4 @@ def query_change(x): def close(cur): - cursor.close() + cur.close() diff --git a/test.py b/test.py index 5ac60fe..ad3103c 100644 --- a/test.py +++ b/test.py @@ -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))