| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Social.Friends; |
| | 3 | | using DCLServices.MapRendererV2.CoordsUtils; |
| | 4 | | using DCLServices.MapRendererV2.Culling; |
| | 5 | | using DCLServices.MapRendererV2.MapCameraController; |
| | 6 | | using MainScripts.DCL.Helpers.Utils; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Threading; |
| | 10 | | using UnityEngine; |
| | 11 | | using UnityEngine.Pool; |
| | 12 | | using UnityEngine.SocialPlatforms.Impl; |
| | 13 | | using Environment = DCL.Environment; |
| | 14 | |
|
| | 15 | | namespace DCLServices.MapRendererV2.MapLayers.UsersMarkers.Friends |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Updates players' positions within the comms radius |
| | 19 | | /// </summary> |
| | 20 | | internal class FriendsMarkersAreaController : MapLayerControllerBase, IMapLayerController, IZoomScalingLayer |
| | 21 | | { |
| | 22 | | internal const int PREWARM_PER_FRAME = 20; |
| | 23 | |
|
| | 24 | | private readonly IUnityObjectPool<FriendUserMarkerObject> objectsPool; |
| | 25 | | private readonly IObjectPool<IFriendUserMarker> wrapsPool; |
| | 26 | | private readonly IUserProfileBridge userProfileBridge; |
| | 27 | | private readonly IFriendsController friendsController; |
| | 28 | |
|
| | 29 | | private readonly BaseDictionary<string, Player> otherPlayers; |
| | 30 | |
|
| | 31 | | private readonly int prewarmCount; |
| | 32 | |
|
| 96 | 33 | | private readonly Dictionary<string, IFriendUserMarker> markers = new (); |
| | 34 | |
|
| 0 | 35 | | internal IReadOnlyDictionary<string, IFriendUserMarker> Markers => markers; |
| | 36 | |
|
| | 37 | | public FriendsMarkersAreaController(BaseDictionary<string, Player> otherPlayers, |
| | 38 | | IUnityObjectPool<FriendUserMarkerObject> objectsPool, IObjectPool<IFriendUserMarker> wrapsPool, |
| | 39 | | int prewarmCount, Transform parent, ICoordsUtils coordsUtils, IMapCullingController cullingController, IUser |
| | 40 | | IFriendsController friendsController) |
| 96 | 41 | | : base(parent, coordsUtils, cullingController) |
| | 42 | | { |
| 96 | 43 | | this.otherPlayers = otherPlayers; |
| 96 | 44 | | this.objectsPool = objectsPool; |
| 96 | 45 | | this.prewarmCount = prewarmCount; |
| 96 | 46 | | this.wrapsPool = wrapsPool; |
| 96 | 47 | | this.userProfileBridge = userProfileBridge; |
| 96 | 48 | | this.friendsController = friendsController; |
| 96 | 49 | | } |
| | 50 | |
|
| | 51 | | public UniTask Initialize(CancellationToken cancellationToken) |
| | 52 | | { |
| 96 | 53 | | wrapsPool.Prewarm(prewarmCount); |
| 96 | 54 | | return objectsPool.PrewarmAsync(prewarmCount, PREWARM_PER_FRAME, LinkWithDisposeToken(cancellationToken).Tok |
| | 55 | | } |
| | 56 | |
|
| | 57 | | protected override void DisposeImpl() |
| | 58 | | { |
| 6 | 59 | | objectsPool.Clear(); |
| 6 | 60 | | wrapsPool.Clear(); |
| | 61 | |
|
| 6 | 62 | | otherPlayers.OnAdded -= OnOtherPlayerAdded; |
| 6 | 63 | | otherPlayers.OnRemoved -= OnOtherPlayerRemoved; |
| 6 | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnOtherPlayerAdded(string id, Player player) |
| | 67 | | { |
| 0 | 68 | | CheckIfIsFriend(player).Forget(); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private async UniTaskVoid CheckIfIsFriend(Player player) |
| | 72 | | { |
| 0 | 73 | | if (await friendsController.GetFriendshipStatus(player.id) != FriendshipStatus.FRIEND) |
| 0 | 74 | | return; |
| | 75 | |
|
| 0 | 76 | | if (markers.ContainsKey(player.id)) |
| 0 | 77 | | return; |
| | 78 | |
|
| 0 | 79 | | UserProfile recipientProfile = userProfileBridge.Get(player.id); |
| | 80 | |
|
| 0 | 81 | | try { recipientProfile ??= await userProfileBridge.RequestFullUserProfileAsync(player.id, CancellationToken. |
| 0 | 82 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 83 | | { |
| 0 | 84 | | throw; |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | var wrap = wrapsPool.Get(); |
| 0 | 88 | | wrap.TrackPlayer(player); |
| 0 | 89 | | wrap.SetProfilePicture(recipientProfile.face256SnapshotURL); |
| 0 | 90 | | wrap.SetPlayerName(player.name); |
| 0 | 91 | | markers.Add(player.id, wrap); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | private void OnOtherPlayerRemoved(string id, Player player) |
| | 95 | | { |
| 0 | 96 | | if (markers.TryGetValue(id, out var marker)) |
| | 97 | | { |
| 0 | 98 | | wrapsPool.Release(marker); |
| 0 | 99 | | markers.Remove(id); |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public void ApplyCameraZoom(float baseZoom, float zoom) |
| | 104 | | { |
| 0 | 105 | | foreach (var marker in markers.Values) |
| 0 | 106 | | marker.SetZoom(coordsUtils.ParcelSize, baseZoom, zoom); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public void ResetToBaseScale() |
| | 110 | | { |
| 0 | 111 | | foreach (var marker in markers.Values) |
| 0 | 112 | | marker.ResetScale(coordsUtils.ParcelSize); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public UniTask Enable(CancellationToken cancellationToken) |
| | 116 | | { |
| 0 | 117 | | foreach (var pair in otherPlayers.Get()) |
| 0 | 118 | | OnOtherPlayerAdded(pair.Key, pair.Value); |
| | 119 | |
|
| 0 | 120 | | otherPlayers.OnAdded += OnOtherPlayerAdded; |
| 0 | 121 | | otherPlayers.OnRemoved += OnOtherPlayerRemoved; |
| 0 | 122 | | return UniTask.CompletedTask; |
| | 123 | | } |
| | 124 | |
|
| | 125 | | public UniTask Disable(CancellationToken cancellationToken) |
| | 126 | | { |
| 88 | 127 | | otherPlayers.OnAdded -= OnOtherPlayerAdded; |
| 88 | 128 | | otherPlayers.OnRemoved -= OnOtherPlayerRemoved; |
| | 129 | |
|
| 176 | 130 | | foreach (IFriendUserMarker marker in markers.Values) |
| 0 | 131 | | wrapsPool.Release(marker); |
| | 132 | |
|
| 88 | 133 | | markers.Clear(); |
| 88 | 134 | | return UniTask.CompletedTask; |
| | 135 | | } |
| | 136 | | } |
| | 137 | | } |