Ejemplo n.º 1
0
        public async Task <IActionResult> DeleteAsset(Int64 id)
        {
            Console.WriteLine($"/community/assetBrowser/deleteAsset/{id}{Request.QueryString}");

            var asset = await _assetManager.FindByIdAsync(id);

            var user = await _userManager.GetUserAsync(User);

            // make sure the asset exists
            // and is used
            if (asset == null ||
                !asset.Used)
            {
                return(Ok());
            }

            // make sure the asset author
            // is the current user
            if (asset.AuthorId == user.Id)
            {
                if (!await _assetManager.DeleteAsync(asset))
                {
                    return(StatusCode(500));
                }
            }

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Asset(Int64?id)
        {
            Console.WriteLine($"/pollinator/atom/asset/{id}{Request.QueryString}");

            var assets = new List <SporeServerAsset>();

            if (id != null)
            {
                // parameter is asset id
                var asset = await _assetManager.FindByIdAsync((Int64)id, true);

                if (asset != null)
                {
                    assets.Add(asset);
                }
            }
            else
            {
                // loop over all id queries
                foreach (var idQuery in Request.Query["id"])
                {
                    // make sure we can parse the id
                    // and that the asset exists
                    if (Int64.TryParse(idQuery, out Int64 assetId))
                    {
                        var asset = await _assetManager.FindByIdAsync(assetId, true);

                        if (asset != null)
                        {
                            assets.Add(asset);
                        }
                    }
                }
            }

            Console.WriteLine(AtomFeedBuilder.CreateFromTemplate(
                                  new AssetTemplate(assets.ToArray())
                                  ).ToContentResult().Content);

            return(AtomFeedBuilder.CreateFromTemplate(
                       new AssetTemplate(assets.ToArray())
                       ).ToContentResult());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Status(Int64 id)
        {
            Console.WriteLine($"/pollinator/Upload/Status/{id}");

            var user = await _userManager.GetUserAsync(User);

            var asset = await _assetManager.FindByIdAsync(id);

            Int64 nextId = user.NextAssetId;

            // we're only successful when
            //  * the asset exists
            //  * the asset is used
            //  * the asset has the same author
            //    as the currently logged in user
            //  * the id is the same as the user's reserved id
            bool success = (asset != null &&
                            asset.Used &&
                            asset.AuthorId == user.Id &&
                            user.NextAssetId == id);

            // when the requirements have been met,
            // reserve a new asset
            if (success)
            {
                // when reserving a new asset fails, error out
                if (!await _assetManager.ReserveAsync(user))
                {
                    return(StatusCode(500));
                }

                // tell the client about the new id
                nextId = user.NextAssetId;
            }

            return(XmlBuilder.CreateFromTemplate(
                       new StatusTemplate(nextId, success)
                       ).ToContentResult());
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> HandShake()
        {
            var user = await _userManager.GetUserAsync(User);

            var asset = await _assetManager.FindByIdAsync(user.NextAssetId);

            var aggregators = await _aggregatorManager.FindByAuthorAsync(user);

            var userSubscriptions = await _userSubscriptionManager.FindAllByAuthorAsync(user);

            var aggregatorSubscriptions = await _aggregatorSubscriptionManager.FindAllByAuthorAsync(user);

            var aggregatorSubscriptionCounts = new List <Int32>();

            // retrieve subscription count for each aggregator
            foreach (var aggregator in aggregators)
            {
                var aggregatorSubscriptionCount = await _aggregatorSubscriptionManager.GetSubscriberCountAsync(aggregator);

                aggregatorSubscriptionCounts.Add(aggregatorSubscriptionCount);
            }

            // reserve new asset when
            //      * user has no reserved asset
            //      * user has a used asset
            if (user.NextAssetId == 0 ||
                (asset != null && asset.Used))
            {
                // when reserving a new asset fails, error out
                if (!await _assetManager.ReserveAsync(user))
                {
                    return(StatusCode(500));
                }
            }

            return(AtomFeedBuilder.CreateFromTemplate(
                       new HandshakeTemplate(user, aggregators, aggregatorSubscriptionCounts.ToArray(), userSubscriptions, aggregatorSubscriptions)
                       ).ToContentResult());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> OnGet(Int64 id)
        {
            Console.WriteLine($"/community/assetBrowser/comment/{id}");

            Asset = await _assetManager.FindByIdAsync(id, true);

            AssetExists = (Asset != null);

            if (!AssetExists)
            {
                return(Page());
            }

            // if the comment query exists,
            // try to add the comment
            SentComment    = Request.Query["comment"];
            HasSentComment = !String.IsNullOrEmpty(SentComment) && SentComment.Length <= 150;
            if (HasSentComment)
            {
                var author = await _userManager.GetUserAsync(User);

                if (!await _assetCommentManager.AddAsync(Asset, author, SentComment))
                {
                    return(StatusCode(500));
                }

                // no need to tell the user we've sent it
                // to the asset author for approval, when
                // the asset author comments on their own
                // creation
                HasSentComment = Asset.AuthorId != author.Id;
            }

            Comments = await _assetCommentManager.FindAllApprovedByAssetAsync(Asset);

            CommentsCount = Comments == null ? 0 : Comments.Length;
            return(Page());
        }
        public async Task <IActionResult> OnGet(Int32?index)
        {
            Console.WriteLine($"/community/assetBrowser/createSporecast/{index}{Request.QueryString}");

            string searchString = Request.Query["searchText"];
            string filterString = Request.Query["filter"];

            SearchString = searchString;
            FilterString = filterString;

            bool searchName = !String.IsNullOrEmpty(searchString);
            bool searchModelType = false, searchAssetType = false;

            // if we can parse filterString, make sure we can use it
            if (Int64.TryParse(filterString, out Int64 filterId))
            {
                searchModelType = Enum.IsDefined(typeof(SporeModelType), filterId);
                searchAssetType = Enum.IsDefined(typeof(SporeAssetType), filterId);
            }

            // search the assets using LINQ
            var queryAssets = _assetManager.GetAllAssets()
                              .Include(a => a.Author)
                              .Where(a => a.Used &&
                                     (
                                         (
                                             (!searchName) ||
                                             (
                                                 a.Author.UserName.Contains(searchString) ||
                                                 a.Name.Contains(searchString)
                                             )
                                         )
                                         &&
                                         (
                                             (searchModelType && a.ModelType == (SporeModelType)filterId) ||
                                             (searchAssetType && a.Type == (SporeAssetType)filterId) ||
                                             (!searchModelType && !searchAssetType)
                                         )
                                     )
                                     ).OrderBy(a => a.Name);

            CurrentIndex  = index ?? 0;
            NextIndex     = CurrentIndex + 8;
            PreviousIndex = CurrentIndex - 8;

            SearchCount = await queryAssets
                          .CountAsync();

            Assets = await queryAssets
                     .Skip(CurrentIndex)
                     .Take(8)
                     .ToArrayAsync();

            AssetCount = Assets.Length;

            string name        = Request.Query["scName"];
            string description = Request.Query["scDesc"];

            // when name isn't defined, return the page
            // if it is defined, attempt to add the aggregator
            if (String.IsNullOrEmpty(name))
            {
                return(Page());
            }

            // loop over each asset query
            // and add valid ones to the assets list
            var assets = new List <SporeServerAsset>();

            foreach (var assetString in Request.Query["asset"])
            {
                if (Int64.TryParse(assetString, out Int64 assetId))
                {
                    var asset = await _assetManager.FindByIdAsync(assetId, true);

                    if (asset != null)
                    {
                        assets.Add(asset);
                    }
                }
            }

            // make sure the aggregator has assets
            if (assets.Count == 0)
            {
                return(Page());
            }

            // add aggregator
            var author = await _userManager.GetUserAsync(User);

            var aggregator = await _aggregatorManager.AddAsync(author, name, description, assets.ToArray());

            if (aggregator == null)
            {
                return(StatusCode(500));
            }

            // redirect to /pollinator/atom/aggregator/{id}
            return(Redirect($"https://pollinator.spore.com/pollinator/atom/aggregator/{aggregator.AggregatorId}"));
        }
        public async Task <IActionResult> AssetUploadServlet([FromForm] AssetUploadForm formAsset)
        {
            Console.WriteLine($"/pollinator/public-interface/AssetUploadServlet{Request.QueryString}");

            // the game client always sends the slurp query
            // and it's always either 0 or 1
            if (!Request.Query.ContainsKey("slurp") ||
                !int.TryParse(Request.Query["slurp"], out int slurpValue) ||
                (slurpValue != 0 && slurpValue != 1))
            {
                return(Ok());
            }

            Int64 parentId = 0;

            // the game sometimes sends a parent id,
            // make sure we can parse it
            if (Request.Query.ContainsKey("parent") &&
                !Int64.TryParse(Request.Query["parent"], out parentId))
            {
                return(Ok());
            }

            SporeServerAsset parentAsset = null;

            // when parentId is not 0,
            // try to find parent asset
            if (parentId != 0)
            {
                parentAsset = await _assetManager.FindByIdAsync(parentId);
            }

            // the game always sends the type id
            // make sure we can parse it
            // and that's it a valid id
            if (!Int64.TryParse(formAsset.TypeId.TrimStart('0', 'x'),
                                NumberStyles.HexNumber,
                                null,
                                out Int64 typeId) ||
                !Enum.IsDefined(typeof(SporeAssetType), typeId))
            {
                Console.WriteLine($"invalid type id: {typeId}");
                return(Ok());
            }

            var user = await _userManager.GetUserAsync(User);

            // make sure the requested assetId is the user's nextAssetId
            if (user.NextAssetId != formAsset.AssetId)
            {
                return(Ok());
            }

            var asset = await _assetManager.FindByIdAsync(formAsset.AssetId);

            // make sure the asset exists and
            // make sure it isn't already used
            if (asset == null ||
                asset.Used)
            {
                return(Ok());
            }

            // make sure the asset doesn't go over any limits
            if ((formAsset.Description != null &&
                 formAsset.Description.Length > 256) ||
                (formAsset.ModelData != null &&
                 formAsset.ModelData.FileName.Length > 32) ||
                (formAsset.Tags != null &&
                 formAsset.Tags.Length > 256))
            {
                return(Ok());
            }

            // save the asset
            if (!await _assetManager.AddAsync(formAsset,
                                              asset,
                                              parentAsset,
                                              (slurpValue == 1),
                                              (SporeAssetType)typeId))
            {
                return(StatusCode(500));
            }

            return(Ok());
        }
Ejemplo n.º 8
0
        public async Task <bool> AddAsync(Stream eventStream, SporeServerUser author)
        {
            try
            {
                var events = await EventsModel.SerializeAsync(eventStream);

                foreach (var eventsEvent in events.Events)
                {
                    switch (eventsEvent.Verb)
                    {
                    // Unlocked Achievement
                    case (Int64)SporeEventType.AchievementUnlocked:
                        await _achievementManager.UnlockAsync(eventsEvent.Args[0], author);

                        break;

                    // Befriended Creature
                    case (Int64)SporeEventType.CreatureBefriended:
                        break;

                    // Extincted Creature
                    case (Int64)SporeEventType.CreatureExtinction:
                        break;

                    // Adventure Won
                    case (Int64)SporeEventType.AdventureWon:
                    {
                        var adventureAssetId    = eventsEvent.AssetId;
                        var percentageCompleted = (Int32)eventsEvent.Args[0];
                        var timeInMs            = (Int32)eventsEvent.Args[1];
                        // Args[2] contains the amount of captain points earned
                        // maybe a thing to track in the future?
                        // seems to be unrequired for anything ingame though
                        var captainAssetId = eventsEvent.Args[3];

                        var adventureAsset = await _assetManager.FindByIdAsync(adventureAssetId);

                        var captainAsset = await _assetManager.FindByIdAsync(captainAssetId);

                        // make sure we can find the needed assets
                        if (adventureAsset == null || captainAsset == null)
                        {
                            break;
                        }

                        await _leaderboardManager.AddAsync(adventureAsset, captainAsset, percentageCompleted, timeInMs, author);
                    }
                    break;

                    // Adventure Lost
                    case (Int64)SporeEventType.AdventureLost:
                        break;

                    // Adventure Captain Stats
                    case (Int64)SporeEventType.AdventureCaptainStats:
                        break;

                    // Adventure Captain Name
                    case (Int64)SporeEventType.AdventureCaptainName:
                        break;

                    // Adventure Captain Unlocked Parts
                    case (Int64)SporeEventType.AdventureCaptainUnlockedParts:
                        break;

                    // Unsupported
                    default:
                        _logger.LogWarning($"Invalid Event Verb 0x{eventsEvent.Verb:x}");
                        break;
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                _logger.LogError($"Failed To Add Event For {author.Id}: {e}");
                return(false);
            }
        }