Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions GenOnlineService/Controllers/Friends/SocialController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public async Task AcceptPendingRequest(Int64 target_user_id)
return;
}

HelperFunction_AcceptFriendRequest(source_user_id, target_user_id);
await HelperFunction_AcceptFriendRequest(source_user_id, target_user_id);

UserSession? sourceSession = WebSocketManager.GetDataFromUser(source_user_id);
if (sourceSession != null)
Expand Down Expand Up @@ -319,7 +319,7 @@ public async Task AddFriend(Int64 target_user_id)
if (userData.GetSocialContainer().PendingRequests.Contains(target_user_id))
{
// accept their request
HelperFunction_AcceptFriendRequest(requester_user_id, target_user_id);
await HelperFunction_AcceptFriendRequest(requester_user_id, target_user_id);
}
else
{
Expand Down Expand Up @@ -628,4 +628,4 @@ public async Task Remove_Block(Int64 target_user_id)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
private async Task HelperFunction_AcceptFriendRequest(Int64 source_user_id, Int64 target_user_id)
{
try
{
// target user does NOT need to be signed in
UserSession? sourceData = WebSocketManager.GetDataFromUser(source_user_id);
UserSession? targetData = WebSocketManager.GetDataFromUser(target_user_id);

// remove the request from requestor (online version)
if (sourceData != null)
{
sourceData.GetSocialContainer().PendingRequests.Remove(target_user_id);
}

// remove the request from requestor (db)
await Database.Functions.Auth.RemovePendingFriendRequest(GlobalDatabaseInstance.g_Database, source_user_id, target_user_id);

// Add to both players friends list (online version and db)

// SHARED db (we only have to add this once and it covers both players)
await Database.Functions.Auth.CreateFriendship(GlobalDatabaseInstance.g_Database, source_user_id, target_user_id);

// source player
if (sourceData != null)
{
// sess
sourceData.GetSocialContainer().Friends.Add(target_user_id);
}

// target player
{
// sess
if (targetData != null)
{
targetData.GetSocialContainer().Friends.Add(source_user_id);
}
}

// notify the source player that the target player is online, if they are
if (sourceData != null && targetData != null)
{
WebSocketMessage_Social_FriendStatusChanged friendStatusChangedEvent = new();
friendStatusChangedEvent.msg_id = (int)EWebSocketMessageID.SOCIAL_FRIEND_ONLINE_STATUS_CHANGED;
friendStatusChangedEvent.display_name = targetData.m_strDisplayName;
friendStatusChangedEvent.online = true;
byte[] bytesJSON = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(friendStatusChangedEvent));

sourceData.QueueWebsocketSend(bytesJSON);
}

// notify the target player that the source player accepted their request
if (targetData != null && sourceData != null)
{
WebSocketMessage_Social_FriendRequestAccepted friendRequestAcceptedEvent = new();
friendRequestAcceptedEvent.msg_id = (int)EWebSocketMessageID.SOCIAL_FRIEND_FRIEND_REQUEST_ACCEPTED_BY_TARGET;
friendRequestAcceptedEvent.display_name = sourceData.m_strDisplayName;
byte[] bytesJSON = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(friendRequestAcceptedEvent));

targetData.QueueWebsocketSend(bytesJSON);
}
}
catch (Exception ex)
{
// Log the exception to prevent unobserved task exceptions
Console.WriteLine($"Error in HelperFunction_AcceptFriendRequest: {ex.Message}");
}
}