2022-07-03 12:30:36 +03:00
|
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
2022-06-11 23:34:55 +03:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
# Create your models here.
|
2022-07-03 12:30:36 +03:00
|
|
|
from game.models import Player, Deck, Hero
|
2022-06-21 11:36:55 +03:00
|
|
|
|
|
|
|
|
2022-06-21 20:26:32 +03:00
|
|
|
class PlayerInQueue(models.Model):
|
2022-06-21 11:36:55 +03:00
|
|
|
# TODO use redis for storing
|
2022-06-22 00:11:52 +03:00
|
|
|
player = models.OneToOneField(Player, unique=True, on_delete=models.CASCADE)
|
2022-06-23 14:45:34 +03:00
|
|
|
channel_name = models.CharField(max_length=50, blank=False)
|
2022-06-24 01:04:35 +03:00
|
|
|
deck = models.ForeignKey(Deck, on_delete=models.CASCADE)
|
|
|
|
score = models.IntegerField()
|
2022-06-21 11:36:55 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.player.name} in que with score {self.score}"
|
2022-06-24 01:04:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Room(models.Model):
|
|
|
|
slug = models.SlugField(max_length=16, unique=True)
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
ended = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"room with slug {self.slug}"
|
|
|
|
|
|
|
|
|
|
|
|
class PlayerInRoom(models.Model):
|
|
|
|
player = models.OneToOneField(Player, unique=True, on_delete=models.CASCADE)
|
|
|
|
room = models.ForeignKey(Room, on_delete=models.CASCADE, related_name="players")
|
|
|
|
first = models.BooleanField()
|
|
|
|
score = models.IntegerField(blank=False)
|
|
|
|
deck = models.ForeignKey(Deck, on_delete=models.CASCADE, related_name="decks")
|
2022-06-26 18:04:09 +03:00
|
|
|
online = models.BooleanField(default=False)
|
2022-06-26 19:00:53 +03:00
|
|
|
channel_name = models.CharField(max_length=50, blank=True, null=True)
|
2022-06-24 01:04:35 +03:00
|
|
|
|
2022-07-02 00:05:05 +03:00
|
|
|
def get_state(self):
|
|
|
|
return GameState.objects.filter(player=self.player, room=self.room).last()
|
|
|
|
|
2022-06-24 01:04:35 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.player.name} in room {self.room.slug}"
|
2022-07-02 00:05:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
class GameState(models.Model):
|
|
|
|
room = models.ForeignKey(Room, on_delete=models.CASCADE)
|
|
|
|
player = models.ForeignKey(Player, on_delete=models.CASCADE)
|
|
|
|
round = models.IntegerField(blank=False)
|
|
|
|
message = models.CharField(max_length=100, blank=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ["room", "player", "round"]
|
2022-07-03 12:30:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
class HeroInGame(models.Model):
|
|
|
|
hero = models.ForeignKey(Hero, on_delete=models.CASCADE)
|
|
|
|
player = models.ForeignKey(PlayerInRoom, on_delete=models.CASCADE)
|
|
|
|
room = models.ForeignKey(Room, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
# state on board
|
|
|
|
x = models.IntegerField(
|
|
|
|
blank=False, validators=[MinValueValidator(1), MaxValueValidator(8)]
|
|
|
|
)
|
|
|
|
y = models.IntegerField(
|
|
|
|
blank=False, validators=[MinValueValidator(1), MaxValueValidator(8)]
|
|
|
|
)
|
|
|
|
health = models.IntegerField(blank=False)
|
|
|
|
dead = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.hero.type} in room {self.room.slug}"
|
|
|
|
|
|
|
|
def save(
|
|
|
|
self, force_insert=False, force_update=False, using=None, update_fields=None
|
|
|
|
):
|
|
|
|
if not self.health and not self.dead:
|
|
|
|
self.health = self.hero.health
|
|
|
|
|
|
|
|
super().save(force_insert, force_update, using, update_fields)
|
2022-07-30 00:00:59 +03:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ["x", "y", "room"]
|