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