mirror of
https://github.com/Crocmagnon/charasheet.git
synced 2024-11-05 06:13:55 +01:00
Add interactivity for HP
This commit is contained in:
parent
6f392a4603
commit
c2b2a156a2
3 changed files with 53 additions and 3 deletions
|
@ -98,8 +98,36 @@
|
|||
<td>{{ character.health_max }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">PV restants</th>
|
||||
<td>{{ character.health_remaining }}</td>
|
||||
<th scope="row">
|
||||
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>
|
||||
<th scope="row">PM max</th>
|
||||
|
|
|
@ -3,4 +3,7 @@ from django.urls import path
|
|||
from character import views
|
||||
|
||||
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"),
|
||||
]
|
||||
|
|
|
@ -11,3 +11,22 @@ def character_view(request: WSGIRequest, pk: int) -> HttpResponse:
|
|||
character = get_object_or_404(Character.objects.select_related("player"), pk=pk)
|
||||
context = {"character": character}
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue