示例#1
0
        public Team(string teamID)
        {
            NHLTeamID = Convert.ToInt32(teamID);

            // Create API call URL by appending the team ID to the URL
            string teamLink = NHLAPIServiceURLs.teams + teamID;

            var json = DataAccessLayer.ExecuteAPICall(teamLink);

            var specificTeam = json.SelectToken("teams[0]").ToObject <JObject>();

            // Populate the raw JSON to a property
            teamJson = specificTeam;


            if (specificTeam.ContainsKey("teamName"))
            {
                TeamName = json.SelectToken("teams[0].teamName").ToString();
            }

            if (specificTeam.ContainsKey("abbreviation"))
            {
                TeamAbbreviation = json.SelectToken("teams[0].abbreviation").ToString();
            }

            if (specificTeam.ContainsKey("locationName"))
            {
                TeamCity = json.SelectToken("teams[0].locationName").ToString();
            }

            if (specificTeam.ContainsKey("firstYearOfPlay"))
            {
                FirstYearOfPlay = json.SelectToken("teams[0].firstYearOfPlay").ToString();
            }

            if (specificTeam.ContainsKey("conference"))
            {
                //Division - need to implement division class
                teamConference = new Conference(Convert.ToInt32(json.SelectToken("teams[0].conference.id")));
            }

            if (specificTeam.ContainsKey("division"))
            {
                teamDivision = new Division(Convert.ToInt32(json.SelectToken("teams[0].division.id")));
            }
            // Not all venues have an ID in the API (like Maple Leafs Scotiabank Arena), so need to check
            if (specificTeam.ContainsKey("venue"))
            {
                var venueJson = json.SelectToken("teams[0].venue").ToObject <JObject>();

                if (venueJson.ContainsKey("id"))
                {
                    TeamVenue = new Venue(json.SelectToken("teams[0].venue.id").ToString());
                }
            }


            if (specificTeam.ContainsKey("officialSiteUrl"))
            {
                webSite = json.SelectToken("teams[0].officialSiteUrl").ToString();
            }
        }
示例#2
0
        // Constructor for Game class from a specific Game ID
        public Game(string theGameID)
        {
            // Populate the gameID property
            gameID = theGameID;

            // Get the URL for the API call to a specific Game
            string theGame = NHLAPIServiceURLs.specificGame;

            // Replace placeholder value ("###") in the placeholder URL with the requested GameID.
            gameLink = theGame.Replace("###", theGameID);

            // Execute the API call
            var json = DataAccessLayer.ExecuteAPICall(gameLink);

            // Populate the raw JSON into the gameJson property
            gameJson = json;

            // Populate the rest of the Game class properties
            gameType          = json.SelectToken("gameData.game.type").ToString();
            season            = json.SelectToken("gameData.game.season").ToString();
            gameDate          = json.SelectToken("gameData.datetime.dateTime").ToString();
            abstractGameState = json.SelectToken("gameData.status.abstractGameState").ToString();
            codedGameState    = json.SelectToken("gameData.status.codedGameState").ToString();
            detailedState     = json.SelectToken("gameData.status.detailedState").ToString();
            statusCode        = json.SelectToken("gameData.status.statusCode").ToString();
            homeTeam          = new Team(json.SelectToken("gameData.teams.home.id").ToString());
            awayTeam          = new Team(json.SelectToken("gameData.teams.away.id").ToString());
            JObject venueJson = JObject.Parse(json.SelectToken("gameData.teams.home").ToString());

            if (venueJson.ContainsKey("venue"))
            {
                JObject venueObject = JObject.Parse(json.SelectToken("gameData.teams.home.venue").ToString());
                if (venueObject.ContainsKey("id"))
                {
                    gameVenue = new Venue(json.SelectToken("gameData.teams.home.venue.id").ToString());
                }
            }

            // If the Abstract Game State is "Live" or "Final", populate the in-game data.
            if (abstractGameState != "Preview")
            {
                var    periodInfo = JArray.Parse(json.SelectToken("liveData.linescore.periods").ToString());
                Period tempPeriod;
                periodData = new List <Period>();

                // If there is period data, populate the list of periods.
                if (periodInfo.Count > 0)
                {
                    foreach (var period in periodInfo)
                    {
                        tempPeriod = new Period(gameID, (JObject)period, homeTeam.NHLTeamID.ToString(), awayTeam.NHLTeamID.ToString());
                        periodData.Add(tempPeriod);
                    }
                }


                //Populate the Box Score if it exists
                if (!(json.SelectToken("liveData.boxscore") is null))
                {
                    gameBoxScore = new BoxScore(json.SelectToken("gameData.teams.home.id").ToString(), json.SelectToken("gameData.teams.away.id").ToString(), json.SelectToken("liveData.boxscore").ToObject <JObject>());
                }


                // Populating the players
                // Create a JSON
                //var playerJson = JObject.Parse(json.SelectToken("gameData.players").ToString());


                //IList<JToken> results = playerJson.Children().ToList();
                IList <JToken> results = JObject.Parse(json.SelectToken("gameData.players").ToString()).Children().ToList();

                // Create a placeholder List<Player> for populating the game roster
                List <Player> playerList = new List <Player>();

                // Add each player to the List<Player> placeholder
                if (results.Children().Count() > 0)
                {
                    foreach (JToken result in results.Children())
                    {
                        playerList.Add(new Player(Convert.ToInt32(result["id"])));
                    }
                }


                // Populate gameParticipants property with List<Player> placeholder.
                if (playerList.Count > 0)
                {
                    gameParticipants = playerList;
                }

                var gameEventsJson = JArray.Parse(json.SelectToken("liveData.plays.allPlays").ToString());

                GameEvent aGameEvent = new GameEvent();
                gameEvents = new List <GameEvent>();

                if (gameEventsJson.Count > 0)
                {
                    foreach (var item in gameEventsJson)
                    {
                        aGameEvent = new GameEvent(item);
                        gameEvents.Add(aGameEvent);
                    }
                }
            }

            gameContent = new GameContent(theGameID);
        }