private string BuildRanks(SeasonWeek currentWeek) { var weeklyWinners = BuildWeeklyWinnersDictionary(currentWeek); var leaguePayouts = BuildLeaguePayoutsDictionary(currentWeek.SeasonYear); var rankDetail = (from rd in context.ExecuteQuery<RankDetail>(NPGGFFL2.queries.RanksQuery.Replace("{{Year}}", currentWeek.SeasonYear.ToString())) group rd by new { rd.OwnerId, rd.TeamName, rd.SeasonYear, rd.SeasonType, rd.SeasonWeekNum, rd.SeasonWeekId } into g select new { OwnerId = g.Key.OwnerId, TeamName = g.Key.TeamName, SeasonYear = g.Key.SeasonYear, SeasonType = g.Key.SeasonType, SeasonWeekNum = g.Key.SeasonWeekNum, SeasonWeekId = g.Key.SeasonWeekId, PlayerStats = g.ToList().OrderBy(i => i.LineupPriority).ThenByDescending(i => i.Points), TotalPoints = g.Sum(ps => ps.Points), Win = (weeklyWinners.ContainsKey(g.Key.SeasonWeekId) ? (weeklyWinners[g.Key.SeasonWeekId].Equals(g.Key.OwnerId) ? true : false) : false) }) .OrderBy(i => i.SeasonYear) .ThenBy(i => i.SeasonType) .ThenBy(i => i.SeasonWeekNum) .ThenByDescending(i => i.TotalPoints); var rank = (from r in rankDetail group r by new { r.OwnerId, r.TeamName } into g select new { OwnerId = g.Key.OwnerId, TeamName = g.Key.TeamName, TotalPoints = g.Sum(r => r.TotalPoints), RankDetail = g.ToList() }) .OrderByDescending(r => r.TotalPoints) .Select((r, i) => new { OwnerId = r.OwnerId, TeamName = r.TeamName, TotalPoints = r.TotalPoints, DollarsMade = (leaguePayouts.ContainsKey(i+1) ? leaguePayouts[i+1] : 0) + (r.RankDetail.Where(rd => rd.Win == true).Count() * 50), RankDetail = r.RankDetail }); return JsonConvert.SerializeObject(rank); }
public bool UpdateGames(string updategames) { bool success = true; foreach (var sweek in seasonWeeks) { var season = Convert.ToInt32(sweek.Split('-')[0].Trim()); var stype = sweek.Split('-')[1].Trim(); var week = Convert.ToInt32(sweek.Split('-')[2].Trim()); var xml = HelperMethods.LoadHtmlFromUrl(string.Format("http://www.nfl.com/ajax/scorestrip?season={0}&seasonType={1}&week={2}", season, stype, week)); var gameXml = XDocument.Parse(xml); if (gameXml.Descendants("g").Count() > 0) { #region Insert Season Week var seasonWeekExists = context.SeasonWeeks.Where(sw => sw.SeasonWeekId == sweek).FirstOrDefault(); if (seasonWeekExists == null) { seasonWeekExists = new SeasonWeek(); seasonWeekExists.SeasonWeekId = sweek; seasonWeekExists.SeasonYear = season; seasonWeekExists.SeasonType = stype; seasonWeekExists.SeasonWeekNum = week; seasonWeekExists.StartDtm = DateTime.Now; seasonWeekExists.EndDtm = DateTime.Now; context.SeasonWeeks.InsertOnSubmit(seasonWeekExists); } context.SubmitChanges(); #endregion #region Insert Games foreach (var game in gameXml.Descendants("g")) { var gameId = Convert.ToInt32(game.Attribute("eid").Value); var gameYear = Convert.ToInt32(gameId.ToString().Substring(0, 4)); var gameMonth = Convert.ToInt32(gameId.ToString().Substring(4, 2)); var gameDay = Convert.ToInt32(gameId.ToString().Substring(6, 2)); var gameTime = game.Attribute("t").Value; var gameHour = Convert.ToInt32(gameTime.Split(':')[0].Trim()); var gameMinute = Convert.ToInt32(gameTime.Split(':')[1].Trim()); var gameExists = context.Games.Where(g => g.GameId == gameId).FirstOrDefault(); if (gameExists == null) { Game g = new Game(); g.GameId = gameId; g.ESPNId = 0; g.SeasonWeekId = sweek; g.HomeTeamId = game.Attribute("h").Value.ToLower(); g.AwayTeamId = game.Attribute("v").Value.ToLower(); g.GameDtm = new DateTime(gameYear, gameMonth, gameDay, gameHour, gameMinute, 0); context.Games.InsertOnSubmit(g); } } context.SubmitChanges(); #endregion #region Update Start & End Dtm var firstGame = seasonWeekExists.Games.OrderBy(g => g.GameDtm).FirstOrDefault(); var lastGame = seasonWeekExists.Games.OrderByDescending(g => g.GameDtm).FirstOrDefault(); var dt = firstGame.GameDtm; while (dt.DayOfWeek != DayOfWeek.Wednesday) dt = dt.AddDays(-1); seasonWeekExists.StartDtm = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0); dt = lastGame.GameDtm; while (dt.DayOfWeek != DayOfWeek.Tuesday) dt = dt.AddDays(1); seasonWeekExists.EndDtm = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59); context.SubmitChanges(); #endregion } } return success; }
private Dictionary<string,string> BuildWeeklyWinnersDictionary(SeasonWeek currentWeek) { Dictionary<string, string> dict = new Dictionary<string, string>(); var weeklyWinnerQuery = context.ExecuteQuery<WeeklyWinner>(NPGGFFL2.queries.WeeklyWinnersQuery.Replace("{{Year}}", currentWeek.SeasonYear.ToString())); foreach (var ww in weeklyWinnerQuery) { dict.Add(ww.SeasonWeekId, ww.OwnerId); } return dict; }
partial void DeleteSeasonWeek(SeasonWeek instance);
partial void UpdateSeasonWeek(SeasonWeek instance);
partial void InsertSeasonWeek(SeasonWeek instance);