| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | |
|
| | 6 | | namespace DCL.Social.Chat.Mentions |
| | 7 | | { |
| | 8 | | public class MemoryChatMentionSuggestionProvider : IChatMentionSuggestionProvider |
| | 9 | | { |
| | 10 | | private const int ITERATIONS_PER_FRAME = 10; |
| | 11 | |
|
| | 12 | | private readonly UserProfileController userProfileController; |
| | 13 | | private readonly DataStore dataStore; |
| | 14 | |
|
| 9 | 15 | | public MemoryChatMentionSuggestionProvider(UserProfileController userProfileController, |
| | 16 | | DataStore dataStore) |
| | 17 | | { |
| 9 | 18 | | this.userProfileController = userProfileController; |
| 9 | 19 | | this.dataStore = dataStore; |
| 9 | 20 | | } |
| | 21 | |
|
| | 22 | | public async UniTask<List<UserProfile>> GetNearbyProfilesStartingWith(string name, int count, CancellationToken |
| | 23 | | { |
| 2 | 24 | | var iterations = 0; |
| 2 | 25 | | List<UserProfile> results = new List<UserProfile>(); |
| | 26 | |
|
| 9 | 27 | | foreach ((string key, Player value) in dataStore.player.otherPlayers) |
| | 28 | | { |
| 3 | 29 | | if (value.name.StartsWith(name, StringComparison.OrdinalIgnoreCase)) |
| | 30 | | { |
| 3 | 31 | | UserProfile profile = userProfileController.AllProfiles.Get(key); |
| 3 | 32 | | if (profile == null) continue; |
| | 33 | |
|
| 3 | 34 | | results.Add(profile); |
| | 35 | |
|
| 3 | 36 | | if (results.Count >= count) break; |
| | 37 | | } |
| | 38 | |
|
| 2 | 39 | | if (iterations >= ITERATIONS_PER_FRAME) |
| 0 | 40 | | await UniTask.NextFrame(cancellationToken); |
| | 41 | |
|
| 2 | 42 | | iterations++; |
| | 43 | | } |
| | 44 | |
|
| 2 | 45 | | return results; |
| 2 | 46 | | } |
| | 47 | |
|
| | 48 | | public async UniTask<List<UserProfile>> GetProfilesFromChatChannelsStartingWith(string name, string channelId, i |
| | 49 | | { |
| 2 | 50 | | var iterations = 0; |
| 2 | 51 | | List<UserProfile> results = new List<UserProfile>(); |
| | 52 | |
|
| 2 | 53 | | if (!dataStore.channels.availableMembersByChannel.TryGetValue(channelId, out var availableMembers)) |
| 0 | 54 | | return results; |
| | 55 | |
|
| 11 | 56 | | foreach (string userId in availableMembers) |
| | 57 | | { |
| 4 | 58 | | UserProfile profile = userProfileController.AllProfiles.Get(userId); |
| 4 | 59 | | if (profile == null) continue; |
| | 60 | |
|
| 4 | 61 | | if (profile.userName.StartsWith(name, StringComparison.OrdinalIgnoreCase)) |
| | 62 | | { |
| 2 | 63 | | results.Add(profile); |
| | 64 | |
|
| 2 | 65 | | if (results.Count >= count) break; |
| | 66 | | } |
| | 67 | |
|
| 3 | 68 | | if (iterations >= ITERATIONS_PER_FRAME) |
| 0 | 69 | | await UniTask.NextFrame(cancellationToken); |
| | 70 | |
|
| 3 | 71 | | iterations++; |
| | 72 | | } |
| | 73 | |
|
| 2 | 74 | | return results; |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | public async UniTask<List<UserProfile>> GetProfilesStartingWith(string name, int count, CancellationToken cancel |
| 2 | 78 | | await GetProfilesStartingWith(name, count, userProfileController.AllProfiles.GetValues(), cancellationToken) |
| | 79 | |
|
| | 80 | | public async UniTask<List<UserProfile>> GetProfilesStartingWith(string name, int count, IEnumerable<UserProfile> |
| | 81 | | CancellationToken cancellationToken) |
| | 82 | | { |
| 2 | 83 | | var iterations = 0; |
| 2 | 84 | | List<UserProfile> results = new List<UserProfile>(); |
| | 85 | |
|
| 17 | 86 | | foreach (UserProfile profile in profiles) |
| | 87 | | { |
| 7 | 88 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 89 | |
|
| 7 | 90 | | if (profile == null) continue; |
| | 91 | |
|
| 7 | 92 | | if (profile.userName.StartsWith(name, StringComparison.OrdinalIgnoreCase)) |
| | 93 | | { |
| 3 | 94 | | results.Add(profile); |
| | 95 | |
|
| 3 | 96 | | if (results.Count >= count) break; |
| | 97 | | } |
| | 98 | |
|
| 6 | 99 | | if (iterations >= ITERATIONS_PER_FRAME) |
| 0 | 100 | | await UniTask.NextFrame(cancellationToken); |
| | 101 | |
|
| 6 | 102 | | iterations++; |
| | 103 | | } |
| | 104 | |
|
| 2 | 105 | | return results; |
| 2 | 106 | | } |
| | 107 | | } |
| | 108 | | } |