From 0a4f0503c0b56a8542a36a9e64a6e56fea03ac3f Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 30 Oct 2022 18:42:55 +0100 Subject: [PATCH] Fix refacto --- src/character/views.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/character/views.py b/src/character/views.py index 62d871a..d355c8a 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -23,7 +23,7 @@ def character_health_change(request: WSGIRequest, pk: int) -> HttpResponse: character = get_object_or_404( Character.objects.only("health_max", "health_remaining"), pk=pk ) - value = get_updated_value(character.health_max, request) + value = get_updated_value(request, character.health_remaining, character.health_max) character.health_remaining = value character.save(update_fields=["health_remaining"]) return HttpResponse(character.health_remaining) @@ -34,24 +34,26 @@ def character_mana_change(request: WSGIRequest, pk: int) -> HttpResponse: character = get_object_or_404( Character.objects.only("mana_remaining", "level", "value_intelligence"), pk=pk ) - value = get_updated_value(character.mana_max, request) + value = get_updated_value(request, character.mana_remaining, character.mana_max) character.mana_remaining = value character.save(update_fields=["mana_remaining"]) return HttpResponse(character.mana_remaining) -def get_updated_value(max_value, request): - value = request.GET.get("value") - if value == "ko": - value = 0 - elif value == "max": - value = max_value +def get_updated_value( + request: WSGIRequest, remaining_value: int, max_value: int +) -> int: + form_value = request.GET.get("value") + if form_value == "ko": + remaining_value = 0 + elif form_value == "max": + remaining_value = max_value else: - value = int(value) - value += value - value = min([max_value, value]) - value = max([0, value]) - return value + form_value = int(form_value) + remaining_value += form_value + remaining_value = min([max_value, remaining_value]) + remaining_value = max([0, remaining_value]) + return remaining_value @login_required