| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Interface; |
| | 3 | | using UnityEngine; |
| | 4 | | using System; |
| | 5 | | using Variables.RealmsInfo; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using Decentraland.Bff; |
| | 9 | | using Google.Protobuf; |
| | 10 | | using System.Threading; |
| | 11 | |
|
| | 12 | | namespace DCL |
| | 13 | | { |
| | 14 | | public class RealmsInfoHandler : IRealmsInfoBridge |
| | 15 | | { |
| 4 | 16 | | private RealmsInfoModel model = new RealmsInfoModel(); |
| | 17 | |
|
| 1 | 18 | | public CurrentRealmVariable playerRealm => DataStore.i.realm.playerRealm; |
| 1 | 19 | | public BaseCollection<RealmModel> realmsInfo => DataStore.i.realm.realmsInfo; |
| 0 | 20 | | private BaseVariable<AboutResponse.Types.AboutConfiguration> playerRealmAboutConfiguration => DataStore.i.realm. |
| 0 | 21 | | private BaseVariable<AboutResponse.Types.LambdasInfo> playerRealmAboutLambda => DataStore.i.realm.playerRealmAbo |
| 0 | 22 | | private BaseVariable<AboutResponse.Types.ContentInfo> playerRealmAboutContent => DataStore.i.realm.playerRealmAb |
| | 23 | |
|
| 4 | 24 | | private BaseVariable<string> realmName => DataStore.i.realm.realmName; |
| | 25 | | private UniTaskCompletionSource<IReadOnlyList<RealmModel>> fetchRealmsTask; |
| | 26 | |
|
| | 27 | | public void Set(string json) |
| | 28 | | { |
| 2 | 29 | | JsonUtility.FromJsonOverwrite(json, model); |
| 2 | 30 | | Set(model); |
| 2 | 31 | | } |
| | 32 | |
|
| | 33 | | internal void Set(RealmsInfoModel newModel) |
| | 34 | | { |
| 5 | 35 | | model = newModel; |
| | 36 | |
|
| 5 | 37 | | if (!string.IsNullOrEmpty(model.current?.serverName)) |
| | 38 | | { |
| 4 | 39 | | DataStore.i.realm.playerRealm.Set(model.current.Clone()); |
| 4 | 40 | | realmName.Set(DataStore.i.realm.playerRealm.Get().serverName); |
| | 41 | | } |
| | 42 | |
|
| 5 | 43 | | List<RealmModel> realms = newModel.realms != null ? newModel.realms.ToList() : new List<RealmModel>(); |
| 5 | 44 | | DataStore.i.realm.realmsInfo.Set(realms); |
| | 45 | |
|
| 5 | 46 | | if (fetchRealmsTask != null) |
| | 47 | | { |
| 0 | 48 | | fetchRealmsTask.TrySetResult(realms); |
| 0 | 49 | | fetchRealmsTask = null; |
| | 50 | | } |
| 5 | 51 | | } |
| | 52 | |
|
| | 53 | | public void SetAbout(string json) |
| | 54 | | { |
| 0 | 55 | | JsonParser jsonParser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true)); |
| 0 | 56 | | AboutResponse aboutResponse = jsonParser.Parse<AboutResponse>(json); |
| 0 | 57 | | playerRealmAboutConfiguration.Set(aboutResponse.Configurations); |
| 0 | 58 | | playerRealmAboutContent.Set(aboutResponse.Content); |
| 0 | 59 | | playerRealmAboutLambda.Set(aboutResponse.Lambdas); |
| 0 | 60 | | realmName.Set(aboutResponse.Configurations.RealmName); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public UniTask<IReadOnlyList<RealmModel>> FetchRealmsInfo(CancellationToken cancellationToken) |
| | 64 | | { |
| | 65 | | try |
| | 66 | | { |
| 0 | 67 | | if (fetchRealmsTask != null) |
| 0 | 68 | | return fetchRealmsTask.Task.AttachExternalCancellation(cancellationToken) |
| | 69 | | .Timeout(TimeSpan.FromSeconds(30)); |
| | 70 | |
|
| 0 | 71 | | fetchRealmsTask = new UniTaskCompletionSource<IReadOnlyList<RealmModel>>(); |
| | 72 | |
|
| 0 | 73 | | WebInterface.FetchRealmsInfo(); |
| | 74 | |
|
| 0 | 75 | | return fetchRealmsTask.Task.AttachExternalCancellation(cancellationToken) |
| | 76 | | .Timeout(TimeSpan.FromSeconds(30)); |
| | 77 | | } |
| 0 | 78 | | catch (Exception) |
| | 79 | | { |
| 0 | 80 | | fetchRealmsTask = null; |
| 0 | 81 | | throw; |
| | 82 | | } |
| 0 | 83 | | } |
| | 84 | | } |
| | 85 | |
|
| | 86 | | [Serializable] |
| | 87 | | public class RealmsInfoModel |
| | 88 | | { |
| | 89 | | public CurrentRealmModel current; |
| | 90 | | public RealmModel[] realms; |
| | 91 | | } |
| | 92 | | } |