Skip to content

Commit

Permalink
tournament: match warning v2
Browse files Browse the repository at this point in the history
  • Loading branch information
evnsh committed Nov 14, 2024
1 parent aec6cd7 commit 89b41d0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 42 deletions.
9 changes: 9 additions & 0 deletions backend/api/consumers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ async def friend_request(self, event):
except Exception as _:
pass

async def upcoming_match(self, event):
try:
await self.send(json.dumps({
"type": "upcoming_match",
"message": event["message"]
}))
except Exception as _:
pass

async def challenge_update(self, event):
try:
await self.send(json.dumps({
Expand Down
62 changes: 22 additions & 40 deletions backend/api/consumers/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,56 +215,38 @@ async def start_next_match(self):

# Warn players of the next match
for player in next_match['players']:
await self.send_prune_message(player['userID'], f"Your next match in tournament '{self.tournament.name}' is starting soon!")
await self.notify_upcoming_match(player['userID'], True)

# Get the next next match
next_next_match = await self.get_next_unstarted_match()
if next_next_match:
for player in next_next_match['players']:
await self.send_prune_message(player['userID'], f"Your match in tournament '{self.tournament.name}' is coming up next. Please be ready!")
await self.notify_upcoming_match(player['userID'], False)

return next_match
finally:
await self.delete_lock(lock_key)

async def send_prune_message(self, user_id, content):
prune_user = await sync_to_async(User.objects.get)(userID="user_ai")
conversation = await self.get_or_create_prune_conversation(user_id)

message = await sync_to_async(conversation.messages.create)(
messageID=generate_id("msg"),
sender=prune_user,
content=content
)

await self.channel_layer.group_send(
f"chat_{user_id}",
{
"type": "conversation_update",
"conversationID": conversation.conversationID,
"sender": get_safe_profile(UserSerializer(prune_user).data, me=False),
"message": MessageSerializer(message).data
}
)
@sync_to_async
def get_or_create_prune_conversation(self, user_id):
user = User.objects.get(userID=user_id)
prune_user = User.objects.get(userID="user_ai")

conversation = Conversation.objects.filter(
participants__userID__in=[user_id, "user_ai"],
conversationType='private_message'
).annotate(participant_count=Count('participants')).filter(participant_count=2).first()

if not conversation:
conversation = Conversation.objects.create(
conversationID=generate_id("conv"),
conversationType='private_message'

async def notify_upcoming_match(self, userID, imminent):
channel_layer = get_channel_layer()
group_name = f"chat_{userID}"

message = "Your match is starting soon!" if imminent else "Your match is coming up next. Please be ready!"

try:
logger.info(f"Sending upcoming match notification to user {userID}. Message: {message}")
await channel_layer.group_send(
group_name,
{
"type": "upcoming_match",
"message": {
"content": message,
}
}
)
conversation.participants.add(user, prune_user)
conversation.save()

return conversation
logger.info(f"Successfully sent upcoming match notification to user {userID}")
except Exception as e:
logger.error(f"Failed to send upcoming match notification. User: {userID}, Error: {str(e)}")

@sync_to_async
def set_match_start_time(self, match_id):
Expand Down
Binary file added frontend/public/images/crab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions frontend/src/components/Notification/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@ const Notification = ({ notifications }) => {
key={notification.id}
className={`${notification.type} ${notification.isVisible ? '' : 'hide'}`}
>
<h4>{notification.type.toUpperCase()}</h4>
<p>{notification.message}</p>
{notification.type === 'crab' ? (
<>
<div className="avatar">
<img src="/images/crab.png" alt="Crab Avatar" />
</div>
<div className="message">
<p>{notification.message}</p>
</div>
</>
) : (
<>
<h4>{notification.type.toUpperCase()}</h4>
<p>{notification.message}</p>
</>
)}
</NotificationContainer>
))}
</NotificationWrapper>
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/components/Notification/styles/Notification.styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,25 @@ export const NotificationContainer = styled.div`
border-left-color: #ffaa00;
background-color: rgba(50, 50, 20, 0.9);
}
&.crab {
border-left-color: #0088ff;
background-color: rgba(20, 20, 50, 0.9);
flex-direction: row;
align-items: center;
.avatar {
margin-right: 16px;
img {
width: 40px;
height: 40px;
border-radius: 50%;
}
}
.message {
flex: 1;
}
}
`;
2 changes: 2 additions & 0 deletions frontend/src/context/ChatContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ export const ChatProvider = ({ children }) => {
} else if (userFrom.status === 'accepted') {
addNotification('info', tRef.current('chat.notifications.friendRequest.accepted', { username: `${userTo.displayName}` }));
};
} else if (response.type === 'upcoming_match') {
addNotification('crab', response.message.content);
} else if (response.type === 'challenge_update') {
const formattedData = {
...response,
Expand Down

0 comments on commit 89b41d0

Please sign in to comment.