From a9a487e176966ece9a907a54d13b59ab59086e09 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 11 Feb 2024 12:53:16 +0100 Subject: [PATCH] allow modifying health by custom increments #8 --- .../character/character_details.html | 56 ++++++++-------- src/character/tests/test_interactions.py | 64 +++++++++++++++++++ src/character/views.py | 31 ++++++++- 3 files changed, 124 insertions(+), 27 deletions(-) diff --git a/src/character/templates/character/character_details.html b/src/character/templates/character/character_details.html index 9bd5990..db5f619 100644 --- a/src/character/templates/character/character_details.html +++ b/src/character/templates/character/character_details.html @@ -262,32 +262,36 @@ Points de vie {% if character|managed_by:user %} -
- - - - -
+
+ {% csrf_token %} +
+ + + + + +
+
{% endif %} {{ character.health_remaining }} / {{ character.health_max }} diff --git a/src/character/tests/test_interactions.py b/src/character/tests/test_interactions.py index 939730e..5b27f8a 100644 --- a/src/character/tests/test_interactions.py +++ b/src/character/tests/test_interactions.py @@ -93,6 +93,70 @@ def test_create_character(selenium: WebDriver, live_server: LiveServer): assert getattr(character, name) == value +@pytest.mark.django_db() +def test_change_health(selenium: WebDriver, live_server: LiveServer): + call_command("loaddata", "initial_data") + username, password = "user1", "some_password" + player = User.objects.create_user(username, password=password) + character = baker.make(Character, player=player) + character.health_remaining = character.health_max + character.save() + login(selenium, live_server, username, password) + selenium.find_element( + By.CSS_SELECTOR, + f".character[data-id='{character.id}'] .btn-success", + ).click() + assert selenium.find_element(By.ID, "health-remaining").text == str( + character.health_remaining, + ) + + controls = selenium.find_element(By.ID, "health-controls") + + controls.find_element(By.CSS_SELECTOR, "button[type='submit'][value='ko']").click() + assert selenium.find_element(By.ID, "health-remaining").text == "0" + + controls.find_element(By.CSS_SELECTOR, "button[type='submit'][value='max']").click() + assert selenium.find_element(By.ID, "health-remaining").text == str( + character.health_max, + ) + + controls.find_element( + By.CSS_SELECTOR, + "button[type='submit'][value='positive']", + ).click() + assert selenium.find_element(By.ID, "health-remaining").text == str( + character.health_max, + ) + + controls.find_element( + By.CSS_SELECTOR, + "button[type='submit'][value='negative']", + ).click() + assert selenium.find_element(By.ID, "health-remaining").text == str( + character.health_max - 1, + ) + + health_input = controls.find_element(By.CSS_SELECTOR, "input[name='value']") + health_input.clear() + health_input.send_keys("5") + + controls.find_element( + By.CSS_SELECTOR, + "button[type='submit'][value='positive']", + ).click() + assert selenium.find_element(By.ID, "health-remaining").text == str( + character.health_max, + ) + + controls.find_element( + By.CSS_SELECTOR, + "button[type='submit'][value='negative']", + ).click() + assert selenium.find_element(By.ID, "health-remaining").text == str( + character.health_max - 5, + ) + + @pytest.mark.django_db() def test_list_characters(selenium: WebDriver, live_server: LiveServer): # Load fixtures diff --git a/src/character/views.py b/src/character/views.py index 62d4136..e853b74 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -123,7 +123,11 @@ def character_health_change(request, pk: int): ), pk=pk, ) - value = get_updated_value(request, character.health_remaining, character.health_max) + value = post_updated_value( + request, + character.health_remaining, + character.health_max, + ) character.health_remaining = value character.save(update_fields=["health_remaining"]) response = HttpResponse(value) @@ -232,6 +236,31 @@ def character_luck_points_change(request, pk: int): return HttpResponse(value) +def post_updated_value( + request, + remaining_value: float, + max_value: float, +) -> int: + action = request.POST.get("action") + if action == "ko": + return 0 + if action == "max": + return int(max_value) + + multiplier = 0 + if action == "positive": + multiplier = 1 + elif action == "negative": + multiplier = -1 + + form_value = int(request.POST.get("value")) + remaining_value += form_value * multiplier + remaining_value = min([max_value, remaining_value]) + remaining_value = max([0, remaining_value]) + + return int(remaining_value) + + def get_updated_value( request, remaining_value: float,