| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class RealmTracker |
| | 6 | | { |
| 0 | 7 | | HashSet<TrackedRealmInfo> realmListeners = new HashSet<TrackedRealmInfo>(); |
| | 8 | |
|
| 0 | 9 | | public UserProfile profile { private set; get; } |
| 0 | 10 | | public FriendsController.UserStatus status { private set; get; } |
| | 11 | | Color backgroundColor; |
| | 12 | |
|
| 0 | 13 | | public RealmTracker(string userId, Color backgroundColor) |
| | 14 | | { |
| 0 | 15 | | profile = UserProfileController.userProfilesCatalog.Get(userId); |
| 0 | 16 | | this.backgroundColor = backgroundColor; |
| 0 | 17 | | } |
| | 18 | |
|
| 0 | 19 | | public void SetStatus(FriendsController.UserStatus newStatus) { status = newStatus; } |
| | 20 | |
|
| | 21 | | public void AddListener(TrackedRealmInfo listener) |
| | 22 | | { |
| 0 | 23 | | listener.OnListenerDisposed += OnListenerDisposed; |
| 0 | 24 | | realmListeners.Add(listener); |
| 0 | 25 | | listener.OnFriendAdded(profile, backgroundColor); |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | public void RemoveListener(TrackedRealmInfo listener) |
| | 29 | | { |
| 0 | 30 | | OnListenerRemoved(listener); |
| 0 | 31 | | realmListeners.Remove(listener); |
| 0 | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public bool HasListeners() { return realmListeners.Count > 0; } |
| | 35 | |
|
| | 36 | | public bool HasChangedRealm(string serverName) |
| | 37 | | { |
| 0 | 38 | | if (realmListeners.Count > 0 && realmListeners.First().ContainRealm(serverName)) |
| | 39 | | { |
| 0 | 40 | | return false; |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | return true; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public void RemoveAllListeners() |
| | 47 | | { |
| 0 | 48 | | if (!HasListeners()) |
| | 49 | | { |
| 0 | 50 | | return; |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | using (var listenerIterator = realmListeners.GetEnumerator()) |
| | 54 | | { |
| 0 | 55 | | while (listenerIterator.MoveNext()) |
| | 56 | | { |
| 0 | 57 | | OnListenerRemoved(listenerIterator.Current); |
| | 58 | | } |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | realmListeners.Clear(); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public bool IsOnline() |
| | 65 | | { |
| 0 | 66 | | if (status.presence != PresenceStatus.ONLINE) |
| 0 | 67 | | return false; |
| 0 | 68 | | if (status.realm == null) |
| 0 | 69 | | return false; |
| 0 | 70 | | return !string.IsNullOrEmpty(status.realm.serverName); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | void OnListenerDisposed(TrackedRealmInfo listener) |
| | 74 | | { |
| 0 | 75 | | listener.OnListenerDisposed -= OnListenerDisposed; |
| 0 | 76 | | realmListeners.Remove(listener); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | void OnListenerRemoved(TrackedRealmInfo listener) |
| | 80 | | { |
| 0 | 81 | | listener.OnListenerDisposed -= OnListenerDisposed; |
| 0 | 82 | | listener.OnFriendRemoved(profile); |
| 0 | 83 | | } |
| | 84 | | } |