| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using JetBrains.Annotations; |
| | 5 | | using DCL.Friends.WebApi; |
| | 6 | | using DCL.Interface; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public class FriendsController : MonoBehaviour, IFriendsController |
| | 10 | | { |
| | 11 | | private const bool VERBOSE = false; |
| 12 | 12 | | public static FriendsController i { get; private set; } |
| | 13 | |
|
| | 14 | | public event Action<int> OnTotalFriendsUpdated; |
| 0 | 15 | | public int AllocatedFriendCount => friends.Count(f => f.Value.friendshipStatus == FriendshipStatus.FRIEND); |
| | 16 | |
|
| | 17 | | private void Awake() |
| | 18 | | { |
| 33 | 19 | | i = this; |
| 33 | 20 | | } |
| | 21 | |
|
| 4 | 22 | | public bool IsInitialized { get; private set; } |
| | 23 | |
|
| | 24 | | public int ReceivedRequestCount => |
| 1 | 25 | | friends.Values.Count(status => status.friendshipStatus == FriendshipStatus.REQUESTED_FROM); |
| | 26 | |
|
| 1 | 27 | | public int TotalFriendCount { get; private set; } |
| 1 | 28 | | public int TotalFriendRequestCount => TotalReceivedFriendRequestCount + TotalSentFriendRequestCount; |
| 33 | 29 | | public int TotalReceivedFriendRequestCount { get; private set; } |
| 0 | 30 | | public int TotalSentFriendRequestCount { get; private set; } |
| 0 | 31 | | public int TotalFriendsWithDirectMessagesCount { get; private set; } |
| | 32 | |
|
| 33 | 33 | | public readonly Dictionary<string, UserStatus> friends = new Dictionary<string, UserStatus>(); |
| | 34 | |
|
| | 35 | | public UserStatus GetUserStatus(string userId) |
| | 36 | | { |
| 0 | 37 | | if (!friends.ContainsKey(userId)) |
| 0 | 38 | | return new UserStatus {userId = userId, friendshipStatus = FriendshipStatus.NOT_FRIEND}; |
| | 39 | |
|
| 0 | 40 | | return friends[userId]; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public bool ContainsStatus(string friendId, FriendshipStatus status) |
| | 44 | | { |
| 0 | 45 | | if (!friends.ContainsKey(friendId)) return false; |
| 0 | 46 | | return friends[friendId].friendshipStatus == status; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public event Action<string, UserStatus> OnUpdateUserStatus; |
| | 50 | | public event Action<string, FriendshipAction> OnUpdateFriendship; |
| | 51 | | public event Action<string> OnFriendNotFound; |
| | 52 | | public event Action OnInitialized; |
| | 53 | | public event Action<List<FriendWithDirectMessages>> OnAddFriendsWithDirectMessages; |
| | 54 | | public event Action<int, int> OnTotalFriendRequestUpdated; |
| | 55 | |
|
| | 56 | | public Dictionary<string, UserStatus> GetAllocatedFriends() |
| | 57 | | { |
| 0 | 58 | | return new Dictionary<string, UserStatus>(friends); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public void RejectFriendship(string friendUserId) |
| | 62 | | { |
| 0 | 63 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 64 | | { |
| | 65 | | action = WebInterface.FriendshipAction.REJECTED, |
| | 66 | | userId = friendUserId |
| | 67 | | }); |
| 0 | 68 | | } |
| | 69 | |
|
| 1 | 70 | | public bool IsFriend(string userId) => friends.ContainsKey(userId); |
| | 71 | |
|
| | 72 | | public void RemoveFriend(string friendId) |
| | 73 | | { |
| 0 | 74 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 75 | | { |
| | 76 | | action = WebInterface.FriendshipAction.DELETED, |
| | 77 | | userId = friendId |
| | 78 | | }); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void GetFriends(int limit, int skip) => |
| 0 | 82 | | WebInterface.GetFriends(limit, skip); |
| | 83 | |
|
| | 84 | | public void GetFriends(string usernameOrId, int limit) |
| | 85 | | { |
| 0 | 86 | | WebInterface.GetFriends(usernameOrId, limit); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit, |
| | 90 | | int receivedSkip) |
| | 91 | | { |
| 0 | 92 | | WebInterface.GetFriendRequests(sentLimit, sentSkip, receivedLimit, |
| | 93 | | receivedSkip); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void GetFriendsWithDirectMessages(int limit, int skip) |
| | 97 | | { |
| 0 | 98 | | WebInterface.GetFriendsWithDirectMessages("", limit, skip); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public void GetFriendsWithDirectMessages(string userNameOrId, int limit) |
| | 102 | | { |
| 0 | 103 | | WebInterface.GetFriendsWithDirectMessages(userNameOrId, limit, 0); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public void RequestFriendship(string friendUserId) |
| | 107 | | { |
| 0 | 108 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 109 | | { |
| | 110 | | userId = friendUserId, |
| | 111 | | action = WebInterface.FriendshipAction.REQUESTED_TO |
| | 112 | | }); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public void CancelRequest(string friendUserId) |
| | 116 | | { |
| 0 | 117 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 118 | | { |
| | 119 | | userId = friendUserId, |
| | 120 | | action = WebInterface.FriendshipAction.CANCELLED |
| | 121 | | }); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public void AcceptFriendship(string friendUserId) |
| | 125 | | { |
| 0 | 126 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 127 | | { |
| | 128 | | userId = friendUserId, |
| | 129 | | action = WebInterface.FriendshipAction.APPROVED |
| | 130 | | }); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | // called by kernel |
| | 134 | | [UsedImplicitly] |
| | 135 | | public void FriendNotFound(string name) |
| | 136 | | { |
| 0 | 137 | | OnFriendNotFound?.Invoke(name); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | // called by kernel |
| | 141 | | [UsedImplicitly] |
| | 142 | | public void InitializeFriends(string json) |
| | 143 | | { |
| 0 | 144 | | if (IsInitialized) |
| 0 | 145 | | return; |
| | 146 | |
|
| 0 | 147 | | IsInitialized = true; |
| | 148 | |
|
| 0 | 149 | | var msg = JsonUtility.FromJson<FriendshipInitializationMessage>(json); |
| | 150 | |
|
| 0 | 151 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 0 | 152 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 0 | 153 | | OnInitialized?.Invoke(); |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | // called by kernel |
| | 157 | | [UsedImplicitly] |
| | 158 | | public void AddFriends(string json) |
| | 159 | | { |
| 0 | 160 | | var msg = JsonUtility.FromJson<AddFriendsPayload>(json); |
| | 161 | |
|
| 0 | 162 | | TotalFriendCount = msg.totalFriends; |
| 0 | 163 | | OnTotalFriendsUpdated?.Invoke(TotalFriendCount); |
| | 164 | |
|
| 0 | 165 | | AddFriends(msg.friends); |
| 0 | 166 | | } |
| | 167 | |
|
| | 168 | | // called by kernel |
| | 169 | | [UsedImplicitly] |
| | 170 | | public void AddFriendRequests(string json) |
| | 171 | | { |
| 0 | 172 | | var msg = JsonUtility.FromJson<AddFriendRequestsPayload>(json); |
| | 173 | |
|
| 0 | 174 | | TotalReceivedFriendRequestCount = msg.totalReceivedFriendRequests; |
| 0 | 175 | | TotalSentFriendRequestCount = msg.totalSentFriendRequests; |
| 0 | 176 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 177 | |
|
| 0 | 178 | | foreach (var userId in msg.requestedFrom) |
| | 179 | | { |
| 0 | 180 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 181 | | {action = FriendshipAction.REQUESTED_FROM, userId = userId}); |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | foreach (var userId in msg.requestedTo) |
| | 185 | | { |
| 0 | 186 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 187 | | {action = FriendshipAction.REQUESTED_TO, userId = userId}); |
| | 188 | | } |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | // called by kernel |
| | 192 | | [UsedImplicitly] |
| | 193 | | public void AddFriendsWithDirectMessages(string json) |
| | 194 | | { |
| 0 | 195 | | var friendsWithDMs = JsonUtility.FromJson<AddFriendsWithDirectMessagesPayload>(json); |
| 0 | 196 | | TotalFriendsWithDirectMessagesCount = friendsWithDMs.totalFriendsWithDirectMessages; |
| | 197 | |
|
| 0 | 198 | | AddFriends(friendsWithDMs.currentFriendsWithDirectMessages.Select(messages => messages.userId)); |
| | 199 | |
|
| 0 | 200 | | OnAddFriendsWithDirectMessages?.Invoke(friendsWithDMs.currentFriendsWithDirectMessages.ToList()); |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | // called by kernel |
| | 204 | | [UsedImplicitly] |
| | 205 | | public void UpdateUserPresence(string json) |
| | 206 | | { |
| 0 | 207 | | UserStatus newUserStatus = JsonUtility.FromJson<UserStatus>(json); |
| | 208 | |
|
| 0 | 209 | | if (!friends.ContainsKey(newUserStatus.userId)) |
| 0 | 210 | | return; |
| | 211 | |
|
| | 212 | | // Kernel doesn't send the friendship status on this call, we have to keep it or it gets defaulted |
| 0 | 213 | | newUserStatus.friendshipStatus = friends[newUserStatus.userId].friendshipStatus; |
| | 214 | |
|
| 0 | 215 | | UpdateUserStatus(newUserStatus); |
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | // called by kernel |
| | 219 | | [UsedImplicitly] |
| | 220 | | public void UpdateFriendshipStatus(string json) |
| | 221 | | { |
| 6 | 222 | | FriendshipUpdateStatusMessage msg = JsonUtility.FromJson<FriendshipUpdateStatusMessage>(json); |
| 6 | 223 | | UpdateFriendshipStatus(msg); |
| 6 | 224 | | } |
| | 225 | |
|
| | 226 | | // called by kernel |
| | 227 | | [UsedImplicitly] |
| | 228 | | public void UpdateTotalFriendRequests(string json) |
| | 229 | | { |
| 0 | 230 | | var msg = JsonUtility.FromJson<UpdateTotalFriendRequestsPayload>(json); |
| 0 | 231 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 0 | 232 | | TotalSentFriendRequestCount = msg.totalSentRequests; |
| 0 | 233 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | // called by kernel |
| | 237 | | [UsedImplicitly] |
| | 238 | | public void UpdateTotalFriends(string json) |
| | 239 | | { |
| 0 | 240 | | var msg = JsonUtility.FromJson<UpdateTotalFriendsPayload>(json); |
| 0 | 241 | | TotalFriendCount = msg.totalFriends; |
| 0 | 242 | | OnTotalFriendsUpdated?.Invoke(TotalFriendCount); |
| 0 | 243 | | } |
| | 244 | |
|
| | 245 | | private void UpdateUserStatus(UserStatus newUserStatus) |
| | 246 | | { |
| 0 | 247 | | if (!friends.ContainsKey(newUserStatus.userId)) |
| | 248 | | { |
| 0 | 249 | | friends.Add(newUserStatus.userId, newUserStatus); |
| 0 | 250 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| 0 | 251 | | } |
| | 252 | | else |
| | 253 | | { |
| 0 | 254 | | if (!friends[newUserStatus.userId].Equals(newUserStatus)) |
| | 255 | | { |
| 0 | 256 | | friends[newUserStatus.userId] = newUserStatus; |
| 0 | 257 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| | 258 | | } |
| | 259 | | } |
| 0 | 260 | | } |
| | 261 | |
|
| | 262 | | private void UpdateFriendshipStatus(FriendshipUpdateStatusMessage msg) |
| | 263 | | { |
| 6 | 264 | | var friendshipStatus = ToFriendshipStatus(msg.action); |
| 6 | 265 | | var userId = msg.userId; |
| | 266 | |
|
| 6 | 267 | | if (friends.ContainsKey(userId) && friends[userId].friendshipStatus == friendshipStatus) |
| 0 | 268 | | return; |
| | 269 | |
|
| 6 | 270 | | if (!friends.ContainsKey(userId)) |
| 2 | 271 | | friends.Add(userId, new UserStatus { userId = userId }); |
| | 272 | |
|
| 6 | 273 | | if (ItsAnOutdatedUpdate(userId, friendshipStatus)) |
| 0 | 274 | | return; |
| | 275 | |
|
| 6 | 276 | | friends[userId].friendshipStatus = friendshipStatus; |
| | 277 | |
|
| 6 | 278 | | if (friendshipStatus == FriendshipStatus.FRIEND) |
| 2 | 279 | | friends[userId].friendshipStartedTime = DateTime.UtcNow; |
| | 280 | |
|
| | 281 | | if (VERBOSE) |
| | 282 | | Debug.Log($"Change friend status of {userId} to {friends[userId].friendshipStatus}"); |
| | 283 | |
|
| 6 | 284 | | if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| 2 | 285 | | friends.Remove(userId); |
| | 286 | |
|
| 6 | 287 | | OnUpdateFriendship?.Invoke(userId, msg.action); |
| 6 | 288 | | } |
| | 289 | |
|
| | 290 | | private bool ItsAnOutdatedUpdate(string userId, FriendshipStatus friendshipStatus) |
| | 291 | | { |
| 6 | 292 | | return friendshipStatus == FriendshipStatus.REQUESTED_FROM |
| | 293 | | && friends[userId].friendshipStatus == FriendshipStatus.FRIEND |
| | 294 | | && (DateTime.UtcNow - friends[userId].friendshipStartedTime).TotalSeconds < 5; |
| | 295 | | } |
| | 296 | |
|
| | 297 | | private static FriendshipStatus ToFriendshipStatus(FriendshipAction action) |
| | 298 | | { |
| | 299 | | switch (action) |
| | 300 | | { |
| | 301 | | case FriendshipAction.NONE: |
| | 302 | | break; |
| | 303 | | case FriendshipAction.APPROVED: |
| 2 | 304 | | return FriendshipStatus.FRIEND; |
| | 305 | | case FriendshipAction.REJECTED: |
| 0 | 306 | | return FriendshipStatus.NOT_FRIEND; |
| | 307 | | case FriendshipAction.CANCELLED: |
| 0 | 308 | | return FriendshipStatus.NOT_FRIEND; |
| | 309 | | case FriendshipAction.REQUESTED_FROM: |
| 0 | 310 | | return FriendshipStatus.REQUESTED_FROM; |
| | 311 | | case FriendshipAction.REQUESTED_TO: |
| 2 | 312 | | return FriendshipStatus.REQUESTED_TO; |
| | 313 | | case FriendshipAction.DELETED: |
| 2 | 314 | | return FriendshipStatus.NOT_FRIEND; |
| | 315 | | } |
| | 316 | |
|
| 0 | 317 | | return FriendshipStatus.NOT_FRIEND; |
| | 318 | | } |
| | 319 | |
|
| | 320 | | private void AddFriends(IEnumerable<string> friendIds) |
| | 321 | | { |
| 0 | 322 | | foreach (var friendId in friendIds) |
| | 323 | | { |
| 0 | 324 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 325 | | {action = FriendshipAction.APPROVED, userId = friendId}); |
| | 326 | | } |
| 0 | 327 | | } |
| | 328 | |
|
| | 329 | | [ContextMenu("Change user stats to online")] |
| | 330 | | public void FakeOnlineFriend() |
| | 331 | | { |
| 0 | 332 | | var friend = friends.Values.First(); |
| 0 | 333 | | UpdateUserStatus(new UserStatus |
| | 334 | | { |
| | 335 | | userId = friend.userId, |
| | 336 | | position = friend.position, |
| | 337 | | presence = PresenceStatus.ONLINE, |
| | 338 | | friendshipStatus = friend.friendshipStatus, |
| | 339 | | friendshipStartedTime = friend.friendshipStartedTime |
| | 340 | | }); |
| 0 | 341 | | } |
| | 342 | |
|
| | 343 | | [ContextMenu("Force initialization")] |
| | 344 | | public void ForceInitialization() |
| | 345 | | { |
| 0 | 346 | | InitializeFriends(JsonUtility.ToJson(new FriendshipInitializationMessage())); |
| 0 | 347 | | } |
| | 348 | | } |