2022-11-02 23:10:48 +01:00
|
|
|
from django.db import models
|
2023-01-17 10:55:10 +01:00
|
|
|
from django.db.models import F, Q
|
2022-11-19 10:23:01 +01:00
|
|
|
from django.urls import reverse
|
2022-11-02 23:10:48 +01:00
|
|
|
from django_extensions.db.models import TimeStampedModel
|
|
|
|
|
2022-11-02 23:52:28 +01:00
|
|
|
from character.models import Character
|
|
|
|
from common.models import UniquelyNamedModel, UniquelyNamedModelManager
|
|
|
|
|
|
|
|
|
|
|
|
class PartyQuerySet(models.QuerySet):
|
|
|
|
def managed_by(self, user):
|
|
|
|
return self.filter(game_master=user)
|
|
|
|
|
|
|
|
def played_by(self, user):
|
|
|
|
return self.filter(characters__in=Character.objects.filter(player=user))
|
|
|
|
|
2023-01-16 18:02:14 +01:00
|
|
|
def played_or_mastered_by(self, user):
|
|
|
|
return self.filter(
|
|
|
|
Q(game_master=user)
|
2023-02-28 12:34:45 +01:00
|
|
|
| Q(characters__in=Character.objects.filter(player=user)),
|
2023-01-16 18:02:14 +01:00
|
|
|
).distinct()
|
|
|
|
|
2022-11-03 00:02:28 +01:00
|
|
|
def related_to(self, user):
|
|
|
|
return self.filter(
|
|
|
|
Q(game_master=user)
|
|
|
|
| Q(characters__in=Character.objects.filter(player=user))
|
2023-02-28 12:34:45 +01:00
|
|
|
| Q(invited_characters__in=Character.objects.filter(player=user)),
|
2022-11-03 00:02:28 +01:00
|
|
|
).distinct()
|
|
|
|
|
2022-11-06 15:35:32 +01:00
|
|
|
def invited_to(self, user):
|
|
|
|
return self.filter(invited_characters__in=Character.objects.filter(player=user))
|
|
|
|
|
2022-11-02 23:52:28 +01:00
|
|
|
|
|
|
|
class PartyManager(UniquelyNamedModelManager):
|
|
|
|
pass
|
2022-11-02 23:10:48 +01:00
|
|
|
|
|
|
|
|
2023-02-28 12:34:45 +01:00
|
|
|
class Party(UniquelyNamedModel, TimeStampedModel, models.Model): # noqa: DJ008
|
2022-11-02 23:10:48 +01:00
|
|
|
game_master = models.ForeignKey(
|
|
|
|
"common.User",
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name="parties",
|
|
|
|
verbose_name="meneur de jeu",
|
|
|
|
)
|
2022-11-06 15:19:21 +01:00
|
|
|
invited_characters = models.ManyToManyField(
|
|
|
|
"character.Character",
|
|
|
|
blank=True,
|
|
|
|
related_name="invites",
|
|
|
|
verbose_name="personnages invités",
|
|
|
|
)
|
2022-11-02 23:10:48 +01:00
|
|
|
characters = models.ManyToManyField(
|
|
|
|
"character.Character",
|
|
|
|
blank=True,
|
|
|
|
related_name="parties",
|
|
|
|
verbose_name="personnages",
|
|
|
|
)
|
|
|
|
|
2022-11-02 23:52:28 +01:00
|
|
|
objects = PartyManager.from_queryset(PartyQuerySet)()
|
|
|
|
|
2022-11-02 23:10:48 +01:00
|
|
|
class Meta(UniquelyNamedModel.Meta, TimeStampedModel.Meta):
|
|
|
|
verbose_name = "Groupe"
|
|
|
|
verbose_name_plural = "Groupes"
|
2022-11-19 10:23:01 +01:00
|
|
|
|
|
|
|
def get_absolute_url(self) -> str:
|
|
|
|
return reverse("party:details", kwargs={"pk": self.pk})
|
|
|
|
|
|
|
|
def reset_stats(self):
|
|
|
|
for character in self.characters.all():
|
|
|
|
character.reset_stats()
|
2023-01-16 18:02:14 +01:00
|
|
|
|
|
|
|
|
2023-01-17 10:55:10 +01:00
|
|
|
class BattleEffectQuerySet(models.QuerySet):
|
|
|
|
def increase_rounds(self):
|
|
|
|
self.temporary().update(remaining_rounds=F("remaining_rounds") + 1)
|
|
|
|
|
|
|
|
def decrease_rounds(self):
|
|
|
|
self.active().update(remaining_rounds=F("remaining_rounds") - 1)
|
|
|
|
|
|
|
|
def active(self):
|
|
|
|
return self.filter(remaining_rounds__gt=0)
|
|
|
|
|
|
|
|
def terminated(self):
|
|
|
|
return self.filter(remaining_rounds=0)
|
|
|
|
|
|
|
|
def permanent(self):
|
|
|
|
return self.filter(remaining_rounds=-1)
|
|
|
|
|
|
|
|
def temporary(self):
|
|
|
|
return self.exclude(remaining_rounds=-1)
|
|
|
|
|
|
|
|
|
2023-01-16 18:02:14 +01:00
|
|
|
class BattleEffectManager(models.Manager):
|
2023-01-17 10:55:10 +01:00
|
|
|
pass
|
2023-01-16 18:02:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BattleEffect(TimeStampedModel, models.Model):
|
|
|
|
name = models.CharField(max_length=100, blank=False, null=False, verbose_name="nom")
|
|
|
|
target = models.CharField(
|
2023-02-28 12:34:45 +01:00
|
|
|
max_length=100,
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
verbose_name="cible",
|
2023-01-16 18:02:14 +01:00
|
|
|
)
|
|
|
|
description = models.TextField(blank=True, null=False, verbose_name="description")
|
|
|
|
remaining_rounds = models.SmallIntegerField(
|
|
|
|
blank=False,
|
|
|
|
default=-1,
|
|
|
|
verbose_name="nombre de tours restants",
|
|
|
|
help_text="-1 pour un effet permanent",
|
|
|
|
)
|
|
|
|
party = models.ForeignKey(
|
|
|
|
"party.Party",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="effects",
|
|
|
|
verbose_name="groupe",
|
|
|
|
)
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
"common.User",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="effects",
|
|
|
|
verbose_name="créé par",
|
|
|
|
)
|
|
|
|
|
2023-01-17 10:55:10 +01:00
|
|
|
objects = BattleEffectManager.from_queryset(BattleEffectQuerySet)()
|
2023-01-17 10:07:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def remaining_percent(self) -> float:
|
|
|
|
max_display_percent = 5
|
|
|
|
if self.remaining_rounds >= max_display_percent or self.remaining_rounds < 0:
|
|
|
|
return 100
|
|
|
|
return self.remaining_rounds / max_display_percent * 100
|
2023-02-28 12:34:45 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|