| | 1 | | using UnityEngine; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using Random = UnityEngine.Random; |
| | 5 | |
|
| | 6 | | public class RealmTrackerController : IDisposable |
| | 7 | | { |
| 13 | 8 | | Dictionary<IRealmTrackerHandler, TrackedRealmInfo> listeners = new Dictionary<IRealmTrackerHandler, TrackedRealmInfo |
| 13 | 9 | | Dictionary<string, RealmTracker> friends = new Dictionary<string, RealmTracker>(); |
| | 10 | |
|
| | 11 | | IFriendsController friendsController; |
| | 12 | | Color[] friendColors; |
| | 13 | |
|
| 13 | 14 | | public RealmTrackerController(IFriendsController friendsController, Color[] friendColors) |
| | 15 | | { |
| 13 | 16 | | this.friendsController = friendsController; |
| | 17 | |
|
| 13 | 18 | | if (friendColors != null && friendColors.Length > 0) |
| | 19 | | { |
| 13 | 20 | | this.friendColors = friendColors; |
| 13 | 21 | | } |
| | 22 | | else |
| | 23 | | { |
| 0 | 24 | | this.friendColors = new Color[] { Color.white }; |
| | 25 | | } |
| | 26 | |
|
| 13 | 27 | | if (friendsController != null) |
| | 28 | | { |
| 0 | 29 | | if (!friendsController.isInitialized) |
| | 30 | | { |
| 0 | 31 | | friendsController.OnInitialized += OnFriendsInitialized; |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | friendsController.OnUpdateUserStatus += OnUpdateUserStatus; |
| | 35 | | } |
| 13 | 36 | | } |
| | 37 | |
|
| | 38 | | public void Dispose() |
| | 39 | | { |
| 0 | 40 | | if (friendsController != null) |
| | 41 | | { |
| 0 | 42 | | friendsController.OnInitialized -= OnFriendsInitialized; |
| 0 | 43 | | friendsController.OnUpdateUserStatus -= OnUpdateUserStatus; |
| | 44 | | } |
| 0 | 45 | | listeners.Clear(); |
| 0 | 46 | | friends.Clear(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void AddHandler(IRealmTrackerHandler listener) |
| | 50 | | { |
| | 51 | | TrackedRealmInfo wrapper; |
| 2 | 52 | | if (listeners.TryGetValue(listener, out wrapper)) |
| | 53 | | { |
| 0 | 54 | | return; |
| | 55 | | } |
| | 56 | |
|
| 2 | 57 | | wrapper = new TrackedRealmInfo(listener); |
| | 58 | |
|
| 2 | 59 | | if (friendsController != null && friendsController.isInitialized) |
| | 60 | | { |
| 0 | 61 | | ProcessNewListener(wrapper); |
| | 62 | | } |
| | 63 | |
|
| 2 | 64 | | listeners.Add(listener, wrapper); |
| 2 | 65 | | } |
| | 66 | |
|
| | 67 | | public void RemoveHandler(IRealmTrackerHandler listener) |
| | 68 | | { |
| | 69 | | TrackedRealmInfo wrapper; |
| 0 | 70 | | if (listeners.TryGetValue(listener, out wrapper)) |
| | 71 | | { |
| 0 | 72 | | wrapper.Dispose(); |
| 0 | 73 | | listeners.Remove(listener); |
| | 74 | | } |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | public void RemoveAllHandlers() |
| | 78 | | { |
| 1 | 79 | | using (var listenersIterator = listeners.GetEnumerator()) |
| | 80 | | { |
| 1 | 81 | | while (listenersIterator.MoveNext()) |
| | 82 | | { |
| 0 | 83 | | listenersIterator.Current.Value.Dispose(); |
| | 84 | | } |
| 1 | 85 | | } |
| | 86 | |
|
| 1 | 87 | | listeners.Clear(); |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | void OnUpdateUserStatus(string userId, FriendsController.UserStatus status) |
| | 91 | | { |
| 0 | 92 | | if (!friendsController.isInitialized) |
| 0 | 93 | | return; |
| | 94 | |
|
| | 95 | | RealmTracker friend; |
| 0 | 96 | | if (!friends.TryGetValue(userId, out friend)) |
| | 97 | | { |
| 0 | 98 | | friend = new RealmTracker(userId, friendColors[Random.Range(0, friendColors.Length)]); |
| 0 | 99 | | friends.Add(userId, friend); |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | friend.SetStatus(status); |
| | 103 | |
|
| 0 | 104 | | if (!friend.IsOnline()) |
| | 105 | | { |
| 0 | 106 | | friend.RemoveAllListeners(); |
| 0 | 107 | | } |
| | 108 | | else |
| | 109 | | { |
| 0 | 110 | | ProcessFriendRealm(friend, status.realm?.serverName); |
| | 111 | | } |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | void OnFriendsInitialized() |
| | 115 | | { |
| 0 | 116 | | friendsController.OnInitialized -= OnFriendsInitialized; |
| | 117 | |
|
| 0 | 118 | | using (var friendsIterator = friendsController.GetFriends().GetEnumerator()) |
| | 119 | | { |
| 0 | 120 | | while (friendsIterator.MoveNext()) |
| | 121 | | { |
| 0 | 122 | | RealmTracker friend = new RealmTracker(friendsIterator.Current.Key, friendColors[Random.Range(0, friendC |
| 0 | 123 | | friend.SetStatus(friendsIterator.Current.Value); |
| 0 | 124 | | friends.Add(friendsIterator.Current.Key, friend); |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| 0 | 128 | | using (var listenersIterator = listeners.GetEnumerator()) |
| | 129 | | { |
| 0 | 130 | | while (listenersIterator.MoveNext()) |
| | 131 | | { |
| 0 | 132 | | ProcessNewListener(listenersIterator.Current.Value); |
| | 133 | | } |
| 0 | 134 | | } |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | void ProcessFriendRealm(RealmTracker friend, string serverName) |
| | 138 | | { |
| 0 | 139 | | if (!friend.HasChangedRealm(serverName)) |
| 0 | 140 | | return; |
| | 141 | |
|
| 0 | 142 | | friend.RemoveAllListeners(); |
| | 143 | |
|
| 0 | 144 | | using (var listenersIterator = listeners.GetEnumerator()) |
| | 145 | | { |
| 0 | 146 | | while (listenersIterator.MoveNext()) |
| | 147 | | { |
| 0 | 148 | | if (listenersIterator.Current.Value.ContainRealm(serverName)) |
| | 149 | | { |
| 0 | 150 | | friend.AddListener(listenersIterator.Current.Value); |
| | 151 | | } |
| | 152 | | } |
| 0 | 153 | | } |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | void ProcessNewListener(TrackedRealmInfo listener) |
| | 157 | | { |
| 0 | 158 | | string friendRealm = string.Empty; |
| 0 | 159 | | using (var friendIterator = friends.GetEnumerator()) |
| | 160 | | { |
| 0 | 161 | | while (friendIterator.MoveNext()) |
| | 162 | | { |
| 0 | 163 | | if (!friendIterator.Current.Value.IsOnline()) |
| | 164 | | { |
| | 165 | | continue; |
| | 166 | | } |
| | 167 | |
|
| 0 | 168 | | friendRealm = friendIterator.Current.Value.status.realm?.serverName; |
| | 169 | |
|
| 0 | 170 | | if (listener.ContainRealm(friendRealm)) |
| | 171 | | { |
| 0 | 172 | | friendIterator.Current.Value.AddListener(listener); |
| | 173 | | } |
| | 174 | | } |
| 0 | 175 | | } |
| 0 | 176 | | } |
| | 177 | | } |