public JsonResult GroupStats(long groupid) { var gStatReply = ""; using (var DB = new WWContext()) { var day1LynchInfo = DB.GroupDay1Lynch(groupid).FirstOrDefault(); var night1DieInfo = DB.GroupNight1Death(groupid).FirstOrDefault(); var day1DieInfo = DB.GroupDay1Death(groupid).FirstOrDefault(); var surviverInfo = DB.GroupSurvivor(groupid).FirstOrDefault(); ////most slutty //// TODO Add visits to database ////most common wolf //var commonWolfInfo = (from dbgp in DB.GamePlayers // join dbg in DB.Games on dbgp.GameId equals dbg.Id // where dbgp.Role == "Wolf" && dbg.GroupName == groupName // group dbgp by new { dbgp.PlayerId, dbgp.Role } // into grp // select new // { // name = DB.Players.FirstOrDefault(x => x.Id == grp.Key.PlayerId), // count = grp.Select(x => x.GameId).Distinct().Count(), // games = DB.GamePlayers.Count(x => x.PlayerId == grp.Key.PlayerId) // }).Where(x => x.games >= 10) // .OrderByDescending(x => x.count * 100 / x.games) // .FirstOrDefault(); ////most common villager //var commonVillagerInfo = (from dbgp in DB.GamePlayers // join dbg in DB.Games on dbgp.GameId equals dbg.Id // where dbgp.Role == "Villager" && dbg.GroupName == groupName // group dbgp by new { dbgp.PlayerId, dbgp.Role } // into grp // select new // { // name = DB.Players.FirstOrDefault(x => x.Id == grp.Key.PlayerId), // count = grp.Select(x => x.GameId).Distinct().Count(), // games = DB.GamePlayers.Count(x => x.PlayerId == grp.Key.PlayerId) // }).Where(x => x.games >= 10) // .OrderByDescending(x => x.count * 100 / x.games) // .FirstOrDefault(); //var timesWolfsWon = // DB.Games.Count( // x => // new[] { "Wolves", "Wolf" }.Contains(x.Winner) && // x.GroupName == groupName); //var timesVillageWon = // DB.Games.Count( // x => (x.Winner == "Village") && x.GroupName == groupName); //var timesTannerWon = // DB.Games.Count(x => x.Winner == "Tanner" && x.GroupName == groupName); //now build the reply string night1dielink, day1lynchlink, day1dielink, survivorlink; night1dielink = night1DieInfo != null ? $"<a href='../Player/{night1DieInfo.TelegramId}'>{night1DieInfo.Name}</a>" : "Not enough games"; day1lynchlink = day1LynchInfo != null ? $"<a href='../Player/{day1LynchInfo.TelegramId}'>{day1LynchInfo.Name}</a>" : "Not enough games"; day1dielink = day1DieInfo != null ? $"<a href='../Player/{day1DieInfo.TelegramId}'>{day1DieInfo.Name}</a>" : "Not enough games"; survivorlink = surviverInfo != null ? $"<a href='../Player/{surviverInfo.TelegramId}'>{surviverInfo.Name}</a>" : "Not enough games"; gStatReply += "<table class=\"table table-hover\"><tbody>" + $"<tr><td>Games played total</td><td><b>{DB.Games.Count(x => x.GroupId == groupid)}</b></td></tr>" + // $"<tr><td>Times Wolf / Wolves have won</td><td><b>{timesWolfsWon}</b></td></tr>" + //$"<tr><td>Times Village won</td><td><b>{timesVillageWon}</b></td></tr>" + //$"<tr><td>Times Tanner won</td><td><b>{timesTannerWon}</b></td></tr>" + $"<tr><td>Most likely to die first night</td><td><b>{night1dielink}</td><td>{(night1DieInfo?.pct ?? 0)}%</b></td></tr>" + $"<tr><td>Most likely to get lynched first day</td><td><b>{day1lynchlink}</td><td>{(day1LynchInfo?.pct ?? 0)}%</b></td></tr>" + $"<tr><td>Most likely to die first 24 hours</td><td><b>{day1dielink}</td><td>{(day1DieInfo?.pct ?? 0)}%</b></td></tr>" + $"<tr><td>Best survivor</td><td><b>{survivorlink}</td><td>{surviverInfo?.pct ?? 0}%</b></td></tr>" + //$"<tr><td>Most common villager</td><td><b>{commonVillagerInfo?.name.Name}</td><td>{(commonVillagerInfo?.count ?? 0) * 100 / commonVillagerInfo?.games ?? 1}%</b></td></tr>" + //$"<tr><td>Most common wolf</td><td><b>{commonWolfInfo?.name.Name}</td><td>{(commonWolfInfo?.count ?? 0) * 100 / commonWolfInfo?.games ?? 1}%</b></td></tr>" + "</tbody></table>"; } return(Json(gStatReply, JsonRequestBehavior.AllowGet)); }
static void GroupStats(long groupid) { Console.WriteLine("Group stats for " + groupid); try { var runStats = false; using (var db = new WWContext()) { //first, check that we should even run stats on this group var grp = db.Groups.FirstOrDefault(x => x.GroupId == groupid); var stat = db.GroupStats.FirstOrDefault(x => x.GroupId == groupid); if (grp == null) //that's a problem.. { return; } 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) { return; } var gamesPlayed = grp.Games.Count; var night1death = db.GroupNight1Death(groupid).FirstOrDefault(); var day1lynch = db.GroupDay1Lynch(groupid).FirstOrDefault(); var day1death = db.GroupDay1Death(groupid).FirstOrDefault(); var survivor = 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 (survivor != null) { stat.BestSurvivor = survivor.Name; stat.BestSurvivorPercent = (int)survivor.pct; } stat.GamesPlayed = gamesPlayed; stat.LastRun = DateTime.Now; if (day1death != null) { stat.MostDeadFirstDay = day1death.Name; stat.MostDeadFirstPercent = day1death.pct; } if (night1death != null) { stat.MostKilledFirstNight = night1death.Name; stat.MostKilledFirstPercent = night1death.pct; } if (day1lynch != null) { stat.MostLynchedFirstNight = day1lynch.Name; stat.MostLynchFirstPercent = day1lynch.pct; } db.SaveChanges(); } } catch { // ignored } }
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 } }