def handle(self, *args, **options): cur_round = TabSettings.get("cur_round") - 1 print(("Simulating round %s..." % cur_round)) rounds_to_simulate = Round.objects.filter(round_number=cur_round, victor=Round.NONE) for round_obj in rounds_to_simulate: self.__simulate_round(round_obj)
def handle(self, *args, **options): cur_round = TabSettings.get("cur_round") - 1 print(("Simulating round %s..." % cur_round)) rounds_to_simulate = Round.objects.filter(round_number=cur_round, victor=Round.NONE) for r in rounds_to_simulate: print(r) self.__simulate_round(r)
def count_valid_teams(self): num_teams, num_valid_teams = 0, 0 num_rounds = TabSettings.get('tot_rounds') for team in Team.objects.all(): num_teams += 1 num_forfeits = NoShow.objects.filter(no_show_team=team).count() if num_rounds - num_forfeits >= 3: num_valid_teams += 1 return num_valid_teams, num_teams
def ranks_for_debater(debater, average_ironmen=True): """Returns a list of ranks for the provided debater In most normal rounds the ranks of the debater are the ranks the judge gave them, but there are some special circumstances. Forfeits: If a debater won by forfeit, they get their average ranks If a debater lost by forfeit, they get ranks of 0 If a debater was Noshow for a round, they get ranks of 0 Iron Mans: If a debater is an iron man for a round, they get the average of their ranks for that round Byes: If a debater wins in a bye, they get their average ranks If a debater was late to a lenient round, they get average ranks """ team = debater.team() # We start counting at 1, so when cur_round says 6 that means that we are # in round 5 and should have 5 ranks num_ranks = TabSettings.get("cur_round") - 1 debater_roundstats = debater.roundstats_set.all() debater_ranks = [] ranks_per_round = defaultdict(list) # We might have multiple roundstats per round if we have iron men, so first # group by round number for roundstat in debater_roundstats: ranks_per_round[roundstat.round.round_number].append(roundstat) for round_number in range(1, num_ranks + 1): roundstats = ranks_per_round[round_number] if roundstats: ranks = [float(rs.ranks) for rs in roundstats] avg_ranks = sum(ranks) / float(len(roundstats)) roundstat = roundstats[0] if won_by_forfeit(roundstat.round, team): debater_ranks.append(avg_deb_ranks(debater)) elif forfeited_round(roundstat.round, team): debater_ranks.append(MAXIMUM_DEBATER_RANKS) else: if average_ironmen: debater_ranks.append(avg_ranks) else: debater_ranks.extend(ranks) else: ranks = debater_abnormal_round_ranks(debater, round_number) if ranks is not None: debater_ranks.append(ranks) debater_ranks = list(map(float, debater_ranks)) return debater_ranks
def backup_round(dst_filename=None, round_number=None, btime=None): if round_number is None: round_number = TabSettings.get("cur_round", "no-round-number") if btime is None: btime = int(time.time()) print("Trying to backup to backups directory") if dst_filename is None: dst_filename = "site_round_%i_%i" % (round_number, btime) dst_filename = _generate_unique_key(dst_filename) return LocalDump(dst_filename).backup()
def backup_round(dst_filename=None, round_number=None, btime=None): if round_number is None: round_number = TabSettings.get("cur_round") if btime is None: btime = int(time.time()) print("Trying to backup to backups directory") if dst_filename == None: dst_filename = "site_round_%i_%i" % (round_number, btime) if backup_exists(dst_filename): dst_filename += "_%i" % btime return copy_db(DATABASE_PATH, get_backup_filename(dst_filename))
def backup_round(dst_filename = None, round_number = None, btime = None): if round_number is None: round_number = TabSettings.get("cur_round") if btime is None: btime = int(time.time()) print("Trying to backup to backups directory") if dst_filename == None: dst_filename = "site_round_%i_%i" % (round_number, btime) if backup_exists(dst_filename): dst_filename += "_%i" % btime return copy_db(DATABASE_PATH, get_backup_filename(dst_filename))
def handle(self, *args, **options): cur_round = TabSettings.get("cur_round") - 1 host = options["host"] csrf_threads = [] rounds = Round.objects.filter(round_number=cur_round, victor=Round.NONE) for round_obj in rounds: judge = round_obj.chair csrf_threads.append( GetCsrfThread(host, judge.ballot_code, round_obj)) num_errors = 0 while csrf_threads: cur_csrf_threads = [] for _ in range(min(len(csrf_threads), options["connections"])): cur_csrf_threads.append(csrf_threads.pop()) for thr in cur_csrf_threads: thr.start() for thr in cur_csrf_threads: thr.join() result_threads = [] for thr in cur_csrf_threads: num_errors += num_errors csrf_token, num_errors = thr.result if csrf_token is None: print("no csrf token") result_thread = SubmitResultThread(thr.host, thr.ballot_code, csrf_token, thr.round_obj) result_threads.append(result_thread) for thr in result_threads: thr.start() for thr in result_threads: thr.join() for thr in result_threads: num_errors += thr.num_errors print("Done with one batch! Sleeping!") time.sleep(2) print("Done!") print("Total errors: %s" % num_errors)
def avg_deb_ranks(debater): """ Computes the average debater ranks for the supplied debater Generally this consistes of finding all the ranks we have, averaging iron men ranks, and then dividing by the length. This does not count forfeit losses or noshow's as 7 because having ranks of 3.5 in the first place is penalty enough, and some tab polices may want forfeits to count as average ranks. """ real_ranks = [] num_ranks = TabSettings.get("cur_round") - 1 debater_roundstats = debater.roundstats_set.all() team = debater.team() ranks_per_round = defaultdict(list) # We might have multiple roundstats per round if we have iron men, so first # group by round number for roundstat in debater_roundstats: ranks_per_round[roundstat.round.round_number].append(roundstat) for round_number in range(1, num_ranks + 1): roundstats = ranks_per_round[round_number] if roundstats: ranks = [float(rs.ranks) for rs in roundstats] avg_ranks = sum(ranks) / float(len(roundstats)) roundstat = roundstats[0] if (won_by_forfeit(roundstat.round, team) or forfeited_round(roundstat.round, team)): continue real_ranks.append(avg_ranks) if not real_ranks: return 0 else: return float(sum(real_ranks)) / float(len(real_ranks))
def handle(self, *args, **options): backup_dir = options['backup_directory'] path = BACKUP_PREFIX self.stdout.write( "Creating directory for current tournament in backup directory") tournament_dir = os.path.join(backup_dir, str(int(time.time()))) if not os.path.exists(tournament_dir): os.makedirs(tournament_dir) if not os.path.exists(path + "/backups"): os.makedirs(path + "/backups") self.stdout.write( "Copying current tournament state to backup tournament directory: %s" % tournament_dir) try: shutil.copy(path + "/pairing_db.sqlite3", tournament_dir) shutil.rmtree(tournament_dir + "/backups", ignore_errors=True) shutil.copytree(path + "/backups", tournament_dir + "/backups") except (IOError, os.error) as why: self.stdout.write("Failed to backup current tournament state") print(why) sys.exit(1) self.stdout.write("Clearing data from database") try: call_command("flush", interactive=False) except (IOError, os.error) as why: self.stdout.write("Failed to clear database") print(why) sys.exit(1) self.stdout.write("Creating tab/entry users") tab = User.objects.create_user("tab", None, options["tab_password"]) tab.is_staff = True tab.is_admin = True tab.is_superuser = True tab.save() entry = User.objects.create_user("entry", None, options["entry_password"]) self.stdout.write("Setting default tab settings") TabSettings.set("tot_rounds", 5) TabSettings.set("lenient_late", 0) TabSettings.set("cur_round", 1) self.stdout.write("Cleaning up old backups") try: shutil.rmtree(path + "/backups") os.makedirs(path + "/backups") except (IOError, os.error) as why: self.stdout.write( "Failed to copy clean database to pairing_db.sqlite3") print(why) sys.exit(1) self.stdout.write( "Done setting up tournament, after backing up old one. New tournament information:" ) self.stdout.write( "%s | %s" % ("Username".ljust(10, " "), "Password".ljust(10, " "))) self.stdout.write( "%s | %s" % ("tab".ljust(10, " "), options['tab_password'].ljust(10, " "))) self.stdout.write( "%s | %s" % ("entry".ljust(10, " "), options['entry_password'].ljust(10, " ")))
def handle(self, *args, **options): if len(args) != 1: self.print_help('./manage.py', 'initialize_tourney') raise CommandError('Please supply valid arguments') backup_dir = args[0] path = BACKUP_PREFIX for user in ['tab', 'entry']: option_name = '%s_password' % user if options[option_name] is None or options[option_name].strip( ) == '': self.stdout.write( "No password provided for %s, generating password" % user) options[option_name] = User.objects.make_random_password( length=8) self.stdout.write("Proceeding to tournament creation") self.stdout.write( "Creating directory for current tournament in backup directory") tournament_dir = os.path.join(backup_dir, str(int(time.time()))) if not os.path.exists(tournament_dir): os.makedirs(tournament_dir) if not os.path.exists(path + "/backups"): os.makedirs(path + "/backups") self.stdout.write( "Copying current tournament state to backup tournament directory: %s" % tournament_dir) try: shutil.copy(path + "/pairing_db.sqlite3", tournament_dir) shutil.rmtree(tournament_dir + "/backups", ignore_errors=True) shutil.copytree(path + "/backups", tournament_dir + "/backups") except (IOError, os.error) as why: self.stdout.write("Failed to backup current tournament state") print(why) sys.exit(1) self.stdout.write("Clearing data from database") try: call_command("flush", interactive=False) except (IOError, os.error) as why: self.stdout.write("Failed to clear database") print(why) sys.exit(1) self.stdout.write("Creating tab/entry users") tab = User.objects.create_user("tab", None, options["tab_password"]) tab.is_staff = True tab.is_admin = True tab.is_superuser = True tab.save() entry = User.objects.create_user("entry", None, options["entry_password"]) self.stdout.write("Setting default tab settings") TabSettings.set("tot_rounds", 5) TabSettings.set("lenient_late", 0) TabSettings.set("cur_round", 1) self.stdout.write("Cleaning up old backups") try: shutil.rmtree(path + "/backups") os.makedirs(path + "/backups") except (IOError, os.error) as why: self.stdout.write( "Failed to copy clean database to pairing_db.sqlite3") print why sys.exit(1) self.stdout.write( "Done setting up tournament, after backing up old one. New tournament information:" ) self.stdout.write( "%s | %s" % ("Username".ljust(10, " "), "Password".ljust(10, " "))) self.stdout.write( "%s | %s" % ("tab".ljust(10, " "), options['tab_password'].ljust(10, " "))) self.stdout.write( "%s | %s" % ("entry".ljust(10, " "), options['entry_password'].ljust(10, " ")))
def speaks_for_debater(debater, average_ironmen=True): """Returns a list of speaks for the provided debater In most normal rounds the speaks of the debater are the speaks the judge gave them, but there are some special circumstances. Forfeits: If a debater won by forfeit, they get their average speaks If a debater lost by forfeit, they get speaks of 0 If a debater was Noshow for a round, they get speaks of 0 Iron Mans: If a debater is an iron man for a round, they get the average of their speaks for that round Byes: If a debater wins in a bye, they get their average speaks If a debater was late to a lenient round, they get average speaks """ team = debater.team() # We start counting at 1, so when cur_round says 6 that means that we are # in round 5 and should have 5 speaks num_speaks = TabSettings.get("cur_round") - 1 debater_roundstats = debater.roundstats_set.all() debater_speaks = [] speaks_per_round = defaultdict(list) # We might have multiple roundstats per round if we have iron men, so first # group by round number for roundstat in debater_roundstats: speaks_per_round[roundstat.round.round_number].append(roundstat) for round_number in range(1, num_speaks + 1): roundstats = speaks_per_round[round_number] if roundstats: # This is so if in the odd chance we get a debater paired in # twice we take the speaks they actually got roundstats.sort(key=lambda rs: rs.speaks, reverse=True) roundstat = roundstats[0] if not len(set(rs.round for rs in roundstats)) == 1: roundstats = roundstats[:1] speaks = [float(rs.speaks) for rs in roundstats] avg_speaks = sum(speaks) / float(len(roundstats)) if won_by_forfeit(roundstat.round, team): debater_speaks.append(avg_deb_speaks(debater)) elif forfeited_round(roundstat.round, team): debater_speaks.append(MINIMUM_DEBATER_SPEAKS) else: if average_ironmen: debater_speaks.append(avg_speaks) else: debater_speaks.extend(speaks) else: speaks = debater_abnormal_round_speaks(debater, round_number) if speaks is not None: debater_speaks.append(speaks) debater_speaks = list(map(float, debater_speaks)) return debater_speaks