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