From eeb564930dbe41c7e3469388b8ece66fd6e82b41 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Thu, 7 Jul 2022 23:54:03 +0300 Subject: [PATCH] added hero position on deck for initial deck --- README.md | 7 +++++- game/api/v1/serializers.py | 10 +++++++- game/apps.py | 7 ++++-- game/models.py | 37 +++++++----------------------- game/services/deck_handler.py | 43 +++++++++++++++++++++++++++++++++++ game/signals.py | 12 ++++++++++ room/services/game_logic.py | 3 +++ 7 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 game/services/deck_handler.py create mode 100644 game/signals.py create mode 100644 room/services/game_logic.py diff --git a/README.md b/README.md index 900bca3..56e1fc9 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/game/api/v1/serializers.py b/game/api/v1/serializers.py index 5010f1b..1cdf3c5 100644 --- a/game/api/v1/serializers.py +++ b/game/api/v1/serializers.py @@ -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 diff --git a/game/apps.py b/game/apps.py index 8ad49cb..25a8f7d 100644 --- a/game/apps.py +++ b/game/apps.py @@ -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 diff --git a/game/models.py b/game/models.py index 9e2c859..2044c6e 100644 --- a/game/models.py +++ b/game/models.py @@ -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" diff --git a/game/services/deck_handler.py b/game/services/deck_handler.py new file mode 100644 index 0000000..8de6dee --- /dev/null +++ b/game/services/deck_handler.py @@ -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) diff --git a/game/signals.py b/game/signals.py new file mode 100644 index 0000000..31f5529 --- /dev/null +++ b/game/signals.py @@ -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) diff --git a/room/services/game_logic.py b/room/services/game_logic.py new file mode 100644 index 0000000..cc474f8 --- /dev/null +++ b/room/services/game_logic.py @@ -0,0 +1,3 @@ +def move_handler(prev_x, prev_y, x, y, room, player): + # TODO: implement move logic + check user identity + pass \ No newline at end of file