feat: add a path without capabilities (closes #28)

This commit is contained in:
Gabriel Augendre 2023-06-19 17:43:27 +02:00
parent 4124b65c7a
commit c667ed54c6
6 changed files with 53 additions and 15 deletions

View file

@ -171,7 +171,7 @@ class CharacterAdmin(admin.ModelAdmin):
}, },
), ),
("Race", {"fields": ["racial_capability"]}), ("Race", {"fields": ["racial_capability"]}),
("Capacités", {"fields": ["capabilities"]}), ("Voies & capacités", {"fields": ["paths", "capabilities"]}),
("Chance", {"fields": [("luck_points_max", "luck_points_remaining")]}), ("Chance", {"fields": [("luck_points_max", "luck_points_remaining")]}),
("Mana", {"fields": [("mana_max", "mana_remaining")]}), ("Mana", {"fields": [("mana_max", "mana_remaining")]}),
( (

View file

@ -0,0 +1,21 @@
# Generated by Django 4.2.2 on 2023-06-19 15:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("character", "0042_alter_pet_damage"),
]
operations = [
migrations.AddField(
model_name="character",
name="paths",
field=models.ManyToManyField(
blank=True,
to="character.path",
verbose_name="voies",
),
),
]

View file

@ -1 +1 @@
0042_alter_pet_damage 0043_character_paths

View file

@ -254,6 +254,7 @@ class Character(models.Model):
blank=True, blank=True,
verbose_name="capacités", verbose_name="capacités",
) )
paths = models.ManyToManyField("character.Path", blank=True, verbose_name="voies")
equipment = models.TextField(blank=True, verbose_name="équipement") equipment = models.TextField(blank=True, verbose_name="équipement")
luck_points_remaining = models.PositiveSmallIntegerField( luck_points_remaining = models.PositiveSmallIntegerField(
@ -428,8 +429,10 @@ class Character(models.Model):
def get_capabilities_by_path(self) -> dict[Path, list[CharacterCapability]]: def get_capabilities_by_path(self) -> dict[Path, list[CharacterCapability]]:
capabilities_by_path = collections.defaultdict(list) capabilities_by_path = collections.defaultdict(list)
character_capabilities = self.capabilities.all() character_capabilities = set(self.capabilities.all())
character_paths = {capability.path for capability in character_capabilities} character_paths = {
capability.path for capability in character_capabilities
} | set(self.paths.all())
for path in character_paths: for path in character_paths:
for capability in path.capabilities.all(): for capability in path.capabilities.all():
capabilities_by_path[capability.path].append( capabilities_by_path[capability.path].append(

View file

@ -103,8 +103,7 @@ def add_path(request, pk: int):
path: Path = form.cleaned_data.get("character_path") or form.cleaned_data.get( path: Path = form.cleaned_data.get("character_path") or form.cleaned_data.get(
"other_path", "other_path",
) )
cap = path.get_next_capability(character) character.paths.add(path)
character.capabilities.add(cap)
context["add_path_form"] = AddPathForm(character) context["add_path_form"] = AddPathForm(character)
else: else:
context["add_path_form"] = form context["add_path_form"] = form
@ -400,8 +399,18 @@ def remove_last_in_path(request, character_pk: int, path_pk: int):
Character.objects.managed_by(request.user), Character.objects.managed_by(request.user),
pk=character_pk, pk=character_pk,
) )
capabilities = character.capabilities.filter(path_id=path_pk).values_list(
"rank",
flat=True,
)
if len(capabilities) == 0:
character.paths.remove(path_pk)
else:
last_rank = max( last_rank = max(
character.capabilities.filter(path_id=path_pk).values_list("rank", flat=True), character.capabilities.filter(path_id=path_pk).values_list(
"rank",
flat=True,
),
) )
cap = Capability.objects.get(path_id=path_pk, rank=last_rank) cap = Capability.objects.get(path_id=path_pk, rank=last_rank)
character.capabilities.remove(cap) character.capabilities.remove(cap)

View file

@ -1,3 +1,4 @@
import logging
import os import os
from pathlib import Path from pathlib import Path
@ -41,12 +42,16 @@ INTERNAL_IPS = [
ALLOWED_HOSTS = env("ALLOWED_HOSTS") ALLOWED_HOSTS = env("ALLOWED_HOSTS")
if DEBUG: if DEBUG:
try:
import socket import socket
hostname = socket.gethostname() hostname = socket.gethostname()
ip = socket.gethostbyname(hostname) ip = socket.gethostbyname(hostname)
INTERNAL_IPS.append(ip) INTERNAL_IPS.append(ip)
ALLOWED_HOSTS.append(ip) ALLOWED_HOSTS.append(ip)
except Exception as e:
logging.info("couldn't setup allowed host in debug: %s", e)
CSRF_TRUSTED_ORIGINS = env("CSRF_TRUSTED_ORIGINS") CSRF_TRUSTED_ORIGINS = env("CSRF_TRUSTED_ORIGINS")