Exemplo n.º 1
0
        /// <summary>
        /// Gets all owned game stats.
        /// </summary>
        /// <param name="steamUserID">The steam user identifier.</param>
        /// <returns>All games with achievement stats.</returns>
        /// <exception cref="ArgumentNullException">steamUserID</exception>
        /// <exception cref="ArgumentException">
        /// No owned games found.
        /// or
        /// No played games found.
        /// </exception>
        public async Task <IEnumerable <ISteamUserGameStats> > GetAllOwnedGameStats(string steamUserID)
        {
            if (string.IsNullOrWhiteSpace(steamUserID))
            {
                throw new ArgumentNullException("steamUserID");
            }

            var allOwnedGames = await _steamAPIService.GetGamesOwnedByAUser(steamUserID);

            if (allOwnedGames == null || allOwnedGames.Games == null || !allOwnedGames.Games.Any())
            {
                throw new ArgumentException("No owned games found.");
            }

            _log.Info($"All owned games count: {allOwnedGames.GameCount}");

            double unlockedAchievements             = 0;
            double totalAchievements                = 0;
            List <ISteamUserGameStats> allGameStats = new List <ISteamUserGameStats>();

            var playedGames = allOwnedGames.Games.Where(e => e.Playtime > 0);

            if (playedGames == null || !playedGames.Any())
            {
                throw new ArgumentException("No played games found.");
            }

            _log.Info($"All played games: {playedGames.Count()}");

            foreach (var game in playedGames)
            {
                _log.Info($"Game: {game.ID} & Playtime: {game.Playtime}");

                ISteamUserGameStats gameStats = await _steamAPIService.GetAchievmentsForAGame(game.ID, steamUserID);

                if (gameStats != null && gameStats.Achievements != null && gameStats.Achievements.Any())
                {
                    allGameStats.Add(gameStats);
                    _log.Info($"{gameStats.GameName}");
                    var unlocked = this.GetGameAchievementCount(gameStats.Achievements);
                    if (unlocked > 0)
                    {
                        _log.Info($"{game.ID}:{gameStats.GameName} - Achievements Unlocked: {unlocked}");
                        _log.Info($"{game.ID}:{gameStats.GameName} - Achievements Total   : {gameStats.Achievements.Count()}");
                        unlockedAchievements += unlocked;
                        totalAchievements    += gameStats.Achievements.Count();
                    }
                }
                else
                {
                    _log.Info($"{game.ID} - No game stats");
                }
            }

            return(allGameStats);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the average game completion.
        /// </summary>
        /// <param name="steamUserID">The steam user identifier.</param>
        /// <returns>The average game completion.</returns>
        public async Task <double> GetAverageGameCompletion(string steamUserID)
        {
            if (string.IsNullOrWhiteSpace(steamUserID))
            {
                throw new ArgumentNullException("steamUserID");
            }

            double unlockedAchievements = 0;
            double totalAchievements    = 0;
            var    allOwnedGames        = await _steamAPIService.GetGamesOwnedByAUser(steamUserID);

            if (allOwnedGames == null || allOwnedGames.Games == null || !allOwnedGames.Games.Any())
            {
                throw new ArgumentException("No owned games found.");
            }

            _log.Info($"All owned games count: {allOwnedGames.GameCount}");

            var playedGames = allOwnedGames.Games.Where(e => e.Playtime > 0);

            if (playedGames == null || !playedGames.Any())
            {
                throw new ArgumentException("No played games found.");
            }

            _log.Info($"All played games: {playedGames.Count()}");

            foreach (var game in playedGames)
            {
                _log.Info($"Game: {game.ID} & Playtime: {game.Playtime}");

                ISteamUserGameStats gameStats = await _steamAPIService.GetAchievmentsForAGame(game.ID, steamUserID);

                if (gameStats != null && gameStats.Achievements != null && gameStats.Achievements.Any())
                {
                    _log.Info($"{gameStats.GameName}");
                    var unlocked = this.GetGameAchievementCount(gameStats.Achievements);
                    if (unlocked > 0)
                    {
                        _log.Info($"{game.ID}:{gameStats.GameName} - Achievements Unlocked: {unlocked}");
                        _log.Info($"{game.ID}:{gameStats.GameName} - Achievements Total   : {gameStats.Achievements.Count()}");
                        unlockedAchievements += unlocked;
                        totalAchievements    += gameStats.Achievements.Count();
                    }
                }
                else
                {
                    _log.Info($"{game.ID} - No game stats");
                }
            }

            if (totalAchievements <= 0 && unlockedAchievements > 0)
            {
                _log.Error($"Divide by zero error with: {totalAchievements} / {unlockedAchievements}");
                throw new DivideByZeroException("The total count of achivements is 0 but there are unlocked achievements.");
            }

            _log.Info($"Total unlocked achivements: {unlockedAchievements}");
            _log.Info($"Total achivements: {totalAchievements}");

            return((unlockedAchievements / totalAchievements) * (100 / 1));
        }