// NOTE: Parameter format for date is YYYY-MM-DD public Schedule(string gameDate) { string gameDateScheduleURL = NHLAPIServiceURLs.todaysGames + "?date=" + gameDate; var json = DataAccessLayer.ExecuteAPICall(gameDateScheduleURL); // Populate the raw JSON feed to the scheduleJson property. scheduleJson = json; if (json.ContainsKey("totalItems")) { totalItems = json["totalItems"].ToString(); } else { totalItems = "0"; } if (json.ContainsKey("totalEvents")) { totalEvents = json["totalEvents"].ToString(); } else { totalEvents = "0"; } if (json.ContainsKey("totalGames")) { totalGames = json["totalGames"].ToString(); } else { totalGames = "0"; } if (json.ContainsKey("totalMatches")) { totalMatches = json["totalMatches"].ToString(); } else { totalMatches = "0"; } scheduleDate = gameDate; List <Game> scheduledGames = new List <Game>(); var scheduleArray = JArray.Parse(json.SelectToken("dates").ToString()); // If the day has schedule games, add them to the object. if (scheduleArray.Count > 0) { foreach (var game in scheduleArray[0]["games"]) { Game aGame = new Game(game["gamePk"].ToString()); scheduledGames.Add(aGame); if (season == "" || season == null) { season = aGame.season; } } games = scheduledGames; } }
// 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); }