def getTeam(self, players, form, points): teams = get_teams_by_players(list(p.id for p in players)) if not teams: name = self.makeTeamName(form.cleaned_data['name'], players) # determining team name team = form.save(commit=False) # making team object team.name = name team.points = points team.games = 1 try: # actual saving of new team team.save() form.save_m2m() # do not forget m2m (players) except: team.delete() # if any errors appear - make sure no garbage is left. return '' else: # simple update team = teams[0] team.points = team.points+points team.games = team.games+1 team.save() return team
def post(self, request, *args, **kwargs): error = '' msg = '' form = self.MakerForm(request.POST) if form.is_valid(): players = form.cleaned_data['players'] if (players <= 4 and players > 1) or players == 6: # checking number of players to match possible combinations # there can be 3 types of teams: one player, two players and three players onePlayerTeams = {} twoPlayerTeams = {} threePlayerTeams = {} allPlayers = list(Player.objects.all()) if len(allPlayers) < players: # there might be not enough players in the database for the desired matchup error = "Not enough players (need " + str(players) + ", have " + str(len(allPlayers)) + ")" else: teams = [] # init - building dicts of player points for player in allPlayers: # for one player teams it is easy - simply put all the players into the dict. team = get_teams_by_players([player]) if team: onePlayerTeams[player.name] = team[0].points else: onePlayerTeams[player.name] = 0 if players > 2: # for two player teams all possible combinations need to be investigated. teamCombos = list(itertools.combinations(allPlayers, 2)) for player in teamCombos: team = get_teams_by_players([player[0],player[1]]) teamName = player[0].name + '+' + player[1].name; if team: twoPlayerTeams[teamName] = team[0].points else: twoPlayerTeams[teamName] = onePlayerTeams[player[0].name] + onePlayerTeams[player[1].name] if players == 6: # for three player teams all combinations of three players are proposed, having in mind the previously generated 2 player teams. teamCombos = list(itertools.combinations(allPlayers, 3)) for player in teamCombos: team = get_teams_by_players([player[0],player[1],player[2]]) teamName = player[0].name + '+' + player[1].name + '+' + player[2].name; if team: threePlayerTeams[teamName] = team[0].points else: points = twoPlayerTeams[player[0].name + '+' + player[1].name] leftOver = 2 if twoPlayerTeams[player[1].name + '+' + player[2].name] > points: points = twoPlayerTeams[player[1].name + '+' + player[2].name] leftOver = 0 if twoPlayerTeams[player[0].name + '+' + player[2].name] > points: points = twoPlayerTeams[player[0].name + '+' + player[2].name] leftOver = 1 points += onePlayerTeams[player[leftOver].name] threePlayerTeams[teamName] = points if players == 2: points = onePlayerTeams if players == 4: points = twoPlayerTeams if players == 6: points = threePlayerTeams if players == 3: # when there are 3 players, match is 1 vs. 2. So in the points dict there must be both one player and two player teams. points = onePlayerTeams points.update(twoPlayerTeams) combos = list(itertools.combinations(points.keys(), 2)) # making actual matchups by combining all the possible pairs from points match = self.makeMatch(combos, points, players) # here the magic happens. player1 = combos[match][0] # result team 1 player2 = combos[match][1] # result team 2 msg = 'Best Match: ' + player1 + '(' + str(points[player1]) + ') vs. ' + player2 + '(' + str(points[player2]) + ')' else: error = "Valid number of players: 2, 3, 4, 6" else: error = "Wrong form data" context = {} context['msg'] = msg context['error'] = error context['form'] = self.MakerForm() context['title'] = "MatchMaker" return self.render_to_response(context)