added hero position on deck for initial deck

This commit is contained in:
Alexander Karpov 2022-07-07 23:54:03 +03:00
parent c0dcde9ff3
commit eeb564930d
7 changed files with 86 additions and 33 deletions

View File

@ -15,7 +15,12 @@ $ python3 manage.py loaddata media/dump_data/hero_model_fixture.json
$ docker run -p 6379:6379 -d redis:5
```
### run
### dev run
```shell
$ python3 manage.py runserver 0.0.0.0:8000
```
### prod run
```shell
$ daphne -b 0.0.0.0 -p 8000 chess_backend.asgi:application
```

View File

@ -42,6 +42,14 @@ class ListHeroSerializer(serializers.ModelSerializer):
)
class ListHeroInDeckSerializer(serializers.ModelSerializer):
hero = ListHeroSerializer()
class Meta:
model = HeroInDeck
fields = ("hero", "x", "y")
class CreatePlayerSerializer(serializers.ModelSerializer):
class Meta:
model = Player
@ -102,7 +110,7 @@ class GetPlayerSerializer(serializers.ModelSerializer):
class GetDeckSerializer(serializers.ModelSerializer):
player = GetPlayerSerializer()
heroes = ListHeroSerializer(many=True)
heroes = ListHeroInDeckSerializer(many=True)
class Meta:
model = Deck

View File

@ -2,5 +2,8 @@ from django.apps import AppConfig
class GameConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'game'
default_auto_field = "django.db.models.BigAutoField"
name = "game"
def ready(self):
import game.signals

View File

@ -14,9 +14,9 @@ from game.services.jwt import sign_jwt
class HeroTypes(models.TextChoices):
wizard = "WIZARD", "wizard"
archer = "ARCHER", "archer"
warrior = "WARRIOR", "warrior"
wizard = "WIZARD", "wizard"
king = "KING", "king"
@ -32,32 +32,6 @@ class Player(models.Model):
name = models.CharField(max_length=100, blank=True)
created = models.DateTimeField(auto_now_add=True)
def save(
self, force_insert=False, force_update=False, using=None, update_fields=None
):
"""saves user and creates deck for him with 16 heroes"""
super(Player, self).save()
PlayerAuthSession.objects.create(player=self)
deck = Deck.objects.create(player=self)
types = (
["KING"]
+ ["ARCHER" for _ in range(4)]
+ ["WARRIOR" for _ in range(6)]
+ ["WIZARD" for _ in range(2)]
+ [random.choice(HeroTypes.choices[:3])[0] for _ in range(3)]
)
for t in types:
hero = Hero()
hero.player = self
hero.type = t
hero.health = random.randint(0, 10)
hero.attack = random.randint(0, 10)
hero.speed = random.randint(0, 10)
hero.save()
HeroInDeck.objects.create(deck=deck, hero=hero)
def get_last_deck(self):
return Deck.objects.filter(player=self).last()
@ -149,10 +123,9 @@ class Deck(models.Model):
return f"{self.player.name}'s deck"
def get_heroes(self):
return [x.hero for x in HeroInDeck.objects.filter(deck=self)]
return HeroInDeck.objects.filter(deck=self)
def heroes(self):
# added for better DRF view
return self.get_heroes()
def score(self):
@ -177,6 +150,12 @@ class HeroInDeck(models.Model):
related_name="hero_in_deck",
related_query_name="decks",
)
x = models.IntegerField(
blank=False, validators=[MinValueValidator(1), MaxValueValidator(8)]
)
y = models.IntegerField(
blank=False, validators=[MinValueValidator(1), MaxValueValidator(2)]
)
class Meta:
db_table = "hero_in_deck"

View File

@ -0,0 +1,43 @@
import random
from game.models import Deck, Player, HeroTypes, Hero, HeroInDeck
def create_first_deck(player: Player):
deck = Deck.objects.create(player=player)
positions = [
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
]
types = (
["KING"]
+ ["ARCHER" for _ in range(4)]
+ ["WARRIOR" for _ in range(6)]
+ ["WIZARD" for _ in range(2)]
+ [random.choice(HeroTypes.choices[:2])[0] for _ in range(3)]
)
for t in types:
hero = Hero()
hero.player = player
hero.type = t
# set random position on deck for heroes
if t == "KING":
pos_x = 4
pos_y = 0
positions[0][4] = hero
else:
pos_x = random.randint(0, 7)
pos_y = random.randint(0, 1)
while positions[pos_y][pos_x] is not None:
pos_x = random.randint(0, 7)
pos_y = random.randint(0, 1)
positions[pos_y][pos_x] = hero
hero.health = random.randint(0, 10)
hero.attack = random.randint(0, 10)
hero.speed = random.randint(0, 10)
hero.save()
HeroInDeck.objects.create(deck=deck, hero=hero, x=pos_x + 1, y=pos_y + 1)

12
game/signals.py Normal file
View File

@ -0,0 +1,12 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Deck, Player, PlayerAuthSession
from .services.deck_handler import create_first_deck
@receiver(post_save, sender=Player)
def create_player(sender, instance, created, **kwargs):
if created:
print("bebr")
PlayerAuthSession.objects.create(player=instance)
create_first_deck(instance)

View File

@ -0,0 +1,3 @@
def move_handler(prev_x, prev_y, x, y, room, player):
# TODO: implement move logic + check user identity
pass