Skip to content

Commit

Permalink
Merge pull request #325 from TheSecretOrganization/notif-tournament
Browse files Browse the repository at this point in the history
notif tournament
  • Loading branch information
Neffi42 authored Oct 21, 2024
2 parents a78b0d1 + dcc05b2 commit 42bb238
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
8 changes: 4 additions & 4 deletions django/src/chat/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def connect(self):

if self.scope['url_route']['kwargs'].get('username'):
await self.notify_other_user(other_username)

async def disconnect(self, close_code):
await self.channel_layer.group_discard(f'user_{self.username}', self.channel_name)
await self.remove_active_user()
Expand Down Expand Up @@ -53,7 +53,7 @@ async def receive(self, text_data):
'username': username,
}
)

async def notify_other_user(self, other_username):
other_user_channel = f'user_{other_username}'
await self.channel_layer.group_send(
Expand All @@ -63,8 +63,8 @@ async def notify_other_user(self, other_username):
'from_user': self.username,
}
)
async def send_chat_history(self):

async def send_chat_history(self, event=None):
messages = await self.redis.lrange(f'chat_{self.room_group_name}_messages', 0, -1)
history = [json.loads(message) for message in messages]

Expand Down
2 changes: 2 additions & 0 deletions django/src/ft_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def register(request: HttpRequest):
if not data or not all(k in data for k in ['register-username', 'register-password']):
return JsonResponse({'error': _('Missing fields')}, status=400)
try:
if data['register-username'].lower() == "server":
raise IntegrityError()
validate_password(data['register-password'])
get_user_model().objects.create_user(data['register-username'], data['register-password'])
logger.info(f"user '{data['register-username']}' created.")
Expand Down
21 changes: 15 additions & 6 deletions django/src/games/pong.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def connect(self):
await self.send_message(
"client", "game_pad", {"game_pad": self.pad_n, "host": self.host}
)
if self.tournament_name != "0":
if self.is_tournament_game:
await self.send_message(
"client",
"tournament_name",
Expand All @@ -62,6 +62,14 @@ async def initialize_game(self):
self.tournament_name = self.check_missing_param(
query_params, "tournament_name"
)
self.is_tournament_game = self.tournament_name != "0"
white_list = await self.get_decoded_list(f"pong_{self.room_id}_white_list")

if self.is_tournament_game and self.user.username not in white_list:
raise ConnectionRefusedError(
"User not in white list"
)

self.power = query_params.get("power", ["False"])[0] == "True"
self.host = await self.redis.get(self.room_id) == None
self.pad_n = "pad_1" if self.host else "pad_2"
Expand Down Expand Up @@ -94,18 +102,14 @@ async def disconnect(self, close_code):
self.connected = False

if hasattr(self, "host") and self.host:
await self.channel_layer.group_send(
f"{self.room_id}_watcher",
{"type": "watcher_stop", "id": self.room_id},
)
players = await self.redis.lrange(f"pong_{self.room_id}_id", 0, -1)

if hasattr(self, "game_task"):
self.game_task.cancel()

await self.redis.delete(self.room_id)
await self.redis.delete(f"pong_{self.room_id}_id")
await self.redis.delete(f"{self.room_id}_watcher")
await self.redis.delete(f"pong_{self.room_id}_white_list")
logger.info(f"Room {self.room_id} has been closed by the host.")

if self.mode == "online" and len(players) == 2:
Expand Down Expand Up @@ -357,6 +361,11 @@ def check_missing_param(
else:
return param

async def get_decoded_list(self, name):
l = await self.redis.lrange(name, 0, -1)
decoded_list = [el.decode("utf-8") for el in l]
return decoded_list

class Info:
def __init__(
self,
Expand Down
32 changes: 32 additions & 0 deletions django/src/games/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ async def mix(self):
"type": "send_play_elements",
},
)
await self.send_ids_to_general()

async def send_play_elements(self, event=None):
player_pairs = await self.redis.get(f"pong_{self.name}_player_pairs")
Expand All @@ -225,6 +226,37 @@ async def send_play_elements(self, event=None):
):
await self.send_message(self.CLIENT, {"type": "unlock_play"})

async def send_ids_to_general(self):
group_name = "chat_general"
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"
)

for i, (player1, player2) in enumerate(player_pairs):
message = f"[{self.name}] - {player1} x {player2}"

if player2 != "-":
await self.redis.rpush(f"pong_{uuid_list[i]}_white_list", player1)
await self.redis.rpush(f"pong_{uuid_list[i]}_white_list", player2)
message = f"{message}\n{uuid_list[i]}"

formatted_message = json.dumps(
{"message": message, "username": "server"}
)
await self.redis.rpush(f"chat_{group_name}_messages", formatted_message)

await self.channel_layer.group_add(group_name, self.channel_name)
await self.channel_layer.group_send(
group_name, {"type": "send_chat_history"}
)
await self.channel_layer.group_discard(group_name, self.channel_name)

async def send_chat_history(self, event=None):
return

async def play(self):
await self.redis.set(f"pong_{self.name}_play", 0)
await self.send_message(
Expand Down

0 comments on commit 42bb238

Please sign in to comment.