From 8a21e54179ec0c24a487e1889c7901f801077c04 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 2 Nov 2022 13:03:20 +0100 Subject: [PATCH] Change initiative mechanism --- src/character/admin.py | 11 ++++- .../0027_character_initiative_misc.py | 18 +++++++++ src/character/migrations/max_migration.txt | 2 +- src/character/models/character.py | 8 +++- src/character/templates/character/view.html | 40 +++++++++++++++++-- src/character/urls.py | 10 +++++ src/character/views.py | 23 +++++++++++ 7 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 src/character/migrations/0027_character_initiative_misc.py diff --git a/src/character/admin.py b/src/character/admin.py index ac4dc73..617f319 100644 --- a/src/character/admin.py +++ b/src/character/admin.py @@ -132,7 +132,14 @@ class CharacterAdmin(admin.ModelAdmin): ), ( "Combat", - {"fields": ["initiative", "attack_melee", "attack_range", "attack_magic"]}, + { + "fields": [ + ("initiative_misc", "modifier_initiative"), + "attack_melee", + "attack_range", + "attack_magic", + ] + }, ), ("Vitalité", {"fields": [("health_max", "health_remaining")]}), ("Défense", {"fields": ["armor", "shield", "defense_misc", "defense"]}), @@ -163,7 +170,7 @@ class CharacterAdmin(admin.ModelAdmin): "modifier_intelligence", "modifier_wisdom", "modifier_charisma", - "initiative", + "modifier_initiative", "attack_melee", "attack_range", "attack_magic", diff --git a/src/character/migrations/0027_character_initiative_misc.py b/src/character/migrations/0027_character_initiative_misc.py new file mode 100644 index 0000000..91ef1a9 --- /dev/null +++ b/src/character/migrations/0027_character_initiative_misc.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.2 on 2022-11-02 12:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("character", "0026_alter_capability_name"), + ] + + operations = [ + migrations.AddField( + model_name="character", + name="initiative_misc", + field=models.SmallIntegerField(default=0, verbose_name="divers initiative"), + ), + ] diff --git a/src/character/migrations/max_migration.txt b/src/character/migrations/max_migration.txt index 12f5778..3642965 100644 --- a/src/character/migrations/max_migration.txt +++ b/src/character/migrations/max_migration.txt @@ -1 +1 @@ -0026_alter_capability_name +0027_character_initiative_misc diff --git a/src/character/models/character.py b/src/character/models/character.py index a805bad..ab68831 100644 --- a/src/character/models/character.py +++ b/src/character/models/character.py @@ -148,6 +148,10 @@ class Character(models.Model): shield = models.PositiveSmallIntegerField(verbose_name="bouclier", default=0) defense_misc = models.SmallIntegerField(verbose_name="divers défense", default=0) + initiative_misc = models.SmallIntegerField( + verbose_name="divers initiative", default=0 + ) + capabilities = models.ManyToManyField( "character.Capability", blank=True, verbose_name="capacités" ) @@ -218,8 +222,8 @@ class Character(models.Model): return modifier(self.value_charisma) @property - def initiative(self) -> int: - return self.value_dexterity + def modifier_initiative(self) -> int: + return self.modifier_dexterity + self.initiative_misc @property def attack_melee(self) -> int: diff --git a/src/character/templates/character/view.html b/src/character/templates/character/view.html index eb63d64..3d589bc 100644 --- a/src/character/templates/character/view.html +++ b/src/character/templates/character/view.html @@ -67,11 +67,45 @@ - Initiative + + Initiative divers +
+ + + + +
+ + + {{ character.initiative_misc }} + + + + Mod. initiative - {{ character.initiative }} + data-bs-title="{{ character.modifier_dexterity }} (mod. DEX) + {{ character.initiative_misc }} (divers)" + hx-get="{% url "character:get_initiative" pk=character.pk %}" + hx-trigger="update_initiative from:#fight-table"> + {{ character.modifier_initiative }} diff --git a/src/character/urls.py b/src/character/urls.py index 7a0e8e9..73fb7d7 100644 --- a/src/character/urls.py +++ b/src/character/urls.py @@ -28,6 +28,16 @@ urlpatterns = [ views.character_defense_misc_change, name="defense_misc_change", ), + path( + "/get_initiative/", + views.character_get_initiative, + name="get_initiative", + ), + path( + "/initiative_misc_change/", + views.character_initiative_misc_change, + name="initiative_misc_change", + ), path( "/equipment_change/", views.character_equipment_change, diff --git a/src/character/views.py b/src/character/views.py index 59bd62f..c600169 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -106,6 +106,18 @@ def character_defense_misc_change(request, pk: int): return trigger_client_event(response, "update_defense", {}) +@login_required +def character_initiative_misc_change(request, pk: int): + character = get_object_or_404( + Character.objects.filter(player=request.user).only("initiative_misc"), pk=pk + ) + value = get_updated_value(request, character.initiative_misc, float("inf")) + character.initiative_misc = value + character.save(update_fields=["initiative_misc"]) + response = HttpResponse(value) + return trigger_client_event(response, "update_initiative", {}) + + @login_required def character_luck_points_change(request, pk: int): character = get_object_or_404( @@ -147,6 +159,17 @@ def character_get_defense(request, pk: int): return HttpResponse(character.defense) +@login_required +def character_get_initiative(request, pk: int): + character = get_object_or_404( + Character.objects.filter(player=request.user).only( + "initiative_misc", "value_dexterity" + ), + pk=pk, + ) + return HttpResponse(character.modifier_initiative) + + @login_required def character_notes_change(request, pk: int): return update_text_field(request, pk, "notes")