Esempio n. 1
0
        public static IPlayerData CreateFromData(JToken player)
        {
            ESPNPlayerData stats = new ESPNPlayerData();

            stats.DisplayName = (string)player["player"]["fullName"];
            stats.SourceID    = (string)player["player"]["id"];
            stats.Outlook     = (string)player["player"]["seasonOutlook"];

            HashSet <Position> allPositions  = new HashSet <Position>();
            List <int>         espnPositions = new List <int>(((JArray)player["player"]["eligibleSlots"]).Select(s => (int)s).ToArray());

            foreach (int espnPosition in espnPositions)
            {
                List <Position> positions;
                if (PositionMapping.TryGetValue(espnPosition, out positions))
                {
                    foreach (Position position in positions)
                    {
                        allPositions.Add(position);
                    }
                }
            }
            stats.Positions = allPositions.ToList();

            Dictionary <string, string> espnStats = new Dictionary <string, string>();
            JToken statsRoot = player["player"]["stats"];

            if (statsRoot != null && statsRoot.Count() > 0)
            {
                bool foundStats = false;
                foreach (JToken statSet in player["player"]["stats"].Children())
                {
                    foreach (string espnStat in ESPNConstants.Stats.Batters.All.Union(ESPNConstants.Stats.Pitchers.All))
                    {
                        string statValueStr = (string)statSet["stats"][espnStat];
                        if (!string.IsNullOrEmpty(statValueStr))
                        {
                            espnStats[espnStat] = statValueStr;
                            foundStats          = true;
                        }
                    }

                    if (foundStats)
                    {
                        break;
                    }
                }
            }
            ESPNConstants.Stats.MapESPNStatDictionaryToDataModelStatDictionary(espnStats, stats.Stats);

            return(stats);
        }