| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | | using TMPro; |
| | 6 | | using System.Linq; |
| | 7 | | using DCL; |
| | 8 | | using static DCL.Interface.WebInterface; |
| | 9 | | using DCL.Interface; |
| | 10 | |
|
| | 11 | | internal class HotSceneCellView : MonoBehaviour |
| | 12 | | { |
| | 13 | | const int THMBL_MARKETPLACE_WIDTH = 196; |
| | 14 | | const int THMBL_MARKETPLACE_HEIGHT = 143; |
| | 15 | | const int THMBL_MARKETPLACE_SIZEFACTOR = 50; |
| | 16 | |
|
| | 17 | | [Header("Animators")] |
| | 18 | | [SerializeField] Animator viewAnimator; |
| | 19 | |
|
| | 20 | | [SerializeField] ShowHideAnimator jumpInButtonAnimator; |
| | 21 | |
|
| | 22 | | [Header("Crowd")] |
| | 23 | | [SerializeField] GameObject crowdCountContainer; |
| | 24 | |
|
| | 25 | | [SerializeField] TextMeshProUGUI crowdCount; |
| | 26 | |
|
| | 27 | | [Header("Events")] |
| | 28 | | [SerializeField] GameObject eventsContainer; |
| | 29 | |
|
| | 30 | | [Header("Friends")] |
| | 31 | | [SerializeField] ExploreFriendsView friendsView; |
| | 32 | |
|
| | 33 | | [SerializeField] GameObject friendsContainer; |
| | 34 | |
|
| | 35 | | [Header("Scene")] |
| | 36 | | [SerializeField] TextMeshProUGUI sceneName; |
| | 37 | |
|
| | 38 | | [SerializeField] internal RawImageFillParent thumbnailImage; |
| | 39 | | [SerializeField] UIHoverCallback sceneInfoButton; |
| | 40 | |
|
| | 41 | | [Header("UI")] |
| | 42 | | [SerializeField] UIHoverCallback jumpInHoverArea; |
| | 43 | |
|
| | 44 | | [SerializeField] Button_OnPointerDown jumpIn; |
| | 45 | | [SerializeField] Sprite errorThumbnail; |
| | 46 | |
|
| | 47 | | public delegate void JumpInDelegate(Vector2Int coords, string serverName, string layerName); |
| | 48 | |
|
| | 49 | | public static event JumpInDelegate OnJumpIn; |
| | 50 | |
|
| | 51 | | public static event Action<HotSceneCellView> OnInfoButtonPointerDown; |
| | 52 | | public static event Action OnInfoButtonPointerExit; |
| | 53 | |
|
| | 54 | | public event Action<Texture2D> OnThumbnailSet; |
| | 55 | |
|
| 0 | 56 | | public MapInfoHandler mapInfoHandler { private set; get; } |
| 0 | 57 | | public FriendsHandler friendsHandler { private set; get; } |
| 0 | 58 | | public ThumbnailHandler thumbnailHandler { private set; get; } |
| 0 | 59 | | public AnimationHandler animationHandler { private set; get; } |
| | 60 | |
|
| | 61 | | private ViewPool<ExploreFriendsView> friendPool; |
| | 62 | | private Dictionary<string, ExploreFriendsView> friendViewById; |
| | 63 | | private bool isLoaded = false; |
| | 64 | | private bool isInitialized = false; |
| 44 | 65 | | private Queue<HotScenesController.HotSceneInfo.Realm> nextMostPopulatedRealms = new Queue<HotScenesController.HotSce |
| 44 | 66 | | private JumpInPayload lastJumpInTried = new JumpInPayload(); |
| | 67 | |
|
| | 68 | | HotScenesController.HotSceneInfo hotSceneInfo; |
| | 69 | |
|
| | 70 | | protected void Awake() |
| | 71 | | { |
| 8 | 72 | | crowdCountContainer.SetActive(false); |
| 8 | 73 | | eventsContainer.SetActive(false); |
| | 74 | |
|
| 8 | 75 | | jumpInHoverArea.OnPointerEnter += () => |
| | 76 | | { |
| 0 | 77 | | jumpInButtonAnimator.gameObject.SetActive(true); |
| 0 | 78 | | jumpInButtonAnimator.Show(); |
| 0 | 79 | | }; |
| 8 | 80 | | jumpInHoverArea.OnPointerExit += () => jumpInButtonAnimator.Hide(); |
| 8 | 81 | | sceneInfoButton.OnPointerDown += () => jumpInButtonAnimator.Hide(true); |
| | 82 | |
|
| | 83 | | // NOTE: we don't use the pointer down callback to avoid being mistakenly pressed while dragging |
| 8 | 84 | | jumpIn.onClick.AddListener(JumpInPressed); |
| | 85 | |
|
| 8 | 86 | | sceneInfoButton.OnPointerDown += () => OnInfoButtonPointerDown?.Invoke(this); |
| 8 | 87 | | sceneInfoButton.OnPointerExit += () => OnInfoButtonPointerExit?.Invoke(); |
| | 88 | |
|
| 8 | 89 | | Initialize(); |
| 8 | 90 | | } |
| | 91 | |
|
| | 92 | | public void Initialize() |
| | 93 | | { |
| 14 | 94 | | if (isInitialized) |
| 6 | 95 | | return; |
| | 96 | |
|
| 8 | 97 | | isInitialized = true; |
| | 98 | |
|
| 8 | 99 | | friendPool = new ViewPool<ExploreFriendsView>(friendsView, 0); |
| 8 | 100 | | friendViewById = new Dictionary<string, ExploreFriendsView>(); |
| | 101 | |
|
| 8 | 102 | | mapInfoHandler = new MapInfoHandler(); |
| | 103 | |
|
| 8 | 104 | | friendsHandler = new FriendsHandler(mapInfoHandler); |
| 8 | 105 | | friendsHandler.OnFriendAddedEvent += OnFriendAdded; |
| 8 | 106 | | friendsHandler.OnFriendRemovedEvent += OnFriendRemoved; |
| | 107 | |
|
| 8 | 108 | | thumbnailHandler = new ThumbnailHandler(); |
| 8 | 109 | | animationHandler = new AnimationHandler(viewAnimator); |
| 8 | 110 | | } |
| | 111 | |
|
| | 112 | | public void Setup(HotScenesController.HotSceneInfo sceneInfo) |
| | 113 | | { |
| 6 | 114 | | hotSceneInfo = sceneInfo; |
| 6 | 115 | | mapInfoHandler.SetMinimapSceneInfo(sceneInfo); |
| | 116 | |
|
| 6 | 117 | | OnCrowdInfoUpdated(sceneInfo); |
| 6 | 118 | | OnMapInfoUpdated(sceneInfo); |
| 6 | 119 | | } |
| | 120 | |
|
| | 121 | | public void Clear() |
| | 122 | | { |
| 0 | 123 | | thumbnailImage.texture = null; |
| 0 | 124 | | thumbnailHandler.Dispose(); |
| 0 | 125 | | thumbnailImage.gameObject.SetActive(false); |
| 0 | 126 | | isLoaded = false; |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public void JumpInPressed() |
| | 130 | | { |
| 0 | 131 | | HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv |
| 0 | 132 | | hotSceneInfo.realms = hotSceneInfo.realms.OrderByDescending(x => x.usersCount).ToArray(); |
| 0 | 133 | | nextMostPopulatedRealms.Clear(); |
| 0 | 134 | | for (int i = 0; i < hotSceneInfo.realms.Length; i++) |
| | 135 | | { |
| 0 | 136 | | bool isArchipelagoRealm = string.IsNullOrEmpty(hotSceneInfo.realms[i].layer); |
| | 137 | |
|
| 0 | 138 | | if (isArchipelagoRealm || hotSceneInfo.realms[i].usersCount < hotSceneInfo.realms[i].maxUsers) |
| | 139 | | { |
| 0 | 140 | | realm = hotSceneInfo.realms[i]; |
| 0 | 141 | | if (i < hotSceneInfo.realms.Length - 1) |
| | 142 | | { |
| 0 | 143 | | nextMostPopulatedRealms = new Queue<HotScenesController.HotSceneInfo.Realm>(hotSceneInfo.realms.ToLi |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | break; |
| | 147 | | } |
| | 148 | | } |
| | 149 | |
|
| 0 | 150 | | if (!string.IsNullOrEmpty(realm.serverName)) |
| | 151 | | { |
| 0 | 152 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 0 | 153 | | RealmsInfoBridge.OnRealmConnectionSuccess += OnRealmConnectionSuccess; |
| 0 | 154 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 0 | 155 | | RealmsInfoBridge.OnRealmConnectionFailed += OnRealmConnectionFailed; |
| | 156 | | } |
| | 157 | |
|
| 0 | 158 | | SetLastJumpInTried(hotSceneInfo.baseCoords, realm.serverName, realm.layer); |
| 0 | 159 | | OnJumpIn?.Invoke(hotSceneInfo.baseCoords, realm.serverName, realm.layer); |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | private void SetLastJumpInTried(Vector2 position, string serverName, string layer) |
| | 163 | | { |
| 0 | 164 | | lastJumpInTried.gridPosition = position; |
| 0 | 165 | | lastJumpInTried.realm.serverName = serverName; |
| 0 | 166 | | lastJumpInTried.realm.layer = layer; |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | private void OnRealmConnectionSuccess(JumpInPayload successRealm) |
| | 170 | | { |
| 0 | 171 | | bool isArchipelagoRealm = string.IsNullOrEmpty(successRealm.realm.layer); |
| | 172 | |
|
| 0 | 173 | | if (successRealm.gridPosition != lastJumpInTried.gridPosition || |
| | 174 | | successRealm.realm.serverName != lastJumpInTried.realm.serverName || |
| | 175 | | (!isArchipelagoRealm && successRealm.realm.layer != lastJumpInTried.realm.layer)) |
| 0 | 176 | | return; |
| | 177 | |
|
| 0 | 178 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 0 | 179 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | private void OnRealmConnectionFailed(JumpInPayload failedRealm) |
| | 183 | | { |
| 0 | 184 | | bool isArchipelagoRealm = string.IsNullOrEmpty(failedRealm.realm.layer); |
| | 185 | |
|
| 0 | 186 | | if (failedRealm.gridPosition != lastJumpInTried.gridPosition || |
| | 187 | | failedRealm.realm.serverName != lastJumpInTried.realm.serverName || |
| | 188 | | (!isArchipelagoRealm && failedRealm.realm.layer != lastJumpInTried.realm.layer)) |
| 0 | 189 | | return; |
| | 190 | |
|
| 0 | 191 | | if (nextMostPopulatedRealms.Count > 0) |
| | 192 | | { |
| 0 | 193 | | WebInterface.NotifyStatusThroughChat("Trying to connect to the next more populated realm..."); |
| 0 | 194 | | HotScenesController.HotSceneInfo.Realm nextRealmToTry = nextMostPopulatedRealms.Dequeue(); |
| 0 | 195 | | SetLastJumpInTried(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer); |
| 0 | 196 | | OnJumpIn?.Invoke(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer); |
| 0 | 197 | | } |
| | 198 | | else |
| | 199 | | { |
| 0 | 200 | | WebInterface.NotifyStatusThroughChat("You'll stay in your current realm."); |
| 0 | 201 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 0 | 202 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 0 | 203 | | OnJumpIn?.Invoke(hotSceneInfo.baseCoords, null, null); |
| | 204 | | } |
| 0 | 205 | | } |
| | 206 | |
|
| | 207 | | private void OnDestroy() |
| | 208 | | { |
| 8 | 209 | | friendPool.Dispose(); |
| 8 | 210 | | thumbnailHandler.Dispose(); |
| | 211 | |
|
| 8 | 212 | | friendsHandler.OnFriendAddedEvent -= OnFriendAdded; |
| 8 | 213 | | friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved; |
| | 214 | |
|
| 8 | 215 | | RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed; |
| 8 | 216 | | RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess; |
| 8 | 217 | | } |
| | 218 | |
|
| | 219 | | private void OnEnable() |
| | 220 | | { |
| 10 | 221 | | jumpInButtonAnimator.gameObject.SetActive(false); |
| 10 | 222 | | jumpInHoverArea.enabled = isLoaded; |
| 10 | 223 | | thumbnailImage.gameObject.SetActive(isLoaded); |
| | 224 | |
|
| 10 | 225 | | if (isLoaded) |
| | 226 | | { |
| 0 | 227 | | animationHandler.SetLoaded(); |
| | 228 | | } |
| 10 | 229 | | } |
| | 230 | |
|
| | 231 | | private void OnCrowdInfoUpdated(HotScenesController.HotSceneInfo info) |
| | 232 | | { |
| 6 | 233 | | crowdCount.text = info.usersTotalCount.ToString(); |
| 6 | 234 | | crowdCountContainer.SetActive(info.usersTotalCount > 0); |
| 6 | 235 | | } |
| | 236 | |
|
| | 237 | | private void OnFriendAdded(UserProfile profile, Color backgroundColor) |
| | 238 | | { |
| 2 | 239 | | var view = friendPool.GetView(); |
| 2 | 240 | | view.SetUserProfile(profile, backgroundColor); |
| 2 | 241 | | friendViewById.Add(profile.userId, view); |
| 2 | 242 | | } |
| | 243 | |
|
| | 244 | | private void OnFriendRemoved(UserProfile profile) |
| | 245 | | { |
| 2 | 246 | | if (friendViewById.TryGetValue(profile.userId, out ExploreFriendsView view)) |
| | 247 | | { |
| 2 | 248 | | friendPool.PoolView(view); |
| 2 | 249 | | friendViewById.Remove(profile.userId); |
| | 250 | | } |
| 2 | 251 | | } |
| | 252 | |
|
| | 253 | | private void OnMapInfoUpdated(HotScenesController.HotSceneInfo sceneInfo) |
| | 254 | | { |
| 6 | 255 | | sceneName.text = sceneInfo.name; |
| | 256 | |
|
| 6 | 257 | | FetchThumbnail(sceneInfo.thumbnail, |
| 6 | 258 | | onFail: () => FetchThumbnail(MapUtils.GetMarketPlaceThumbnailUrl(sceneInfo.parcels, THMBL_MARKETPLACE_WIDTH, |
| 0 | 259 | | onFail: () => SetThumbnail(errorThumbnail.texture))); |
| 6 | 260 | | } |
| | 261 | |
|
| 24 | 262 | | private void FetchThumbnail(string url, Action onFail) { thumbnailHandler.FetchThumbnail(url, SetThumbnail, onFail); |
| | 263 | |
|
| | 264 | | private void SetThumbnail(Texture2D texture) |
| | 265 | | { |
| 0 | 266 | | thumbnailImage.texture = texture; |
| 0 | 267 | | thumbnailImage.gameObject.SetActive(true); |
| 0 | 268 | | OnThumbnailSet?.Invoke(texture); |
| | 269 | |
|
| 0 | 270 | | SetLoaded(); |
| 0 | 271 | | } |
| | 272 | |
|
| | 273 | | private void SetLoaded() |
| | 274 | | { |
| 0 | 275 | | isLoaded = true; |
| 0 | 276 | | animationHandler.SetLoaded(); |
| 0 | 277 | | jumpInHoverArea.enabled = true; |
| 0 | 278 | | } |
| | 279 | | } |