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