Beispiel #1
0
        public Task <StreamerLoyaltyData> GetStreamerLoyaltyDataAsync(string userId)
        {
            return(Task.Run(() =>
            {
                var session = GetSession();
                if (session == null)
                {
                    return new StreamerLoyaltyData();
                }

                var user = gameData.GetUser(session.AccountId);
                if (user == null)
                {
                    return new StreamerLoyaltyData();
                }

                var data = gameData.GetStreamerLoyalties(user.Id);
                var sessions = gameData.FindSessions(x => x.UserId == user.Id);

                if (sessions.Count == 0)
                {
                    return new StreamerLoyaltyData();
                }

                var startTime = sessions.Min(x => x.Started);
                var stopTime = sessions.Max(x => x.Started);

                var stoppedSessions = sessions.Where(x => x.Stopped != null).ToList();
                if (stoppedSessions.Count > 0)
                {
                    var lastStoppedSessionTime = stoppedSessions.Max(x => x.Stopped.Value);
                    if (lastStoppedSessionTime > stopTime)
                    {
                        stopTime = lastStoppedSessionTime;
                    }
                }

                var totalBitsCheered = 0L;
                var totalSubsGifted = 0L;
                var totalPlayerTime = TimeSpan.Zero;
                var totalSubscribers = 0;
                var loyalties = new List <PlayerLoyalty>();
                foreach (var d in data)
                {
                    var u = gameData.GetUser(d.UserId);
                    if (u == null || string.IsNullOrEmpty(u.UserName))
                    {
                        continue;
                    }

                    totalSubsGifted += d.GiftedSubs;
                    totalBitsCheered += d.CheeredBits;
                    if (d.IsSubscriber)
                    {
                        ++totalSubscribers;
                    }
                    if (TimeSpan.TryParse(d.Playtime, out var val))
                    {
                        totalPlayerTime += val;
                    }

                    var totalPlayTime = TimeSpan.Zero;
                    if (d.Playtime != null)
                    {
                        TimeSpan.TryParse(d.Playtime, out totalPlayTime);
                    }

                    loyalties.Add(new PlayerLoyalty
                    {
                        UserName = u.UserName,
                        DisplayName = u.DisplayName,
                        TwitchUserId = u.UserId,
                        CheeredBits = d.CheeredBits,
                        Experience = d.Experience,
                        GiftedSubs = d.GiftedSubs,
                        IsModerator = d.IsModerator,
                        IsSubscriber = d.IsSubscriber,
                        IsVip = d.IsVip,
                        Level = d.Level,
                        Points = d.Points,
                        RankId = d.RankId,
                        TotalPlayTime = totalPlayTime
                    });
                }

                return new StreamerLoyaltyData
                {
                    SessionCount = sessions.Count,
                    TotalPlayerTime = totalPlayerTime,
                    TotalStreamTime = (stopTime - startTime),
                    FirstStream = startTime,
                    TotalBitsCheered = totalBitsCheered,
                    TotalSubsGifted = totalSubsGifted,
                    TotalSubscribers = totalSubscribers,
                    UserLoyalties = loyalties
                                    .OrderByDescending(x => x.Level)
                                    .ThenByDescending(x => x.TotalPlayTime)
                                    .ToList()
                };
            }));
        }