| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.Friends.WebApi; |
| | 6 | | using UnityEngine; |
| | 7 | | using Random = UnityEngine.Random; |
| | 8 | |
|
| | 9 | | public class LazyLoadingFriendsControllerMock : IFriendsController |
| | 10 | | { |
| | 11 | | private const int MAX_AMOUNT_OF_FAKE_FRIENDS = 130; |
| | 12 | | private const int TOTAL_RECEIVED_REQUESTS = 14; |
| | 13 | | private const int TOTAL_SENT_REQUESTS = 18; |
| | 14 | | private const int TOTAL_FRIENDS = 46; |
| | 15 | |
|
| | 16 | | private readonly FriendsController controller; |
| | 17 | | private readonly UserProfileController userProfileController; |
| | 18 | |
|
| | 19 | | private int amountOfFriendsWithDirectMessagesRequested; |
| 0 | 20 | | private int lastFriendWithDirectMessageIndex = -1; |
| | 21 | |
|
| | 22 | | public event Action OnInitialized |
| | 23 | | { |
| 0 | 24 | | add => controller.OnInitialized += value; |
| 0 | 25 | | remove => controller.OnInitialized -= value; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public event Action<string, FriendshipAction> OnUpdateFriendship |
| | 29 | | { |
| 0 | 30 | | add => controller.OnUpdateFriendship += value; |
| 0 | 31 | | remove => controller.OnUpdateFriendship -= value; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public event Action<string, UserStatus> OnUpdateUserStatus |
| | 35 | | { |
| 0 | 36 | | add => controller.OnUpdateUserStatus += value; |
| 0 | 37 | | remove => controller.OnUpdateUserStatus -= value; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public event Action<string> OnFriendNotFound |
| | 41 | | { |
| 0 | 42 | | add => controller.OnFriendNotFound += value; |
| 0 | 43 | | remove => controller.OnFriendNotFound -= value; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public event Action<List<FriendWithDirectMessages>> OnAddFriendsWithDirectMessages |
| | 47 | | { |
| 0 | 48 | | add => controller.OnAddFriendsWithDirectMessages += value; |
| 0 | 49 | | remove => controller.OnAddFriendsWithDirectMessages -= value; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public event Action<int, int> OnTotalFriendRequestUpdated |
| | 53 | | { |
| 0 | 54 | | add => controller.OnTotalFriendRequestUpdated += value; |
| 0 | 55 | | remove => controller.OnTotalFriendRequestUpdated -= value; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public event Action<int> OnTotalFriendsUpdated |
| | 59 | | { |
| 0 | 60 | | add => controller.OnTotalFriendsUpdated += value; |
| 0 | 61 | | remove => controller.OnTotalFriendsUpdated -= value; |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | public int AllocatedFriendCount => controller.AllocatedFriendCount; |
| 0 | 65 | | public bool IsInitialized => controller.IsInitialized; |
| 0 | 66 | | public int ReceivedRequestCount => controller.ReceivedRequestCount; |
| 0 | 67 | | public int TotalFriendCount => controller.TotalFriendCount; |
| 0 | 68 | | public int TotalFriendRequestCount => controller.TotalFriendRequestCount; |
| 0 | 69 | | public int TotalReceivedFriendRequestCount => controller.TotalReceivedFriendRequestCount; |
| 0 | 70 | | public int TotalSentFriendRequestCount => controller.TotalSentFriendRequestCount; |
| 0 | 71 | | public int TotalFriendsWithDirectMessagesCount => controller.TotalFriendsWithDirectMessagesCount; |
| | 72 | |
|
| 0 | 73 | | public LazyLoadingFriendsControllerMock( |
| | 74 | | FriendsController controller, |
| | 75 | | UserProfileController userProfileController) |
| | 76 | | { |
| 0 | 77 | | this.controller = controller; |
| 0 | 78 | | this.userProfileController = userProfileController; |
| | 79 | |
|
| | 80 | | // TODO: Use it when the friends service is down |
| 0 | 81 | | SimulateDelayedResponseFor_InitializeFriends().Forget(); |
| 0 | 82 | | } |
| | 83 | |
|
| 0 | 84 | | public Dictionary<string, UserStatus> GetAllocatedFriends() => controller.GetAllocatedFriends(); |
| | 85 | |
|
| 0 | 86 | | public UserStatus GetUserStatus(string userId) => controller.GetUserStatus(userId); |
| | 87 | |
|
| 0 | 88 | | public bool ContainsStatus(string friendId, FriendshipStatus status) => controller.ContainsStatus(friendId, status); |
| | 89 | |
|
| | 90 | | public void RequestFriendship(string friendUserId) |
| | 91 | | { |
| 0 | 92 | | controller.RequestFriendship(friendUserId); |
| 0 | 93 | | UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget(); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void CancelRequest(string friendUserId) |
| | 97 | | { |
| 0 | 98 | | controller.CancelRequest(friendUserId); |
| 0 | 99 | | UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget(); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public void AcceptFriendship(string friendUserId) |
| | 103 | | { |
| 0 | 104 | | controller.AcceptFriendship(friendUserId); |
| 0 | 105 | | UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget(); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | public void RejectFriendship(string friendUserId) |
| | 109 | | { |
| 0 | 110 | | controller.RejectFriendship(friendUserId); |
| 0 | 111 | | UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget(); |
| 0 | 112 | | } |
| | 113 | |
|
| 0 | 114 | | public bool IsFriend(string userId) => controller.IsFriend(userId); |
| | 115 | |
|
| | 116 | | public void RemoveFriend(string friendId) |
| | 117 | | { |
| 0 | 118 | | controller.RemoveFriend(friendId); |
| 0 | 119 | | UpdateFriendshipCount(Random.Range(0, 100), Random.Range(0, 100), Random.Range(0, 100)).Forget(); |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | public void GetFriends(int limit, int skip) |
| | 123 | | { |
| 0 | 124 | | GetFakeFriendsAsync(limit, skip, "ff").Forget(); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public void GetFriends(string usernameOrId, int limit) |
| | 128 | | { |
| 0 | 129 | | GetFakeFriendsAsync(limit, 0, usernameOrId).Forget(); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | public void GetFriendRequests( |
| | 133 | | int sentLimit, |
| | 134 | | int sentSkip, |
| | 135 | | int receivedLimit, |
| | 136 | | int receivedSkip) |
| | 137 | | { |
| 0 | 138 | | GetFakeRequestsAsync(sentLimit).Forget(); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | public void GetFriendsWithDirectMessages(int limit, int skip) |
| | 142 | | { |
| 0 | 143 | | SimulateDelayedResponseFor_GetFriendsWithDirectMessages(limit).Forget(); |
| 0 | 144 | | } |
| | 145 | |
|
| | 146 | | public void GetFriendsWithDirectMessages(string userNameOrId, int limit) |
| | 147 | | { |
| 0 | 148 | | SimulateDelayedResponseFor_GetFriendsWithDirectMessages(userNameOrId, limit).Forget(); |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | private async UniTask SimulateDelayedResponseFor_GetFriendsWithDirectMessages(int limit) |
| | 152 | | { |
| 0 | 153 | | await UniTask.Delay(Random.Range(1000, 3000)); |
| | 154 | |
|
| 0 | 155 | | controller.AddFriendsWithDirectMessages( |
| | 156 | | CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(limit)); |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | private string CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(int numberOfUsers) |
| | 160 | | { |
| 0 | 161 | | AddFriendsWithDirectMessagesPayload mockedPayload = new AddFriendsWithDirectMessagesPayload(); |
| 0 | 162 | | List<FriendWithDirectMessages> mockedFriendWithDirectMessages = new List<FriendWithDirectMessages>(); |
| | 163 | |
|
| 0 | 164 | | int indexToStart = lastFriendWithDirectMessageIndex + 1; |
| | 165 | |
|
| 0 | 166 | | for (int i = indexToStart; i < indexToStart + numberOfUsers; i++) |
| | 167 | | { |
| 0 | 168 | | if (amountOfFriendsWithDirectMessagesRequested >= MAX_AMOUNT_OF_FAKE_FRIENDS) |
| | 169 | | break; |
| | 170 | |
|
| 0 | 171 | | string fakeUserId = $"fakeuser{i + 1}"; |
| | 172 | |
|
| 0 | 173 | | mockedFriendWithDirectMessages.Add( |
| | 174 | | new FriendWithDirectMessages |
| | 175 | | { |
| | 176 | | userId = fakeUserId, |
| | 177 | | lastMessageBody = $"This is the last message sent for {fakeUserId}", |
| | 178 | | lastMessageTimestamp = DateTimeOffset.UtcNow.AddMinutes(-(i + 1)).ToUnixTimeMilliseconds() |
| | 179 | | }); |
| | 180 | |
|
| 0 | 181 | | amountOfFriendsWithDirectMessagesRequested++; |
| 0 | 182 | | lastFriendWithDirectMessageIndex = i; |
| | 183 | |
|
| 0 | 184 | | CreateFakeFriend(fakeUserId); |
| | 185 | | } |
| | 186 | |
|
| 0 | 187 | | mockedPayload.currentFriendsWithDirectMessages = mockedFriendWithDirectMessages.ToArray(); |
| 0 | 188 | | mockedPayload.totalFriendsWithDirectMessages = MAX_AMOUNT_OF_FAKE_FRIENDS; |
| | 189 | |
|
| 0 | 190 | | return JsonUtility.ToJson(mockedPayload); |
| | 191 | | } |
| | 192 | |
|
| | 193 | | private async UniTask SimulateDelayedResponseFor_GetFriendsWithDirectMessages(string userNameOrId, int limit) |
| | 194 | | { |
| 0 | 195 | | await UniTask.Delay(Random.Range(1000, 3000)); |
| | 196 | |
|
| 0 | 197 | | controller.AddFriendsWithDirectMessages( |
| | 198 | | CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(userNameOrId, limit)); |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | private string CreateMockedDataFor_AddFriendsWithDirectMessagesPayload(string userNameOrId, int numberOfUsers) |
| | 202 | | { |
| 0 | 203 | | List<string> allFriendsWithDMsInServer = new List<string>(); |
| 0 | 204 | | for (int i = 0; i < MAX_AMOUNT_OF_FAKE_FRIENDS; i++) |
| | 205 | | { |
| 0 | 206 | | allFriendsWithDMsInServer.Add($"fakeuser{i + 1}"); |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | List<string> resultsFound = |
| 0 | 210 | | allFriendsWithDMsInServer.Where(x => x.ToLower().Contains(userNameOrId.ToLower())).ToList(); |
| | 211 | |
|
| 0 | 212 | | AddFriendsWithDirectMessagesPayload mockedPayload = new AddFriendsWithDirectMessagesPayload(); |
| 0 | 213 | | List<FriendWithDirectMessages> mockedFriendWithDirectMessages = new List<FriendWithDirectMessages>(); |
| | 214 | |
|
| 0 | 215 | | for (int i = 0; i < resultsFound.Count; i++) |
| | 216 | | { |
| 0 | 217 | | if (i >= numberOfUsers) |
| | 218 | | break; |
| | 219 | |
|
| 0 | 220 | | mockedFriendWithDirectMessages.Add( |
| | 221 | | new FriendWithDirectMessages |
| | 222 | | { |
| | 223 | | userId = resultsFound[i], |
| | 224 | | lastMessageBody = $"This is the last message sent for {resultsFound[i]}", |
| | 225 | | lastMessageTimestamp = DateTimeOffset.UtcNow.AddMinutes(-(i + 1)).ToUnixTimeMilliseconds() |
| | 226 | | }); |
| | 227 | |
|
| 0 | 228 | | CreateFakeFriend(resultsFound[i]); |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | mockedPayload.currentFriendsWithDirectMessages = mockedFriendWithDirectMessages.ToArray(); |
| 0 | 232 | | mockedPayload.totalFriendsWithDirectMessages = MAX_AMOUNT_OF_FAKE_FRIENDS; |
| | 233 | |
|
| 0 | 234 | | return JsonUtility.ToJson(mockedPayload); |
| | 235 | | } |
| | 236 | |
|
| | 237 | | private async UniTask GetFakeFriendsAsync(int limit, int skip, string name) |
| | 238 | | { |
| 0 | 239 | | var friendIds = new List<string>(); |
| | 240 | |
|
| 0 | 241 | | await UniTask.Delay(Random.Range(20, 500)); |
| | 242 | |
|
| 0 | 243 | | var characters = new[] |
| | 244 | | { |
| | 245 | | 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' |
| | 246 | | }; |
| | 247 | |
|
| 0 | 248 | | var max = Mathf.Min(skip + Random.Range(1, limit), TOTAL_FRIENDS); |
| | 249 | |
|
| 0 | 250 | | for (var i = skip; i < max; i++) |
| | 251 | | { |
| 0 | 252 | | var userId = ""; |
| 0 | 253 | | for (var x = 0; x < 8; x++) |
| 0 | 254 | | userId += characters[Random.Range(0, characters.Length)]; |
| | 255 | |
|
| 0 | 256 | | friendIds.Add(userId); |
| | 257 | |
|
| 0 | 258 | | userProfileController.AddUserProfileToCatalog(JsonUtility.ToJson(new UserProfileModel |
| | 259 | | { |
| | 260 | | userId = userId, |
| | 261 | | name = $"{name}-{i}-{userId}", |
| | 262 | | ethAddress = userId, |
| | 263 | | snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{i + 1}/256"} |
| | 264 | | })); |
| | 265 | | } |
| | 266 | |
|
| 0 | 267 | | await UniTask.Delay(Random.Range(20, 500)); |
| | 268 | |
|
| 0 | 269 | | var payload = new AddFriendsPayload |
| | 270 | | { |
| | 271 | | friends = friendIds.ToArray(), |
| | 272 | | totalFriends = TOTAL_FRIENDS |
| | 273 | | }; |
| | 274 | |
|
| 0 | 275 | | controller.AddFriends(JsonUtility.ToJson(payload)); |
| 0 | 276 | | } |
| | 277 | |
|
| | 278 | | private async UniTask GetFakeRequestsAsync(int limit) |
| | 279 | | { |
| 0 | 280 | | var characters = new[] |
| | 281 | | { |
| | 282 | | 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' |
| | 283 | | }; |
| | 284 | |
|
| 0 | 285 | | await UniTask.Delay(Random.Range(20, 500)); |
| | 286 | |
|
| 0 | 287 | | var fromUserIds = new List<string>(); |
| 0 | 288 | | var maxReceivedRequests = Mathf.Min(TOTAL_RECEIVED_REQUESTS, Random.Range(1, limit)); |
| | 289 | |
|
| 0 | 290 | | for (var i = 0; i < maxReceivedRequests; i++) |
| | 291 | | { |
| 0 | 292 | | var userId = ""; |
| 0 | 293 | | for (var x = 0; x < 8; x++) |
| 0 | 294 | | userId += characters[Random.Range(0, characters.Length)]; |
| | 295 | |
|
| 0 | 296 | | fromUserIds.Add(userId); |
| | 297 | |
|
| 0 | 298 | | userProfileController.AddUserProfileToCatalog(JsonUtility.ToJson(new UserProfileModel |
| | 299 | | { |
| | 300 | | userId = userId, |
| | 301 | | name = $"ffrrq-{i}-{userId}", |
| | 302 | | ethAddress = userId, |
| | 303 | | snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{i + 1}/256"} |
| | 304 | | })); |
| | 305 | | } |
| | 306 | |
|
| 0 | 307 | | var toUserIds = new List<string>(); |
| 0 | 308 | | var maxSentRequests = Mathf.Min(TOTAL_SENT_REQUESTS, Random.Range(1, limit)); |
| | 309 | |
|
| 0 | 310 | | for (var i = 0; i < maxSentRequests; i++) |
| | 311 | | { |
| 0 | 312 | | var userId = ""; |
| 0 | 313 | | for (var x = 0; x < 8; x++) |
| 0 | 314 | | userId += characters[Random.Range(0, characters.Length)]; |
| | 315 | |
|
| 0 | 316 | | toUserIds.Add(userId); |
| | 317 | |
|
| 0 | 318 | | userProfileController.AddUserProfileToCatalog(JsonUtility.ToJson(new UserProfileModel |
| | 319 | | { |
| | 320 | | userId = userId, |
| | 321 | | name = $"ffsrq-{i}-{userId}", |
| | 322 | | ethAddress = userId, |
| | 323 | | snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{i + 1}/256"} |
| | 324 | | })); |
| | 325 | | } |
| | 326 | |
|
| 0 | 327 | | await UniTask.Delay(Random.Range(20, 500)); |
| | 328 | |
|
| 0 | 329 | | var payload = new AddFriendRequestsPayload |
| | 330 | | { |
| | 331 | | requestedFrom = fromUserIds.ToArray(), |
| | 332 | | requestedTo = toUserIds.ToArray(), |
| | 333 | | totalReceivedFriendRequests = TOTAL_RECEIVED_REQUESTS, |
| | 334 | | totalSentFriendRequests = TOTAL_SENT_REQUESTS |
| | 335 | | }; |
| | 336 | |
|
| 0 | 337 | | controller.AddFriendRequests(JsonUtility.ToJson(payload)); |
| 0 | 338 | | } |
| | 339 | |
|
| | 340 | | private void CreateFakeFriend(string userId) |
| | 341 | | { |
| 0 | 342 | | if (controller.friends.ContainsKey(userId)) |
| 0 | 343 | | return; |
| | 344 | |
|
| 0 | 345 | | controller.friends.Add(userId, new UserStatus |
| | 346 | | { |
| | 347 | | userId = userId, |
| | 348 | | position = new Vector2(Random.Range(-100, 101), Random.Range(-100, 101)), |
| | 349 | | realm = new UserStatus.Realm |
| | 350 | | { |
| | 351 | | serverName = "dg", |
| | 352 | | layer = "" |
| | 353 | | }, |
| | 354 | | presence = Random.Range(0, 2) == 0 ? PresenceStatus.OFFLINE : PresenceStatus.ONLINE, |
| | 355 | | friendshipStatus = FriendshipStatus.FRIEND, |
| | 356 | | friendshipStartedTime = DateTime.UtcNow |
| | 357 | | }); |
| 0 | 358 | | } |
| | 359 | |
|
| | 360 | | private async UniTask SimulateDelayedResponseFor_InitializeFriends() |
| | 361 | | { |
| 0 | 362 | | await UniTask.Delay(Random.Range(1000, 3000)); |
| | 363 | |
|
| 0 | 364 | | controller.InitializeFriends( |
| | 365 | | CreateFakeFriendsInitialization()); |
| 0 | 366 | | } |
| | 367 | |
|
| | 368 | | private string CreateFakeFriendsInitialization() |
| | 369 | | { |
| 0 | 370 | | var mockedPayload = new FriendshipInitializationMessage |
| | 371 | | { |
| | 372 | | totalReceivedRequests = TOTAL_RECEIVED_REQUESTS |
| | 373 | | }; |
| | 374 | |
|
| 0 | 375 | | return JsonUtility.ToJson(mockedPayload); |
| | 376 | | } |
| | 377 | |
|
| | 378 | | private async UniTask UpdateFriendshipCount(int totalReceivedRequests, int totalSentRequests, int totalFriends) |
| | 379 | | { |
| 0 | 380 | | await UniTask.Delay(Random.Range(100, 2000)); |
| | 381 | |
|
| 0 | 382 | | var requestsPayload = new UpdateTotalFriendRequestsPayload |
| | 383 | | { |
| | 384 | | totalReceivedRequests = totalReceivedRequests, |
| | 385 | | totalSentRequests = totalSentRequests |
| | 386 | | }; |
| 0 | 387 | | controller.UpdateTotalFriendRequests(JsonUtility.ToJson(requestsPayload)); |
| | 388 | |
|
| 0 | 389 | | var friendsPayload = new UpdateTotalFriendsPayload |
| | 390 | | { |
| | 391 | | totalFriends = totalFriends |
| | 392 | | }; |
| 0 | 393 | | controller.UpdateTotalFriends(JsonUtility.ToJson(friendsPayload)); |
| 0 | 394 | | } |
| | 395 | | } |