public void ScoresDoubleHeader() { Dictionary <String, String> expectedParams = new Dictionary <string, string> { { "Command", "baseball" }, { "Parameter", "Score" }, { "Time", "DayOfWeek=today;" }, { "Team", "mockteam" }, }; Mock <IBaseballSensor> sensor = new Mock <IBaseballSensor>(MockBehavior.Strict); sensor.Setup(s => s.IsValid).Returns(true); sensor.Setup(s => s.Teams).Returns(_mockTeams); sensor.Setup(s => s.League).Returns("MLB"); BaseballGame game1 = CreateBaseballGame(GameState.Completed, 1, 3, true, new List <string>()); BaseballGame game2 = CreateBaseballGame(GameState.Started, 4, 3, true, new List <string>()); game2.InningNumber = 5; game2.TopOfInning = true; game2.NumberOnBase = 3; game2.NumberOfOuts = 2; sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame> { game1, game2 }); AddComponentToConfigurationManager(sensor.Object); CurrentConversation = new BaseballConversation(GetConfigurationManager(), new List <IOInterfaceReference>()); Assert.AreEqual("In the first game, The Mockers lost to the Fakers by a score of 3 to 1.. In the second game, The Mockers are beating the Fakers 4 to 3. with 3 on and 2 outs in the top of the fifth..", RunSingleConversation <BaseballConversation>(expectedParams)); sensor.Verify(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0]), Times.Exactly(1)); }
private void FreeMLBTVTest(GameState?gameState, String expectedResponse) { Dictionary <String, String> expectedParams = new Dictionary <string, string> { { "Command", "baseball" }, { "Parameter", "MLBTVFreeGame" }, { "Time", "DayOfWeek=today;" }, { "Team", "" }, }; Mock <IBaseballSensor> sensor = new Mock <IBaseballSensor>(MockBehavior.Strict); sensor.Setup(s => s.IsValid).Returns(true); sensor.Setup(s => s.Teams).Returns(_mockTeams); sensor.Setup(s => s.League).Returns("MLB"); if (gameState.HasValue) { BaseballGame game = CreateBaseballGame(gameState.Value, 5, 2, true, new List <String> { "MLB.tv" }); sensor.Setup(s => s.LoadAllScores(It.IsAny <DateTime>())).Returns(new List <BaseballGame> { game }); } else { sensor.Setup(s => s.LoadAllScores(It.IsAny <DateTime>())).Returns(new List <BaseballGame>()); } AddComponentToConfigurationManager(sensor.Object); CurrentConversation = new BaseballConversation(GetConfigurationManager(), new List <IOInterfaceReference>()); Assert.AreEqual(expectedResponse, RunSingleConversation <BaseballConversation>(expectedParams)); sensor.Verify(s => s.LoadAllScores(It.IsAny <DateTime>()), Times.Exactly(1)); }
private BaseballGame CreateBaseballGame(GameState gameState, int mockScore, int fakeScore, bool mockIsHome, List <String> channels) { BaseballGame game = new BaseballGame(); game.Channels = channels; game.State = gameState; game.HomeTeamScore = new BaseballTeamScore(); game.AwayTeamScore = new BaseballTeamScore(); if (mockIsHome) { game.HomeTeamScore.Team = _mockTeams[0]; game.HomeTeamScore.Score = mockScore; game.AwayTeamScore.Team = _mockTeams[1]; game.AwayTeamScore.Score = fakeScore; } else { game.AwayTeamScore.Team = _mockTeams[0]; game.AwayTeamScore.Score = mockScore; game.HomeTeamScore.Team = _mockTeams[1]; game.HomeTeamScore.Score = fakeScore; } if (game.HomeTeamScore.Score > game.AwayTeamScore.Score) { game.WinningTeamScore = game.HomeTeamScore; game.LosingTeamScore = game.AwayTeamScore; } else { game.WinningTeamScore = game.AwayTeamScore; game.LosingTeamScore = game.HomeTeamScore; } game.StartingTime = new DateTime(2014, 12, 19, 8, 0, 0); return(game); }
public void WhichChannelOneGameFinishedOneInProgress() { Dictionary <String, String> expectedParams = new Dictionary <string, string> { { "Command", "baseball" }, { "Parameter", "TVChannel" }, { "Time", "DayOfWeek=today;" }, { "Team", "mockteam" }, }; Mock <IBaseballSensor> sensor = new Mock <IBaseballSensor>(MockBehavior.Strict); sensor.Setup(s => s.IsValid).Returns(true); sensor.Setup(s => s.Teams).Returns(_mockTeams); sensor.Setup(s => s.League).Returns("MLB"); BaseballGame completedGame = CreateBaseballGame(GameState.Completed, 5, 2, true, new List <String> { "ESPN" }); BaseballGame inProgressGame = CreateBaseballGame(GameState.Started, 5, 2, true, new List <String> { "ESPN" }); sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame> { completedGame, inProgressGame }); AddComponentToConfigurationManager(sensor.Object); CurrentConversation = new BaseballConversation(GetConfigurationManager(), new List <IOInterfaceReference>()); Assert.AreEqual("The Mockers game is available on ESPN.", RunSingleConversation <BaseballConversation>(expectedParams)); sensor.Verify(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0]), Times.Exactly(1)); }
private String RespondToMLBTVGame(IBaseballSensor sensor) { BaseballGame game = sensor.LoadAllScores(DateTime.Now).SingleOrDefault(g => g.Channels.Any(c => c.Equals("MLB.tv"))); if (game != null) { switch (game.State) { case GameState.NoGame: return(String.Format("Error, something went wrong!")); case GameState.GameHasntStarted: return(String.Format("The {0} at {1} game at {2} today is free on MLB.TV.", game.AwayTeamScore.Team.Name, game.HomeTeamScore.Team.Name, game.StartingTime.ToShortTimeString())); case GameState.RainDelay: return(String.Format("The {0} at {1} game, which is currently under a rain delay, is free on MLB.TV.", game.AwayTeamScore.Team.Name, game.HomeTeamScore.Team.Name)); case GameState.Started: return(String.Format("The {0} at {1} game playing currently is free on MLB.TV.", game.AwayTeamScore.Team.Name, game.HomeTeamScore.Team.Name)); case GameState.Completed: return("The free game today has already completed."); } } return("There are no free games on MLB.TV available today!"); }
public void BaseballGameTestMethod() { BaseballGame baseballGame = new BaseballGame(); Assert.AreEqual(30, baseballGame.CalPoints(new string[] { "5", "2", "C", "D", "+" })); Assert.AreEqual(5, baseballGame.CalPoints(new string[] { "5" })); Assert.AreEqual(7, baseballGame.CalPoints(new string[] { "5", "2" })); }
private void TurnToChannelTest(GameState?gameState, bool isOnTV, bool includesRemotes, String expectedResponse) { Dictionary <String, String> expectedParams = new Dictionary <string, string> { { "Command", "baseball" }, { "Parameter", "TurnToGame" }, { "Time", "DayOfWeek=today;" }, { "Team", "mockteam" }, }; Mock <IBaseballSensor> sensor = new Mock <IBaseballSensor>(MockBehavior.Strict); sensor.Setup(s => s.IsValid).Returns(true); sensor.Setup(s => s.Teams).Returns(_mockTeams); sensor.Setup(s => s.League).Returns("MLB"); if (gameState.HasValue) { List <String> channels = new List <string>(); if (isOnTV) { channels.Add("ESPN"); } BaseballGame game = CreateBaseballGame(gameState.Value, 5, 2, true, channels); sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame> { game }); } else { sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame>()); } AddComponentToConfigurationManager(sensor.Object); List <IOInterfaceReference> references = new List <IOInterfaceReference>(); Mock <ITVRemote> remote = new Mock <ITVRemote>(MockBehavior.Strict); Semaphore channelTurn = new Semaphore(0, 1); if (includesRemotes) { remote.Setup(s => s.Name).Returns("Mock Name"); remote.Setup(s => s.GetChannels()).Returns(new List <String> { "ESPN" }); remote.Setup(s => s.SendChannel("ESPN")).Callback(() => channelTurn.Release()); AddComponentToConfigurationManager(remote.Object); IOInterfaceReference remoteRef = new IOInterfaceReference(); remoteRef.Properties.Single(s => s.Name.Equals("References")).Setter("Mock Name"); references.Add(remoteRef); } CurrentConversation = new BaseballConversation(GetConfigurationManager(), references); Assert.AreEqual(expectedResponse, RunSingleConversation <BaseballConversation>(expectedParams)); sensor.Verify(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0]), Times.Exactly(1)); if (includesRemotes) { channelTurn.WaitOne(); remote.Verify(s => s.SendChannel("ESPN"), Times.Exactly(1)); } }
private String RespondToTVChannel(IBaseballSensor sensor, BaseballTeam team, DateTime time) { BaseballGame lastGame = null; foreach (BaseballGame game in sensor.LoadScoresForTeam(time, team)) { List <String> possibleChannels = GetChannelsGameIsOn(sensor, game); lastGame = game; switch (game.State) { case GameState.NoGame: return(String.Format("The {0} are not playing today.", team.Name)); case GameState.GameHasntStarted: if (!possibleChannels.Any()) { return(String.Format("The {0} game will not be available on national TV.", team.Name)); } else { return(String.Format("The {0} game will be available on {1} at {2}", team.Name, SayList(possibleChannels), game.StartingTime.ToShortTimeString())); } case GameState.RainDelay: if (!possibleChannels.Any()) { return(String.Format("The {0} game is not available on national TV.", team.Name)); } else { return(String.Format("The {0} game is currently in a rain delay, but is available on {1}", team.Name, SayList(possibleChannels))); } case GameState.Started: if (!possibleChannels.Any()) { return(String.Format("The {0} game is not available on national TV.", team.Name)); } else { return(String.Format("The {0} game is available on {1}.", team.Name, SayList(possibleChannels))); } } } if (lastGame != null) { if (lastGame.State == GameState.Postponed) { return(RespondForOneGame(lastGame, team)); } return(String.Format("The game has already completed. {0}", RespondForOneGame(lastGame, team))); } return(String.Format("The {0} are not playing today.", team.Name)); }
private List <String> GetChannelsGameIsOn(IBaseballSensor sensor, BaseballGame game) { List <String> possibleChannels = new List <string>(); foreach (String channel in game.Channels) { switch (channel) { case "ESPN": possibleChannels.Add("ESPN"); break; case "ESPN2": possibleChannels.Add("ESPN2"); break; case "FOX": possibleChannels.Add("Fox"); break; case "WGN": possibleChannels.Add("WGN"); break; case "MLBN": possibleChannels.Add("The MLB Network"); break; case "TBS": possibleChannels.Add("TBS"); break; default: break; } } return(possibleChannels.Distinct().ToList()); }
private void WhatChannelTest(GameState?gameState, bool isOnTV, String expectedResponse) { Dictionary <String, String> expectedParams = new Dictionary <string, string> { { "Command", "baseball" }, { "Parameter", "TVChannel" }, { "Time", "DayOfWeek=today;" }, { "Team", "mockteam" }, }; Mock <IBaseballSensor> sensor = new Mock <IBaseballSensor>(MockBehavior.Strict); sensor.Setup(s => s.IsValid).Returns(true); sensor.Setup(s => s.Teams).Returns(_mockTeams); sensor.Setup(s => s.League).Returns("MLB"); if (gameState.HasValue) { List <String> channels = new List <string>(); if (isOnTV) { channels.Add("ESPN"); } BaseballGame game = CreateBaseballGame(gameState.Value, 5, 2, true, channels); sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame> { game }); } else { sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame>()); } AddComponentToConfigurationManager(sensor.Object); CurrentConversation = new BaseballConversation(GetConfigurationManager(), new List <IOInterfaceReference>()); Assert.AreEqual(expectedResponse, RunSingleConversation <BaseballConversation>(expectedParams)); sensor.Verify(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0]), Times.Exactly(1)); }
private void SingleScoreTest(GameState?gameState, int mockScore, int fakeScore, bool isMockHome, bool isTopOfInning, String expectedResponse) { Dictionary <String, String> expectedParams = new Dictionary <string, string> { { "Command", "baseball" }, { "Parameter", "Score" }, { "Time", "DayOfWeek=today;" }, { "Team", "mockteam" }, }; Mock <IBaseballSensor> sensor = new Mock <IBaseballSensor>(MockBehavior.Strict); sensor.Setup(s => s.IsValid).Returns(true); sensor.Setup(s => s.Teams).Returns(_mockTeams); sensor.Setup(s => s.League).Returns("MLB"); if (gameState.HasValue) { BaseballGame game = CreateBaseballGame(gameState.Value, mockScore, fakeScore, isMockHome, new List <string>()); game.InningNumber = 5; game.TopOfInning = isTopOfInning; game.NumberOnBase = 3; game.NumberOfOuts = 2; sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame> { game }); } else { sensor.Setup(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0])).Returns(new List <BaseballGame>()); } AddComponentToConfigurationManager(sensor.Object); CurrentConversation = new BaseballConversation(GetConfigurationManager(), new List <IOInterfaceReference>()); Assert.AreEqual(expectedResponse, RunSingleConversation <BaseballConversation>(expectedParams)); sensor.Verify(s => s.LoadScoresForTeam(It.IsAny <DateTime>(), _mockTeams[0]), Times.Exactly(1)); }
public IEnumerable <BaseballGame> LoadAllScores(DateTime date) { XmlDocument scoreboardDoc = new XmlDocument(); scoreboardDoc.Load(GetScoreboardFile(date)); XmlDocument masterScoreboardDoc = new XmlDocument(); masterScoreboardDoc.Load(GetMasterScoreboardFile(date)); XmlDocument epgDoc = new XmlDocument(); epgDoc.Load(GetEpgFile(date)); foreach (XmlElement gameXml in scoreboardDoc.SelectSingleNode("scoreboard").ChildNodes) { String id = gameXml.SelectSingleNode("game").Attributes["id"].Value; String masterID = ConvertIdToMaster(id); XmlElement masterGameXml = masterScoreboardDoc.SelectSingleNode(String.Format("games/game[@id='{0}']", masterID)) as XmlElement; XmlElement epgGameXml = epgDoc.SelectSingleNode(String.Format("epg/game[@id='{0}']", masterID)) as XmlElement; BaseballGame game = new BaseballGame(); // Postponed games will not have the start time labelled on it. if (epgGameXml.HasAttribute("start") && !String.IsNullOrEmpty(epgGameXml.Attributes["start"].Value)) { game.StartingTime = DateTime.Parse(epgGameXml.Attributes["start"].Value).ToLocalTime(); } BaseballTeamScore score1 = new BaseballTeamScore(); XmlElement teamScore = gameXml.SelectNodes("team")[0] as XmlElement; score1.Team = Teams.Single(t => t.Key.Equals(teamScore.Attributes["name"].Value)); score1.Score = Int32.Parse(teamScore.SelectSingleNode("gameteam").Attributes["R"].Value); BaseballTeamScore score2 = new BaseballTeamScore(); teamScore = gameXml.SelectNodes("team")[1] as XmlElement; score2.Team = Teams.Single(t => t.Key.Equals(teamScore.Attributes["name"].Value)); score2.Score = Int32.Parse(teamScore.SelectSingleNode("gameteam").Attributes["R"].Value); if (score1.Score > score2.Score) { game.WinningTeamScore = score1; game.LosingTeamScore = score2; } else { game.WinningTeamScore = score2; game.LosingTeamScore = score1; } if (score1.Team.Equals(masterGameXml.Attributes["away_team_name"].Value)) { game.AwayTeamScore = score1; game.HomeTeamScore = score2; } else { game.HomeTeamScore = score1; game.AwayTeamScore = score2; } List <String> channels = new List <String>(); if (masterGameXml.SelectNodes("broadcast/home").Count > 0) { channels.AddRange(GetChannels(masterGameXml.SelectSingleNode("broadcast/home") as XmlElement)); } if (masterGameXml.SelectNodes("broadcast/away").Count > 0) { channels.AddRange(GetChannels(masterGameXml.SelectSingleNode("broadcast/away") as XmlElement)); } if (epgGameXml.SelectNodes("game_media/media").Count > 0 && epgGameXml.SelectSingleNode("game_media/media").Attributes["free"].Value.Equals("ALL")) { channels.Add("MLB.tv"); } game.Channels = channels; String state = masterGameXml.SelectSingleNode("status").Attributes["status"].Value; if (state.Equals("Final") || state.Equals("Game Over")) { game.State = GameState.Completed; } else if (state.Equals("Preview")) { game.State = GameState.GameHasntStarted; } else if (state.Equals("In Progress")) { game.NumberOfOuts = Int32.Parse(gameXml.Attributes["outs"].Value); game.NumberOnBase = gameXml.SelectNodes("on_base").Count; game.InningNumber = Int32.Parse(gameXml.SelectSingleNode("inningnum").Attributes["inning"].Value); game.TopOfInning = gameXml.SelectSingleNode("inningnum").Attributes["half"].Equals("T"); game.State = GameState.Started; } else if (state.Equals("Postponed")) { game.State = GameState.Postponed; } else { game.State = GameState.RainDelay; } yield return(game); } }
private String RespondToTurnToGame(IBaseballSensor sensor, BaseballTeam team, DateTime time) { BaseballGame lastGame = null; foreach (BaseballGame game in sensor.LoadScoresForTeam(time, team)) { List <String> possibleChannels = GetChannelsGameIsOn(sensor, game); lastGame = game; switch (game.State) { case GameState.NoGame: return(String.Format("The {0} are not playing today.", team.Name)); case GameState.GameHasntStarted: if (possibleChannels.Any()) { return(String.Format("The {0} will be playing at {1}.", team.Name, game.StartingTime.ToShortTimeString())); } else { return(String.Format("The {0} game will not be available on national TV.", team.Name)); } case GameState.RainDelay: if (possibleChannels.Any()) { return(String.Format("The {0} game is currently in a rain delay, but is available on ESPN.", team.Name)); } else { return(String.Format("The {0} game is not available on national TV.", team.Name)); } case GameState.Started: if (possibleChannels.Any() && _remotes.Any()) { IEnumerable <String> possibleRemoteNames = _remotes.Select(r => r.Name); IEnumerable <ITVRemote> remotes = ConfigManager.FindAllComponentsOfType <ITVRemote>(); foreach (ITVRemote remote in remotes) { if (possibleRemoteNames.Contains(remote.Name) && possibleChannels.Any(c => remote.GetChannels().Contains(c))) { Task.Run(() => remote.SendChannel(possibleChannels.First(c => remote.GetChannels().Contains(c)))); } } return("OK"); } else if (possibleChannels.Any()) { return(String.Format("There is no T V remote connected to me for me to use. The {0} game is on {1}.", team.Name, SayList(possibleChannels))); } else { return(String.Format("The {0} game is not on a national broadcast!", team.Name)); } case GameState.Postponed: case GameState.Completed: break; } } if (lastGame != null) { if (lastGame.State == GameState.Postponed) { return(RespondForOneGame(lastGame, team)); } return(String.Format("The game has already completed. {0}", RespondForOneGame(lastGame, team))); } return(String.Format("The {0} are not playing today.", team.Name)); }
private String RespondForOneGame(BaseballGame score, BaseballTeam team) { String startingPhrase = String.Empty; switch (score.State) { case GameState.Started: if (score.WinningTeamScore.Score == score.LosingTeamScore.Score) { if (score.WinningTeamScore.Team.Equals(team)) { startingPhrase = String.Format("The {0} and the {1} are tied at {2} a piece. ", team.Name, score.LosingTeamScore.Team.Name, score.WinningTeamScore.Score); } else { startingPhrase = String.Format("The {0} and the {1} are tied at {2} a piece. ", team.Name, score.WinningTeamScore.Team.Name, score.WinningTeamScore.Score); } } else if (score.WinningTeamScore.Team.Equals(team)) { startingPhrase = String.Format("The {0} are beating the {1} {2} to {3}. ", team.Name, score.LosingTeamScore.Team.Name, score.WinningTeamScore.Score, score.LosingTeamScore.Score); } else { startingPhrase = String.Format("The {0} are losing to the {1} by a score of {2} to {3}. ", team.Name, score.WinningTeamScore.Team.Name, score.WinningTeamScore.Score, score.LosingTeamScore.Score); } String inningNum = SayOrdinal(score.InningNumber); if (score.TopOfInning) { startingPhrase += String.Format("with {2} on and {0} outs in the top of the {1}.", score.NumberOfOuts, inningNum, score.NumberOnBase); } else { startingPhrase += String.Format("with {2} on and {0} outs in the bottom of the {1}.", score.NumberOfOuts, inningNum, score.NumberOnBase); } break; case GameState.RainDelay: startingPhrase = String.Format("The {0} game is currently in a rain delay.", team.Name); break; case GameState.Postponed: startingPhrase = String.Format("The {0} game has been postponed.", team.Name); break; case GameState.NoGame: startingPhrase = String.Format("The {0} did not have a game that day.", team.Name); break; case GameState.GameHasntStarted: startingPhrase = String.Format("The {0} will be playing at {1}.", team.Name, score.StartingTime.ToShortTimeString()); break; case GameState.Completed: if (score.WinningTeamScore.Team.Equals(team)) { startingPhrase = String.Format("The {0} beat the {1} by a score of {2} to {3}.", team.Name, score.LosingTeamScore.Team.Name, score.WinningTeamScore.Score, score.LosingTeamScore.Score); } else { startingPhrase = String.Format("The {0} lost to the {1} by a score of {2} to {3}.", team.Name, score.WinningTeamScore.Team.Name, score.WinningTeamScore.Score, score.LosingTeamScore.Score); } break; } return(startingPhrase); }