diff --git a/src/character/models/character.py b/src/character/models/character.py index e01134a..17b935d 100644 --- a/src/character/models/character.py +++ b/src/character/models/character.py @@ -394,3 +394,10 @@ class Character(models.Model): def managed_by(self, user): return self in Character.objects.managed_by(user) + + def reset_stats(self): + self.health_remaining = self.health_max + self.mana_remaining = self.mana_max + self.luck_points_remaining = self.luck_points_max + self.recovery_points_remaining = self.recovery_points_max + self.save() diff --git a/src/character/templates/character/character_details.html b/src/character/templates/character/character_details.html index 15e80f6..9b6a4de 100644 --- a/src/character/templates/character/character_details.html +++ b/src/character/templates/character/character_details.html @@ -17,7 +17,9 @@ {% endif %} {% if character|managed_by:user %}

- Edit + Modifier + • + Réinitialiser les stats

{% endif %}

diff --git a/src/character/templates/character/character_reset_stats.html b/src/character/templates/character/character_reset_stats.html new file mode 100644 index 0000000..4c3651b --- /dev/null +++ b/src/character/templates/character/character_reset_stats.html @@ -0,0 +1,17 @@ +{% extends "common/base.html" %} + +{% block title %}Réinitialisation des stats de {{ character.name }}{% endblock %} + +{% block content %} +

Réinitialisation des stats de {{ character.name }}

+
+ {% csrf_token %} +

+ Êtes-vous certain de vouloir réinitialiser les stats de {{ character.name }} ?
+ Cette action est irréversible. +

+ +
+{% endblock %} diff --git a/src/character/tests/test_interactions.py b/src/character/tests/test_interactions.py index d5798cb..e9a5c7e 100644 --- a/src/character/tests/test_interactions.py +++ b/src/character/tests/test_interactions.py @@ -6,7 +6,7 @@ from pytest_django.live_server_helper import LiveServer from selenium.webdriver.common.by import By from selenium.webdriver.firefox.webdriver import WebDriver -from character.models import Character +from character.models import Character, Profile from common.models import User @@ -144,6 +144,40 @@ def test_delete_character(selenium: WebDriver, live_server: LiveServer): assert Character.objects.filter(pk=characters[0].pk).first() is None +@pytest.mark.django_db +def test_reset_stats_view( + selenium: WebDriver, live_server: LiveServer, initial_data: None +): + username, password = "user", "some_password" + player = User.objects.create_user(username, password=password) + profile = Profile.objects.get(name__iexact="Magicien") + + character = baker.make(Character, player=player, profile=profile) + character.health_max = 20 + character.health_remaining = 15 + character.value_intelligence = 15 + character.level = 3 + character.mana_remaining = character.mana_max - 1 + character.recovery_points_remaining = 2 + character.value_charisma = 15 + character.luck_points_remaining = character.luck_points_max - 2 + character.save() + + login(selenium, live_server, username, password) + + url = reverse("character:view", kwargs={"pk": character.pk}) + selenium.get(live_server.url + url) + selenium.find_element(By.ID, "reset-stats").click() + selenium.find_element(By.CSS_SELECTOR, "[type=submit]").click() + assert selenium.current_url == live_server.url + character.get_absolute_url() + + character.refresh_from_db() + assert character.health_remaining == character.health_max + assert character.mana_remaining == character.mana_max + assert character.recovery_points_remaining == character.recovery_points_max + assert character.luck_points_remaining == character.luck_points_max + + def login( selenium: WebDriver, live_server: LiveServer, username: str, password: str ) -> None: diff --git a/src/character/urls.py b/src/character/urls.py index f0e3112..1684631 100644 --- a/src/character/urls.py +++ b/src/character/urls.py @@ -85,4 +85,5 @@ urlpatterns = [ "/remove_state//", views.remove_state, name="remove_state" ), path("/add_state//", views.add_state, name="add_state"), + path("/reset_stats/", views.reset_stats, name="reset_stats"), ] diff --git a/src/character/views.py b/src/character/views.py index 9a21fc2..d6b25c8 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -409,3 +409,16 @@ def add_state(request, pk: int, state_pk: int): request, "character/snippets/character_details/states.html", context ) return trigger_client_event(response, "refresh_tooltips", {}) + + +@login_required +def reset_stats(request, pk: int): + character: Character = get_object_or_404( + Character.objects.managed_by(request.user), pk=pk + ) + context = {"character": character} + if request.method == "POST": + character.reset_stats() + messages.success(request, f"Les stats de {character} ont été réinitialisées.") + return redirect(character) + return render(request, "character/character_reset_stats.html", context)