This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
195 lines (129 loc) · 5.53 KB
/
process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from db import *
from gen import *
from random import shuffle
tour_name = n_teams = n_members = roundno = p_tb_col = winner = 0
def rounds(teams):
""" recursive function used to get round inputs and save it in db """
global roundno
roundno += 1
shuffle(teams)
create_round_tb(roundno, tour_name)
seperator(2)
#seperate tournament match for 3 teams
if len(teams) == 3:
print('3 team')
return _3teams(teams)
#Splitting teams in 2s
match_making = [tuple(teams[x : x + 2]) for x in range(0, len(teams), 2)]
#Checking for odd team
oddteam_id = (match_making.pop())[0] if len(match_making[-1]) == 1 else None # and type(match_making[-1]) == type(tuple())
#insert teams data on round table
for team in match_making:
insert_round(team, roundno, tour_name)
for _id in range(1, len(match_making) + 1):
get_winner(_id, roundno, tour_name)
win_ids = [int(x) for x in get_win_ids(roundno, tour_name)]
if oddteam_id:
return oddteam((oddteam_id, win_ids[0]), _id + 1, win_ids[1:])
if len(win_ids) == 1:
return win_ids[0]
return rounds(win_ids) #qualifying winners to next round
def _3teams(teams):
""" different gameplay algorithm for tournament with 3 teams in total/round """
global roundno
t1, t2, t3 = teams
match_id = 1
insert_round((t1, t2), roundno, tour_name)
m1winner = get_winner(match_id, roundno, tour_name); match_id += 1
t4 = t2 if m1winner == t1 else t1
insert_round((t3, t4), roundno, tour_name)
m2winner = get_winner(match_id, roundno, tour_name); match_id += 1
if m2winner != t4:
insert_round((m1winner, m2winner), roundno, tour_name)
return get_winner(match_id, roundno, tour_name)
elif m2winner == t4:
insert_round((m1winner, t3), roundno, tour_name)
m3winner = get_winner(match_id, roundno, tour_name); match_id += 1
if m3winner == t3:
print("Round has resulted in draw! \nReplaying round!")
roundno += 1
create_round_tb(roundno, tour_name)
return _3teams(teams)
else:
insert_round((m1winner, m2winner), roundno, tour_name)
return get_winner(match_id, roundno, tour_name)
def oddteam(teams, match_id, win_ids):
""" gets winner input and stores it on db """
insert_round(teams, roundno, tour_name)
win_ids.append(get_winner(match_id, roundno, tour_name))
return rounds(win_ids)
def teams():
""" gets team input team and calls function for members' name """
for i in range(1, n_teams + 1):
name = input(f"\nEnter team ({i}) name: ")
insert('teams', tour_name, 'team_name', f'"{name}"')
members_data(name)
seperator()
print('Completed creating tables for all teams!')
def members_data(team_name):
""" function to get input members' names """
members = tuple()
for i in range(1, n_members + 1):
members += (input(f'Enter member {i}\'s name: '),)
val = f"'{team_name}', {str(members)[1:-1] if len(members) > 1 else str(members)[1:-2]}"
insert('players', tour_name, p_tb_col, val)
def initialize_db():
""" creates all required tables within the database with the tournament's name """
#creating db for the whole tournament
create_db(tour_name)
#creating table for the list of teams and their wins/loss
create_table(
"teams",
"team_id INT PRIMARY KEY AUTO_INCREMENT,\
team_name VARCHAR(30),\
wins INT NOT NULL DEFAULT 0,\
loss INT NOT NULL DEFAULT 0",
tour_name)
#creating table for list of teams with members data
create_table(
"players",
"team_id INT PRIMARY KEY AUTO_INCREMENT,\
team_name VARCHAR(30)",
tour_name)
#adding columns for the members
add_member_col(n_members, "players", tour_name)
seperator()
def user_inputs():
""" to get basic input about the tournament """
global tour_name, n_teams, n_members, p_tb_col
tour_name = unique_input("Enter tournament's name : ", show_dbs(), "Tournament {} has already been created! \nTry a new name\n\n")
n_teams = get_num_input("Enter total no of teams : ")
n_members = get_num_input("Enter no of members on each team : ")
p_tb_col = get_member_col(('team_name',), n_members)
seperator()
def save_winner():
""" saves the winner of the current tournament in the global database """
create_table("winner",
"id INT NOT NULL,\
name VARCHAR(30) NOT NULL,\
wins INT NOT NULL,\
loss INT NOT NULL",
tour_name)
add_member_col(n_members, "winner", tour_name)
insert("winner", tour_name,
get_member_col(("id", "name", "wins", "loss"), n_members),
str(', '.join([(str(x)) if type(x) != type('') else ('"'+str(x)+'"') for x in winner_data(1, 'x')])))
data = fetch('winner', tour_name)[0][:2] + (len(fetch('teams', tour_name, 'team_id')),)
insert('data', 'tournament_data',
"tournament_name, winner_id, winner, total_teams",
f"'{tour_name}', {data[0]}, '{data[1]}', {data[2]}")
def tournament_flow():
''' calls all the function required for the proper execution of this code in order '''
user_inputs() #Gets all data from user (Tournament name, total no of teams, total no. of members in each team)
initialize_db() #creates all neccessary db / tables in the beginning
teams() #Func call to get team and members data
global winner
winner = rounds([x for x in range(1, n_teams + 1)])
winner = winner_data(winner, tour_name)
save_winner()
return winner[1], tour_name