Пример #1
0
        public void Add(IUserMessage message)
        {
            if (!Stats.TryGetValue(message.Author.Id, out var stats))
            {
                stats = new ValueStats();
                Stats[message.Author.Id] = stats;
            }
            stats.TotalSent++;
            stats.AddAverage(message.Content.Length, ref stats.AverageLength);
            var midnight = new DateTime(message.Timestamp.Year, message.Timestamp.Month, message.Timestamp.Day);
            var diff     = message.Timestamp - midnight;

            stats.AddAverage(diff.TotalSeconds, ref stats.AverageSecondsIntoDay);

            AllStats.TotalSent++;
            AllStats.AddAverage(message.Content.Length, ref AllStats.AverageLength);
            AllStats.AddAverage(diff.TotalSeconds, ref AllStats.AverageSecondsIntoDay);

            if (message.Content.StartsWith("$") || message.Content.StartsWith("^"))
            {
                return;
            }
            foreach (var word in message.Content.Split(" "))
            {
                if (word.Length >= 3 && word.All(x => x.IsAlphaNumeric()))
                {
                    WordCount.Increment(word.ToLower());
                }
            }
        }
Пример #2
0
        public JObject GetJson()
        {
            var ordered = Stats.OrderByDescending(x => x.Value.TotalSent).Where(x => x.Key != 0).Select(x => x.Key).ToList();

            if (ordered.Count > max)
            {
                var os = new ValueStats();
                Stats[0]        = os;
                DisplayNames[0] = $"Other ({max - ordered.Count})";
                foreach (var other in ordered.Skip(max))
                {
                    // new = old + (val - old / n)
                    var ex = Stats[other];
                    for (int i = 0; i < ex.TotalSent; i++)
                    {
                        os.TotalSent++;
                        os.AddAverage(ex.AverageLength, ref os.AverageLength);
                        os.AddAverage(ex.AverageSecondsIntoDay, ref os.AverageSecondsIntoDay);
                    }
                }
                ordered = ordered.Take(max).ToList();
                ordered.Add(0);
            }
            var jobj = new JObject();

            jobj["total"] = AllStats.ToJson();
            foreach (var id in ordered)
            {
                var stats  = Stats[id];
                var key    = DisplayNames.GetValueOrDefault(id, id.ToString());
                var user   = Program.Client.GetUser(id);
                var obj    = new JObject();
                var usrObj = new JObject();
                usrObj["username"]  = user?.Username ?? key;
                usrObj["avatar"]    = user?.GetAnyAvatarUrl() ?? "n/a";
                obj["user"]         = usrObj;
                obj["stats"]        = stats.ToJson();
                jobj[id.ToString()] = obj;
            }
            jobj["words"] = JObject.FromObject(GetWords());
            return(jobj);
        }