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"]}),
("Capacités", {"fields": ["capabilities"]}),
("Voies & capacités", {"fields": ["paths", "capabilities"]}),
("Chance", {"fields": [("luck_points_max", "luck_points_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,
verbose_name="capacités",
)
paths = models.ManyToManyField("character.Path", blank=True, verbose_name="voies")
equipment = models.TextField(blank=True, verbose_name="équipement")
luck_points_remaining = models.PositiveSmallIntegerField(
@ -428,8 +429,10 @@ class Character(models.Model):
def get_capabilities_by_path(self) -> dict[Path, list[CharacterCapability]]:
capabilities_by_path = collections.defaultdict(list)
character_capabilities = self.capabilities.all()
character_paths = {capability.path for capability in character_capabilities}
character_capabilities = set(self.capabilities.all())
character_paths = {
capability.path for capability in character_capabilities
} | set(self.paths.all())
for path in character_paths:
for capability in path.capabilities.all():
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(
"other_path",
)
cap = path.get_next_capability(character)
character.capabilities.add(cap)
character.paths.add(path)
context["add_path_form"] = AddPathForm(character)
else:
context["add_path_form"] = form
@ -400,11 +399,21 @@ def remove_last_in_path(request, character_pk: int, path_pk: int):
Character.objects.managed_by(request.user),
pk=character_pk,
)
last_rank = max(
character.capabilities.filter(path_id=path_pk).values_list("rank", flat=True),
capabilities = character.capabilities.filter(path_id=path_pk).values_list(
"rank",
flat=True,
)
cap = Capability.objects.get(path_id=path_pk, rank=last_rank)
character.capabilities.remove(cap)
if len(capabilities) == 0:
character.paths.remove(path_pk)
else:
last_rank = max(
character.capabilities.filter(path_id=path_pk).values_list(
"rank",
flat=True,
),
)
cap = Capability.objects.get(path_id=path_pk, rank=last_rank)
character.capabilities.remove(cap)
context = {
"character": character,
"add_path_form": AddPathForm(character),

View file

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