Fix refacto

This commit is contained in:
Gabriel Augendre 2022-10-30 18:42:55 +01:00
parent 85df1baa52
commit 0a4f0503c0

View file

@ -23,7 +23,7 @@ def character_health_change(request: WSGIRequest, pk: int) -> HttpResponse:
character = get_object_or_404( character = get_object_or_404(
Character.objects.only("health_max", "health_remaining"), pk=pk 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.health_remaining = value
character.save(update_fields=["health_remaining"]) character.save(update_fields=["health_remaining"])
return HttpResponse(character.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 = get_object_or_404(
Character.objects.only("mana_remaining", "level", "value_intelligence"), pk=pk 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.mana_remaining = value
character.save(update_fields=["mana_remaining"]) character.save(update_fields=["mana_remaining"])
return HttpResponse(character.mana_remaining) return HttpResponse(character.mana_remaining)
def get_updated_value(max_value, request): def get_updated_value(
value = request.GET.get("value") request: WSGIRequest, remaining_value: int, max_value: int
if value == "ko": ) -> int:
value = 0 form_value = request.GET.get("value")
elif value == "max": if form_value == "ko":
value = max_value remaining_value = 0
elif form_value == "max":
remaining_value = max_value
else: else:
value = int(value) form_value = int(form_value)
value += value remaining_value += form_value
value = min([max_value, value]) remaining_value = min([max_value, remaining_value])
value = max([0, value]) remaining_value = max([0, remaining_value])
return value return remaining_value
@login_required @login_required