Ejemplo n.º 1
0
def add_participant(game, player, rated):
    u = User.objects.get(username=player)
    pl = Player.objects.get(user=u)
    g = Game.objects.get(custom_slug=game)
    p = Participation(game=g, player=pl, rated=rated)
    p.save()
    return p
Ejemplo n.º 2
0
def join_game(request, game_custom_slug):
    gameid = request.POST.get('gameid')
    game = Game.objects.get(game_id=gameid)

    username = request.POST.get('user')
    user = User.objects.get(username=username)
    player = Player.objects.get(user=user)

    # Check for participation in games with conflicting times to the one the user is trying to join.
    gameConflicts = game_conflicts(player, game)

    game_conflict = True
    player_added = False

    if game:
        if not gameConflicts:
            game_conflict = False
            if game.free_slots != 0:
                game.free_slots -= 1
                game.save()
                p = Participation(player=player, game=game)
                p.save()
                player_added = True

    data = {'player_added': player_added, 'game_conflict': game_conflict}

    return JsonResponse(data)
Ejemplo n.º 3
0
def create_game(request):
    game_form = GameForm()

    # An HTTP POST?
    if request.method == 'POST':
        game_form = GameForm(request.POST)

        # Have we been provided with a valid form?
        if game_form.is_valid():
            # Save, but don't commit
            game = game_form.save(commit=False)

            # Combine date and time for DateTimeField
            date = game_form.cleaned_data["date"]
            time = game_form.cleaned_data["time"]
            game.start = datetime.datetime.combine(date, time).replace(tzinfo=pytz.UTC)
            # Calculate end (start + duration)
            game.end = game.start + datetime.timedelta(hours=+game.duration)

            # Prevent games from being created in the past or within two hours from current time, return with appropriate message.
            game_creation_limit = game.start + datetime.timedelta(hours=-2)
            if (game_creation_limit < datetime.datetime.now(pytz.utc)):
                return render(request, 'fives/create_game.html', {'game_form': game_form, 'conflictMessage': "You can't create a game in the past or within two hours from now."})

            # Check for conflicting games and return with a list of all games that conflict.
            conflictingGames = game_conflicts(request.user.player, game)
            if conflictingGames:
                return render(request, 'fives/create_game.html', {'game_form': game_form, 'conflictingGames': conflictingGames})

            # Get latitiude and longitude from address
            # Source: https://geopy.readthedocs.io/en/1.10.0/
            geolocator = Nominatim(scheme='http')
            try:
                location = geolocator.geocode(game.street + " " + game.city)
                game.latitude = location.latitude
                game.longitude = location.longitude
            except AttributeError:
                return render(request, 'fives/create_game.html', {'game_form': game_form, 'message': "Please provide a valid address."})

            # Get host entry from current user
            game.host = request.user

            # Save the new Game to the database
            game.save()

            # Add the user to the list of the game's participants.
            user_player = Player.objects.get(user=request.user)
            p = Participation(player=user_player, game=game)
            p.save()

            # Direct the user to the view of the newly created game.
            return HttpResponseRedirect(reverse('show_game', kwargs={'game_custom_slug':game.custom_slug}))

        else:
            # The supplied form contained errors - print them to the terminal.
            print(game_form.errors)

    return render(request, 'fives/create_game.html', {'game_form': game_form})
def create_participation(game, player, rated):
    p = Participation(game=game, player=player, rated=rated)
    p.save()
    return p