def view_round(request, round_number, errors = None): if errors is None: errors = [] valid_pairing, byes = True, [] print "1: ",time.time() round_pairing = list(Round.objects.filter(round_number = round_number)) random.seed(1337) random.shuffle(round_pairing) round_pairing.sort(key = lambda x: tab_logic.team_comp(x, round_number), reverse = True) print "2: ",time.time() #For the template since we can't pass in something nicer like a hash round_info = [[pair]+[None]*8 for pair in round_pairing] for pair in round_info: pair[1] = tab_logic.tot_wins(pair[0].gov_team) pair[2] = tab_logic.tot_speaks(pair[0].gov_team) pair[3] = tab_logic.num_govs(pair[0].gov_team) pair[4] = tab_logic.num_opps(pair[0].gov_team) pair[5] = tab_logic.tot_wins(pair[0].opp_team) pair[6] = tab_logic.tot_speaks(pair[0].opp_team) pair[7] = tab_logic.num_govs(pair[0].opp_team) pair[8] = tab_logic.num_opps(pair[0].opp_team) print "3: ",time.time() paired_teams = [team.gov_team for team in round_pairing] + [team.opp_team for team in round_pairing] n_over_two = Team.objects.filter(checked_in=True).count() / 2 valid_pairing = len(round_pairing) >= n_over_two for present_team in Team.objects.filter(checked_in=True): if not (present_team in paired_teams): errors.append("%s was not in the pairing" % (present_team)) byes.append(present_team) pairing_exists = len(round_pairing) > 0 excluded_judges = Judge.objects.exclude(judges__round_number = round_number).filter(checkin__round_number = round_number) non_checkins = Judge.objects.exclude(judges__round_number = round_number).exclude(checkin__round_number = round_number) size = max(map(len, [excluded_judges, non_checkins, byes])) # The minimum rank you want to warn on warning = 5 judge_slots = [1,2,3] print "4: ",time.time() # A seemingly complex one liner to do a fairly simple thing # basically this generates the table that the HTML will display such that the output looks like: # [ Byes ][Judges not in round but checked in][Judges not in round but not checked in] # [ Team1][ CJudge1 ][ Judge1 ] # [ Team2][ CJudge2 ][ Judge2 ] # [ ][ CJudge3 ][ Judge3 ] # [ ][ ][ Judge4 ] excluded_people = zip(*map( lambda x: x+[""]*(size-len(x)), [list(byes), list(excluded_judges), list(non_checkins)])) return render_to_response('display_info.html', locals(), context_instance=RequestContext(request))
def determine_gov_opp(all_pairs): final_pairings = [] for p in all_pairs: if tab_logic.num_govs(p[0]) < tab_logic.num_govs(p[1]): #p[0] should be gov final_pairings +=[[p[0],p[1]]] elif tab_logic.num_govs(p[1]) < tab_logic.num_govs(p[0]): #p[1] should be gov final_pairings +=[[p[1],p[0]]] elif tab_logic.num_opps(p[0]) < tab_logic.num_opps(p[1]): #p[1] should be gov final_pairings +=[[p[1],p[0]]] elif tab_logic.num_opps(p[1]) < tab_logic.num_opps(p[0]): #p[0] should be gov final_pairings +=[[p[0],p[1]]] elif random.randint(0,1) == 0: final_pairings +=[[p[0],p[1]]] else: final_pairings +=[[p[1],p[0]]] return final_pairings
def view_team(request, team_id): team_id = int(team_id) try: team = Team.objects.get(pk=team_id) stats = [] stats.append(("Wins", tab_logic.tot_wins(team))) stats.append(("Total Speaks", tab_logic.tot_speaks(team))) stats.append(("Govs", tab_logic.num_govs(team))) stats.append(("Opps", tab_logic.num_opps(team))) stats.append(("Opp Wins", tab_logic.opp_strength(team))) stats.append(("Been Pullup", tab_logic.pull_up_count(team))) stats.append(("Hit Pullup", tab_logic.hit_pull_up_count(team))) except Team.DoesNotExist: return render_to_response('error.html', { 'error_type': "View Team", 'error_name': str(team_id), 'error_info': "No such Team" }, context_instance=RequestContext(request)) if request.method == 'POST': form = TeamForm(request.POST, instance=team) if form.is_valid(): try: form.save() except ValueError: return render_to_response('error.html', { 'error_type': "Team", 'error_name': "[" + form.cleaned_data['name'] + "]", 'error_info': "Team name cannot be validated, most likely a non-existent team" }, context_instance=RequestContext( request)) return render_to_response( 'thanks.html', { 'data_type': "Team", 'data_name': "[" + form.cleaned_data['name'] + "]" }, context_instance=RequestContext(request)) else: form = TeamForm(instance=team) links = [('/team/' + str(team_id) + '/scratches/view/', 'Scratches for ' + str(team.name), False), ('/team/' + str(team_id) + '/delete/', 'Delete', True)] for deb in team.debaters.all(): links.append( ('/debater/' + str(deb.id) + '/', "View %s" % deb.name, False)) return render_to_response('data_entry.html', { 'title': "Viewing Team: %s" % (team.name), 'form': form, 'links': links, 'team_obj': team, 'team_stats': stats }, context_instance=RequestContext(request)) return render_to_response('data_entry.html', {'form': form}, context_instance=RequestContext(request))
def determine_gov_opp(all_pairs): final_pairings = [] for p in all_pairs: if tab_logic.num_govs(p[0]) < tab_logic.num_govs( p[1]): #p[0] should be gov final_pairings += [[p[0], p[1]]] elif tab_logic.num_govs(p[1]) < tab_logic.num_govs( p[0]): #p[1] should be gov final_pairings += [[p[1], p[0]]] elif tab_logic.num_opps(p[0]) < tab_logic.num_opps( p[1]): #p[1] should be gov final_pairings += [[p[1], p[0]]] elif tab_logic.num_opps(p[1]) < tab_logic.num_opps( p[0]): #p[0] should be gov final_pairings += [[p[0], p[1]]] elif random.randint(0, 1) == 0: final_pairings += [[p[0], p[1]]] else: final_pairings += [[p[1], p[0]]] return final_pairings
def view_team(request, team_id): team_id = int(team_id) try: team = Team.objects.get(pk=team_id) stats = [] stats.append(("Wins", tab_logic.tot_wins(team))) stats.append(("Total Speaks", tab_logic.tot_speaks(team))) stats.append(("Govs", tab_logic.num_govs(team))) stats.append(("Opps", tab_logic.num_opps(team))) stats.append(("Opp Wins", tab_logic.opp_strength(team))) stats.append(("Been Pullup", tab_logic.pull_up_count(team))) stats.append(("Hit Pullup", tab_logic.hit_pull_up_count(team))) except Team.DoesNotExist: return render_to_response('error.html', {'error_type': "View Team", 'error_name': str(team_id), 'error_info':"No such Team"}, context_instance=RequestContext(request)) if request.method == 'POST': form = TeamForm(request.POST,instance=team) if form.is_valid(): try: form.save() except ValueError: return render_to_response('error.html', {'error_type': "Team", 'error_name': "["+form.cleaned_data['name']+"]", 'error_info':"Team name cannot be validated, most likely a non-existent team"}, context_instance=RequestContext(request)) return render_to_response('thanks.html', {'data_type': "Team", 'data_name': "["+form.cleaned_data['name']+"]"}, context_instance=RequestContext(request)) else: form = TeamForm(instance=team) links = [('/team/'+str(team_id)+'/scratches/view/','Scratches for '+str(team.name), False), ('/team/'+str(team_id)+'/delete/', 'Delete', True)] for deb in team.debaters.all(): links.append(('/debater/'+str(deb.id)+'/', "View %s" % deb.name, False)) return render_to_response('data_entry.html', {'title':"Viewing Team: %s"%(team.name), 'form': form, 'links': links, 'team_obj':team, 'team_stats':stats}, context_instance=RequestContext(request)) return render_to_response('data_entry.html', {'form': form}, context_instance=RequestContext(request))
def team_stats(request, team_id): team_id = int(team_id) try: team = Team.objects.get(pk=team_id) stats = {} stats["seed"] = Team.get_seed_display(team).split(" ")[0] stats["wins"] = tab_logic.tot_wins(team) stats["total_speaks"] = tab_logic.tot_speaks(team) stats["govs"] = tab_logic.num_govs(team) stats["opps"] = tab_logic.num_opps(team) data = {'success': True, 'result':stats} except Team.DoesNotExist: data = {'success': False} data = simplejson.dumps(data) return HttpResponse(data, mimetype='application/json')
def team_stats(request, team_id): team_id = int(team_id) try: team = Team.objects.get(pk=team_id) stats = {} stats["seed"] = Team.get_seed_display(team).split(" ")[0] stats["wins"] = tab_logic.tot_wins(team) stats["total_speaks"] = tab_logic.tot_speaks(team) stats["govs"] = tab_logic.num_govs(team) stats["opps"] = tab_logic.num_opps(team) data = {'success': True, 'result': stats} except Team.DoesNotExist: data = {'success': False} data = simplejson.dumps(data) return HttpResponse(data, mimetype='application/json')
def calc_weight(team_a, team_b, team_a_ind, team_b_ind, team_a_opt, team_b_opt, team_a_opt_ind, team_b_opt_ind): """ Calculate the penalty for a given pairing Args: team_a - the first team in the pairing team_b - the second team in the pairing team_a_ind - the position in the pairing of team_a team_b_ind - the position in the pairing of team_b team_a_opt - the optimal power paired team for team_a to be paired with team_b_opt - the optimal power paired team for team_b to be paired with team_a_opt_ind - the position in the pairing of team_a_opt team_b_opt_ind - the position in the pairing of team_b_opt """ # Get configuration values all_settings = dict([(ts.key, ts.value) for ts in TabSettings.objects.all()]) def try_get(key, default=None): try: return int(all_settings[key]) except: return default current_round = try_get("cur_round", 1) tot_rounds = try_get("tot_rounds", 5) power_pairing_multiple = try_get("power_pairing_multiple", -1) high_opp_penalty = try_get("high_opp_penalty", 0) high_gov_penalty = try_get("high_gov_penalty", -100) high_high_opp_penalty = try_get("higher_opp_penalty", -10) same_school_penalty = try_get("same_school_penalty", -1000) hit_pull_up_before = try_get("hit_pull_up_before", -10000) hit_team_before = try_get("hit_team_before", -100000) # Penalize for being far away from ideal power pairings if current_round == 1: wt = power_pairing_multiple * ( abs(team_a_opt.seed - team_b.seed) + abs(team_b_opt.seed - team_a.seed)) / 2.0 else: wt = power_pairing_multiple * (abs(team_a_opt_ind - team_b_ind) + abs(team_b_opt_ind - team_a_ind)) / 2.0 half = int(tot_rounds / 2) + 1 # Penalize for both teams having n/2 + 1 opps, meaning we'll have to give # a fifth opp to one of the teams if tab_logic.num_opps(team_a) >= half and tab_logic.num_opps( team_b) >= half: wt += high_opp_penalty # Penalize for both teams having n/2+2 opps, meaning we'll have to give # a fourth opp to one of the teams if tab_logic.num_opps(team_a) >= half + 1 and tab_logic.num_opps( team_b) >= half + 1: wt += high_high_opp_penalty # Penalize for both teams having n/2 + 1 govs, meaning we'll have to give # a fourth gov to one of the teams if tab_logic.num_govs(team_a) >= half and tab_logic.num_govs( team_b) >= half: wt += high_gov_penalty # Penalize for teams being from the same school if team_a.school == team_b.school: wt += same_school_penalty # Penalize for team hitting pull-up more than once if (tab_logic.hit_pull_up(team_a) and tab_logic.tot_wins(team_b) < tab_logic.tot_wins(team_a)) or ( tab_logic.hit_pull_up(team_b) and tab_logic.tot_wins(team_a) < tab_logic.tot_wins(team_b)): wt += hit_pull_up_before # Penalize for teams hitting each other before if tab_logic.hit_before(team_a, team_b): wt += hit_team_before return wt
def calc_weight(team_a, team_b, team_a_ind, team_b_ind, team_a_opt, team_b_opt, team_a_opt_ind, team_b_opt_ind): """ Calculate the penalty for a given pairing Args: team_a - the first team in the pairing team_b - the second team in the pairing team_a_ind - the position in the pairing of team_a team_b_ind - the position in the pairing of team_b team_a_opt - the optimal power paired team for team_a to be paired with team_b_opt - the optimal power paired team for team_b to be paired with team_a_opt_ind - the position in the pairing of team_a_opt team_b_opt_ind - the position in the pairing of team_b_opt """ # Get configuration values all_settings = dict([(ts.key, ts.value) for ts in TabSettings.objects.all()]) def try_get(key, default= None): try: return int(all_settings[key]) except: return default current_round = try_get("cur_round", 1) tot_rounds = try_get("tot_rounds", 5) power_pairing_multiple = try_get("power_pairing_multiple", -1) high_opp_penalty = try_get("high_opp_penalty", 0) high_gov_penalty = try_get("high_gov_penalty", -100) high_high_opp_penalty = try_get("higher_opp_penalty", -10) same_school_penalty = try_get("same_school_penalty", -1000) hit_pull_up_before = try_get("hit_pull_up_before", -10000) hit_team_before = try_get("hit_team_before", -100000) # Penalize for being far away from ideal power pairings if current_round == 1: wt = power_pairing_multiple * (abs(team_a_opt.seed - team_b.seed) + abs(team_b_opt.seed - team_a.seed))/2.0 else: wt = power_pairing_multiple * (abs(team_a_opt_ind - team_b_ind) + abs(team_b_opt_ind - team_a_ind))/2.0 half = int(tot_rounds / 2) + 1 # Penalize for both teams having n/2 + 1 opps, meaning we'll have to give # a fifth opp to one of the teams if tab_logic.num_opps(team_a) >= half and tab_logic.num_opps(team_b) >= half: wt += high_opp_penalty # Penalize for both teams having n/2+2 opps, meaning we'll have to give # a fourth opp to one of the teams if tab_logic.num_opps(team_a) >= half+1 and tab_logic.num_opps(team_b) >= half+1: wt += high_high_opp_penalty # Penalize for both teams having n/2 + 1 govs, meaning we'll have to give # a fourth gov to one of the teams if tab_logic.num_govs(team_a) >= half and tab_logic.num_govs(team_b) >= half: wt += high_gov_penalty # Penalize for teams being from the same school if team_a.school == team_b.school: wt += same_school_penalty # Penalize for team hitting pull-up more than once if (tab_logic.hit_pull_up(team_a) and tab_logic.tot_wins(team_b) < tab_logic.tot_wins(team_a)) or (tab_logic.hit_pull_up(team_b) and tab_logic.tot_wins(team_a) < tab_logic.tot_wins(team_b)): wt += hit_pull_up_before # Penalize for teams hitting each other before if tab_logic.hit_before(team_a, team_b): wt += hit_team_before return wt
def view_round(request, round_number, errors=None): if errors is None: errors = [] valid_pairing, byes = True, [] print "1: ", time.time() round_pairing = list(Round.objects.filter(round_number=round_number)) random.seed(1337) random.shuffle(round_pairing) round_pairing.sort(key=lambda x: tab_logic.team_comp(x, round_number), reverse=True) print "2: ", time.time() #For the template since we can't pass in something nicer like a hash round_info = [[pair] + [None] * 8 for pair in round_pairing] for pair in round_info: pair[1] = tab_logic.tot_wins(pair[0].gov_team) pair[2] = tab_logic.tot_speaks(pair[0].gov_team) pair[3] = tab_logic.num_govs(pair[0].gov_team) pair[4] = tab_logic.num_opps(pair[0].gov_team) pair[5] = tab_logic.tot_wins(pair[0].opp_team) pair[6] = tab_logic.tot_speaks(pair[0].opp_team) pair[7] = tab_logic.num_govs(pair[0].opp_team) pair[8] = tab_logic.num_opps(pair[0].opp_team) print "3: ", time.time() paired_teams = [team.gov_team for team in round_pairing ] + [team.opp_team for team in round_pairing] n_over_two = Team.objects.filter(checked_in=True).count() / 2 valid_pairing = len(round_pairing) >= n_over_two for present_team in Team.objects.filter(checked_in=True): if not (present_team in paired_teams): errors.append("%s was not in the pairing" % (present_team)) byes.append(present_team) pairing_exists = len(round_pairing) > 0 excluded_judges = Judge.objects.exclude( judges__round_number=round_number).filter( checkin__round_number=round_number) non_checkins = Judge.objects.exclude( judges__round_number=round_number).exclude( checkin__round_number=round_number) size = max(map(len, [excluded_judges, non_checkins, byes])) # The minimum rank you want to warn on warning = 5 judge_slots = [1, 2, 3] print "4: ", time.time() # A seemingly complex one liner to do a fairly simple thing # basically this generates the table that the HTML will display such that the output looks like: # [ Byes ][Judges not in round but checked in][Judges not in round but not checked in] # [ Team1][ CJudge1 ][ Judge1 ] # [ Team2][ CJudge2 ][ Judge2 ] # [ ][ CJudge3 ][ Judge3 ] # [ ][ ][ Judge4 ] excluded_people = zip(*map( lambda x: x + [""] * (size - len(x)), [list(byes), list(excluded_judges), list(non_checkins)])) return render_to_response('display_info.html', locals(), context_instance=RequestContext(request))