From 7812e982b0325b54ec86769b3b736ce2a5b1e35a Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Fri, 8 Jul 2022 01:16:41 +0300 Subject: [PATCH] updated hero placement for room create --- game/models.py | 2 +- game/signals.py | 3 +-- room/services/room_create.py | 25 +++++++++++++++---------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/game/models.py b/game/models.py index 2044c6e..48dd8a1 100644 --- a/game/models.py +++ b/game/models.py @@ -92,7 +92,7 @@ class Hero(models.Model): self, force_insert=False, force_update=False, using=None, update_fields=None ): self.model_f = random.choice(HeroModelSet.objects.filter(hero_type=self.type)) - super(Hero, self).save() + super(Hero, self).save(force_insert, force_update, using, update_fields) class Meta: indexes = [models.Index(fields=["uuid"])] diff --git a/game/signals.py b/game/signals.py index 31f5529..c3190d9 100644 --- a/game/signals.py +++ b/game/signals.py @@ -1,12 +1,11 @@ from django.db.models.signals import post_save from django.dispatch import receiver -from .models import Deck, Player, PlayerAuthSession +from .models import 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/room_create.py b/room/services/room_create.py index 46e4bfa..9922d43 100644 --- a/room/services/room_create.py +++ b/room/services/room_create.py @@ -2,8 +2,8 @@ from asgiref.sync import sync_to_async from random import randint from common.generators import generate_charset -from game.models import Player -from room.models import Room, PlayerInRoom, GameState +from game.models import Player, Deck +from room.models import Room, PlayerInRoom, GameState, HeroInGame @sync_to_async @@ -21,7 +21,7 @@ def create_room( first_player = randint(1, 2) - PlayerInRoom.objects.create( + p1 = PlayerInRoom.objects.create( player=player_1, room=room, score=player_score_1, @@ -29,17 +29,22 @@ def create_room( first=first_player == 1, ) - PlayerInRoom.objects.create( + p2 = PlayerInRoom.objects.create( player=player_2, room=room, score=player_score_2, deck_id=deck_id_2, first=first_player == 2, ) - GameState.objects.create( - room=room, player=player_1, round=0, message="Game started" - ) - GameState.objects.create( - room=room, player=player_2, round=0, message="Game started" - ) + for p, d_id in [(p1, deck_id_1), (p2, deck_id_2)]: + GameState.objects.create(room=room, player=p, round=0, message="Game started") + for hero in Deck.objects.get(id=d_id).heroes.all(): + if p.first: + HeroInGame.objects.create( + hero=hero, player=p, room=room, x=hero.x, y=hero.y + ) + else: + HeroInGame.objects.create( + hero=hero, player=p, room=room, x=hero.x, y=8 - hero.y + ) return room.slug