Add interactivity for HP

This commit is contained in:
Gabriel Augendre 2022-10-30 17:37:09 +01:00
parent 6f392a4603
commit c2b2a156a2
3 changed files with 53 additions and 3 deletions

View file

@ -98,8 +98,36 @@
<td>{{ character.health_max }}</td> <td>{{ character.health_max }}</td>
</tr> </tr>
<tr> <tr>
<th scope="row">PV restants</th> <th scope="row">
<td>{{ character.health_remaining }}</td> PV restants
<div class="btn-group btn-group-sm" role="group">
<button
hx-get="{% url "character:health_change" pk=character.pk %}?value=ko"
hx-target="#health-remaining"
hx-swap="innerHTML"
type="button"
class="btn btn-outline-danger">0</button>
<button
hx-get="{% url "character:health_change" pk=character.pk %}?value=-1"
hx-target="#health-remaining"
hx-swap="innerHTML"
type="button"
class="btn btn-danger">-</button>
<button
hx-get="{% url "character:health_change" pk=character.pk %}?value=1"
hx-target="#health-remaining"
hx-swap="innerHTML"
type="button"
class="btn btn-success">+</button>
<button
hx-get="{% url "character:health_change" pk=character.pk %}?value=max"
hx-target="#health-remaining"
hx-swap="innerHTML"
type="button"
class="btn btn-outline-success">max</button>
</div>
</th>
<td id="health-remaining">{{ character.health_remaining }}</td>
</tr> </tr>
<tr> <tr>
<th scope="row">PM max</th> <th scope="row">PM max</th>

View file

@ -3,4 +3,7 @@ from django.urls import path
from character import views from character import views
app_name = "character" app_name = "character"
urlpatterns = [path("<int:pk>/", views.character_view, name="view")] urlpatterns = [
path("<int:pk>/", views.character_view, name="view"),
path("<int:pk>/health_change", views.character_health_change, name="health_change"),
]

View file

@ -11,3 +11,22 @@ def character_view(request: WSGIRequest, pk: int) -> HttpResponse:
character = get_object_or_404(Character.objects.select_related("player"), pk=pk) character = get_object_or_404(Character.objects.select_related("player"), pk=pk)
context = {"character": character} context = {"character": character}
return render(request, "character/view.html", context) return render(request, "character/view.html", context)
@login_required
def character_health_change(request: WSGIRequest, pk: int) -> HttpResponse:
character = get_object_or_404(Character, pk=pk)
value = request.GET.get("value")
if value == "ko":
character.health_remaining = 0
elif value == "max":
character.health_remaining = character.health_max
else:
value = int(value)
character.health_remaining += value
character.health_remaining = min(
[character.health_max, character.health_remaining]
)
character.health_remaining = max([0, character.health_remaining])
character.save(update_fields=["health_remaining"])
return HttpResponse(character.health_remaining)