From 3846778d003dd6695de5f6eb223ce08d81725bcb Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 31 Oct 2022 00:20:55 +0100 Subject: [PATCH] Display modifier next to weapon --- src/character/admin.py | 1 + .../migrations/0019_weapon_category.py | 18 +++++++++++++ .../migrations/0020_alter_weapon_category.py | 26 +++++++++++++++++++ src/character/migrations/max_migration.txt | 2 +- src/character/models/character.py | 13 +++++++++- src/character/models/equipment.py | 8 ++++++ src/character/templates/character/view.html | 5 +++- .../templatetags/character_extras.py | 13 ++++++++++ 8 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/character/migrations/0019_weapon_category.py create mode 100644 src/character/migrations/0020_alter_weapon_category.py diff --git a/src/character/admin.py b/src/character/admin.py index e3e6f6e..42c2924 100644 --- a/src/character/admin.py +++ b/src/character/admin.py @@ -145,6 +145,7 @@ class CharacterAdmin(admin.ModelAdmin): "defense", "mana_max", "recovery_points_max", + "luck_points_max", ] filter_horizontal = [ "capabilities", diff --git a/src/character/migrations/0019_weapon_category.py b/src/character/migrations/0019_weapon_category.py new file mode 100644 index 0000000..0a5830c --- /dev/null +++ b/src/character/migrations/0019_weapon_category.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.2 on 2022-10-30 23:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("character", "0018_character_damage_reduction"), + ] + + operations = [ + migrations.AddField( + model_name="weapon", + name="category", + field=models.CharField(default="NON", max_length=3), + ), + ] diff --git a/src/character/migrations/0020_alter_weapon_category.py b/src/character/migrations/0020_alter_weapon_category.py new file mode 100644 index 0000000..3774d0b --- /dev/null +++ b/src/character/migrations/0020_alter_weapon_category.py @@ -0,0 +1,26 @@ +# Generated by Django 4.1.2 on 2022-10-30 23:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("character", "0019_weapon_category"), + ] + + operations = [ + migrations.AlterField( + model_name="weapon", + name="category", + field=models.CharField( + choices=[ + ("MEL", "corps à corps"), + ("RAN", "à distance"), + ("NON", "aucune"), + ], + default="NON", + max_length=3, + ), + ), + ] diff --git a/src/character/migrations/max_migration.txt b/src/character/migrations/max_migration.txt index d49abb0..f5a4bb0 100644 --- a/src/character/migrations/max_migration.txt +++ b/src/character/migrations/max_migration.txt @@ -1 +1 @@ -0018_character_damage_reduction +0020_alter_weapon_category diff --git a/src/character/models/character.py b/src/character/models/character.py index edb0e98..0f64aa8 100644 --- a/src/character/models/character.py +++ b/src/character/models/character.py @@ -7,6 +7,7 @@ from django_extensions.db.models import TimeStampedModel from character.models import Capability, Path from character.models.dice import Dice +from character.models.equipment import Weapon from common.models import DocumentedModel, UniquelyNamedModel @@ -218,7 +219,9 @@ class Character(models.Model): Profile.MagicalStrength.CHARISMA: self.modifier_charisma, Profile.MagicalStrength.NONE: 0, } - return modifier_map.get(Profile.MagicalStrength(self.profile.magical_strength)) + return modifier_map.get( + Profile.MagicalStrength(self.profile.magical_strength), 0 + ) @property def defense(self) -> int: @@ -252,6 +255,14 @@ class Character(models.Model): def luck_points_max(self) -> int: return 3 + self.modifier_charisma + def get_modifier_for_weapon(self, weapon: Weapon) -> int: + modifier_map = { + Weapon.Category.RANGE: self.modifier_dexterity, + Weapon.Category.MELEE: self.modifier_strength, + Weapon.Category.NONE: 0, + } + return modifier_map.get(Weapon.Category(weapon.category), 0) + def get_capabilities_by_path(self) -> dict[Path, list[Capability]]: capabilities_by_path = collections.defaultdict(list) for capability in self.capabilities.all(): diff --git a/src/character/models/equipment.py b/src/character/models/equipment.py index 587f1b3..a9d6bfc 100644 --- a/src/character/models/equipment.py +++ b/src/character/models/equipment.py @@ -5,8 +5,16 @@ from common.models import UniquelyNamedModel class Weapon(UniquelyNamedModel, TimeStampedModel, models.Model): + class Category(models.TextChoices): + MELEE = "MEL", "corps à corps" + RANGE = "RAN", "à distance" + NONE = "NON", "aucune" + damage = models.CharField(max_length=50, blank=True, verbose_name="dégâts") special = models.TextField(blank=True, verbose_name="spécial") + category = models.CharField( + max_length=3, choices=Category.choices, default=Category.NONE + ) class Meta: verbose_name = "Arme" diff --git a/src/character/templates/character/view.html b/src/character/templates/character/view.html index 94c3e00..25da45f 100644 --- a/src/character/templates/character/view.html +++ b/src/character/templates/character/view.html @@ -303,7 +303,10 @@ {% for weapon in character.weapons.all %} {{ weapon.name }} - 1D20 + + + 1D20 + {{ character|weapon_modifier:weapon }} + {{ weapon.damage }} {{ weapon.special }} diff --git a/src/character/templatetags/character_extras.py b/src/character/templatetags/character_extras.py index ccca227..81ccc6f 100644 --- a/src/character/templatetags/character_extras.py +++ b/src/character/templatetags/character_extras.py @@ -1,5 +1,7 @@ from django import template +from character.models import Character, Weapon + register = template.Library() @@ -14,3 +16,14 @@ def modifier(value): @register.filter def sub(value, arg): return value - arg + + +@register.filter +def weapon_modifier(character: Character, weapon: Weapon): + value = character.get_modifier_for_weapon(weapon) + if value > 0: + return f"+ {value}" + elif value < 0: + return f"- {abs(value)}" + else: + return ""