2021-01-03 06:04:51 +00:00
import time , asyncio , jsonpickle , random , math
from games import team , game
2021-01-03 11:56:40 +00:00
from discord import Embed , Color
2021-01-02 06:10:52 +00:00
import database as db
class league ( object ) :
def __init__ ( self , name , subleagues_dic ) :
self . subleagues = { } #key: name, value: [divisions]
self . max_days
self . day = 1
self . name = name
self . subleagues = subleagues_dic
class division ( object ) :
def __init__ ( self ) :
2021-01-03 11:06:51 +00:00
self . teams = { } #key: team object, value: {wins; rd (run diff)}
2021-01-02 06:10:52 +00:00
2021-01-03 06:04:51 +00:00
class tournament ( object ) :
2021-01-04 04:18:45 +00:00
def __init__ ( self , name , team_dic , series_length = 5 , finals_series_length = 7 , max_innings = 9 , id = None , secs_between_games = 300 , secs_between_rounds = 600 ) :
2021-01-03 11:06:51 +00:00
self . name = name
self . teams = team_dic #same format as division, wins/losses will be used for seeding later
self . bracket = None
self . results = None
self . series_length = series_length
2021-01-04 04:18:45 +00:00
self . finals_length = finals_series_length
2021-01-03 11:09:27 +00:00
self . game_length = max_innings
2021-01-03 11:06:51 +00:00
self . active = False
2021-01-04 04:18:45 +00:00
self . delay = secs_between_games
self . round_delay = secs_between_rounds
self . finals = False
self . id = id
if id is None :
self . id = random . randint ( 1111 , 9999 )
else :
self . id = id
2021-01-03 06:04:51 +00:00
2021-01-03 11:06:51 +00:00
def build_bracket ( self , random_sort = False , by_wins = False ) :
teams_list = list ( self . teams . keys ( ) ) . copy ( )
2021-01-03 06:04:51 +00:00
2021-01-03 11:06:51 +00:00
if random_sort :
2021-01-03 06:04:51 +00:00
def sorter ( team_in_list ) :
return random . random ( )
elif by_wins :
def sorter ( team_in_list ) :
2021-01-03 11:56:40 +00:00
return self . teams [ team_in_list ] [ " wins " ] #sorts by wins
2021-01-03 06:04:51 +00:00
2021-01-03 11:06:51 +00:00
else : #sort by average stars
2021-01-03 06:04:51 +00:00
def sorter ( team_in_list ) :
return team_in_list . average_stars ( )
teams_list . sort ( key = sorter , reverse = True )
2021-01-04 04:18:45 +00:00
2021-01-03 06:04:51 +00:00
2021-01-03 11:06:51 +00:00
bracket_layers = int ( math . ceil ( math . log ( len ( teams_list ) , 2 ) ) )
2021-01-03 06:04:51 +00:00
empty_slots = int ( math . pow ( 2 , bracket_layers ) - len ( teams_list ) )
for i in range ( 0 , empty_slots ) :
teams_list . append ( None )
2021-01-03 11:06:51 +00:00
previous_bracket_layer = teams_list . copy ( )
2021-01-03 11:56:40 +00:00
for i in range ( 0 , bracket_layers - 1 ) :
2021-01-03 11:06:51 +00:00
this_layer = [ ]
for pair in range ( 0 , int ( len ( previous_bracket_layer ) / 2 ) ) :
if pair % 2 == 0 : #if even number
this_layer . insert ( 0 + int ( pair / 2 ) , [ previous_bracket_layer . pop ( 0 ) , previous_bracket_layer . pop ( - 1 ) ] ) #every other pair goes at front of list, moving forward
else :
2021-01-04 04:18:45 +00:00
this_layer . insert ( 0 - int ( ( 1 + pair ) / 2 ) , [ previous_bracket_layer . pop ( int ( len ( previous_bracket_layer ) / 2 ) - 1 ) , previous_bracket_layer . pop ( int ( len ( previous_bracket_layer ) / 2 ) ) ] ) #every other pair goes at end of list, moving backward
2021-01-03 11:06:51 +00:00
previous_bracket_layer = this_layer
self . bracket = bracket ( previous_bracket_layer , bracket_layers )
2021-01-04 04:18:45 +00:00
def round_check ( self ) :
if self . bracket . depth == 1 :
self . finals = True
return True
else :
return False
2021-01-03 11:06:51 +00:00
class bracket ( object ) :
2021-01-04 04:18:45 +00:00
this_bracket = [ ]
2021-01-03 11:06:51 +00:00
def __init__ ( self , bracket_list , depth ) :
self . this_bracket = bracket_list
self . depth = depth
self . bottom_row = [ ]
def get_bottom_row ( self ) :
2021-01-04 04:18:45 +00:00
self . depth = 1
2021-01-03 11:06:51 +00:00
self . bottom_row = [ ]
self . dive ( self . this_bracket )
return self . bottom_row
def dive ( self , branch ) :
if not isinstance ( branch [ 0 ] , list ) : #if it's a pair of games
self . bottom_row . append ( branch )
else :
2021-01-04 04:18:45 +00:00
self . depth + = 1
return self . dive ( branch [ 0 ] ) , self . dive ( branch [ 1 ] )
def set_winners_dive ( self , winners_list , index = 0 , branch = None , parent = None ) :
if branch is None :
branch = self . this_bracket . copy ( )
if not isinstance ( branch [ 0 ] , list ) : #if it's a pair of games
if branch [ 0 ] . name in winners_list or branch [ 1 ] is None :
winner = branch [ 0 ]
if parent is not None :
parent [ index ] = winner
elif branch [ 1 ] . name in winners_list :
winner = branch [ 1 ]
if parent is not None :
parent [ index ] = winner
else :
self . set_winners_dive ( winners_list , index = 0 , branch = branch [ 0 ] , parent = branch )
self . set_winners_dive ( winners_list , index = 1 , branch = branch [ 1 ] , parent = branch )
if parent is None :
self . this_bracket = branch
return branch