Пример #1
0
        private IEnumerable <Game> EnumerateUserGames(string userName, int?year, Play[] plays)
        {
            Log.Info("Enumerating user collection");
            var userCollection = BggService.GetUserCollectionAsync(userName).ToArrayAsync().GetAwaiter().GetResult();

            Log.Info("Enumerating user games");
            return(plays.GroupBy(play => play.GameId).Select(perGamePlays =>
            {
                var game = new
                {
                    Id = perGamePlays.Key,
                    Name = perGamePlays.First().GameName,
                    perGamePlays.First().IsExpansion,
                    ParentId = perGamePlays.First().ParentGameId
                };
                var allGamePlays = EnumerateUserPlays(userName, id: game.Id).ToArray();

                var gameCollection = userCollection.Where(collection => collection.GameId == game.Id).ToArray();
                if (gameCollection.Length == 0) // not really a problem since we only want comments for metadata
                {
                    //Log.Warning("** Failed to find game in user collection:");
                    //Log.Warning(game.Name);
                    //Log.Warning($"boardgamegeek.com/{(game.IsExpansion ? "boardgameexpansion" : "boardgame")}/{game.Id}");
                }
                else if (gameCollection.Length > 1)
                {
                    Log.Warning("** Found multiple games in user collection:");
                    Log.Warning(game.Name);
                    Log.Warning($"boardgamegeek.com/{(game.IsExpansion ? "boardgameexpansion" : "boardgame")}/{game.Id}");
                }
                var gameComments = string.Join(@"\n", gameCollection.Select(collection => collection.Comments?.Replace("\n", @"\n")));

                return new Game
                {
                    Id = game.Id,
                    Name = game.Name,
                    IsExpansion = game.IsExpansion,
                    ParentId = game.ParentId,
                    Plays = perGamePlays.Where(play => !play.IsSession || !play.IsIncomplete).Sum(play => play.Quantity),
                    Sessions = perGamePlays.Sum(play => play.Quantity),
                    CumulativePlays = allGamePlays.Where(play => (year == null || play.Date.Year <= year) && (!play.IsSession || !play.IsIncomplete)).Sum(play => play.Quantity),
                    CumulativeSessions = allGamePlays.Where(play => year == null || play.Date.Year <= year).Sum(play => play.Quantity),
                    IsHighlight = Game.IsHighlightMatch(gameComments, year),
                    IsNew = year != null && allGamePlays.All(play => play.Date.Year >= year),
                    Comments = gameComments
                };
            }));
        }