| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class FriendsController : MonoBehaviour, IFriendsController |
| | 7 | | { |
| | 8 | | public static bool VERBOSE = false; |
| 12 | 9 | | public static FriendsController i { get; private set; } |
| | 10 | |
|
| 1 | 11 | | public int friendCount => friends.Count(f => f.Value.friendshipStatus == FriendshipStatus.FRIEND); |
| | 12 | |
|
| 18 | 13 | | void Awake() { i = this; } |
| | 14 | |
|
| | 15 | | private const bool KERNEL_CAN_REMOVE_ENTRIES = false; |
| 2 | 16 | | public bool isInitialized { get; private set; } = false; |
| | 17 | |
|
| | 18 | | public int ReceivedRequestCount => |
| 1 | 19 | | friends.Values.Count(status => status.friendshipStatus == FriendshipStatus.REQUESTED_FROM); |
| | 20 | |
|
| 9 | 21 | | public Dictionary<string, UserStatus> friends = new Dictionary<string, UserStatus>(); |
| | 22 | |
|
| | 23 | | [System.Serializable] |
| | 24 | | public class UserStatus |
| | 25 | | { |
| | 26 | | [System.Serializable] |
| | 27 | | public class Realm |
| | 28 | | { |
| | 29 | | public string serverName; |
| | 30 | | public string layer; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public Realm realm; |
| | 34 | | public Vector2 position; |
| | 35 | | public string userId; |
| | 36 | | public FriendshipStatus friendshipStatus; |
| | 37 | | public PresenceStatus presence; |
| | 38 | | [NonSerialized] public DateTime friendshipStartedTime; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | [System.Serializable] |
| | 42 | | public class FriendshipInitializationMessage |
| | 43 | | { |
| | 44 | | public string[] currentFriends; |
| | 45 | | public string[] requestedTo; |
| | 46 | | public string[] requestedFrom; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | [System.Serializable] |
| | 50 | | public class FriendshipUpdateStatusMessage |
| | 51 | | { |
| | 52 | | public string userId; |
| | 53 | | public FriendshipAction action; |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public UserStatus GetUserStatus(string userId) |
| | 57 | | { |
| 0 | 58 | | if (!friends.ContainsKey(userId)) |
| 0 | 59 | | return new UserStatus() { userId = userId, friendshipStatus = FriendshipStatus.NOT_FRIEND }; |
| | 60 | |
|
| 0 | 61 | | return friends[userId]; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public bool ContainsStatus(string friendId, FriendshipStatus status) |
| | 65 | | { |
| 0 | 66 | | if (!friends.ContainsKey(friendId)) return false; |
| 0 | 67 | | return friends[friendId].friendshipStatus == status; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | public event Action<string, UserStatus> OnUpdateUserStatus; |
| | 71 | | public event Action<string, FriendshipAction> OnUpdateFriendship; |
| | 72 | | public event Action<string> OnFriendNotFound; |
| | 73 | | public event Action OnInitialized; |
| | 74 | |
|
| 0 | 75 | | public Dictionary<string, UserStatus> GetFriends() { return new Dictionary<string, UserStatus>(friends); } |
| | 76 | |
|
| | 77 | | public void RejectFriendship(string friendUserId) |
| | 78 | | { |
| 0 | 79 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 80 | | { |
| | 81 | | userId = friendUserId, |
| | 82 | | action = FriendshipAction.REJECTED |
| | 83 | | }); |
| 0 | 84 | | } |
| | 85 | |
|
| 1 | 86 | | public bool IsFriend(string userId) => friends.ContainsKey(userId); |
| | 87 | |
|
| | 88 | | public void RemoveFriend(string friendId) |
| | 89 | | { |
| 0 | 90 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 91 | | { |
| | 92 | | action = FriendshipAction.DELETED, |
| | 93 | | userId = friendId |
| | 94 | | }); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void RequestFriendship(string friendUserId) |
| | 98 | | { |
| 0 | 99 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 100 | | { |
| | 101 | | userId = friendUserId, |
| | 102 | | action = FriendshipAction.REQUESTED_TO |
| | 103 | | }); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public void CancelRequest(string friendUserId) |
| | 107 | | { |
| 0 | 108 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 109 | | { |
| | 110 | | userId = friendUserId, |
| | 111 | | action = FriendshipAction.CANCELLED |
| | 112 | | }); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public void AcceptFriendship(string friendUserId) |
| | 116 | | { |
| 0 | 117 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 118 | | { |
| | 119 | | userId = friendUserId, |
| | 120 | | action = FriendshipAction.APPROVED |
| | 121 | | }); |
| 0 | 122 | | } |
| | 123 | |
|
| 0 | 124 | | public void FriendNotFound(string name) { OnFriendNotFound?.Invoke(name); } |
| | 125 | |
|
| | 126 | | public void InitializeFriends(string json) |
| | 127 | | { |
| 0 | 128 | | isInitialized = true; |
| | 129 | |
|
| 0 | 130 | | FriendshipInitializationMessage msg = JsonUtility.FromJson<FriendshipInitializationMessage>(json); |
| 0 | 131 | | HashSet<string> processedIds = new HashSet<string>(); |
| | 132 | |
|
| 0 | 133 | | foreach (var userId in msg.currentFriends) |
| | 134 | | { |
| 0 | 135 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage() |
| | 136 | | { action = FriendshipAction.APPROVED, userId = userId }); |
| 0 | 137 | | if (!processedIds.Contains(userId)) |
| 0 | 138 | | processedIds.Add(userId); |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | foreach (var userId in msg.requestedFrom) |
| | 142 | | { |
| 0 | 143 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage() |
| | 144 | | { action = FriendshipAction.REQUESTED_FROM, userId = userId }); |
| 0 | 145 | | if (!processedIds.Contains(userId)) |
| 0 | 146 | | processedIds.Add(userId); |
| | 147 | | } |
| | 148 | |
|
| 0 | 149 | | foreach (var userId in msg.requestedTo) |
| | 150 | | { |
| 0 | 151 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage() |
| | 152 | | { action = FriendshipAction.REQUESTED_TO, userId = userId }); |
| 0 | 153 | | if (!processedIds.Contains(userId)) |
| 0 | 154 | | processedIds.Add(userId); |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | Queue<string> newFriends = new Queue<string>(); |
| | 158 | |
|
| 0 | 159 | | foreach (var kvp in friends) |
| | 160 | | { |
| 0 | 161 | | if (!processedIds.Contains(kvp.Key)) |
| | 162 | | { |
| 0 | 163 | | newFriends.Enqueue(kvp.Key); |
| | 164 | | } |
| | 165 | | } |
| | 166 | |
|
| 0 | 167 | | while (newFriends.Count > 0) |
| | 168 | | { |
| 0 | 169 | | var userId = newFriends.Dequeue(); |
| | 170 | |
|
| | 171 | | if (KERNEL_CAN_REMOVE_ENTRIES) |
| | 172 | | { |
| | 173 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage() |
| | 174 | | { action = FriendshipAction.NONE, userId = userId }); |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | if (friends.ContainsKey(userId)) |
| 0 | 178 | | friends.Remove(userId); |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | OnInitialized?.Invoke(); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | public void UpdateUserStatus(UserStatus newUserStatus) |
| | 185 | | { |
| 0 | 186 | | if (!friends.ContainsKey(newUserStatus.userId)) |
| | 187 | | { |
| 0 | 188 | | friends.Add(newUserStatus.userId, newUserStatus); |
| 0 | 189 | | } |
| | 190 | | else |
| | 191 | | { |
| 0 | 192 | | friends[newUserStatus.userId] = newUserStatus; |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | public void UpdateUserPresence(string json) |
| | 199 | | { |
| 0 | 200 | | UserStatus newUserStatus = JsonUtility.FromJson<UserStatus>(json); |
| | 201 | |
|
| 0 | 202 | | if (!friends.ContainsKey(newUserStatus.userId)) |
| 0 | 203 | | return; |
| | 204 | |
|
| | 205 | | // Kernel doesn't send the friendship status on this call, we have to keep it or it gets defaulted |
| 0 | 206 | | newUserStatus.friendshipStatus = friends[newUserStatus.userId].friendshipStatus; |
| | 207 | |
|
| 0 | 208 | | UpdateUserStatus(newUserStatus); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | public void UpdateFriendshipStatus(FriendshipUpdateStatusMessage msg) |
| | 212 | | { |
| 6 | 213 | | var friendshipStatus = ToFriendshipStatus(msg.action); |
| 6 | 214 | | var userId = msg.userId; |
| | 215 | |
|
| 6 | 216 | | if (friends.ContainsKey(userId) && friends[userId].friendshipStatus == friendshipStatus) |
| 0 | 217 | | return; |
| | 218 | |
|
| 6 | 219 | | if (!friends.ContainsKey(userId)) |
| 2 | 220 | | friends.Add(userId, new UserStatus()); |
| | 221 | |
|
| 6 | 222 | | if (ItsAnOutdatedUpdate(userId, friendshipStatus)) |
| 0 | 223 | | return; |
| | 224 | |
|
| 6 | 225 | | friends[userId].friendshipStatus = friendshipStatus; |
| | 226 | |
|
| 6 | 227 | | if (friendshipStatus == FriendshipStatus.FRIEND) |
| 2 | 228 | | friends[userId].friendshipStartedTime = DateTime.UtcNow; |
| | 229 | |
|
| 6 | 230 | | if (VERBOSE) |
| 0 | 231 | | Debug.Log($"Change friend status of {userId} to {friends[userId].friendshipStatus}"); |
| | 232 | |
|
| 6 | 233 | | if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| 2 | 234 | | friends.Remove(userId); |
| | 235 | |
|
| 6 | 236 | | OnUpdateFriendship?.Invoke(userId, msg.action); |
| 6 | 237 | | } |
| | 238 | |
|
| | 239 | | public void UpdateFriendshipStatus(string json) |
| | 240 | | { |
| 0 | 241 | | FriendshipUpdateStatusMessage msg = JsonUtility.FromJson<FriendshipUpdateStatusMessage>(json); |
| 0 | 242 | | UpdateFriendshipStatus(msg); |
| 0 | 243 | | } |
| | 244 | |
|
| | 245 | | private bool ItsAnOutdatedUpdate(string userId, FriendshipStatus friendshipStatus) |
| | 246 | | { |
| 6 | 247 | | return friendshipStatus == FriendshipStatus.REQUESTED_FROM |
| | 248 | | && friends[userId].friendshipStatus == FriendshipStatus.FRIEND |
| | 249 | | && (DateTime.UtcNow - friends[userId].friendshipStartedTime).TotalSeconds < 5; |
| | 250 | | } |
| | 251 | |
|
| | 252 | | private static FriendshipStatus ToFriendshipStatus(FriendshipAction action) |
| | 253 | | { |
| | 254 | | switch (action) |
| | 255 | | { |
| | 256 | | case FriendshipAction.NONE: |
| | 257 | | break; |
| | 258 | | case FriendshipAction.APPROVED: |
| 2 | 259 | | return FriendshipStatus.FRIEND; |
| | 260 | | case FriendshipAction.REJECTED: |
| 0 | 261 | | return FriendshipStatus.NOT_FRIEND; |
| | 262 | | case FriendshipAction.CANCELLED: |
| 0 | 263 | | return FriendshipStatus.NOT_FRIEND; |
| | 264 | | case FriendshipAction.REQUESTED_FROM: |
| 0 | 265 | | return FriendshipStatus.REQUESTED_FROM; |
| | 266 | | case FriendshipAction.REQUESTED_TO: |
| 2 | 267 | | return FriendshipStatus.REQUESTED_TO; |
| | 268 | | case FriendshipAction.DELETED: |
| 2 | 269 | | return FriendshipStatus.NOT_FRIEND; |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | return FriendshipStatus.NOT_FRIEND; |
| | 273 | | } |
| | 274 | |
|
| | 275 | | [ContextMenu("Change user stats to online")] |
| | 276 | | public void FakeOnlineFriend() |
| | 277 | | { |
| 0 | 278 | | var friend = friends.Values.First(); |
| 0 | 279 | | UpdateUserStatus(new UserStatus |
| | 280 | | { |
| | 281 | | userId = friend.userId, |
| | 282 | | position = friend.position, |
| | 283 | | presence = PresenceStatus.ONLINE, |
| | 284 | | friendshipStatus = friend.friendshipStatus, |
| | 285 | | friendshipStartedTime = friend.friendshipStartedTime |
| | 286 | | }); |
| 0 | 287 | | } |
| | 288 | | } |