guild-journal/guild/models.py

201 lines
5.6 KiB
Python

from django.db import models
from django.urls import reverse
from django.utils.translation import gettext as _
class Player(models.Model):
name = models.CharField(max_length=255, unique=True)
address = models.TextField(_("address"), blank=True)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
class Meta:
verbose_name = _("player")
verbose_name_plural = _("players")
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("guild:player_detail", kwargs={"pk": self.pk})
class Resource(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.TextField()
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
class Meta:
verbose_name = _("resource")
verbose_name_plural = _("resources")
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("guild:resource_detail", kwargs={"pk": self.pk})
class Adventure(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
master = models.ForeignKey(
"Player",
verbose_name=_("master"),
on_delete=models.SET_NULL,
null=True,
blank=True,
)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
@property
def last_session(self):
return self.playsession_set.order_by("-date").first()
class Meta:
verbose_name = _("Adventure")
verbose_name_plural = _("Adventures")
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("guild:adventure_detail", kwargs={"pk": self.pk})
class Character(models.Model):
class Status(models.TextChoices):
ALIVE = "ALIVE", _("Alive")
DEAD = "DEAD", _("Dead")
RETIRED = "RETIRED", _("Retired")
UNKNOWN = "UNKNOWN", _("Unknown")
name = models.CharField(max_length=255)
description = models.TextField()
status = models.CharField(
max_length=16, choices=Status.choices, default=Status.ALIVE
)
player = models.ForeignKey(
"Player",
verbose_name=_("player"),
on_delete=models.SET_NULL,
null=True,
blank=True,
)
picture = models.ImageField(
_("Character Picture"), null=True, blank=True, upload_to="uploads/"
)
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
class Meta:
verbose_name = _("character")
verbose_name_plural = _("characters")
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("guild:character_detail", kwargs={"pk": self.pk})
class PlaySession(models.Model):
date = models.DateField()
adventure = models.ForeignKey(
"Adventure", verbose_name=_("adventure"), on_delete=models.CASCADE
)
characters = models.ManyToManyField("Character", verbose_name=_("characters"))
summary = models.TextField()
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
class Meta:
verbose_name = _("playsession")
verbose_name_plural = _("playsessions")
ordering = ["-date"]
def __str__(self):
return str(self.date)
def get_absolute_url(self):
return (
reverse("guild:adventure_detail", kwargs={"pk": self.adventure.pk})
+ "#playsession"
+ str(self.pk)
)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
PlaySessionSummaryVersion.objects.create(
playsession=self,
summary=self.summary,
)
class PlaySessionSummaryVersion(models.Model):
playsession = models.ForeignKey(
"PlaySession",
verbose_name=_("playsession"),
on_delete=models.SET_NULL,
null=True,
)
summary = models.TextField()
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
class Meta:
verbose_name = _("playsession summary version")
verbose_name_plural = _("playsession summary versions")
ordering = ["-created_at"]
def __str__(self):
return f"{self.playsession} - {self.created_at}"
def get_absolute_url(self):
return reverse("guild:playsession_detail", kwargs={"pk": self.playsession.pk})
class Reward(models.Model):
resource = models.ForeignKey(
"Resource", verbose_name=_("resource"), on_delete=models.CASCADE
)
character = models.ForeignKey(
"Character",
verbose_name=_("character"),
on_delete=models.SET_NULL,
null=True,
blank=True,
)
playsession = models.ForeignKey(
"PlaySession",
verbose_name=_("playsession"),
on_delete=models.CASCADE,
null=False,
)
amount = models.IntegerField()
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(_("updated at"), auto_now=True)
class Meta:
verbose_name = _("reward")
verbose_name_plural = _("rewards")
def __str__(self):
return f"{self.resource.name} ({self.amount}) - {self.character}"
def get_absolute_url(self):
return reverse("guild:reward_detail", kwargs={"pk": self.pk})