Example #1
0
def start_continue_tournament(request):
    if request.method == 'POST':
        tournament_id = request.POST.get('tournament_id')
        current_tournament = Tournament.objects.get(id=tournament_id)
        participants_of_tournament = current_tournament.participants.all().order_by('-elo_rating')
        games, num_of_tours = get_games_and_tours(tournament_id)
        callback_data = {}
        if games:
            for game in Game.objects.filter(tournament=current_tournament):
                if not game.winner and game.opponent_black and game.opponent_white:
                    callback_data['error_text'] = "You haven\'t completed previous tours."
                    return HttpResponse(json.dumps(callback_data), content_type='application/json', status=500)

            participants_by_points = get_participants_tournament_points(participants_of_tournament, games)
            pairs = [tuple(participants_by_points[i:i+2]) for i in range(0, len(participants_by_points), 2)]

            for num_of_game_in_tour, pair in enumerate(pairs):
                try:
                    new_game = Game.objects.create(tournament=Tournament.objects.get(id=tournament_id),
                                                   opponent_white=pair[0][0],
                                                   opponent_black=pair[1][0],
                                                   number_of_tour=num_of_tours+1,
                                                   number_of_game_in_tour=num_of_game_in_tour+1)
                except IndexError:
                    new_game = Game.objects.create(tournament=Tournament.objects.get(id=tournament_id),
                                                   opponent_white=pair[0][0],
                                                   opponent_black=None,
                                                   number_of_tour=num_of_tours+1,
                                                   number_of_game_in_tour=num_of_game_in_tour+1)
                new_game.save()

            return HttpResponse('')
        else:
            list_ids = list(participant.id for participant in participants_of_tournament)
            pairs = [tuple(list_ids[i:i+2]) for i in range(0, len(list_ids), 2)]
            for num_of_game_in_tour, pair in enumerate(pairs):
                try:
                    new_game = Game.objects.create(tournament=Tournament.objects.get(id=tournament_id),
                                                   opponent_white=Participant.objects.get(id=pair[0]),
                                                   opponent_black=Participant.objects.get(id=pair[1]),
                                                   number_of_tour=1,
                                                   number_of_game_in_tour=num_of_game_in_tour+1)
                except IndexError:
                    new_game = Game.objects.create(tournament=Tournament.objects.get(id=tournament_id),
                                                   opponent_white=Participant.objects.get(id=pair[0]),
                                                   opponent_black=None,
                                                   number_of_tour=1,
                                                   number_of_game_in_tour=num_of_game_in_tour+1)
                new_game.save()
            return HttpResponse('')
Example #2
0
def TournamentDetailView(request, tournament_id=None):
    GamesFormSet = modelformset_factory(Game, extra=0)
    if request.method == 'POST' and request.user.is_active:
        if tournament_id is None:
            tournament_form = TournamentForm(request.POST)
        else:
            tournament_form = TournamentForm(request.POST, instance=get_object_or_404(Tournament, pk=tournament_id))
        if tournament_form.is_valid():
            callback_data = {}
            try:
                if ('participants' not in tournament_form.changed_data and tournament_form.instance.game_set.all().count() != 0) or tournament_form.instance.game_set.all().count() == 0:
                    tournament_form.save(commit=True)
                    callback_data['result'] = 'Tournament info successfully saved.'
                    callback_data['tournament_id'] = tournament_form.instance.id
                    return HttpResponse(json.dumps(callback_data), content_type='application/json', status=200)
                else:
                    callback_data['result'] = 'Tournament wasn\'t saved. \n'
                    callback_data['error_text'] = 'You cannot change participants list when tournament is started.'
                    return HttpResponse(json.dumps(callback_data), content_type='application/json', status=500)
            except ValidationError as e:
                callback_data['result'] = 'Tournament wasn\'t saved. \n'
                callback_data['error_text'] = e.message
                return HttpResponse(json.dumps(callback_data), content_type='application/json', status=500)
        else:
            return render_to_response('tournament_detail.html', RequestContext(request, {
                'tournament_form': tournament_form,
            }))
    else:
        if tournament_id is None:
            tournament_form = TournamentForm()
            return render_to_response('tournament_detail.html', RequestContext(request, {
                'tournament_form': tournament_form,
            }))

        elif request.user.is_active:
            current_tournament = get_object_or_404(Tournament, pk=tournament_id)
            is_over = current_tournament.is_over
            tournament_form = TournamentForm(instance=current_tournament)
            games, num_of_tours = get_games_and_tours(tournament_id)
            participants = current_tournament.participants.all()
            participants_by_points = get_participants_tournament_points(participants, games)
            list_of_gameformsets = []
            if games:
                for tour in range(1, num_of_tours+1):
                    gameformset = GamesFormSet(queryset=current_tournament.game_set.filter(number_of_tour=tour),
                                               prefix='tour_'+str(tour)+'_games')
                    for gameform in gameformset:
                        gameform.fields['opponent_black'].queryset = current_tournament.participants
                        gameform.fields['opponent_white'].queryset = current_tournament.participants
                        gameform.fields['opponent_black'].widget.choices.field.empty_label = None
                        gameform.fields['opponent_white'].widget.choices.field.empty_label = None
                    list_of_gameformsets.append(gameformset)
            else:
                list_of_gameformsets = None
            return render_to_response('tournament_detail.html', RequestContext(request, {
                'tournament_is_over': is_over,
                'tournament_form': tournament_form,
                'tournament_title': current_tournament.title,
                'tournament_id': tournament_id,
                'list_of_gameformsets': list_of_gameformsets,
                'participants_by_points': participants_by_points,
            }))
        else:
            tournament = get_object_or_404(Tournament, pk=tournament_id)
            is_over = tournament.is_over
            participants = tournament.participants.all()
            games, num_of_tours = get_games_and_tours(tournament_id)
            participants_by_points = get_participants_tournament_points(participants, games)
            tour_numbers = []
            if games:
                for i in range(num_of_tours):
                    tour_numbers.append(i+1)
            return render_to_response('tournament_detail.html', RequestContext(request, {
                'tournament_is_over': is_over,
                'tournament': tournament,
                'participants': participants,
                'games': games,
                'tour_numbers': tour_numbers,
                'participants_by_points': participants_by_points,
            }))