static void GlobalStats() { Console.WriteLine("Calculating Global Stats"); try { //now do daily counts using (var db = new WWContext()) { Console.WriteLine("Updating Daily Counts"); //get daily counts var counts = db.getDailyCounts(); db.DailyCounts.RemoveRange(db.DailyCounts); foreach (var count in counts) { db.DailyCounts.Add(new DailyCount { Day = count.Day.Value, Games = count.Games.Value, Groups = count.Groups.Value, Users = count.players.Value }); } db.SaveChanges(); Console.WriteLine("Done"); } using (var DB = new WWContext()) { DB.Database.CommandTimeout = 600; Console.WriteLine("Total games.."); var gamesPlayed = DB.Games.Count(); Console.WriteLine("Night 1 Death"); var night1death = DB.GlobalNight1Death().First(); Console.WriteLine("Day 1 Lynch"); var day1lynch = DB.GlobalDay1Lynch().First(); Console.WriteLine("Day 1 Death"); var day1death = DB.GlobalDay1Death().First(); Console.WriteLine("Survivor"); var survivor = DB.GlobalSurvivor().First(); Console.WriteLine("Creating stat object"); var stat = DB.GlobalStats.FirstOrDefault(); if (stat == null) { stat = DB.GlobalStats.Create(); DB.GlobalStats.Add(stat); } stat.PlayersKilled = DB.GamePlayers.Count(x => !x.Survived); stat.PlayersSurvived = DB.GamePlayers.Count(x => x.Survived); stat.TotalGroups = DB.Groups.Count(); stat.TotalPlayers = DB.Players.Count(); stat.BestSurvivor = survivor.Name; stat.BestSurvivorPercent = (int)survivor.pct; stat.BestSurvivorId = survivor.TelegramId; stat.GamesPlayed = gamesPlayed; stat.LastRun = DateTime.Now; stat.MostKilledFirstDay = day1death.Name; stat.MostKilledFirstDayPercent = day1death.pct; stat.MostKilledFirstDayId = day1death.TelegramId; stat.MostKilledFirstNight = night1death.Name; stat.MostKilledFirstPercent = night1death.pct; stat.MostKilledFirstNightId = night1death.TelegramId; stat.MostLynchedFirstDay = day1lynch.Name; stat.MostLynchedFirstPercent = day1lynch.pct; stat.MostLynchedFirstDayId = day1lynch.TelegramId; Console.WriteLine("Saving to database"); DB.SaveChanges(); Console.WriteLine("Done"); } //now do daily counts using (var db = new WWContext()) { Console.WriteLine("Updating Daily Counts"); //get daily counts var counts = db.getDailyCounts(); db.DailyCounts.RemoveRange(db.DailyCounts); foreach (var count in counts) { db.DailyCounts.Add(new DailyCount { Day = count.Day.Value, Games = count.Games.Value, Groups = count.Groups.Value, Users = count.players.Value }); } db.SaveChanges(); Console.WriteLine("Done"); } } catch (Exception e) { while (e.InnerException != null) { e = e.InnerException; } Console.WriteLine(e.Message); } }
public static void GetFullStats(Update u, string[] args) { var name = u.Message.From.FirstName; var id = u.Message.From.Id; var username = u.Message.From.Username; string Content; try { using (var db = new WWContext()) { Content = ""; using (var DB = new WWContext()) { DB.Database.CommandTimeout = 600; var gamePlayed = DB.Games.Count(); var night1death = DB.GlobalNight1Death().First(); var day1lynch = DB.GlobalDay1Lynch().First(); var day1death = DB.GlobalDay1Death().First(); var survivor = DB.GlobalSurvivor().First(); var playersKilled = DB.GamePlayers.Count(x => !x.Survived); var playersSurvived = DB.GamePlayers.Count(x => x.Survived); var totalGroups = DB.Groups.Count(); var totalPlayers = DB.Players.Count(); var bestSurvivor = survivor.Name; var bestSurvivorPercent = (int)survivor.pct; var bestSurvivorId = survivor.TelegramId; var mostKilledFirstDay = day1death.Name; var mostKilledFirstDayPercent = day1death.pct; var mostKilledFirstDayId = day1death.TelegramId; var mostKilledFirstNight = night1death.Name; var mostKilledFirstPercent = night1death.pct; var mostKilledFirstNightId = night1death.TelegramId; var mostLynchedFirstDay = day1lynch.Name; var mostLynchedFirstPercent = day1lynch.pct; var mostLynchedFirstDayId = day1lynch.TelegramId; Content += $"Total Games: {gamePlayed}\nNo. of Groups: {totalGroups}\nNo. of Players: {totalPlayers}\n"; Content += $"Best Survivor: {bestSurvivor} ({bestSurvivorPercent}%)\nMost Killed 1st Night: {mostKilledFirstNight} ({mostKilledFirstPercent}%)\n"; Content += $"Most Lynched 1st Day: {mostLynchedFirstDay} ({mostLynchedFirstPercent}%)"; if (u.Message.Chat.Id < 0) { // group var groupid = u.Message.Chat.Id; var grp = db.Groups.FirstOrDefault(x => x.GroupId == groupid); Content += $"\nGroup Stats for {u.Message.Chat.Title}:\n"; var runStats = false; //first, check that we should even run stats on this group var stat = db.GroupStats.FirstOrDefault(x => x.GroupId == groupid); if (stat == null) { runStats = true; } else { if ((grp.Games.OrderByDescending(x => x.TimeEnded).FirstOrDefault()?.TimeEnded ?? DateTime.MinValue) > (stat.LastRun ?? DateTime.MinValue.AddSeconds(1))) { runStats = true; } } if (!runStats) { // later } var gamesPlayed = grp.Games.Count; var night1death1 = db.GroupNight1Death(groupid).FirstOrDefault(); var day1lynch1 = db.GroupDay1Lynch(groupid).FirstOrDefault(); var day1death1 = db.GroupDay1Death(groupid).FirstOrDefault(); var survivor1 = db.GroupSurvivor(groupid).FirstOrDefault(); if (stat == null) { stat = db.GroupStats.Create(); stat.GroupId = groupid; db.GroupStats.Add(stat); } stat.GroupName = grp.Name; //TODO add this metric later //stat.PlayersKilled = db.GamePlayers.Count(x => !x.Survived); //stat.PlayersSurvived = db.GamePlayers.Count(x => x.Survived); if (survivor1 != null) { stat.BestSurvivor = survivor1.Name; stat.BestSurvivorPercent = (int)survivor1.pct; } stat.GamesPlayed = gamesPlayed; stat.LastRun = DateTime.Now; if (day1death1 != null) { stat.MostDeadFirstDay = day1death1.Name; stat.MostDeadFirstPercent = day1death1.pct; } if (night1death1 != null) { stat.MostKilledFirstNight = night1death1.Name; stat.MostKilledFirstPercent = night1death1.pct; } if (day1lynch1 != null) { stat.MostLynchedFirstNight = day1lynch1.Name; stat.MostLynchFirstPercent = day1lynch1.pct; } Content += $"No. of Games: {gamesPlayed}\n"; Content += $"Best Survivor: {survivor1.Name} ({(int)survivor1.pct}%)\nMost Killed 1st Night: {night1death1.Name} ({night1death1.pct}%)\n"; Content += $"Most Lynched 1st Day: {day1lynch1.Name} ({day1lynch1.pct}%)"; } DB.SaveChanges(); Console.WriteLine("Done"); Bot.Api.SendTextMessage(u.Message.Chat.Id, Content, replyToMessageId: u.Message.MessageId); } } } catch { // nothing } }