def post(): tourney_id = request.GET["tournament"] tourney = Tournament.objects.get(id=tourney_id) versus = request.POST.getlist("versus") teams = [] for team_id in versus: if team_id != "": teams.append(Team.objects.get(id=team_id)) existing_matches = TournamentMatchup.objects.filter(tournament=tourney) match = Matchup() match.team1 = teams[0] match.team2 = teams[1] match.save() tourney_match = TournamentMatchup() tourney_match.tournament = tourney tourney_match.matchup = match tourney_match.round = 0 tourney_match.index = existing_matches.count() tourney_match.save() tourney_teams = [] tourney_teams.append(TournamentTeam.objects.filter(tournament=tourney).filter(team=teams[0]).get()) tourney_teams.append(TournamentTeam.objects.filter(tournament=tourney).filter(team=teams[1]).get()) tourney_teams[0].matchup_index = tourney_match.index * 2 tourney_teams[1].matchup_index = tourney_match.index * 2 + 1 tourney_teams[0].save(); tourney_teams[1].save(); return HttpResponseRedirect("/app/tournament/matchups?tournament=%s" % tourney_id)
def post(): tournament_match_id = request.GET['tournament_match_key'] match = TournamentMatchup.objects.get(id=tournament_match_id) winner_id = int(request.POST['winner']) matchup = match.matchup result = MatchResult() if winner_id == matchup.team1.id: result.winner = matchup.team1 result.loser = matchup.team2 elif winner_id == matchup.team2.id: result.winner = matchup.team2 result.loser = matchup.team1 else: raise Exception("could not determine winner key: %s (%s, %s)" % (winner_id, matchup.team1.id, matchup.team2.id)) update_stats(result.winner, result.loser) result.save() next_round_indices = {0: 0, 1: 0, 2: 1, 3: 1} next_round_index = next_round_indices[match.index] next_round = match.round + 1 if match.round < 2: # look in existing matches for this winner's opponent existing = TournamentMatchup.objects.filter( tournament=match.tournament).filter(round=next_round).filter( index=next_round_index) if existing.count() == 1: next_match = existing[0] next_matchup = next_match.matchup next_matchup.team2 = result.winner next_matchup.save() elif existing.count() == 0: next_match = TournamentMatchup() next_matchup = Matchup() next_matchup.team1 = result.winner next_matchup.save() next_match.tournament = match.tournament next_match.round = next_round next_match.index = next_round_index next_match.matchup = next_matchup next_match.save() else: tourney = match.tournament tourney.completed = True tourney.winner = result.winner tourney.save() match.matchup.delete() match.matchup = None match.result = result match.save() return HttpResponseRedirect("/app/tournament/matchups?tournament=%s" % match.tournament.id)
def post(): tournament_match_id = request.GET['tournament_match_key'] match = TournamentMatchup.objects.get(id=tournament_match_id) winner_id = int(request.POST['winner']) matchup = match.matchup result = MatchResult() if winner_id == matchup.team1.id: result.winner = matchup.team1 result.loser = matchup.team2 elif winner_id == matchup.team2.id: result.winner = matchup.team2 result.loser = matchup.team1 else: raise Exception("could not determine winner key: %s (%s, %s)" % (winner_id, matchup.team1.id, matchup.team2.id)) update_stats(result.winner, result.loser) result.save() next_round_indices = {0:0, 1:0, 2:1, 3:1} next_round_index = next_round_indices[match.index] next_round = match.round + 1 if match.round < 2: # look in existing matches for this winner's opponent existing = TournamentMatchup.objects.filter(tournament=match.tournament).filter(round=next_round).filter(index=next_round_index) if existing.count() == 1: next_match = existing[0] next_matchup = next_match.matchup next_matchup.team2 = result.winner next_matchup.save() elif existing.count() == 0: next_match = TournamentMatchup() next_matchup = Matchup() next_matchup.team1 = result.winner next_matchup.save() next_match.tournament = match.tournament next_match.round = next_round next_match.index = next_round_index next_match.matchup = next_matchup next_match.save() else: tourney = match.tournament tourney.completed = True tourney.winner = result.winner tourney.save() match.matchup.delete() match.matchup = None match.result = result match.save() return HttpResponseRedirect("/app/tournament/matchups?tournament=%s" % match.tournament.id)
def post(): tourney_id = request.GET["tournament"] tourney = Tournament.objects.get(id=tourney_id) versus = request.POST.getlist("versus") teams = [] for team_id in versus: if team_id != "": teams.append(Team.objects.get(id=team_id)) existing_matches = TournamentMatchup.objects.filter(tournament=tourney) match = Matchup() match.team1 = teams[0] match.team2 = teams[1] match.save() tourney_match = TournamentMatchup() tourney_match.tournament = tourney tourney_match.matchup = match tourney_match.round = 0 tourney_match.index = existing_matches.count() tourney_match.save() tourney_teams = [] tourney_teams.append( TournamentTeam.objects.filter(tournament=tourney).filter( team=teams[0]).get()) tourney_teams.append( TournamentTeam.objects.filter(tournament=tourney).filter( team=teams[1]).get()) tourney_teams[0].matchup_index = tourney_match.index * 2 tourney_teams[1].matchup_index = tourney_match.index * 2 + 1 tourney_teams[0].save() tourney_teams[1].save() return HttpResponseRedirect("/app/tournament/matchups?tournament=%s" % tourney_id)