2022-06-05 13:09:27 +03:00
|
|
|
import random
|
2022-06-04 16:01:23 +03:00
|
|
|
import uuid
|
|
|
|
|
2022-06-05 13:09:27 +03:00
|
|
|
from django.core.validators import (
|
|
|
|
MinValueValidator,
|
|
|
|
MaxValueValidator,
|
|
|
|
MinLengthValidator,
|
|
|
|
MaxLengthValidator,
|
|
|
|
)
|
2022-06-04 16:01:23 +03:00
|
|
|
from django.db import models
|
|
|
|
|
2022-06-10 23:33:40 +03:00
|
|
|
from common.generators import generate_charset
|
2022-06-25 12:36:47 +03:00
|
|
|
from game.services.jwt import sign_jwt
|
2022-06-10 23:33:40 +03:00
|
|
|
|
2022-07-02 00:05:05 +03:00
|
|
|
|
|
|
|
class HeroTypes(models.TextChoices):
|
|
|
|
archer = "ARCHER", "archer"
|
|
|
|
warrior = "WARRIOR", "warrior"
|
2022-07-07 23:54:03 +03:00
|
|
|
wizard = "WIZARD", "wizard"
|
2022-07-02 00:05:05 +03:00
|
|
|
king = "KING", "king"
|
2022-06-04 16:01:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Player(models.Model):
|
|
|
|
"""base model to handle and store users"""
|
|
|
|
|
2022-06-05 13:09:27 +03:00
|
|
|
ton_wallet = models.CharField(
|
|
|
|
verbose_name="TON wallet",
|
|
|
|
validators=[MinLengthValidator(48), MaxLengthValidator(48)],
|
|
|
|
max_length=48,
|
|
|
|
unique=True,
|
|
|
|
)
|
2022-06-04 16:01:23 +03:00
|
|
|
name = models.CharField(max_length=100, blank=True)
|
2022-06-05 13:09:27 +03:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
2022-06-08 19:03:38 +03:00
|
|
|
def get_last_deck(self):
|
|
|
|
return Deck.objects.filter(player=self).last()
|
2022-06-04 16:01:23 +03:00
|
|
|
|
2022-06-10 23:33:40 +03:00
|
|
|
def get_auth_session(self):
|
|
|
|
return PlayerAuthSession.objects.get(player=self).jit
|
|
|
|
|
2022-06-25 12:36:47 +03:00
|
|
|
def get_refresh_token(self):
|
|
|
|
return sign_jwt({"jit": self.get_auth_session(), "type": "refresh"})
|
|
|
|
|
|
|
|
def get_access_token(self):
|
|
|
|
return sign_jwt({"id": self.id, "type": "access"}, t_life=3600)
|
|
|
|
|
2022-06-04 16:01:23 +03:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
indexes = [models.Index(fields=["ton_wallet"])]
|
2022-06-05 13:09:27 +03:00
|
|
|
ordering = ["-created"]
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
db_table = "player"
|
2022-06-04 16:01:23 +03:00
|
|
|
verbose_name = "player"
|
|
|
|
verbose_name_plural = "players"
|
|
|
|
|
|
|
|
|
|
|
|
class Hero(models.Model):
|
|
|
|
"""Model to store heroes and their stats, connected to player"""
|
|
|
|
|
2022-06-06 20:15:45 +03:00
|
|
|
uuid = models.UUIDField(
|
|
|
|
default=uuid.uuid4, editable=False, unique=True, primary_key=True
|
|
|
|
)
|
2022-06-05 13:09:27 +03:00
|
|
|
player = models.ForeignKey(
|
|
|
|
Player,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="heroes",
|
|
|
|
related_query_name="hero",
|
|
|
|
)
|
2022-06-04 16:01:23 +03:00
|
|
|
added = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
2022-07-02 00:05:05 +03:00
|
|
|
type = models.CharField(blank=False, choices=HeroTypes.choices, max_length=7)
|
2022-07-02 16:57:02 +03:00
|
|
|
model_f = models.ForeignKey("HeroModelSet", on_delete=models.CASCADE)
|
2022-06-04 16:01:23 +03:00
|
|
|
health = models.IntegerField(
|
2022-06-21 11:36:55 +03:00
|
|
|
validators=[MinValueValidator(1), MaxValueValidator(10)], blank=False
|
2022-06-04 16:01:23 +03:00
|
|
|
)
|
2022-06-05 13:09:27 +03:00
|
|
|
attack = models.IntegerField(
|
2022-06-21 11:36:55 +03:00
|
|
|
validators=[MinValueValidator(1), MaxValueValidator(10)], blank=False
|
2022-06-05 13:09:27 +03:00
|
|
|
)
|
2022-06-04 16:01:23 +03:00
|
|
|
speed = models.IntegerField(
|
2022-06-21 11:36:55 +03:00
|
|
|
validators=[MinValueValidator(1), MaxValueValidator(10)], blank=False
|
2022-06-04 16:01:23 +03:00
|
|
|
)
|
|
|
|
|
2022-07-02 16:57:02 +03:00
|
|
|
def model(self):
|
|
|
|
return self.model_f.model.url
|
2022-06-11 23:34:55 +03:00
|
|
|
|
2022-06-04 16:01:23 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.type} {self.player.name}"
|
|
|
|
|
2022-06-11 23:34:55 +03:00
|
|
|
def save(
|
|
|
|
self, force_insert=False, force_update=False, using=None, update_fields=None
|
|
|
|
):
|
2022-07-02 23:51:15 +03:00
|
|
|
self.model_f = random.choice(HeroModelSet.objects.filter(hero_type=self.type))
|
2022-07-08 01:16:41 +03:00
|
|
|
super(Hero, self).save(force_insert, force_update, using, update_fields)
|
2022-06-11 23:34:55 +03:00
|
|
|
|
2022-06-04 16:01:23 +03:00
|
|
|
class Meta:
|
|
|
|
indexes = [models.Index(fields=["uuid"])]
|
|
|
|
ordering = ["-added"]
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
db_table = "hero"
|
2022-06-04 16:01:23 +03:00
|
|
|
verbose_name = "hero"
|
|
|
|
verbose_name_plural = "heroes"
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
|
2022-07-02 00:05:05 +03:00
|
|
|
class HeroModelSet(models.Model):
|
|
|
|
hero_type = models.CharField(blank=False, choices=HeroTypes.choices, max_length=7)
|
2022-07-02 16:57:02 +03:00
|
|
|
model = models.FileField(upload_to="uploads/")
|
2022-06-11 23:34:55 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
2022-07-02 00:05:05 +03:00
|
|
|
return f"{self.hero_type} model file"
|
2022-06-11 23:34:55 +03:00
|
|
|
|
|
|
|
|
2022-06-06 20:15:45 +03:00
|
|
|
class Deck(models.Model):
|
|
|
|
player = models.ForeignKey(
|
|
|
|
Player,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="decks",
|
|
|
|
related_query_name="deck",
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-06-07 23:54:26 +03:00
|
|
|
return f"{self.player.name}'s deck"
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
def get_heroes(self):
|
2022-07-07 23:54:03 +03:00
|
|
|
return HeroInDeck.objects.filter(deck=self)
|
2022-06-06 20:15:45 +03:00
|
|
|
|
2022-06-07 23:54:26 +03:00
|
|
|
def heroes(self):
|
|
|
|
return self.get_heroes()
|
|
|
|
|
2022-06-21 20:26:32 +03:00
|
|
|
def score(self):
|
|
|
|
return sum([x.attack + x.health + x.speed for x in self.get_heroes()])
|
|
|
|
|
2022-06-06 20:15:45 +03:00
|
|
|
class Meta:
|
|
|
|
db_table = "deck"
|
|
|
|
verbose_name = "deck"
|
|
|
|
verbose_name_plural = "decks"
|
|
|
|
|
|
|
|
|
|
|
|
class HeroInDeck(models.Model):
|
|
|
|
deck = models.ForeignKey(
|
|
|
|
Deck,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="hero_in_deck",
|
|
|
|
related_query_name="heroes",
|
|
|
|
)
|
|
|
|
hero = models.OneToOneField(
|
|
|
|
Hero,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="hero_in_deck",
|
|
|
|
related_query_name="decks",
|
|
|
|
)
|
2022-07-07 23:54:03 +03:00
|
|
|
x = models.IntegerField(
|
|
|
|
blank=False, validators=[MinValueValidator(1), MaxValueValidator(8)]
|
|
|
|
)
|
|
|
|
y = models.IntegerField(
|
|
|
|
blank=False, validators=[MinValueValidator(1), MaxValueValidator(2)]
|
|
|
|
)
|
2022-06-06 20:15:45 +03:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = "hero_in_deck"
|
|
|
|
verbose_name = "Hero in deck"
|
|
|
|
verbose_name_plural = "Heroes in decks"
|
2022-06-10 23:33:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PlayerAuthSession(models.Model):
|
|
|
|
player = models.OneToOneField(
|
|
|
|
Player, unique_for_month=True, on_delete=models.CASCADE
|
|
|
|
)
|
2022-06-22 00:11:52 +03:00
|
|
|
jit = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
def save(
|
|
|
|
self, force_insert=False, force_update=False, using=None, update_fields=None
|
|
|
|
):
|
|
|
|
self.jit = generate_charset(30)
|
|
|
|
super(PlayerAuthSession, self).save(
|
|
|
|
force_insert=force_insert,
|
|
|
|
force_update=force_update,
|
|
|
|
using=using,
|
|
|
|
update_fields=update_fields,
|
|
|
|
)
|