Exemplo n.º 1
0
        /// <summary>
        ///   Takes a string, representing either a ListAssetsResponse or ListUserAssetsResponse proto, and
        ///   fills polyListResult with relevant fields from the response and returns a success status
        ///   if the response is of the expected format, or a failure status if it's not.
        /// </summary>
        public static PolyStatus ParseReturnedAssets(string response, out PolyListAssetsResult polyListAssetsResult)
        {
            // Try and actually parse the string.
            JObject results = JObject.Parse(response);
            IJEnumerable <JToken> assets = results["assets"].AsJEnumerable();

            // If assets is null, check for a userAssets object, which would be present if the response was
            // a ListUserAssets response.
            if (assets == null)
            {
                assets = results["userAssets"].AsJEnumerable();
            }
            if (assets == null)
            {
                // Empty response means there were no assets that matched the request parameters.
                polyListAssetsResult = new PolyListAssetsResult(PolyStatus.Success(), /*totalSize*/ 0);
                return(PolyStatus.Success());
            }
            List <PolyAsset> polyAssets = new List <PolyAsset>();

            foreach (JToken asset in assets)
            {
                PolyAsset polyAsset;
                if (!(asset is JObject))
                {
                    Debug.LogWarningFormat("Ignoring asset since it's not a JSON object: " + asset);
                    continue;
                }
                JObject jObjectAsset = (JObject)asset;
                if (asset["asset"] != null)
                {
                    // If this isn't null, means we are parsing a ListUserAssets response, which has an added
                    // layer of nesting.
                    jObjectAsset = (JObject)asset["asset"];
                }
                PolyStatus parseStatus = ParseAsset(jObjectAsset, out polyAsset);
                if (parseStatus.ok)
                {
                    polyAssets.Add(polyAsset);
                }
                else
                {
                    Debug.LogWarningFormat("Failed to parse a returned asset: {0}", parseStatus);
                }
            }
            var totalSize = results["totalSize"] != null?int.Parse(results["totalSize"].ToString()) : 0;

            var nextPageToken = results["nextPageToken"] != null ? results["nextPageToken"].ToString() : null;

            polyListAssetsResult = new PolyListAssetsResult(PolyStatus.Success(), totalSize, polyAssets, nextPageToken);
            return(PolyStatus.Success());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Processes request results and delivers them to the callback.
        /// </summary>
        /// <param name="result">The result.</param>
        private void ProcessRequestResult(PolyListAssetsResult result, PolyApi.ListAssetsCallback callback)
        {
            if (result == null)
            {
                callback(new PolyStatusOr <PolyListAssetsResult>(PolyStatus.Error("No request result.")));
                return;
            }

            if (result.assets == null)
            {
                // Nothing wrong with the request, there were just no assets that matched those parameters.
                // Put an empty list in the result.
                result.assets = new List <PolyAsset>();
            }

            callback(new PolyStatusOr <PolyListAssetsResult>(result));
        }
Exemplo n.º 3
0
    // Searches directly by Poly URL.
    private void SearchByPolyUrl(string polyUrl, OnActorableSearchResult resultCallback, System.Action <bool> onComplete)
    {
        string[] parts   = polyUrl.Split('/');
        string   assetId = parts[parts.Length - 1];

        PolyApi.GetAsset(assetId, result =>
        {
            PolyListAssetsResult assetsResult;
            List <PolyAsset> assetList = new List <PolyAsset>();
            if (result.Ok)
            {
                // Successfully got the asset. This is good.
                // Is it acceptably licensed?
                if (result.Value.license == PolyAssetLicense.CREATIVE_COMMONS_BY)
                {
                    // Good license. We can use it.
                    assetList.Add(result.Value);
                    assetsResult = new PolyListAssetsResult(PolyStatus.Success(), 1, assetList);
                }
                else
                {
                    // Not CC-By. Can't use it.
                    Debug.LogError("This asset (" + assetId + ") is not licensed by CC-By. Try another asset.");
                    assetsResult = new PolyListAssetsResult(PolyStatus.Error("Asset " + assetId + " is not licensed as CC-By."), 0, assetList);
                }
            }
            else
            {
                // Failed to get the asset. This is bad.
                assetsResult = new PolyListAssetsResult(PolyStatus.Error("Failed to get asset " + assetId), 0, assetList);
            }
            PolySearchCallback(
                new PolyStatusOr <PolyListAssetsResult>(assetsResult), resultCallback, onComplete);
        });
        return;
    }
Exemplo n.º 4
0
    public async Task <ScoredSearchResult> SearchPoly(string term)
    {
        Debug.Log("Searching Poly for term: " + term);

        var request = new PolyListAssetsRequest();

        request.orderBy  = PolyOrderBy.BEST;
        request.keywords = term;
        request.curated  = true;

        PolyListAssetsResult result = null;

        bool error = false;

        PolyApi.ListAssets(request, response =>
        {
            if (response.Ok)
            {
                result = response.Value;
            }
            else
            {
                error = true;
            }
        });

        while (result == null && !error)        //TODO: Add Timeout to avoid infinite loop!
        {
            await Task.Delay(50);
        }

        await new WaitForBackgroundThread();

        PolyAsset bestAsset = null;
        double    bestScore = double.MinValue;

        if (result != null)
        {
            foreach (var asset in result.assets)
            {
                //get how related it is to the search term
                //if doesn't exist in concept net, only accept exact matches (or lev dists <2)
                //made by poly is * 1.5 multiplier (can tweak)

                double score = -1;

                if (asset.displayName.ToLower().Equals(term))
                {
                    score = 1;
                }
                else
                {
                    score = _conceptNet.GetRelationScore(asset.displayName.ToLower(), term);
                }

                Debug.Log(asset.displayName + " by: " + asset.authorName + " (" + score + ") ");;

                if (asset.authorName.Contains("Google"))
                {
                    score = score * 1.75;
                }

                if (score > bestScore)
                {
                    bestScore = score;
                    bestAsset = asset;
                }
            }
        }

        if (bestAsset != null)
        {
            Debug.Log(
                String.Format("Term: {0}, Asset: {1}, Score: {2}", term, bestAsset.displayName + " by: " + bestAsset.authorName, bestScore));
        }
        else
        {
            Debug.Log("No results for term: " + term);
        }

        var bestResult = new ScoredSearchResult();

        bestResult.score     = bestScore;
        bestResult.polyAsset = bestAsset;

        await new WaitForUpdate();

        return(bestResult);
    }