Example #1
0
        /// <summary>
        /// Gets a rendering of the profile banner.
        /// </summary>
        /// <param name="recache"></param>
        /// <returns></returns>
        public async Task <byte[]> GetProfileRenderAsync(bool recache = false)
        {
            if (Profile == null)
            {
                throw new InvalidOperationException("Cannot render a profile if there is no profile");
            }

            if (MMRHistory == null)
            {
                throw new InvalidOperationException("Cannot render a profile if there is no MMR data");
            }

            //Prepare a code which we will use for the cache
            var cacheCode = RENDER_CACHE_REVISION ^ Profile.GetCacheCode() ^ MMRHistory.GetCacheCode();
            var cacheName = Namespace.Combine(Manager.RedisPrefix, "profiles", ApiName, "render", cacheCode);

            //Fetch the cache
            if (!recache && !NO_CACHE_RENDER)
            {
                var cacheResult = await Manager.Redis.FetchStringAsync(cacheName);

                if (cacheResult != null)
                {
                    return(Convert.FromBase64String(cacheResult));
                }
            }

            //Regenerate and recache
            var renderResult = await RenderProfileAsync();

            await Manager.Redis.StoreStringAsync(cacheName, Convert.ToBase64String(renderResult));

            await Manager.Redis.SetExpiryAsync(cacheName, ProfileRenderTLL);

            return(renderResult);
        }
Example #2
0
        /// <summary>
        /// Updates the MMR History.
        /// </summary>
        /// <param name="update"></param>
        /// <returns></returns>
        public async Task <MMRHistory> UpdateMMRHistoryAsync()
        {
            //We need to first update the profile
            if (Profile == null)
            {
                throw new InvalidOperationException("Cannot get MMR if there is no profile");
            }

            //Fetch the history
            var cacheName   = Namespace.Combine(Manager.RedisPrefix, "profiles", ApiName, "history");
            var prevHistory = await Manager.Redis.FetchObjectAsync <MMRHistory>(cacheName);

            if (prevHistory == null || prevHistory.Season != Manager.Season)
            {
                if (Profile != null)
                {
                    //Profile exists, so lets setup the default
                    prevHistory = new MMRHistory()
                    {
                        Season  = Manager.Season,
                        Minimum = Profile.MetaData.MMR,
                        Current = Profile.MetaData.MMR,
                        Maximum = Profile.MetaData.BestMMR,
                        Average = Profile.MetaData.AverageMMR,
                    };
                }
                else
                {
                    //Profile doesn't exist, so early abort.
                    return(null);
                }
            }


            //if we are updating, then lets do so
            if (Profile != null)
            {
                //Create a new history table
                var newHistory = new MMRHistory()
                {
                    Season  = Manager.Season,
                    Average = Profile.MetaData.AverageMMR,
                    Current = Profile.MetaData.MMR,
                    Minimum = prevHistory.Minimum > Profile.MetaData.MMR ? Profile.MetaData.MMR : prevHistory.Minimum,
                    Maximum = prevHistory.Maximum < Profile.MetaData.BestMMR ? Profile.MetaData.BestMMR : prevHistory.Maximum
                };

                //Check for rank changes
                if (newHistory.MaximumRank > prevHistory.MaximumRank)
                {
                    OnSeasonHigh?.Invoke(this, prevHistory, newHistory);
                }

                if (newHistory.Rank != prevHistory.Rank)
                {
                    OnRankChange?.Invoke(this, prevHistory, newHistory);
                }

                //Store the new history and assign it
                prevHistory = newHistory;
                await Manager.Redis.StoreObjectAsync(cacheName, prevHistory);
            }

            //Return the history
            return(MMRHistory = prevHistory);
        }