Exemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            form = CreateGameForm(request.POST, max_players=MAX_PLAYERS)
            if form.is_valid():
                opponent_usernames = []
                for i in range(0, MAX_PLAYERS):
                    field_name = 'opponent_username_{}'.format(i)
                    opponent_usernames.append(form.cleaned_data[field_name])

                try:
                    opponent_users = []
                    for opponent_username in opponent_usernames:
                        if len(opponent_username) > 0:
                            opponent_users.append(
                                User.objects.get(username=opponent_username))
                except User.DoesNotExist:
                    error_message = 'User does not exist! '\
                        'Are you sure the username is correct?'
                    messages.error(request, error_message)
                    context = {'form': form}
                    return render(request, self.template_name, context)

                user_player = Player.objects.get(user=request.user)
                opponent_players = [
                    Player.objects.get(user=opponent_user)
                    for opponent_user in opponent_users
                ]

                # Create a game plus teams and ships for both players
                # Creation in Game -> Team -> Ships order is important
                # to satisfy ForeignKey dependencies.
                game = Game()
                game.save()
                user_team = Team(player=user_player, game=game, last_turn=-2)
                opponent_teams = [
                    Team(player=opponent_player, game=game, last_turn=-1)
                    for opponent_player in opponent_players
                ]
                user_team.save()
                for opponent_team in opponent_teams:
                    opponent_team.save()

                user_ships = make_ships(user_team, Ship.LENGTHS)
                for opponent_team in opponent_teams:
                    opponent_ships = make_ships(opponent_team, Ship.LENGTHS)
                    for user_ship in user_ships:
                        user_ship.save()
                    for opponent_ship in opponent_ships:
                        opponent_ship.save()

                return HttpResponseRedirect(reverse('game', args=[game.id]))
            else:
                messages.error(request, 'Invalid form.')
                context = {'form': form}
                return render(request, self.template_name, context)
        else:
            return HttpResponseRedirect('/login')
Exemplo n.º 2
0
def create(request):
	if request.method == 'POST':
		form = CreateGameForm(request.POST)
		if form.is_valid():
			new_game = form.save()
			return redirect('games:homepage', game_id=new_game.id)
	else:
		form = CreateGameForm()

	return render(request, 'games/create.html', {'form': form})
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            form = CreateGameForm(request.POST, max_players=MAX_PLAYERS)
            if form.is_valid():
                opponent_usernames = []
                for i in range(0, MAX_PLAYERS):
                    field_name = "opponent_username_{}".format(i)
                    opponent_usernames.append(form.cleaned_data[field_name])

                try:
                    opponent_users = []
                    for opponent_username in opponent_usernames:
                        if len(opponent_username) > 0:
                            opponent_users.append(User.objects.get(username=opponent_username))
                except User.DoesNotExist:
                    error_message = "User does not exist! " "Are you sure the username is correct?"
                    messages.error(request, error_message)
                    context = {"form": form}
                    return render(request, self.template_name, context)

                user_player = Player.objects.get(user=request.user)
                opponent_players = [Player.objects.get(user=opponent_user) for opponent_user in opponent_users]

                # Create a game plus teams and ships for both players
                # Creation in Game -> Team -> Ships order is important
                # to satisfy ForeignKey dependencies.
                game = Game()
                game.save()
                user_team = Team(player=user_player, game=game, last_turn=-2)
                opponent_teams = [
                    Team(player=opponent_player, game=game, last_turn=-1) for opponent_player in opponent_players
                ]
                user_team.save()
                for opponent_team in opponent_teams:
                    opponent_team.save()

                user_ships = make_ships(user_team, Ship.LENGTHS)
                for opponent_team in opponent_teams:
                    opponent_ships = make_ships(opponent_team, Ship.LENGTHS)
                    for user_ship in user_ships:
                        user_ship.save()
                    for opponent_ship in opponent_ships:
                        opponent_ship.save()

                return HttpResponseRedirect(reverse("game", args=[game.id]))
            else:
                messages.error(request, "Invalid form.")
                context = {"form": form}
                return render(request, self.template_name, context)
        else:
            return HttpResponseRedirect("/login")
Exemplo n.º 4
0
 def get(self, request, *args, **kwargs):
     form = CreateGameForm(max_players=MAX_PLAYERS)
     context = {'form': form}
     return render(request, self.template_name, context)