-
Notifications
You must be signed in to change notification settings - Fork 4
/
bracketfuncs.py
74 lines (63 loc) · 1.9 KB
/
bracketfuncs.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
# Bracket Functions, utilities for manipulating brackets.
#
#
from collections import OrderedDict
def projected(brackets):
'''
updates a bracket to reflect winnings by seeds.
will mutate the bracket, so to keep original create a
copy before calling
'''
for bracket in brackets:
br = bracket
while br != []:
nbr = []
for match in br:
better = match.part2
if match.part1.seed < match.part2.seed:
better = match.part1
match.setwinner(better)
c0 = (match.wlink)
c1 = match.wlink not in nbr
if c0 and (not match.wlink.isspecial()) and c1:
nbr.append(match.wlink)
br = nbr
def placing(brackets):
'''
given a bracket, returns a dict mapping Participant -> placing
'''
place = OrderedDict()
placing = len(brackets[0])*2 + 1
last = brackets[-1]
rd = last
nrd = []
while(rd != []):
placing = placing - len(rd)
for m in rd:
l = m.loser()
if(l is not None):
place[l] = placing
place.move_to_end(l, False)
if m.wlink is None:
place[m.winner_()] = 1
place.move_to_end(m.winner_(), False)
if m.wlink not in nrd and m.wlink is not None:
nrd.append(m.wlink)
rd = nrd
nrd = []
return place
def getmatchinrd(bracket, rd, nummatches = -1):
'''
given a round, returns the matches in it
'''
returner = []
retset = set()
for m in bracket:
mtch = m.itwlink(rd-1)
if mtch is not None and mtch.uniqueid not in retset:
retset.add(mtch.uniqueid)
returner.append(mtch)
if nummatches > 0:
return returner[:nummatches]
else:
return returner