From 1a8dd708475c265eb16361c0a28b1344896af704 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 30 Oct 2022 17:47:42 +0100 Subject: [PATCH] Update mana --- ...remove_character_mana_consumed_and_more.py | 24 ++++++++++++++ src/character/migrations/max_migration.txt | 2 +- src/character/models/character.py | 8 ++--- src/character/templates/character/view.html | 32 +++++++++++++++++-- src/character/urls.py | 1 + src/character/views.py | 17 ++++++++++ 6 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 src/character/migrations/0014_remove_character_mana_consumed_and_more.py diff --git a/src/character/migrations/0014_remove_character_mana_consumed_and_more.py b/src/character/migrations/0014_remove_character_mana_consumed_and_more.py new file mode 100644 index 0000000..75da308 --- /dev/null +++ b/src/character/migrations/0014_remove_character_mana_consumed_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 4.1.2 on 2022-10-30 16:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("character", "0013_character_money_pa_character_money_pc_and_more"), + ] + + operations = [ + migrations.RemoveField( + model_name="character", + name="mana_consumed", + ), + migrations.AddField( + model_name="character", + name="mana_remaining", + field=models.PositiveSmallIntegerField( + default=0, verbose_name="mana restant" + ), + ), + ] diff --git a/src/character/migrations/max_migration.txt b/src/character/migrations/max_migration.txt index eea0192..2c6ec14 100644 --- a/src/character/migrations/max_migration.txt +++ b/src/character/migrations/max_migration.txt @@ -1 +1 @@ -0013_character_money_pa_character_money_pc_and_more +0014_remove_character_mana_consumed_and_more diff --git a/src/character/models/character.py b/src/character/models/character.py index 0999202..8aba072 100644 --- a/src/character/models/character.py +++ b/src/character/models/character.py @@ -129,8 +129,8 @@ class Character(models.Model): verbose_name="points de chance restants" ) - mana_consumed = models.PositiveSmallIntegerField( - default=0, verbose_name="mana utilisé" + mana_remaining = models.PositiveSmallIntegerField( + default=0, verbose_name="mana restant" ) money_pp = models.PositiveSmallIntegerField(default=0, verbose_name="PP") @@ -219,10 +219,6 @@ class Character(models.Model): def mana_max(self) -> int: return 2 * self.level + self.modifier_intelligence - @property - def mana_remaining(self) -> int: - return self.mana_max - self.mana_consumed - @property def height_m(self) -> float: return round(self.height / 100, 2) diff --git a/src/character/templates/character/view.html b/src/character/templates/character/view.html index 185d3db..b78e730 100644 --- a/src/character/templates/character/view.html +++ b/src/character/templates/character/view.html @@ -135,8 +135,36 @@ {{ character.mana_max }} - PM restants - {{ character.mana_remaining }} + + PM restants +
+ + + + +
+ + {{ character.mana_remaining }} diff --git a/src/character/urls.py b/src/character/urls.py index d01c856..77b4722 100644 --- a/src/character/urls.py +++ b/src/character/urls.py @@ -6,4 +6,5 @@ app_name = "character" urlpatterns = [ path("/", views.character_view, name="view"), path("/health_change", views.character_health_change, name="health_change"), + path("/mana_change", views.character_mana_change, name="mana_change"), ] diff --git a/src/character/views.py b/src/character/views.py index b00c430..cfc5245 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -30,3 +30,20 @@ def character_health_change(request: WSGIRequest, pk: int) -> HttpResponse: character.health_remaining = max([0, character.health_remaining]) character.save(update_fields=["health_remaining"]) return HttpResponse(character.health_remaining) + + +@login_required +def character_mana_change(request: WSGIRequest, pk: int) -> HttpResponse: + character = get_object_or_404(Character, pk=pk) + value = request.GET.get("value") + if value == "ko": + character.mana_remaining = 0 + elif value == "max": + character.mana_remaining = character.mana_max + else: + value = int(value) + character.mana_remaining += value + character.mana_remaining = min([character.mana_max, character.mana_remaining]) + character.mana_remaining = max([0, character.mana_remaining]) + character.save(update_fields=["mana_remaining"]) + return HttpResponse(character.mana_remaining)