From 4f4739fc5199e9a986f83fa8f5ddd2e60a0e65b3 Mon Sep 17 00:00:00 2001 From: Neffi42 <114996380+Neffi42@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:23:39 +0200 Subject: [PATCH 1/2] fix ready button accessible to loser --- django/src/games/tournament.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/django/src/games/tournament.py b/django/src/games/tournament.py index fa76386..6e6364f 100644 --- a/django/src/games/tournament.py +++ b/django/src/games/tournament.py @@ -85,7 +85,6 @@ async def disconnect(self, close_code): and creator == self.user.username ): await self.redis.delete(f"pong_{self.name}_lock") - await self.redis.delete(f"pong_{self.name}_ready") await self.redis.delete(f"pong_{self.name}_creator") await self.redis.delete(f"pong_{self.name}_players") await self.redis.delete(f"pong_{self.name}_usernames") @@ -142,7 +141,9 @@ async def join(self, event): async def lock(self): await self.redis.set(f"pong_{self.name}_lock", 1) await self.redis.copy( - f"pong_{self.name}_players", f"pong_{self.name}_ready_players" + f"pong_{self.name}_players", + f"pong_{self.name}_ready_players", + replace=True, ) await self.send_message(self.GROUP, {"type": "unlock_ready"}) @@ -157,13 +158,11 @@ async def ready(self): await self.redis.lrem( f"pong_{self.name}_ready_players", 1, self.user.username ) - players = await self.get_decoded_list(f"pong_{self.name}_players") - await self.redis.incr(f"pong_{self.name}_ready") - ready_encoded = await self.redis.get(f"pong_{self.name}_ready") - ready = int(ready_encoded.decode("utf-8")) + ready_players = await self.get_decoded_list( + f"pong_{self.name}_ready_players" + ) - if ready >= len(players): - await self.redis.set(f"pong_{self.name}_ready", 0) + if len(ready_players) <= 0: await self.send_message(self.GROUP, {"type": "exec_mix"}) async def exec_mix(self, event): @@ -267,7 +266,9 @@ async def archive(self): continue await self.redis.copy( - f"pong_{self.name}_players", f"pong_{self.name}_ready_players" + f"pong_{self.name}_players", + f"pong_{self.name}_ready_players", + replace=True, ) if broadcast: @@ -299,9 +300,7 @@ async def check_current_games_state(self): ) if len(current_games) <= 0: - await self.channel_layer.group_send( - self.name, {"type": "unlock_ready"} - ) + await self.send_message(self.GROUP, {"type": "unlock_ready"}) async def get_decoded_list(self, name): l = await self.redis.lrange(name, 0, -1) From 157f2ac2de93738d02c184b0284bf9c8f0eeba99 Mon Sep 17 00:00:00 2001 From: Neffi42 <114996380+Neffi42@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:40:06 +0200 Subject: [PATCH 2/2] fix if user is a dick --- django/src/games/tournament.py | 84 +++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/django/src/games/tournament.py b/django/src/games/tournament.py index 6e6364f..659ff45 100644 --- a/django/src/games/tournament.py +++ b/django/src/games/tournament.py @@ -74,6 +74,7 @@ async def connect(self): if lock: await self.archive() await self.check_current_games_state() + await self.send_play_elements() async def disconnect(self, close_code): try: @@ -85,10 +86,12 @@ async def disconnect(self, close_code): and creator == self.user.username ): await self.redis.delete(f"pong_{self.name}_lock") + await self.redis.delete(f"pong_{self.name}_play") await self.redis.delete(f"pong_{self.name}_creator") await self.redis.delete(f"pong_{self.name}_players") await self.redis.delete(f"pong_{self.name}_usernames") await self.redis.delete(f"pong_{self.name}_current_games") + await self.redis.delete(f"pong_{self.name}_player_pairs") await self.redis.delete(f"pong_{self.name}_history") await self.redis.delete(f"pong_{self.name}_history_ids") await self.redis.delete(f"pong_{self.name}_ready_players") @@ -174,50 +177,71 @@ async def exec_mix(self, event): async def mix(self): players = await self.get_decoded_list(f"pong_{self.name}_players") random.shuffle(players) - self.player_pairs = [] - self.uuid_list = [] + player_pairs = [] last_player = None if len(players) % 2 != 0: last_player = players.pop() for i in range(0, len(players), 2): - self.player_pairs.append((players[i], players[i + 1])) - game_id = str(uuid4()) - self.uuid_list.append(game_id) - await self.redis.rpush(f"pong_{self.name}_current_games", game_id) + player_pairs.append((players[i], players[i + 1])) + await self.redis.rpush( + f"pong_{self.name}_current_games", str(uuid4()) + ) if last_player: - self.player_pairs.append((last_player, "-")) + player_pairs.append((last_player, "-")) players.append(last_player) + await self.redis.set( + f"pong_{self.name}_player_pairs", json.dumps(player_pairs) + ) + await self.redis.set(f"pong_{self.name}_play", 1) await self.send_message( self.GROUP, { - "type": "send_player_pairs", - "player_pairs": self.player_pairs, + "type": "send_play_elements", }, ) - await self.send_message(self.CLIENT, {"type": "unlock_play"}) - async def send_player_pairs(self, event): - await self.send_message( - self.CLIENT, - {"type": "player_pairs", "player_pairs": event["player_pairs"]}, - ) + async def send_play_elements(self, event=None): + player_pairs = await self.redis.get(f"pong_{self.name}_player_pairs") + play = await self.redis.get(f"pong_{self.name}_play") + players = await self.get_decoded_list(f"pong_{self.name}_players") + + if player_pairs: + await self.send_message( + self.CLIENT, + { + "type": "player_pairs", + "player_pairs": json.loads(player_pairs), + }, + ) + + if ( + self.user.username in players + and play != None + and int(play.decode("utf-8")) == 1 + ): + await self.send_message(self.CLIENT, {"type": "unlock_play"}) async def play(self): + await self.redis.set(f"pong_{self.name}_play", 0) await self.send_message( self.GROUP, { "type": "send_next_round_id", - "player_pairs": self.player_pairs, - "uuid_list": self.uuid_list, + "player_pairs": json.loads( + await self.redis.get(f"pong_{self.name}_player_pairs") + ), + "uuid_list": await self.get_decoded_list( + f"pong_{self.name}_current_games" + ), }, ) async def send_next_round_id(self, event): - player_pairs: List[Tuple] = event["player_pairs"] + player_pairs: List = event["player_pairs"] uuid_list: List = event["uuid_list"] for i, (player1, player2) in enumerate(player_pairs): @@ -237,12 +261,12 @@ async def archive(self): current_games = await self.get_decoded_list( f"pong_{self.name}_current_games" ) - broadcast = False + one_game_is_over = False for uuid in current_games: try: game = await sync_to_async(pong.objects.get)(uuid=uuid) - broadcast = True + one_game_is_over = True user1 = await sync_to_async(lambda: game.user1.username)() user2 = await sync_to_async(lambda: game.user2.username)() loser = user2 if game.score1 > game.score2 else user1 @@ -265,14 +289,15 @@ async def archive(self): except pong.DoesNotExist: continue - await self.redis.copy( - f"pong_{self.name}_players", - f"pong_{self.name}_ready_players", - replace=True, - ) + if one_game_is_over: + await self.redis.copy( + f"pong_{self.name}_players", + f"pong_{self.name}_ready_players", + replace=True, + ) - if broadcast: - await self.send_message(self.GROUP, {"type": "broadcast_history"}) + if one_game_is_over: + await self.send_message(self.GROUP, {"type": "send_history"}) else: await self.send_history() @@ -280,10 +305,7 @@ async def archive(self): if len(players) < 2: await self.save_tournament_to_db(players[0]) - async def broadcast_history(self, event): - await self.send_history() - - async def send_history(self): + async def send_history(self, event=None): await self.send_message( self.CLIENT, {