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