fixed player model, added board state

This commit is contained in:
Alexander Karpov 2022-08-01 18:22:11 +03:00
parent c8e3b049f5
commit 6c16ff008d
4 changed files with 46 additions and 7 deletions

View File

@ -168,7 +168,7 @@ class HeroInDeck(models.Model):
verbose_name = "Hero in deck" verbose_name = "Hero in deck"
verbose_name_plural = "Heroes in decks" verbose_name_plural = "Heroes in decks"
ordering = ["y", "x"] ordering = ["y", "x"]
unique_together = ["deck", "x", "y"] unique_together = ["hero", "x", "y"]
class PlayerAuthSession(models.Model): class PlayerAuthSession(models.Model):

View File

@ -234,6 +234,10 @@ class RoomConsumer(BaseConsumer):
# Join room group # Join room group
await self.channel_layer.group_add(self.room_group_name, self.channel_name) await self.channel_layer.group_add(self.room_group_name, self.channel_name)
# load and send board
await self.load_board()
await self.send_board()
@sync_to_async @sync_to_async
def get_state(self): def get_state(self):
state = self.scope["player_in_room"].get_state() state = self.scope["player_in_room"].get_state()
@ -250,7 +254,7 @@ class RoomConsumer(BaseConsumer):
if not room: if not room:
return False return False
self.scope["room"] = room self.scope["room"] = room.first()
# check if player can be in a room # check if player can be in a room
p_ids = [x.player.id for x in room.first().players.all()] p_ids = [x.player.id for x in room.first().players.all()]
@ -264,6 +268,7 @@ class RoomConsumer(BaseConsumer):
self.scope["first"] = player.first self.scope["first"] = player.first
self.scope["score"] = player.score self.scope["score"] = player.score
self.scope["deck"] = player.deck.id self.scope["deck"] = player.deck.id
self.scope["state"] = room.first().states.last().round
p_ids.remove(player.player.id) p_ids.remove(player.player.id)
opponent = PlayerInRoom.objects.get(player_id=p_ids[0]) opponent = PlayerInRoom.objects.get(player_id=p_ids[0])
@ -332,14 +337,15 @@ class RoomConsumer(BaseConsumer):
return False return False
async def perform_move(self, data): async def perform_move(self, data):
await move_handler( if await move_handler(
data["px"], data["px"],
data["py"], data["py"],
data["x"], data["x"],
data["y"], data["y"],
self.room_name, self.room_name,
self.scope["player_in_room"], self.scope["player_in_room"],
) ):
await self.send_board()
if self.scope["opponent_channel"] and self.scope["opponent_online"]: if self.scope["opponent_channel"] and self.scope["opponent_online"]:
await self.channel_layer.send( await self.channel_layer.send(
self.scope["opponent_channel"], self.scope["opponent_channel"],
@ -354,6 +360,33 @@ class RoomConsumer(BaseConsumer):
return True return True
return False return False
@sync_to_async
def load_board(self):
# loads bord from db to scope
room = self.scope["room"]
board = [
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
]
for el in room.heroes.all():
board[el.y - 1][el.x - 1] = [el.hero.type, el.health]
self.scope["board"] = board
async def send_board(self):
# sends board to client
await self.send_message(
"INFO",
message=f"game's board for round {self.scope['state']}",
board=self.scope["board"],
)
# info type group message handler # info type group message handler
async def info(self, event): async def info(self, event):
message = event["message"] message = event["message"]

View File

@ -42,7 +42,7 @@ class PlayerInRoom(models.Model):
class GameState(models.Model): class GameState(models.Model):
room = models.ForeignKey(Room, on_delete=models.CASCADE) room = models.ForeignKey(Room, related_name="states", on_delete=models.CASCADE)
player = models.ForeignKey(Player, on_delete=models.CASCADE) player = models.ForeignKey(Player, on_delete=models.CASCADE)
round = models.IntegerField(blank=False) round = models.IntegerField(blank=False)
message = models.CharField(max_length=100, blank=False) message = models.CharField(max_length=100, blank=False)
@ -54,7 +54,7 @@ class GameState(models.Model):
class HeroInGame(models.Model): class HeroInGame(models.Model):
hero = models.ForeignKey(Hero, on_delete=models.CASCADE) hero = models.ForeignKey(Hero, on_delete=models.CASCADE)
player = models.ForeignKey(PlayerInRoom, on_delete=models.CASCADE) player = models.ForeignKey(PlayerInRoom, on_delete=models.CASCADE)
room = models.ForeignKey(Room, on_delete=models.CASCADE) room = models.ForeignKey(Room, related_name="heroes", on_delete=models.CASCADE)
# state on board # state on board
x = models.IntegerField( x = models.IntegerField(
@ -78,4 +78,4 @@ class HeroInGame(models.Model):
super().save(force_insert, force_update, using, update_fields) super().save(force_insert, force_update, using, update_fields)
class Meta: class Meta:
unique_together = ["x", "y", "room"] unique_together = ["x", "y", "hero"]

View File

@ -101,4 +101,10 @@ def move_handler(
h_t = hero.hero.type h_t = hero.hero.type
if _validate_hero_movement(h_t, prev_x, prev_y, x, y, room, first=player.first):
hero.x = x
hero.y = y
hero.save(update_fields=["x", "y"])
return True
_print_board(room) # TODO: Remove in production _print_board(room) # TODO: Remove in production