//TODO change route to just "stats" public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "poststats/{botid:long}")] HttpRequest req, long botId, ILogger log) { var guildCountString = await req.Body.ReadToEndAsync(); var guildCount = int.Parse(guildCountString); var statPostsSerialized = Environment.GetEnvironmentVariable("StatPostsSerialized"); var statPosts = JsonConvert.DeserializeObject <List <StatPost> >(statPostsSerialized); var logWrapper = new LoggerWrapper(log); using (var statWrapper = new StatWrapper()) { var postStatsHelper = new PostStatsHelper(logWrapper, statWrapper); return(await postStatsHelper.RunAsync(guildCount, botId, statPosts)); } }
public StatWrapper GetStatDataByPlayer(string playerId, string year) { // todo: parameter sanity checks string yearKey; if (!_nflGameKeys.TryGetValue(year, out yearKey)) { return(null); } var request = new RestRequest("player/{yearKey}.p.{playerId}/stats"); request.AddUrlSegment("yearKey", yearKey); request.AddUrlSegment("playerId", playerId); request.AddJsonParam(); var stats = new List <Models.StatsModel.Stat>(); var statWrapper = new StatWrapper(); try { var response = _client.Execute(request); var json = JObject.Parse(response.Content); var statMetadataJson = json["fantasy_content"]["player"][0]; var statMetadata = JsonConvert.DeserializeObject <List <Tuple <string, string> > >(statMetadataJson.ToString()); var teamName = statMetadata.FirstOrDefault(tup => tup.Item1 == "editorial_team_full_name").Item2; var teamAbbreviation = statMetadata.FirstOrDefault(tup => tup.Item1 == "editorial_team_abbr").Item2; var playerPosition = statMetadata.FirstOrDefault(tup => tup.Item1 == "display_position").Item2; var playerStats = json["fantasy_content"]["player"][1]["player_stats"]["stats"]; stats = JsonConvert.DeserializeObject <List <Models.StatsModel.Stat> >(playerStats.ToString()); statWrapper.Position = playerPosition; statWrapper.Team = teamName; statWrapper.TeamAbbr = teamAbbreviation; statWrapper.Stats = stats; } catch { throw; } return(statWrapper); }
public void TestFillWeeklyStatsForOffensivePlayers() { var wrapper = new Api.ApiWrapper("nfl"); using (var context = new FantasyContext()) { var players = context.Players.ToList(); var playerStats = context.Stats.ToList(); var noStats = context.NoStats.ToList(); //var years = Enumerable.Range(2001, 13).OrderByDescending(i => i).ToList(); var years = Enumerable.Range(2011, 1).OrderByDescending(i => i).ToList(); var weeks = Enumerable.Range(1, 17).ToList(); var positions = new List <SimplePositionTypes> { SimplePositionTypes.Quarterback, SimplePositionTypes.Runningback, SimplePositionTypes.Receiver, SimplePositionTypes.TightEnd, SimplePositionTypes.Kicker }; var validPlayers = from pl in players join po in positions on pl.PrimaryPosition equals po select pl; foreach (var player in validPlayers) { foreach (var year in years) { foreach (var week in weeks) { var shouldPull = !playerStats.Any(ps => ps.Player.YahooPlayerId == player.YahooPlayerId && ps.Year.Year == year && ps.Week == week) && !noStats.Any(ns => ns.Player == player && ns.Week == week && ns.Year == year); if (!shouldPull) { continue; } var stats = new StatWrapper(); try { stats = wrapper.GetWeeklyStatsByPlayer(player.YahooPlayerId.ToString(), year.ToString(), week); } catch (KeyNotFoundException ex) { var noStat = new NoStats { Player = player, Week = week, Year = year }; context.NoStats.Add(noStat); context.SaveChanges(); continue; } catch { continue; } if (stats.Stats == null) { continue; } if (stats != null) { var simpleStat = new SimpleStats { Player = player, Year = new DateTime(year, 1, 1), Week = week, TeamName = stats.Team ?? "", TeamAbbreviation = stats.TeamAbbr ?? "", Position = player.PrimaryPosition == null ? 0 : player.PrimaryPosition }; foreach (var stat in stats.Stats) { var props = simpleStat.GetType().GetProperties(); foreach (var prop in props) { if (prop.CustomAttributes.Any(p => p.AttributeType == typeof(PlayerMappingAttribute))) { var attr = prop.GetCustomAttributes(typeof(PlayerMappingAttribute), false).FirstOrDefault(); var statId = ((PlayerMappingAttribute)attr).YahooStatId; if (stat.StatDetail.StatId == statId) { if (prop.PropertyType == typeof(int?)) { int nullableValue = 0; int.TryParse(stat.StatDetail.Value, out nullableValue); prop.SetValue(simpleStat, nullableValue); } else { var statValue = Convert.ChangeType(stat.StatDetail.Value, prop.PropertyType); prop.SetValue(simpleStat, statValue); } } } } } context.Stats.Add(simpleStat); context.SaveChanges(); } } } } } }
public void TestFillAnnualStatsForOffensivePlayers() { var wrapper = new Api.ApiWrapper("nfl"); using (var context = new FantasyContext()) { var players = context.Players.ToList(); var playerStats = context.Stats.ToList(); //var years = Enumerable.Range(2001, 13).OrderByDescending(i => i).ToList(); var years = Enumerable.Range(2013, 13).OrderByDescending(i => i).ToList(); var positions = new List <SimplePositionTypes> { //SimplePositionTypes.Quarterback, SimplePositionTypes.Runningback, SimplePositionTypes.Receiver //, //SimplePositionTypes.TightEnd, //SimplePositionTypes.Kicker }; var validPlayers = from pl in players join po in positions on pl.PrimaryPosition equals po select pl; foreach (var player in validPlayers) { foreach (var year in years) { var shouldPull = !playerStats.Any(ps => ps.Player.YahooPlayerId == player.YahooPlayerId && ps.Year.Year == year); if (!shouldPull) { continue; } var stats = new StatWrapper(); try { stats = wrapper.GetStatDataByPlayer(player.YahooPlayerId.ToString(), year.ToString()); } catch { if (stats.Stats == null) { continue; } } if (stats != null) { var simpleStat = new SimpleStats { Player = player, Year = new DateTime(year, 1, 1), Week = null, TeamName = stats.Team, TeamAbbreviation = stats.TeamAbbr }; switch (stats.Position) { case "QB": simpleStat.Position = SimplePositionTypes.Quarterback; break; case "RB": simpleStat.Position = SimplePositionTypes.Runningback; break; case "WR": simpleStat.Position = SimplePositionTypes.Receiver; break; case "TE": simpleStat.Position = SimplePositionTypes.TightEnd; break; case "DEF": simpleStat.Position = SimplePositionTypes.TeamDefense; break; case "K": simpleStat.Position = SimplePositionTypes.Kicker; break; case "CB": simpleStat.Position = SimplePositionTypes.Cornerback; break; case "DT": simpleStat.Position = SimplePositionTypes.DefensiveTackle; break; case "S": simpleStat.Position = SimplePositionTypes.Safety; break; case "LB": simpleStat.Position = SimplePositionTypes.Linebacker; break; case "DE": simpleStat.Position = SimplePositionTypes.DefensiveEnd; break; default: simpleStat.Position = SimplePositionTypes.Unknown; break; } foreach (var stat in stats.Stats) { var props = simpleStat.GetType().GetProperties(); foreach (var prop in props) { if (prop.CustomAttributes.Any(p => p.AttributeType == typeof(PlayerMappingAttribute))) { var attr = prop.GetCustomAttributes(typeof(PlayerMappingAttribute), false).FirstOrDefault(); var statId = ((PlayerMappingAttribute)attr).YahooStatId; if (stat.StatDetail.StatId == statId) { if (prop.PropertyType == typeof(int?)) { int nullableValue = 0; int.TryParse(stat.StatDetail.Value, out nullableValue); prop.SetValue(simpleStat, nullableValue); } else { var statValue = Convert.ChangeType(stat.StatDetail.Value, prop.PropertyType); prop.SetValue(simpleStat, statValue); } } } } } context.Stats.Add(simpleStat); context.SaveChanges(); } } } } }
public StatWrapper GetWeeklyStatsByPlayer(string playerId, string year, int week) { // todo: parameter sanity checks string yearKey; if (!_nflGameKeys.TryGetValue(year, out yearKey)) { return(null); } var request = new RestRequest("player/{yearKey}.p.{playerId}/stats;type=week;week={week}"); request.AddUrlSegment("yearKey", yearKey); request.AddUrlSegment("playerId", playerId); request.AddUrlSegment("week", week.ToString()); request.AddJsonParam(); var stats = new List <Models.StatsModel.Stat>(); var statWrapper = new StatWrapper(); try { var response = _client.Execute(request); var json = JObject.Parse(response.Content); //var statMetadataJson = json["fantasy_content"]["player"][0]; //var statMetadata = JsonConvert.DeserializeObject<StatPlayerData>(statMetadataJson.ToString()); //var teamName = statMetadata.TeamFullName; //var teamAbbreviation = statMetadata.TeamAbbreviation; //var playerPosition = statMetadata.DisplayPosition; try { var playerStats = json["fantasy_content"]["player"][1]["player_stats"]["stats"]; stats = JsonConvert.DeserializeObject <List <Models.StatsModel.Stat> >(playerStats.ToString()); statWrapper.Position = ""; statWrapper.Team = ""; statWrapper.TeamAbbr = ""; statWrapper.Stats = stats; } catch { if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { var playerKey = yearKey + ".p." + playerId; var errorDescription = json["error"]["description"].Value <string>(); if (errorDescription == "Player key '" + playerKey + "' does not exist.") { throw new KeyNotFoundException("Player key " + playerKey + " does not exist."); } } //if(response.ToString().Contains("999 Unable to process")) //{ // throw new Exception //} } } catch { throw; } return(statWrapper); }