| | 1 | | using DCL; |
| | 2 | | using MainScripts.DCL.Controllers.HotScenes; |
| | 3 | | using System; |
| | 4 | | using System.Globalization; |
| | 5 | | using UnityEngine; |
| | 6 | | using Environment = DCL.Environment; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Utils related to the events management in ExploreV2. |
| | 10 | | /// </summary> |
| | 11 | | public static class EventsCardsConfigurator |
| | 12 | | { |
| | 13 | | internal const string LIVE_TAG_TEXT = "LIVE"; |
| | 14 | | private static Service<IHotScenesFetcher> hotScenesFetcher; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Configure a event card with the given model. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="eventCard">Event card to configure.</param> |
| | 20 | | /// <param name="eventInfo">Model to apply.</param> |
| | 21 | | /// <param name="OnEventInfoClicked">Action to inform when the Info button has been clicked.</param> |
| | 22 | | /// <param name="OnEventJumpInClicked">Action to inform when the JumpIn button has been clicked.</param> |
| | 23 | | /// <param name="OnEventSubscribeEventClicked">Action to inform when the Subscribe button has been clicked.</param> |
| | 24 | | /// <param name="OnEventUnsubscribeEventClicked">Action to inform when the Unsubscribe button has been clicked.</par |
| | 25 | | public static EventCardComponentView Configure(EventCardComponentView eventCard, EventCardComponentModel eventInfo, |
| | 26 | | Action<string, bool> OnEventUnsubscribeEventClicked) |
| | 27 | | { |
| 8 | 28 | | eventCard.Configure(eventInfo); |
| | 29 | |
|
| 8 | 30 | | eventCard.onInfoClick?.RemoveAllListeners(); |
| 8 | 31 | | eventCard.onInfoClick?.AddListener(() => OnEventInfoClicked?.Invoke(eventInfo)); |
| 8 | 32 | | eventCard.onBackgroundClick?.RemoveAllListeners(); |
| 8 | 33 | | eventCard.onBackgroundClick?.AddListener(() => OnEventInfoClicked?.Invoke(eventInfo)); |
| 8 | 34 | | eventCard.onJumpInClick?.RemoveAllListeners(); |
| 8 | 35 | | eventCard.onJumpInClick?.AddListener(() => OnEventJumpInClicked?.Invoke(eventInfo.eventFromAPIInfo)); |
| 8 | 36 | | eventCard.onSecondaryJumpInClick?.RemoveAllListeners(); |
| 8 | 37 | | eventCard.onSecondaryJumpInClick?.AddListener(() => OnEventJumpInClicked?.Invoke(eventInfo.eventFromAPIInfo)); |
| 8 | 38 | | eventCard.onSubscribeClick?.AddListener(() => OnEventSubscribeEventClicked?.Invoke(eventInfo.eventId, !string.Is |
| 8 | 39 | | eventCard.onUnsubscribeClick?.AddListener(() => OnEventUnsubscribeEventClicked?.Invoke(eventInfo.eventId, !strin |
| | 40 | |
|
| 8 | 41 | | return eventCard; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public static EventCardComponentModel ConfigureFromAPIData(EventCardComponentModel cardModel, EventFromAPIModel even |
| | 45 | | { |
| 8 | 46 | | cardModel.eventId = eventFromAPI.id; |
| 8 | 47 | | cardModel.eventPictureUri = eventFromAPI.image; |
| 8 | 48 | | cardModel.isLive = eventFromAPI.live; |
| 8 | 49 | | cardModel.liveTagText = LIVE_TAG_TEXT; |
| 8 | 50 | | cardModel.eventDateText = FormatEventDate(eventFromAPI); |
| 8 | 51 | | cardModel.eventName = eventFromAPI.name; |
| 8 | 52 | | cardModel.eventDescription = eventFromAPI.description; |
| 8 | 53 | | cardModel.eventStartedIn = FormatEventStartDate(eventFromAPI); |
| 8 | 54 | | cardModel.eventStartsInFromTo = FormatEventStartDateFromTo(eventFromAPI); |
| 8 | 55 | | cardModel.eventOrganizer = FormatEventOrganized(eventFromAPI); |
| 8 | 56 | | cardModel.eventPlace = FormatEventPlace(eventFromAPI); |
| 8 | 57 | | cardModel.subscribedUsers = eventFromAPI.total_attendees; |
| 8 | 58 | | cardModel.isSubscribed = false; |
| 8 | 59 | | cardModel.coords = new Vector2Int(eventFromAPI.coordinates[0], eventFromAPI.coordinates[1]); |
| 8 | 60 | | cardModel.eventFromAPIInfo = eventFromAPI; |
| 8 | 61 | | cardModel.numberOfUsers = eventFromAPI.world ? GetNumberOfUsersInWorld(eventFromAPI.server) : GetNumberOfUsersIn |
| 8 | 62 | | cardModel.worldAddress = eventFromAPI.world ? eventFromAPI.server : null; |
| | 63 | |
|
| 8 | 64 | | return cardModel; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | private static int GetNumberOfUsersInCoords(Vector2Int coords) |
| | 68 | | { |
| 7 | 69 | | var numberOfUsers = 0; |
| | 70 | |
|
| 7 | 71 | | if (hotScenesFetcher.Ref.ScenesInfo == null) |
| 0 | 72 | | return numberOfUsers; |
| | 73 | |
|
| 7 | 74 | | var sceneFound = false; |
| 14 | 75 | | foreach (var hotSceneInfo in hotScenesFetcher.Ref.ScenesInfo.Value) |
| | 76 | | { |
| 0 | 77 | | foreach (Vector2Int hotSceneParcel in hotSceneInfo.parcels) |
| | 78 | | { |
| 0 | 79 | | if (hotSceneParcel != coords) |
| | 80 | | continue; |
| | 81 | |
|
| 0 | 82 | | numberOfUsers = hotSceneInfo.usersTotalCount; |
| 0 | 83 | | sceneFound = true; |
| 0 | 84 | | break; |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | if (sceneFound) |
| 0 | 88 | | break; |
| | 89 | | } |
| | 90 | |
|
| 7 | 91 | | return numberOfUsers; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | private static int GetNumberOfUsersInWorld(string worldName) |
| | 95 | | { |
| 1 | 96 | | var numberOfUsers = 0; |
| | 97 | |
|
| 1 | 98 | | if (hotScenesFetcher.Ref.WorldsInfo == null) |
| 0 | 99 | | return numberOfUsers; |
| | 100 | |
|
| 2 | 101 | | foreach (var worldInfo in hotScenesFetcher.Ref.WorldsInfo.Value) |
| | 102 | | { |
| 0 | 103 | | if (worldInfo.worldName == worldName) |
| | 104 | | { |
| 0 | 105 | | numberOfUsers = worldInfo.users; |
| 0 | 106 | | break; |
| | 107 | | } |
| | 108 | | } |
| | 109 | |
|
| 1 | 110 | | return numberOfUsers; |
| | 111 | | } |
| | 112 | |
|
| | 113 | | internal static string FormatEventDate(EventFromAPIModel eventFromAPI) |
| | 114 | | { |
| 10 | 115 | | DateTime eventDateTime = Convert.ToDateTime(eventFromAPI.next_start_at).ToUniversalTime(); |
| 10 | 116 | | return eventDateTime.ToString("MMMM d", new CultureInfo("en-US")); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | internal static string FormatEventStartDate(EventFromAPIModel eventFromAPI) |
| | 120 | | { |
| 10 | 121 | | DateTime eventDateTime = Convert.ToDateTime(eventFromAPI.next_start_at).ToUniversalTime(); |
| | 122 | | string formattedDate; |
| | 123 | |
|
| 10 | 124 | | if (eventFromAPI.live) |
| | 125 | | { |
| 10 | 126 | | int daysAgo = (int)Math.Ceiling((DateTime.Now - eventDateTime).TotalDays); |
| 10 | 127 | | int hoursAgo = (int)Math.Ceiling((DateTime.Now - eventDateTime).TotalHours); |
| | 128 | |
|
| 10 | 129 | | if (daysAgo > 0) |
| 10 | 130 | | formattedDate = $"{daysAgo} days ago"; |
| | 131 | | else |
| 0 | 132 | | formattedDate = $"{hoursAgo} hr ago"; |
| | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 0 | 136 | | int daysToStart = (int)Math.Ceiling((eventDateTime - DateTime.Now).TotalDays); |
| 0 | 137 | | int hoursToStart = (int)Math.Ceiling((eventDateTime - DateTime.Now).TotalHours); |
| | 138 | |
|
| 0 | 139 | | if (daysToStart > 0) |
| 0 | 140 | | formattedDate = $"in {daysToStart} days"; |
| | 141 | | else |
| 0 | 142 | | formattedDate = $"in {hoursToStart} hours"; |
| | 143 | | } |
| | 144 | |
|
| 10 | 145 | | return formattedDate; |
| | 146 | | } |
| | 147 | |
|
| | 148 | | internal static string FormatEventStartDateFromTo(EventFromAPIModel eventFromAPI) |
| | 149 | | { |
| 10 | 150 | | CultureInfo cultureInfo = new CultureInfo("en-US"); |
| 10 | 151 | | string formattedDate = string.Empty; |
| | 152 | |
|
| 10 | 153 | | DateTime startTimeDT = Convert.ToDateTime(eventFromAPI.start_at).ToLocalTime(); |
| 10 | 154 | | var startTime12Hour = startTimeDT.ToString("hh:mmtt", CultureInfo.InvariantCulture); |
| 10 | 155 | | startTime12Hour = startTime12Hour.Replace("AM", "am").Replace("PM", "pm"); |
| | 156 | |
|
| 10 | 157 | | DateTime endTimeDT = Convert.ToDateTime(eventFromAPI.finish_at).ToLocalTime(); |
| 10 | 158 | | var endTime12Hour = endTimeDT.ToString("hh:mmtt", CultureInfo.InvariantCulture); |
| 10 | 159 | | endTime12Hour = endTime12Hour.Replace("AM", "am").Replace("PM", "pm"); |
| | 160 | |
|
| 10 | 161 | | TimeSpan startUtcOffset = TimeZoneInfo.Local.GetUtcOffset(startTimeDT); |
| | 162 | |
|
| 40 | 163 | | for (var i = 0; i < eventFromAPI.recurrent_dates.Length; i++) |
| | 164 | | { |
| 10 | 165 | | DateTime recurrentDateDT = Convert.ToDateTime(eventFromAPI.recurrent_dates[i]).ToLocalTime(); |
| | 166 | |
|
| 10 | 167 | | if (recurrentDateDT < DateTime.Today) |
| | 168 | | continue; |
| | 169 | |
|
| 0 | 170 | | var formattedRecurrentDate = $"{recurrentDateDT.ToString("dddd", cultureInfo)}, {recurrentDateDT.ToString("M |
| 0 | 171 | | if (i == eventFromAPI.recurrent_dates.Length - 1 && endTimeDT.DayOfYear > recurrentDateDT.DayOfYear) |
| | 172 | | { |
| 0 | 173 | | var formattedEndDate = $"{endTimeDT.ToString("dddd", cultureInfo)}, {endTimeDT.ToString("MMM", cultureIn |
| | 174 | |
|
| 0 | 175 | | formattedDate = string.Concat( |
| | 176 | | formattedDate, |
| | 177 | | $"{formattedRecurrentDate} from {startTime12Hour} to {formattedEndDate} at {endTime12Hour} (UTC{(sta |
| | 178 | | } |
| | 179 | | else |
| | 180 | | { |
| 0 | 181 | | formattedDate = string.Concat( |
| | 182 | | formattedDate, |
| | 183 | | $"{formattedRecurrentDate} from {startTime12Hour} to {endTime12Hour} (UTC{(startUtcOffset >= TimeSpa |
| | 184 | | } |
| | 185 | | } |
| | 186 | |
|
| 10 | 187 | | return formattedDate; |
| | 188 | | } |
| | 189 | |
|
| | 190 | | internal static string FormatEventOrganized(EventFromAPIModel eventFromAPI) => |
| 10 | 191 | | $"Public, Organized by {eventFromAPI.user_name}"; |
| | 192 | |
|
| | 193 | | internal static string FormatEventPlace(EventFromAPIModel eventFromAPI) => |
| 10 | 194 | | string.IsNullOrEmpty(eventFromAPI.scene_name) ? "Decentraland" : eventFromAPI.scene_name; |
| | 195 | | } |