Example #1
0
    def test_cannot_force_a_creator_user(self):
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon1",
                      status=200)
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon2",
                      status=200)
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon3",
                      status=200)

        pokemon_1 = mommy.make(
            "pokemons.Pokemon",
            poke_id=1,
            name="pokemon1",
            attack=49,
            defense=49,
            hit_points=45,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/1.png",
        )
        pokemon_2 = mommy.make(
            "pokemons.Pokemon",
            poke_id=2,
            name="pokemon2",
            attack=64,
            defense=64,
            hit_points=50,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/2.png",
        )
        pokemon_3 = mommy.make(
            "pokemons.Pokemon",
            poke_id=3,
            name="pokemon3",
            attack=69,
            defense=69,
            hit_points=55,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/3.png",
        )

        current_user = mommy.make("users.User")
        fake_creator_user = mommy.make("users.User")
        opponent = mommy.make("users.User")

        data = {
            "creator": fake_creator_user.id,
            "opponent": opponent.id,
            "creator_pokemon_1": pokemon_1.id,
            "creator_pokemon_2": pokemon_2.id,
            "creator_pokemon_3": pokemon_3.id,
        }

        form = BattleForm(data=data, current_user=current_user)
        self.assertTrue(form.is_valid())

        battle = form.save()
        self.assertEqual(battle.creator, current_user)
Example #2
0
    def test_cannot_create_battle_if_pokemon_points_sum_more_than_600(self):
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon1",
                      status=200)
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon2",
                      status=200)
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon3",
                      status=200)

        pokemon_1 = mommy.make(
            "pokemons.Pokemon",
            poke_id=1,
            name="pokemon1",
            attack=100,
            defense=100,
            hit_points=100,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/1.png",
        )
        pokemon_2 = mommy.make(
            "pokemons.Pokemon",
            poke_id=2,
            name="pokemon2",
            attack=100,
            defense=100,
            hit_points=100,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/2.png",
        )
        pokemon_3 = mommy.make(
            "pokemons.Pokemon",
            poke_id=3,
            name="pokemon3",
            attack=100,
            defense=100,
            hit_points=100,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/3.png",
        )

        current_user = mommy.make("users.User")
        opponent = mommy.make("users.User", email="*****@*****.**")

        data = {
            "opponent": opponent.id,
            "creator_pokemon_1": pokemon_1.id,
            "creator_pokemon_2": pokemon_2.id,
            "creator_pokemon_3": pokemon_3.id,
        }

        form = BattleForm(data=data, current_user=current_user)
        self.assertFalse(form.is_valid())

        self.assertEqual(["Pokemons' points sum cannot be more than 600"],
                         form.errors["__all__"])
Example #3
0
    def test_cannot_create_battle_with_creator_as_opponent(self):
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon1",
                      status=200)
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon2",
                      status=200)
        responses.add(responses.HEAD,
                      f"{POKEAPI_BASE_URL}pokemon/pokemon3",
                      status=200)

        pokemon_1 = mommy.make(
            "pokemons.Pokemon",
            poke_id=1,
            name="pokemon1",
            attack=49,
            defense=49,
            hit_points=45,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/1.png",
        )
        pokemon_2 = mommy.make(
            "pokemons.Pokemon",
            poke_id=2,
            name="pokemon2",
            attack=64,
            defense=64,
            hit_points=50,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/2.png",
        )
        pokemon_3 = mommy.make(
            "pokemons.Pokemon",
            poke_id=3,
            name="pokemon3",
            attack=69,
            defense=69,
            hit_points=55,
            image="https://raw.githubusercontent.com/"
            "PokeAPI/sprites/master/sprites/pokemon/3.png",
        )

        current_user = mommy.make("users.User")

        data = {
            "opponent": current_user.id,
            "creator_pokemon_1": pokemon_1.id,
            "creator_pokemon_2": pokemon_2.id,
            "creator_pokemon_3": pokemon_3.id,
        }

        form = BattleForm(data=data, current_user=current_user)

        self.assertFalse(form.is_valid())
        self.assertIn("opponent", form.errors)
