From 82eb52ba05c056d3decd2d937c809d2fdf9436a4 Mon Sep 17 00:00:00 2001 From: Kariyu42 Date: Mon, 28 Oct 2024 15:41:31 +0100 Subject: [PATCH 1/6] Leaderboard: Handled none existing player --- .../src/components/Leaderboard/tools/PodiumPlayer.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/Leaderboard/tools/PodiumPlayer.js b/frontend/src/components/Leaderboard/tools/PodiumPlayer.js index 7af96dd7..708056d9 100644 --- a/frontend/src/components/Leaderboard/tools/PodiumPlayer.js +++ b/frontend/src/components/Leaderboard/tools/PodiumPlayer.js @@ -25,11 +25,13 @@ const PodiumPlayer = ({ player, position, selectedStat }) => { - handleClickUsername(playerName)} - style={{ paddingTop: '10px', fontWeight: 'bold' }} - > - {playerName} +
+ {playerName !== 'N/A' ? ( + handleClickUsername(playerName)}>{playerName} + ) : ( + N/A + )} +
From 08adf352b03c640c70f0f7f3d1edc65009cf0a99 Mon Sep 17 00:00:00 2001 From: Brandon <103316367+okbrandon@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:13:32 +0100 Subject: [PATCH 2/6] api: Fixed Status and Chat consumers connections --- backend/api/consumers.py | 59 ++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/backend/api/consumers.py b/backend/api/consumers.py index 8c9bdd86..58697fb0 100644 --- a/backend/api/consumers.py +++ b/backend/api/consumers.py @@ -14,6 +14,7 @@ from django.db.models import Q, Count from django.utils import timezone +from django.core.cache import cache from .models import Conversation, User, Relationship, Match, UserSettings from .util import generate_id, get_safe_profile, get_user_id_from_token @@ -49,6 +50,10 @@ async def connect(self): self.user = await sync_to_async(User.objects.get)(userID=userID) self.user_group_name = f"status_{self.user.userID}" + connection_count_key = f"status_user_connections_{self.user.userID}" + connection_count = cache.get(connection_count_key, 0) + cache.set(connection_count_key, connection_count + 1, timeout=None) + await self.channel_layer.group_add( self.user_group_name, self.channel_name @@ -59,16 +64,26 @@ async def connect(self): logger.info(f"[{self.__class__.__name__}] User {self.user.username} connected") async def disconnect(self, close_code): + if self.heartbeat_task and not self.heartbeat_task.done(): self.heartbeat_task.cancel() if self.user is not None: + connection_count_key = f"status_user_connections_{self.user.userID}" + connection_count = cache.get(connection_count_key, 0) - 1 + await self.channel_layer.group_discard( self.user_group_name, self.channel_name ) - await self.update_user_status(False, None) - await self.notify_friends_connection(self.user) - logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected") + + if connection_count > 0: + cache.set(connection_count_key, connection_count, timeout=None) + logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected, {connection_count} connections remaining") + else: + cache.delete(connection_count_key) + await self.update_user_status(False, None) + await self.notify_friends_connection(self.user) + logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected") async def receive(self, text_data): try: @@ -195,24 +210,38 @@ async def connect(self): self.user = await sync_to_async(User.objects.get)(userID=userID) self.user_group_name = f"chat_{self.user.userID}" - await self.channel_layer.group_add( - self.user_group_name, - self.channel_name - ) + connection_count_key = f"chat_user_connections_{self.user.userID}" + connection_count = cache.get(connection_count_key, 0) + cache.set(connection_count_key, connection_count + 1, timeout=None) - logger.info(f"[{self.__class__.__name__}] User {self.user.username} connected") - await self.ensure_conversations_exist(self.user) - logger.info(f"[{self.__class__.__name__}] User {self.user.username} conversations ensured") + if connection_count <= 0: + logger.info(f"[{self.__class__.__name__}] User {self.user.username} connected") + await self.channel_layer.group_add( + self.user_group_name, + self.channel_name + ) + + await self.ensure_conversations_exist(self.user) + logger.info(f"[{self.__class__.__name__}] User {self.user.username} conversations ensured") + else: + logger.info(f"[{self.__class__.__name__}] User {self.user.username} reconnected") await self.accept() async def disconnect(self, close_code): if self.user: - await self.channel_layer.group_discard( - self.user_group_name, - self.channel_name - ) + connection_count_key = f"chat_user_connections_{self.user.userID}" + connection_count = cache.get(connection_count_key, 0) - 1 - logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected") + if connection_count > 0: + cache.set(connection_count_key, connection_count, timeout=None) + logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected, {connection_count} connections remaining") + else: + cache.delete(connection_count_key) + await self.channel_layer.group_discard( + self.user_group_name, + self.channel_name + ) + logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected") async def receive(self, text_data): try: From 08bb326b047b7479d5fd17362a35556c4d4baf75 Mon Sep 17 00:00:00 2001 From: Brandon <103316367+okbrandon@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:42:08 +0100 Subject: [PATCH 3/6] api: Fixed Status and Chat consumers again --- backend/api/consumers.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/backend/api/consumers.py b/backend/api/consumers.py index 58697fb0..4bdea2ac 100644 --- a/backend/api/consumers.py +++ b/backend/api/consumers.py @@ -214,15 +214,14 @@ async def connect(self): connection_count = cache.get(connection_count_key, 0) cache.set(connection_count_key, connection_count + 1, timeout=None) - if connection_count <= 0: - logger.info(f"[{self.__class__.__name__}] User {self.user.username} connected") - await self.channel_layer.group_add( - self.user_group_name, - self.channel_name - ) + await self.channel_layer.group_add( + self.user_group_name, + self.channel_name + ) + if connection_count <= 0: await self.ensure_conversations_exist(self.user) - logger.info(f"[{self.__class__.__name__}] User {self.user.username} conversations ensured") + logger.info(f"[{self.__class__.__name__}] User {self.user.username} connected, conversations ensured") else: logger.info(f"[{self.__class__.__name__}] User {self.user.username} reconnected") await self.accept() @@ -232,15 +231,16 @@ async def disconnect(self, close_code): connection_count_key = f"chat_user_connections_{self.user.userID}" connection_count = cache.get(connection_count_key, 0) - 1 + await self.channel_layer.group_discard( + self.user_group_name, + self.channel_name + ) + if connection_count > 0: cache.set(connection_count_key, connection_count, timeout=None) logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected, {connection_count} connections remaining") else: cache.delete(connection_count_key) - await self.channel_layer.group_discard( - self.user_group_name, - self.channel_name - ) logger.info(f"[{self.__class__.__name__}] User {self.user.username} disconnected") async def receive(self, text_data): From 43d77a1e50dabd088ca723465e288b8ef474b636 Mon Sep 17 00:00:00 2001 From: Brandon <103316367+okbrandon@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:59:54 +0100 Subject: [PATCH 4/6] Home: Fixed contributor card styling Cards not aligned horizontally on Firefox --- frontend/src/components/Home/styles/Contributors.styled.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/Home/styles/Contributors.styled.js b/frontend/src/components/Home/styles/Contributors.styled.js index 239eea1e..5dd5f1ea 100644 --- a/frontend/src/components/Home/styles/Contributors.styled.js +++ b/frontend/src/components/Home/styles/Contributors.styled.js @@ -44,12 +44,14 @@ export const CardsContainer = styled.div` align-items: center; width: 90%; max-width: 1200px; + flex-wrap: wrap; `; export const Cards = styled.div` display: grid; - grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); + grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 2rem; + width: 100%; `; export const Card = styled.div` From 5025cb8255595a308a8f9dfe8c5e55a75fa8b523 Mon Sep 17 00:00:00 2001 From: Brandon <103316367+okbrandon@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:29:57 +0100 Subject: [PATCH 5/6] api: Added a robot emoji as bot display name --- backend/api/apps.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/api/apps.py b/backend/api/apps.py index b13a04a1..52898f0b 100644 --- a/backend/api/apps.py +++ b/backend/api/apps.py @@ -44,13 +44,17 @@ def create_ai_account(sender, **kwargs): User.objects.create( userID="user_ai", username='ai', - displayName='Prune', + displayName='Prune 🤖', email='prune@brandoncodes.dev', password='', lang='EN', avatarID=f"data:image/jpeg;base64,{encoded_avatar}", flags=3 ) + else: + ai_account = User.objects.get(userID="user_ai") + ai_account.displayName = 'Prune 🤖' + ai_account.save() settings, _ = UserSettings.objects.get_or_create(userID="user_ai") store_items = StoreItem.objects.all() From 470b58dcda39f15a90da1c2560ca42fc90a39600 Mon Sep 17 00:00:00 2001 From: Brandon <103316367+okbrandon@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:31:15 +0100 Subject: [PATCH 6/6] Leaderboard: Fixed Podium, now using displayName --- .../src/components/Leaderboard/tools/PodiumPlayer.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/Leaderboard/tools/PodiumPlayer.js b/frontend/src/components/Leaderboard/tools/PodiumPlayer.js index 708056d9..8a6df506 100644 --- a/frontend/src/components/Leaderboard/tools/PodiumPlayer.js +++ b/frontend/src/components/Leaderboard/tools/PodiumPlayer.js @@ -10,11 +10,12 @@ import { PodiumBase, Badge, } from '../styles/Podium.styled'; +import { formatUserData } from '../../../api/user'; const PodiumPlayer = ({ player, position, selectedStat }) => { const navigate = useNavigate(); const positionClass = position === 0 ? 'second' : position === 1 ? 'first' : 'third'; - const playerName = player?.user.username || 'N/A'; + const formattedPlayer = player ? formatUserData(player.user) : { displayName: 'N/A' }; const playerScore = player?.stats[selectedStat] || 0; const handleClickUsername = (username) => { @@ -23,11 +24,11 @@ const PodiumPlayer = ({ player, position, selectedStat }) => { return ( - +
- {playerName !== 'N/A' ? ( - handleClickUsername(playerName)}>{playerName} + {formattedPlayer.displayName !== 'N/A' ? ( + handleClickUsername(formattedPlayer.username)}>{formattedPlayer.displayName} ) : ( N/A )}