Display modifier next to weapon

This commit is contained in:
Gabriel Augendre 2022-10-31 00:20:55 +01:00
parent 4ee558b12d
commit 3846778d00
8 changed files with 83 additions and 3 deletions

View file

@ -145,6 +145,7 @@ class CharacterAdmin(admin.ModelAdmin):
"defense", "defense",
"mana_max", "mana_max",
"recovery_points_max", "recovery_points_max",
"luck_points_max",
] ]
filter_horizontal = [ filter_horizontal = [
"capabilities", "capabilities",

View file

@ -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),
),
]

View file

@ -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,
),
),
]

View file

@ -1 +1 @@
0018_character_damage_reduction 0020_alter_weapon_category

View file

@ -7,6 +7,7 @@ from django_extensions.db.models import TimeStampedModel
from character.models import Capability, Path from character.models import Capability, Path
from character.models.dice import Dice from character.models.dice import Dice
from character.models.equipment import Weapon
from common.models import DocumentedModel, UniquelyNamedModel from common.models import DocumentedModel, UniquelyNamedModel
@ -218,7 +219,9 @@ class Character(models.Model):
Profile.MagicalStrength.CHARISMA: self.modifier_charisma, Profile.MagicalStrength.CHARISMA: self.modifier_charisma,
Profile.MagicalStrength.NONE: 0, 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 @property
def defense(self) -> int: def defense(self) -> int:
@ -252,6 +255,14 @@ class Character(models.Model):
def luck_points_max(self) -> int: def luck_points_max(self) -> int:
return 3 + self.modifier_charisma 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]]: def get_capabilities_by_path(self) -> dict[Path, list[Capability]]:
capabilities_by_path = collections.defaultdict(list) capabilities_by_path = collections.defaultdict(list)
for capability in self.capabilities.all(): for capability in self.capabilities.all():

View file

@ -5,8 +5,16 @@ from common.models import UniquelyNamedModel
class Weapon(UniquelyNamedModel, TimeStampedModel, models.Model): 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") damage = models.CharField(max_length=50, blank=True, verbose_name="dégâts")
special = models.TextField(blank=True, verbose_name="spécial") special = models.TextField(blank=True, verbose_name="spécial")
category = models.CharField(
max_length=3, choices=Category.choices, default=Category.NONE
)
class Meta: class Meta:
verbose_name = "Arme" verbose_name = "Arme"

View file

@ -303,7 +303,10 @@
{% for weapon in character.weapons.all %} {% for weapon in character.weapons.all %}
<tr> <tr>
<th scope="row">{{ weapon.name }}</th> <th scope="row">{{ weapon.name }}</th>
<td>1D20 +</td> <td>
1D20
{{ character|weapon_modifier:weapon }}
</td>
<td>{{ weapon.damage }}</td> <td>{{ weapon.damage }}</td>
<td>{{ weapon.special }}</td> <td>{{ weapon.special }}</td>
</tr> </tr>

View file

@ -1,5 +1,7 @@
from django import template from django import template
from character.models import Character, Weapon
register = template.Library() register = template.Library()
@ -14,3 +16,14 @@ def modifier(value):
@register.filter @register.filter
def sub(value, arg): def sub(value, arg):
return 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 ""