From 0287536ba84a4deb4bb9e420b4650080e27db974 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 31 Oct 2022 22:32:14 +0100 Subject: [PATCH] Allow adding next capability in path --- .../migrations/0025_alter_capability_path.py | 24 ++++++++++++++++++ src/character/migrations/max_migration.txt | 2 +- src/character/models/capabilities.py | 18 ++++++++++++- .../templates/character/capability.html | 9 +++++++ src/character/templates/character/view.html | 24 ++++++++++-------- .../templatetags/character_extras.py | 7 +++++- src/character/urls.py | 25 ++++++++++++------- src/character/views.py | 13 +++++++++- 8 files changed, 98 insertions(+), 24 deletions(-) create mode 100644 src/character/migrations/0025_alter_capability_path.py create mode 100644 src/character/templates/character/capability.html diff --git a/src/character/migrations/0025_alter_capability_path.py b/src/character/migrations/0025_alter_capability_path.py new file mode 100644 index 0000000..b0f0321 --- /dev/null +++ b/src/character/migrations/0025_alter_capability_path.py @@ -0,0 +1,24 @@ +# Generated by Django 4.1.2 on 2022-10-31 21:09 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("character", "0024_alter_character_notes"), + ] + + operations = [ + migrations.AlterField( + model_name="capability", + name="path", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="capabilities", + to="character.path", + verbose_name="voie", + ), + ), + ] diff --git a/src/character/migrations/max_migration.txt b/src/character/migrations/max_migration.txt index 6a5eaa8..3f9ca7a 100644 --- a/src/character/migrations/max_migration.txt +++ b/src/character/migrations/max_migration.txt @@ -1 +1 @@ -0024_alter_character_notes +0025_alter_capability_path diff --git a/src/character/models/capabilities.py b/src/character/models/capabilities.py index 883c3b4..b592551 100644 --- a/src/character/models/capabilities.py +++ b/src/character/models/capabilities.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from django_extensions.db.models import TimeStampedModel @@ -56,10 +58,24 @@ class Path(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.Model): else: return None + def get_next_capability(self, character) -> Capability: + next_rank = character.capabilities.filter(path=self).count() + 1 + return self.capabilities.get(rank=next_rank) + + def has_next_capability(self, character) -> bool: + try: + self.get_next_capability(character) + return True + except Capability.DoesNotExist: + return False + class Capability(DocumentedModel, UniquelyNamedModel, TimeStampedModel, models.Model): path = models.ForeignKey( - "character.Path", on_delete=models.CASCADE, verbose_name="voie" + "character.Path", + on_delete=models.CASCADE, + verbose_name="voie", + related_name="capabilities", ) rank = models.PositiveSmallIntegerField( validators=[MinValueValidator(1), MaxValueValidator(5)], verbose_name="rang" diff --git a/src/character/templates/character/capability.html b/src/character/templates/character/capability.html new file mode 100644 index 0000000..47923ab --- /dev/null +++ b/src/character/templates/character/capability.html @@ -0,0 +1,9 @@ +
  • + + {{ capability.rank }}. + {{ capability.name }} + {% if capability.spell %}{% endif %} + {% if capability.limited %}{% endif %} +
    + {{ capability.description }} +
  • diff --git a/src/character/templates/character/view.html b/src/character/templates/character/view.html index b2c33c4..a62c550 100644 --- a/src/character/templates/character/view.html +++ b/src/character/templates/character/view.html @@ -339,24 +339,26 @@
    {% for path, capabilities in character.get_capabilities_by_path.items %}
    -
    +
    {{ path.display_name }}
    {% if path.notes %}
    {{ path.notes }}
    {% endif %} -
      +
        {% for capability in capabilities %} -
      • - - {{ capability.rank }}. - {{ capability.name }} - {% if capability.spell %}{% endif %} - {% if capability.limited %}{% endif %} -
        - {{ capability.description }} -
      • + {% include "character/capability.html" %} {% endfor %}
      + {% if path|has_next_capability:character %} +
      + +
      + {% endif %}
    {% endfor %} diff --git a/src/character/templatetags/character_extras.py b/src/character/templatetags/character_extras.py index 81ccc6f..44b5dfb 100644 --- a/src/character/templatetags/character_extras.py +++ b/src/character/templatetags/character_extras.py @@ -1,6 +1,6 @@ from django import template -from character.models import Character, Weapon +from character.models import Character, Path, Weapon register = template.Library() @@ -27,3 +27,8 @@ def weapon_modifier(character: Character, weapon: Weapon): return f"- {abs(value)}" else: return "" + + +@register.filter +def has_next_capability(path: Path, character: Character) -> bool: + return path.has_next_capability(character) diff --git a/src/character/urls.py b/src/character/urls.py index 3955347..85a42d3 100644 --- a/src/character/urls.py +++ b/src/character/urls.py @@ -7,33 +7,40 @@ urlpatterns = [ path("", views.characters_list, name="list"), path("create/", views.character_create, name="create"), 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"), path( - "/recovery_points_change", + "/health_change/", views.character_health_change, name="health_change" + ), + path("/mana_change/", views.character_mana_change, name="mana_change"), + path( + "/recovery_points_change/", views.character_recovery_points_change, name="recovery_points_change", ), path( - "/luck_points_change", + "/luck_points_change/", views.character_luck_points_change, name="luck_points_change", ), - path("/notes_change", views.character_notes_change, name="notes_change"), - path("/get_defense", views.character_get_defense, name="get_defense"), + path("/notes_change/", views.character_notes_change, name="notes_change"), + path("/get_defense/", views.character_get_defense, name="get_defense"), path( - "/defense_misc_change", + "/defense_misc_change/", views.character_defense_misc_change, name="defense_misc_change", ), path( - "/equipment_change", + "/equipment_change/", views.character_equipment_change, name="equipment_change", ), path( - "/damage_reduction_change", + "/damage_reduction_change/", views.character_damage_reduction_change, name="damage_reduction_change", ), + path( + "/add_next_in_path//", + views.add_next_in_path, + name="add_next_in_path", + ), ] diff --git a/src/character/views.py b/src/character/views.py index d79c700..a3d8b1c 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -4,7 +4,7 @@ from django.shortcuts import get_object_or_404, redirect, render from django_htmx.http import trigger_client_event from character.forms import EquipmentForm -from character.models import Character +from character.models import Character, Path @login_required @@ -167,3 +167,14 @@ def update_text_field(request, pk, field): setattr(character, field, request.POST.get(field)) character.save(update_fields=[field]) return render(request, f"character/{field}_display.html", context) + + +@login_required +def add_next_in_path(request, character_pk: int, path_pk: int): + character = get_object_or_404( + Character.objects.filter(player=request.user), pk=character_pk + ) + path = get_object_or_404(Path, pk=path_pk) + capability = path.get_next_capability(character) + character.capabilities.add(capability) + return render(request, "character/capability.html", {"capability": capability})