| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Interface; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | |
|
| | 9 | | namespace DCL.Backpack |
| | 10 | | { |
| | 11 | | public class OutfitsController : IDisposable |
| | 12 | | { |
| | 13 | | private readonly LambdaOutfitsService lambdaOutfitsService; |
| | 14 | | private readonly IUserProfileBridge userProfileBridge; |
| | 15 | | private readonly IOutfitsSectionComponentView view; |
| | 16 | | private readonly IBackpackAnalyticsService backpackAnalyticsService; |
| | 17 | | private readonly DataStore dataStore; |
| | 18 | | public event Action<OutfitItem> OnOutfitEquipped; |
| | 19 | |
|
| | 20 | | private CancellationTokenSource cts; |
| | 21 | | private CancellationTokenSource ctsShowOutfit; |
| | 22 | | private OutfitItem[] localOutfits; |
| | 23 | | private bool shouldDeploy; |
| | 24 | | private AvatarModel currentAvatarModel; |
| | 25 | |
|
| 25 | 26 | | public OutfitsController( |
| | 27 | | IOutfitsSectionComponentView view, |
| | 28 | | LambdaOutfitsService lambdaOutfitsService, |
| | 29 | | IUserProfileBridge userProfileBridge, |
| | 30 | | DataStore dataStore, |
| | 31 | | IBackpackAnalyticsService backpackAnalyticsService) |
| | 32 | | { |
| 25 | 33 | | this.view = view; |
| 25 | 34 | | this.lambdaOutfitsService = lambdaOutfitsService; |
| 25 | 35 | | this.userProfileBridge = userProfileBridge; |
| 25 | 36 | | this.backpackAnalyticsService = backpackAnalyticsService; |
| 25 | 37 | | this.dataStore = dataStore; |
| | 38 | |
|
| 25 | 39 | | view.OnOutfitEquipped += OutfitEquip; |
| 25 | 40 | | view.OnOutfitDiscarded += DiscardOutfit; |
| 25 | 41 | | view.OnTrySaveAsGuest += ShowGuestModal; |
| 25 | 42 | | view.OnOutfitLocalSave += SaveOutfitLocally; |
| | 43 | |
|
| 25 | 44 | | localOutfits = new OutfitItem[] |
| | 45 | | { |
| | 46 | | new () { slot = 0 }, |
| | 47 | | new () { slot = 1 }, |
| | 48 | | new () { slot = 2 }, |
| | 49 | | new () { slot = 3 }, |
| | 50 | | new () { slot = 4 } |
| | 51 | | }; |
| | 52 | |
|
| 25 | 53 | | dataStore.HUDs.avatarEditorVisible.OnChange += ChangedVisibility; |
| 25 | 54 | | } |
| | 55 | |
|
| | 56 | | private void SaveOutfitLocally(int outfitIndex) |
| | 57 | | { |
| 0 | 58 | | ctsShowOutfit.SafeCancelAndDispose(); |
| 0 | 59 | | ctsShowOutfit = new CancellationTokenSource(); |
| 0 | 60 | | var outfitItem = new OutfitItem() |
| | 61 | | { |
| | 62 | | outfit = new OutfitItem.Outfit() |
| | 63 | | { |
| | 64 | | bodyShape = currentAvatarModel.bodyShape, |
| | 65 | | eyes = new OutfitItem.eyes() { color = currentAvatarModel.eyeColor }, |
| | 66 | | hair = new OutfitItem.hair() { color = currentAvatarModel.hairColor }, |
| | 67 | | skin = new OutfitItem.skin() { color = currentAvatarModel.skinColor }, |
| | 68 | | wearables = new List<string>(currentAvatarModel.wearables).ToArray(), |
| | 69 | | forceRender = new List<string>(currentAvatarModel.forceRender).ToArray() |
| | 70 | | }, |
| | 71 | | slot = outfitIndex |
| | 72 | | }; |
| | 73 | |
|
| 0 | 74 | | localOutfits[outfitIndex] = outfitItem; |
| 0 | 75 | | view.ShowOutfit(outfitItem, GenerateAvatarModel(outfitItem), ctsShowOutfit.Token).Forget(); |
| 0 | 76 | | shouldDeploy = true; |
| | 77 | |
|
| 0 | 78 | | backpackAnalyticsService.SendOutfitSave(outfitIndex); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | private void ShowGuestModal() => |
| 0 | 82 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| | 83 | |
|
| | 84 | | private void DiscardOutfit(int outfitIndex) |
| | 85 | | { |
| 0 | 86 | | backpackAnalyticsService.SendOutfitDelete(outfitIndex); |
| 0 | 87 | | localOutfits[outfitIndex] = new () { slot = outfitIndex }; |
| 0 | 88 | | shouldDeploy = true; |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OutfitEquip(OutfitItem outfit) |
| | 92 | | { |
| 0 | 93 | | backpackAnalyticsService.SendOutfitEquipped(outfit.slot); |
| 0 | 94 | | OnOutfitEquipped?.Invoke(outfit); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | private void ChangedVisibility(bool current, bool previous) |
| | 98 | | { |
| 173 | 99 | | if (shouldDeploy) |
| 0 | 100 | | WebInterface.SaveUserOutfits(localOutfits); |
| 173 | 101 | | } |
| | 102 | |
|
| | 103 | | public void RequestOwnedOutfits() |
| | 104 | | { |
| 0 | 105 | | cts.SafeCancelAndDispose(); |
| 0 | 106 | | cts = new CancellationTokenSource(); |
| 0 | 107 | | RequestOwnedOutfitsAsync(cts.Token).Forget(); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private async UniTask RequestOwnedOutfitsAsync(CancellationToken ct) |
| | 111 | | { |
| 0 | 112 | | view.SetIsGuest(userProfileBridge.GetOwn().isGuest); |
| 0 | 113 | | (IReadOnlyList<OutfitItem> outfits, int totalAmount) requestOwnedOutfits = await lambdaOutfitsService.Reques |
| 0 | 114 | | OutfitItem[] outfitItems = requestOwnedOutfits.outfits.ToArray(); |
| 0 | 115 | | AudioScriptableObjects.listItemAppear.ResetPitch(); |
| 0 | 116 | | view.SetSlotsAsLoading(outfitItems); |
| | 117 | |
|
| 0 | 118 | | foreach (OutfitItem outfitItem in outfitItems) |
| | 119 | | { |
| 0 | 120 | | localOutfits[outfitItem.slot] = outfitItem; |
| 0 | 121 | | await view.ShowOutfit(outfitItem, GenerateAvatarModel(outfitItem), ct); |
| | 122 | | } |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | private AvatarModel GenerateAvatarModel(OutfitItem outfitItem) |
| | 126 | | { |
| 0 | 127 | | AvatarModel avatarModel = new AvatarModel(); |
| 0 | 128 | | avatarModel.CopyFrom(currentAvatarModel); |
| 0 | 129 | | avatarModel.bodyShape = outfitItem.outfit.bodyShape; |
| 0 | 130 | | avatarModel.wearables = new List<string>(outfitItem.outfit.wearables.ToList()); |
| 0 | 131 | | avatarModel.eyeColor = outfitItem.outfit.eyes.color; |
| 0 | 132 | | avatarModel.hairColor = outfitItem.outfit.hair.color; |
| 0 | 133 | | avatarModel.skinColor = outfitItem.outfit.skin.color; |
| 0 | 134 | | avatarModel.forceRender = new HashSet<string>(outfitItem.outfit.forceRender); |
| 0 | 135 | | return avatarModel; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | public void UpdateAvatarPreview(AvatarModel newAvatarModel) => |
| 26 | 139 | | currentAvatarModel = newAvatarModel; |
| | 140 | |
|
| | 141 | | public void Dispose() |
| | 142 | | { |
| 0 | 143 | | cts.SafeCancelAndDispose(); |
| 0 | 144 | | cts = null; |
| 0 | 145 | | ctsShowOutfit.SafeCancelAndDispose(); |
| 0 | 146 | | ctsShowOutfit = null; |
| 0 | 147 | | view.OnOutfitEquipped -= OutfitEquip; |
| 0 | 148 | | view.OnOutfitDiscarded -= DiscardOutfit; |
| 0 | 149 | | view.OnTrySaveAsGuest -= ShowGuestModal; |
| | 150 | |
|
| 0 | 151 | | dataStore.HUDs.avatarEditorVisible.OnChange -= ChangedVisibility; |
| 0 | 152 | | } |
| | 153 | | } |
| | 154 | | } |