| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | class FriendTracker |
| | 6 | | { |
| 0 | 7 | | HashSet<TrackedSceneInfo> friendListeners = new HashSet<TrackedSceneInfo>(); |
| | 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 FriendTracker(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(TrackedSceneInfo listener) |
| | 22 | | { |
| 0 | 23 | | listener.OnListenerDisposed += OnListenerDisposed; |
| 0 | 24 | | friendListeners.Add(listener); |
| 0 | 25 | | listener.OnFriendAdded(profile, backgroundColor); |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | public void RemoveListener(TrackedSceneInfo listener) |
| | 29 | | { |
| 0 | 30 | | OnListenerRemoved(listener); |
| 0 | 31 | | friendListeners.Remove(listener); |
| 0 | 32 | | } |
| | 33 | |
|
| 0 | 34 | | public bool HasListeners() { return friendListeners.Count > 0; } |
| | 35 | |
|
| | 36 | | public bool HasChangedLocation(Vector2Int coords) |
| | 37 | | { |
| 0 | 38 | | if (friendListeners.Count > 0 && friendListeners.First().ContainCoords(coords)) |
| | 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 = friendListeners.GetEnumerator()) |
| | 54 | | { |
| 0 | 55 | | while (listenerIterator.MoveNext()) |
| | 56 | | { |
| 0 | 57 | | OnListenerRemoved(listenerIterator.Current); |
| | 58 | | } |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | friendListeners.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(TrackedSceneInfo listener) |
| | 74 | | { |
| 0 | 75 | | listener.OnListenerDisposed -= OnListenerDisposed; |
| 0 | 76 | | friendListeners.Remove(listener); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | void OnListenerRemoved(TrackedSceneInfo listener) |
| | 80 | | { |
| 0 | 81 | | listener.OnListenerDisposed -= OnListenerDisposed; |
| 0 | 82 | | listener.OnFriendRemoved(profile); |
| 0 | 83 | | } |
| | 84 | | } |