Example #4
0
def battle_new(request):
    if request.method == 'POST':
        battle_form = BattleForm(request.POST)

        if battle_form.is_valid():
            battle = battle_form.save(commit=False)
            battle.save()

            game = get_object_or_404(Game, pk=battle.game_id)
            player_ids = request.POST.getlist('players')
            random_player_id = random.choice(player_ids)

            counter = 0
            for player_id in player_ids:
                counter += 1

                result = ResultForm().save(commit=False)
                result.battle_id = battle.id
                result.player_id = player_id

                # TODO: Decide how to do when there is more players than 1vs1
                if counter == 1:
                    result.is_home = True
                elif counter == 2:
                    result.is_home = False

                if game.has_coin_toss and player_id == random_player_id:
                    result.has_coin = True
                else:
                    result.has_coin = False

                result.save()

            context = {
                'battle': battle,
                'results': Result.objects.filter(battle_id=battle.id)
            }

            return render(request, 'battles/result_edit.html', context)
    else:
        battle_form = BattleForm()

    context = {
        'players': User.objects.all(),
        'battle_form': battle_form
    }

    return render(request, 'battles/battle_edit.html', context)
Example #5
0
def battle_select_game(request):
    if request.method == 'POST':
        battle_form = BattleForm(request.POST)

        if battle_form.is_valid():
            battle = battle_form.save(commit=False)
            battle.save()

            context = {
                'battle_id': battle.id,
                # TODO: Add filter for same company and same game
                'players': User.objects.all()
            }

            return render(request, 'battles/battle_select_players.html', context)
    else:
        battle_form = BattleForm()

    context = {'battle_form': battle_form}

    return render(request, 'battles/battle_select_game.html', context)
Example #6
0
def battle_edit(request, battle_id):
    battle = get_object_or_404(Battle, pk=battle_id)

    if request.method == 'POST':
        form = BattleForm(request.POST, instance=battle)

        if form.is_valid():
            battle = form.save(commit=False)
            battle.save()
            form.save_m2m()

            context = {'battle': battle}

            return render(request, 'battles/battle_view.html', context)
    else:
        form = BattleForm(instance=battle)

    context = {'players': User.objects.all(), 'form': form}

    return render(request, 'battles/battle_edit.html', context)
Example #7
0
def battle_new(request):
    if request.method == 'POST':
        battle_form = BattleForm(request.POST)

        if battle_form.is_valid():
            battle = battle_form.save(commit=False)
            battle.save()

            game = get_object_or_404(Game, pk=battle.game_id)
            player_ids = request.POST.getlist('players')
            random_player_id = random.choice(player_ids)

            counter = 0
            for player_id in player_ids:
                counter += 1

                result = ResultForm().save(commit=False)
                result.battle_id = battle.id
                result.player_id = player_id

                # TODO: Decide how to do when there is more players than 1vs1
                if counter == 1:
                    result.is_home = True
                elif counter == 2:
                    result.is_home = False

                if game.has_coin_toss and player_id == random_player_id:
                    result.has_coin = True
                else:
                    result.has_coin = False

                result.save()

            context = {
                'battle': battle,
                'results': Result.objects.filter(battle_id=battle.id)
            }

            return render(request, 'battles/result_edit.html', context)
    else:
        battle_form = BattleForm()

    context = {'players': User.objects.all(), 'battle_form': battle_form}

    return render(request, 'battles/battle_edit.html', context)
Example #8
0
def battle_select_game(request):
    if request.method == 'POST':
        battle_form = BattleForm(request.POST)

        if battle_form.is_valid():
            battle = battle_form.save(commit=False)
            battle.save()

            context = {
                'battle_id': battle.id,
                # TODO: Add filter for same company and same game
                'players': User.objects.all()
            }

            return render(request, 'battles/battle_select_players.html',
                          context)
    else:
        battle_form = BattleForm()

    context = {'battle_form': battle_form}

    return render(request, 'battles/battle_select_game.html', context)
Example #9
0
def battle_edit(request, battle_id):
    battle = get_object_or_404(Battle, pk=battle_id)

    if request.method == 'POST':
        form = BattleForm(request.POST, instance=battle)

        if form.is_valid():
            battle = form.save(commit=False)
            battle.save()
            form.save_m2m()

            context = {'battle': battle}

            return render(request, 'battles/battle_view.html', context)
    else:
        form = BattleForm(instance=battle)

    context = {
        'players': User.objects.all(),
        'form': form
    }

    return render(request, 'battles/battle_edit.html', context)