| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Helpers; |
| | 5 | |
|
| | 6 | | public static class DeployedScenesFetcher |
| | 7 | | { |
| | 8 | | private const float DEFAULT_SCENES_CACHE_TIME = 3 * 60; |
| | 9 | | private const float DEFAULT_LAND_CACHE_TIME = 5 * 60; |
| | 10 | |
|
| | 11 | | public static Promise<DeployedScene[]> FetchScenes(ICatalyst catalyst, string[] parcels, float cacheMaxAgeSeconds = |
| | 12 | | { |
| 0 | 13 | | Promise<DeployedScene[]> promise = new Promise<DeployedScene[]>(); |
| 0 | 14 | | catalyst.GetDeployedScenes(parcels, cacheMaxAgeSeconds) |
| | 15 | | .Then(result => |
| | 16 | | { |
| 0 | 17 | | promise.Resolve(result.Select(deployment => new DeployedScene(deployment, catalyst.contentUrl)).ToAr |
| 0 | 18 | | }) |
| 0 | 19 | | .Catch(err => promise.Reject(err)); |
| 0 | 20 | | return promise; |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public static Promise<LandWithAccess[]> FetchLandsFromOwner(ICatalyst catalyst, ITheGraph theGraph, string ethAddres |
| | 24 | | float cacheMaxAgeSecondsLand = DEFAULT_LAND_CACHE_TIME, float cacheMaxAgeSecondsScenes = DEFAULT_SCENES_CACHE_TI |
| | 25 | | { |
| 8 | 26 | | Promise<LandWithAccess[]> resultPromise = new Promise<LandWithAccess[]>(); |
| | 27 | |
|
| 8 | 28 | | List<Land> lands = new List<Land>(); |
| | 29 | |
|
| 8 | 30 | | Promise<string[]> getOwnedParcelsPromise = new Promise<string[]>(); |
| 8 | 31 | | Promise<DeployedScene[]> getDeployedScenesPromise = new Promise<DeployedScene[]>(); |
| 8 | 32 | | theGraph.QueryLands(network, ethAddress, cacheMaxAgeSecondsLand) |
| | 33 | | .Then(landsReceived => |
| | 34 | | { |
| 4 | 35 | | lands = landsReceived; |
| | 36 | |
|
| 4 | 37 | | List<string> parcels = new List<string>(); |
| 8 | 38 | | for (int i = 0; i < landsReceived.Count; i++) |
| | 39 | | { |
| 0 | 40 | | if (landsReceived[i].parcels == null) |
| | 41 | | continue; |
| | 42 | |
|
| 0 | 43 | | parcels.AddRange(landsReceived[i].parcels.Select(parcel => $"{parcel.x},{parcel.y}")); |
| | 44 | | } |
| | 45 | |
|
| 4 | 46 | | getOwnedParcelsPromise.Resolve(parcels.ToArray()); |
| 4 | 47 | | }) |
| 0 | 48 | | .Catch(err => getOwnedParcelsPromise.Reject(err)); |
| | 49 | |
|
| 8 | 50 | | getOwnedParcelsPromise.Then(parcels => |
| | 51 | | { |
| 4 | 52 | | if (parcels.Length > 0) |
| | 53 | | { |
| 0 | 54 | | FetchScenes(catalyst, parcels, cacheMaxAgeSecondsScenes) |
| 0 | 55 | | .Then(scenes => getDeployedScenesPromise.Resolve(scenes)) |
| 0 | 56 | | .Catch(err => getDeployedScenesPromise.Reject(err)); |
| 0 | 57 | | } |
| | 58 | | else |
| | 59 | | { |
| 4 | 60 | | getDeployedScenesPromise.Resolve(new DeployedScene[] { }); |
| | 61 | | } |
| 4 | 62 | | }) |
| 0 | 63 | | .Catch(err => getDeployedScenesPromise.Reject(err)); |
| | 64 | |
|
| 8 | 65 | | getDeployedScenesPromise.Then(scenes => |
| | 66 | | { |
| 4 | 67 | | resultPromise.Resolve(GetLands(lands, scenes)); |
| 4 | 68 | | }) |
| 0 | 69 | | .Catch(err => resultPromise.Reject(err)); |
| | 70 | |
|
| 8 | 71 | | return resultPromise; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | private static LandWithAccess[] GetLands(List<Land> lands, DeployedScene[] scenes) |
| | 75 | | { |
| 4 | 76 | | LandWithAccess[] result = new LandWithAccess[lands.Count]; |
| | 77 | |
|
| 8 | 78 | | for (int i = 0; i < lands.Count; i++) |
| | 79 | | { |
| 0 | 80 | | result[i] = ProcessLand(lands[i], scenes); |
| | 81 | | } |
| | 82 | |
|
| 4 | 83 | | return result; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | private static LandWithAccess ProcessLand(Land land, DeployedScene[] scenes) |
| | 87 | | { |
| 0 | 88 | | List<DeployedScene> scenesInLand = new List<DeployedScene>(); |
| | 89 | |
|
| 0 | 90 | | LandWithAccess result = new LandWithAccess(land); |
| 0 | 91 | | for (int i = 0; i < result.parcels.Length; i++) |
| | 92 | | { |
| 0 | 93 | | DeployedScene sceneInParcel = scenes.FirstOrDefault(scene => scene.parcels.Contains(result.parcels[i]) && !s |
| 0 | 94 | | if (sceneInParcel != null) |
| | 95 | | { |
| 0 | 96 | | sceneInParcel.SetScene(result); |
| 0 | 97 | | scenesInLand.Add(sceneInParcel); |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | result.scenes = scenesInLand; |
| | 102 | |
|
| 0 | 103 | | return result; |
| | 104 | | } |
| | 105 | | } |