Exemplo n.º 1
0
        public static IEnumerable <Player> LoadProjections()
        {
            // Map from JSON file to set of relevant stats for the players in that file
            List <Tuple <string, bool> > playerSets = new List <Tuple <string, bool> >()
            {
                new Tuple <string, bool>(ESPNAPI.LoadBatterProjections(), true),
                new Tuple <string, bool>(ESPNAPI.LoadPitcherProjections(), false),
            };

            foreach (Tuple <string, bool> playerSet in playerSets)
            {
                string json = playerSet.Item1;
                IEnumerable <string> stats = playerSet.Item2 ? ESPNConstants.Stats.Batters.All : ESPNConstants.Stats.Pitchers.All;
                if (!string.IsNullOrEmpty(json))
                {
                    JObject file    = JObject.Parse(json);
                    JArray  players = (JArray)file["players"];
                    foreach (JToken player in players)
                    {
                        JToken statsRoot = player["player"]["stats"];
                        if (statsRoot == null || statsRoot.Count() == 0)
                        {
                            continue;
                        }

                        Player p = new Player();
                        p.FullName      = (string)player["player"]["fullName"];
                        p.Id            = int.Parse((string)player["player"]["id"]);
                        p.Rank          = TryGetValue(player, "player", "draftRanksByRankType", "STANDARD", "rank");
                        p.SeasonOutlook = (string)player["player"]["seasonOutlook"];
                        p.Positions     = new List <int>(((JArray)player["player"]["eligibleSlots"]).Select(s => (int)s).ToArray());
                        p.Stats         = new Dictionary <string, string>();
                        p.IsBatter      = playerSet.Item2;
                        bool foundStats = false;
                        foreach (JToken statSet in player["player"]["stats"].Children())
                        {
                            foreach (string stat in stats)
                            {
                                p.Stats[stat] = (string)statSet["stats"][stat];
                                if (!string.IsNullOrEmpty(p.Stats[stat]))
                                {
                                    foundStats = true;
                                }
                            }

                            if (foundStats)
                            {
                                break;
                            }
                        }
                        if (foundStats)
                        {
                            yield return(p);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void LoadProjectionsIntoRoot(Root root)
        {
            List <string> jsons = new List <string>()
            {
                ESPNAPI.LoadBatterProjections(),
                          ESPNAPI.LoadPitcherProjections()
            };

            Dictionary <string, IPlayerData> espnPlayerData = new Dictionary <string, IPlayerData>();

            foreach (string json in jsons)
            {
                if (!string.IsNullOrEmpty(json))
                {
                    JObject file    = JObject.Parse(json);
                    JArray  players = (JArray)file["players"];
                    foreach (JToken player in players)
                    {
                        IPlayerData playerData = ESPNPlayerData.CreateFromData(player);
                        espnPlayerData[playerData.SourceID] = playerData;
                    }
                }
            }

            Dictionary <string, FantasySports.DataModels.Player> dmPlayers = new Dictionary <string, FantasySports.DataModels.Player>();

            foreach (FantasySports.DataModels.Player player in root.Players.Values)
            {
                FantasySports.DataModels.IPlayerData playerData;
                if (player.PlayerData.TryGetValue(FantasySports.DataModels.Constants.StatSource.ESPNProjections, out playerData))
                {
                    dmPlayers[playerData.SourceID] = player;
                }
            }

            foreach (string sourceId in espnPlayerData.Keys)
            {
                FantasySports.DataModels.Player player;
                if (!dmPlayers.TryGetValue(sourceId, out player))
                {
                    IPlayerData espnPlayer = espnPlayerData[sourceId];
                    player = new FantasySports.DataModels.Player(espnPlayer.DisplayName, root.Players.Count);
                    root.Players[player.Id] = player;
                }
                player.PlayerData[FantasySports.DataModels.Constants.StatSource.ESPNProjections] = espnPlayerData[sourceId];
            }
        }