| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | | using System; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Map; |
| | 7 | | using DCL.Tasks; |
| | 8 | | using RPC.Context; |
| | 9 | | using System.Threading; |
| | 10 | |
|
| | 11 | | public class TeleportPromptHUDController : IHUD |
| | 12 | | { |
| | 13 | | private const string TELEPORT_COMMAND_MAGIC = "magic"; |
| | 14 | | private const string TELEPORT_COMMAND_CROWD = "crowd"; |
| | 15 | |
|
| | 16 | | private const string EVENT_STRING_LIVE = "LIVE"; |
| | 17 | | private const string EVENT_STRING_TODAY = "Today @ {0:HH:mm}"; |
| | 18 | | private const int INITIAL_ANIMATION_DELAY = 500; |
| | 19 | |
|
| 28 | 20 | | internal TeleportPromptHUDView view { get; private set; } |
| | 21 | |
|
| | 22 | | private readonly RestrictedActionsContext restrictedActionsServiceContext; |
| | 23 | | private readonly ITeleportController teleportController; |
| | 24 | | private readonly DataStore dataStore; |
| | 25 | | private readonly IMinimapApiBridge minimapApiBridge; |
| | 26 | | private bool isVisible; |
| | 27 | | private TeleportData teleportData; |
| 2 | 28 | | private CancellationTokenSource cancellationToken = new (); |
| | 29 | | private EventData currentEvent; |
| | 30 | |
|
| 2 | 31 | | public TeleportPromptHUDController(DataStore dataStore, |
| | 32 | | IMinimapApiBridge minimapApiBridge, |
| | 33 | | RestrictedActionsContext restrictedActionsContext, |
| | 34 | | ITeleportController teleportController) |
| | 35 | | { |
| 2 | 36 | | this.dataStore = dataStore; |
| 2 | 37 | | this.minimapApiBridge = minimapApiBridge; |
| | 38 | |
|
| 2 | 39 | | view = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("TeleportPromptHUD")).GetComponent<TeleportProm |
| 2 | 40 | | view.name = "_TeleportPromptHUD"; |
| 2 | 41 | | view.OnCloseEvent += ClosePanel; |
| 2 | 42 | | view.OnTeleportEvent += OnTeleportPressed; |
| | 43 | |
|
| 2 | 44 | | dataStore.HUDs.gotoPanelVisible.OnChange += ChangeVisibility; |
| 2 | 45 | | dataStore.HUDs.gotoPanelCoordinates.OnChange += SetCoordinates; |
| 2 | 46 | | dataStore.world.requestTeleportData.OnChange += ReceivedRequestTeleportData; |
| | 47 | |
|
| 2 | 48 | | restrictedActionsServiceContext = restrictedActionsContext; |
| 2 | 49 | | this.teleportController = teleportController; |
| 2 | 50 | | restrictedActionsServiceContext.TeleportToPrompt += RequestCoordinatesTeleport; |
| 2 | 51 | | } |
| | 52 | |
|
| | 53 | | public void Dispose() |
| | 54 | | { |
| 2 | 55 | | view.OnCloseEvent -= CloseView; |
| 2 | 56 | | view.OnTeleportEvent -= OnTeleportPressed; |
| | 57 | |
|
| 2 | 58 | | if (view) |
| | 59 | | { |
| 2 | 60 | | view.OnCloseEvent -= ClosePanel; |
| 2 | 61 | | view.OnTeleportEvent -= OnTeleportPressed; |
| 2 | 62 | | UnityEngine.Object.Destroy(view.gameObject); |
| | 63 | | } |
| | 64 | |
|
| 2 | 65 | | dataStore.HUDs.gotoPanelVisible.OnChange -= ChangeVisibility; |
| 2 | 66 | | dataStore.HUDs.gotoPanelCoordinates.OnChange -= SetCoordinates; |
| 2 | 67 | | restrictedActionsServiceContext.TeleportToPrompt -= RequestCoordinatesTeleport; |
| 2 | 68 | | } |
| | 69 | |
|
| | 70 | | public void SetVisibility(bool visible) |
| | 71 | | { |
| 1 | 72 | | if (visible) |
| | 73 | | { |
| 1 | 74 | | view.SetInAnimation(); |
| 1 | 75 | | AudioScriptableObjects.fadeIn.Play(true); |
| | 76 | | } |
| | 77 | | else |
| | 78 | | { |
| 0 | 79 | | AudioScriptableObjects.dialogClose.Play(true); |
| 0 | 80 | | currentEvent = null; |
| 0 | 81 | | teleportData = null; |
| 0 | 82 | | view.SetOutAnimation(); |
| 0 | 83 | | view.Reset(); |
| 0 | 84 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 85 | | } |
| 1 | 86 | | isVisible = visible; |
| 1 | 87 | | } |
| | 88 | |
|
| | 89 | | public void RequestJSONTeleport(string teleportDataJson) |
| | 90 | | { |
| 1 | 91 | | Utils.UnlockCursor(); |
| | 92 | |
|
| 1 | 93 | | teleportData = Utils.SafeFromJson<TeleportData>(teleportDataJson); |
| 1 | 94 | | currentEvent = teleportData.sceneEvent; |
| 1 | 95 | | SetVisibility(true); |
| | 96 | |
|
| 1 | 97 | | switch (teleportData.destination) |
| | 98 | | { |
| | 99 | | case TELEPORT_COMMAND_MAGIC: |
| 1 | 100 | | view.ShowTeleportToMagic(); |
| 1 | 101 | | break; |
| | 102 | | case TELEPORT_COMMAND_CROWD: |
| 0 | 103 | | view.ShowTeleportToCrowd(); |
| 0 | 104 | | break; |
| | 105 | | default: |
| 0 | 106 | | cancellationToken = cancellationToken.SafeRestart(); |
| 0 | 107 | | SetCoordinatesAsync(CoordinateUtils.ParseCoordinatesString(teleportData.destination), null, null, cancel |
| | 108 | | break; |
| | 109 | | } |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | private void ClosePanel() |
| | 113 | | { |
| 0 | 114 | | SetVisibility(false); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private void ReceivedRequestTeleportData(string current, string previous) |
| | 118 | | { |
| 0 | 119 | | if (string.IsNullOrEmpty(current) || isVisible) |
| 0 | 120 | | return; |
| | 121 | |
|
| 0 | 122 | | RequestJSONTeleport(current); |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | private void SetCoordinates((ParcelCoordinates coordinates, string realm, Action onAcceptedCallback) current, |
| | 126 | | (ParcelCoordinates coordinates, string realm, Action onAcceptedCallback) previous) |
| | 127 | | { |
| 0 | 128 | | if (current == previous) return; |
| | 129 | |
|
| 0 | 130 | | view.Reset(); |
| 0 | 131 | | cancellationToken = cancellationToken.SafeRestart(); |
| 0 | 132 | | SetCoordinatesAsync(current.coordinates, current.realm, current.onAcceptedCallback, cancellationToken.Token).For |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | private async UniTaskVoid SetCoordinatesAsync(ParcelCoordinates coordinates, string realm, |
| | 136 | | Action onAcceptedCallback, CancellationToken cancellationToken) |
| | 137 | | { |
| | 138 | | try |
| | 139 | | { |
| 0 | 140 | | await minimapApiBridge.GetScenesInformationAroundParcel(new Vector2Int(coordinates.x, coordinates.y), 2, can |
| 0 | 141 | | MinimapMetadata.MinimapSceneInfo sceneInfo = MinimapMetadata.GetMetadata().GetSceneInfo(coordinates.x, coord |
| | 142 | |
|
| 0 | 143 | | view.ShowTeleportToCoords(coordinates.ToString(), sceneInfo?.name, sceneInfo?.owner, sceneInfo?.previewImage |
| | 144 | |
|
| 0 | 145 | | teleportData = new TeleportData |
| | 146 | | { |
| | 147 | | coordinates = coordinates, |
| | 148 | | sceneData = sceneInfo, |
| | 149 | | sceneEvent = currentEvent, |
| | 150 | | realm = realm, |
| | 151 | | onAcceptedCallback = onAcceptedCallback, |
| | 152 | | }; |
| | 153 | |
|
| 0 | 154 | | SetSceneEvent(teleportData.sceneEvent); |
| | 155 | |
|
| 0 | 156 | | await UniTask.Delay(INITIAL_ANIMATION_DELAY, cancellationToken: cancellationToken); |
| | 157 | |
|
| 0 | 158 | | view.SetLoadingCompleted(); |
| 0 | 159 | | } |
| 0 | 160 | | catch (OperationCanceledException) |
| | 161 | | { |
| 0 | 162 | | teleportData = new TeleportData |
| | 163 | | { |
| | 164 | | destination = coordinates.ToString(), |
| | 165 | | sceneData = null, |
| | 166 | | sceneEvent = null, |
| | 167 | | }; |
| 0 | 168 | | } |
| 0 | 169 | | catch (Exception e) { Debug.LogException(e); } |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | private void CloseView() => |
| 0 | 173 | | SetVisibility(false); |
| | 174 | |
|
| | 175 | | private void ChangeVisibility(bool current, bool previous) => |
| 0 | 176 | | SetVisibility(current); |
| | 177 | |
|
| | 178 | | private bool RequestCoordinatesTeleport(int xCoordinate, int yCoordinate) |
| | 179 | | { |
| 0 | 180 | | Utils.UnlockCursor(); |
| | 181 | |
|
| 0 | 182 | | ParcelCoordinates coordinates = new ParcelCoordinates(xCoordinate, yCoordinate); |
| 0 | 183 | | teleportData = new TeleportData() { coordinates = coordinates }; |
| | 184 | |
|
| 0 | 185 | | SetVisibility(true); |
| 0 | 186 | | cancellationToken = cancellationToken.SafeRestart(); |
| 0 | 187 | | SetCoordinatesAsync(coordinates, null, null, cancellationToken.Token).Forget(); |
| 0 | 188 | | return true; |
| | 189 | | } |
| | 190 | |
|
| | 191 | | private void SetSceneEvent(EventData eventData) |
| | 192 | | { |
| 0 | 193 | | if (eventData == null) return; |
| | 194 | |
|
| 0 | 195 | | DateTime dateNow = DateTime.Now; |
| | 196 | | DateTime eventStart; |
| | 197 | | DateTime eventEnd; |
| | 198 | |
|
| 0 | 199 | | if (DateTime.TryParse(eventData.start_at, out eventStart) && DateTime.TryParse(eventData.finish_at, out eventEnd |
| | 200 | | { |
| 0 | 201 | | bool startsToday = eventStart.Date == dateNow.Date; |
| 0 | 202 | | bool isNow = eventStart <= dateNow && dateNow <= eventEnd; |
| | 203 | |
|
| 0 | 204 | | if (isNow || startsToday) |
| | 205 | | { |
| 0 | 206 | | string eventStatus = EVENT_STRING_LIVE; |
| | 207 | |
|
| 0 | 208 | | if (!isNow && startsToday) |
| 0 | 209 | | eventStatus = string.Format(EVENT_STRING_TODAY, eventStart); |
| | 210 | |
|
| 0 | 211 | | view.SetEventInfo(eventData.name, eventStatus, eventData.total_attendees); |
| | 212 | | } |
| | 213 | | } |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | private void OnTeleportPressed() |
| | 217 | | { |
| 0 | 218 | | switch (teleportData.destination) |
| | 219 | | { |
| | 220 | | case TELEPORT_COMMAND_CROWD: |
| 0 | 221 | | teleportController.GoToCrowd(); |
| 0 | 222 | | break; |
| | 223 | | case TELEPORT_COMMAND_MAGIC: |
| 0 | 224 | | teleportController.GoToMagic(); |
| 0 | 225 | | break; |
| | 226 | | default: |
| 0 | 227 | | if (!string.IsNullOrEmpty(teleportData.realm)) |
| 0 | 228 | | teleportController.JumpIn(teleportData.GetCoordinates().x, teleportData.GetCoordinates().y, teleport |
| | 229 | | else |
| 0 | 230 | | teleportController.Teleport(teleportData.GetCoordinates().x, teleportData.GetCoordinates().y); |
| | 231 | | break; |
| | 232 | | } |
| | 233 | |
|
| 0 | 234 | | teleportData?.onAcceptedCallback?.Invoke(); |
| | 235 | |
|
| 0 | 236 | | CloseView(); |
| 0 | 237 | | } |
| | 238 | |
|
| | 239 | | [Serializable] |
| | 240 | | internal class TeleportData |
| | 241 | | { |
| | 242 | | public Vector2Int? coordinates; |
| | 243 | |
|
| | 244 | | public Vector2Int GetCoordinates() => |
| 0 | 245 | | (coordinates ??= new Vector2Int(int.TryParse(destination.Split(',')[0], out int x) ? x : 0, |
| | 246 | | int.TryParse(destination.Split(',')[1], out int y) ? y : 0)); |
| | 247 | |
|
| 1 | 248 | | public string destination = ""; |
| | 249 | | public MinimapMetadata.MinimapSceneInfo sceneData; |
| | 250 | | public EventData sceneEvent; |
| | 251 | | public string realm; |
| | 252 | | public Action onAcceptedCallback; |
| | 253 | | } |
| | 254 | |
|
| | 255 | | [Serializable] |
| | 256 | | internal class EventData |
| | 257 | | { |
| | 258 | | public string name; |
| | 259 | | public int total_attendees; |
| | 260 | | public string start_at; |
| | 261 | | public string finish_at; |
| | 262 | | } |
| | 263 | | } |