public NHLGame(int _gameId) { guid = System.Guid.NewGuid(); gameId = _gameId; homeScore = 0; homeShots = 0; awayScore = 0; awayShots = 0; period = String.Empty; //intermission = false; final = false; time = String.Empty; shootout = false; notStarted = true; overtime = false; mutex = new Object(); thread = new Thread(new ParameterizedThreadStart(LoopThread)); lastGoal = null; originalLastGoal = null; newEvents = new Queue <NHLGameEvent>(); oldEvents = new List <NHLGameEvent>(); nhlGameStats = null; nhlSeasonStats = null; goalDebug = false; }
private void HandleGameEvent(NHLGameEvent gameEvent) { if (gameEvent != null && gameEvent.Type == NHLGameEvent.EventType.EVENT_GOAL) { if (!EventExists(gameEvent)) { System.Console.WriteLine("Goal: {0}", gameEvent.EventText); } if (originalLastGoal == null) { if (!EventExists(gameEvent)) { if (!gameEvent.ScoreChanged) { if (gameEvent.TeamThatScored.CompareTo(HomeTeamCityShort) == 0) { gameEvent.HomeScore++; } else if (gameEvent.TeamThatScored.CompareTo(AwayTeamCityShort) == 0) { gameEvent.AwayScore++; } else { Console.WriteLine("Couldn't find team that scored."); } } AddEvent(gameEvent); originalLastGoal = gameEvent; } } else { if (gameEvent.ScoreChanged) { System.Console.WriteLine("DISALLOWED GOAL?"); } lastGoal = gameEvent; lastGoal.Updated = true; } } else if (gameEvent != null) { bool exists = EventExists(gameEvent); if (exists) { gameEvent.Updated = true; } else { AddEvent(gameEvent); } } }
public NHLGameEvent getAndClearLastGoalEvent() { NHLGameEvent evt = null; lock (mutex) { evt = lastGoalEvent; lastGoalEvent = null; } return evt; }
public void AddEvent(NHLGameEvent newEvent) { lock (mutex) { if (newEvent.Type == NHLGameEvent.EventType.EVENT_GOAL || newEvent.Type == NHLGameEvent.EventType.EVENT_DEBUG) { System.Console.WriteLine("Adding goal event (" + guid + ")"); goalDebug = true; } newEvents.Enqueue(newEvent); } }
//const string playerListPath = "http://debian3800/otto/nhl/jacked/playerlist.xml"; public NHLGameStats(NHLStats _nhlStats, int _gameId, string _homeTeamName, string _awayTeamName, bool _playoffs) { stats = new JackedStatsSet(); thread = new Thread(new ParameterizedThreadStart(LoopThread)); gameId = _gameId; monitorStats = true; mutex = new Object(); nhlStats = _nhlStats; homeTeamName = _homeTeamName; awayTeamName = _awayTeamName; playByPlay = new XmlDocument(); playoffs = _playoffs; lastGoalEvent = null; thread.Start(this); }
void ParseNHLDotComTimeString(string s) { string[] elements = s.Split('|'); if (elements.Length > 1) { if (elements[1].Length > 0) { if (elements[1].CompareTo(time) != 0) { if (lastGoal != null && lastGoal.EventText.CompareTo(originalLastGoal.EventText) != 0) { AddEvent(lastGoal); } lastGoal = null; originalLastGoal = null; } //time = elements[1]; if (elements[1].Contains("PM") || elements[1].Contains("AM")) { notStarted = true; } else { notStarted = false; time = elements[1]; } } if (elements[2].Length > 0) { period = elements[2]; } if (time.CompareTo("Final") == 0) { final = true; } if (!final && String.IsNullOrEmpty(time)) { //Console.WriteLine("Time string from NHL.com is not correct, got NULL value for game (home team city or id): " + (HomeTeamCityShort == null ? Convert.ToString(gameId) : HomeTeamCityShort)); //final = true; } } }
public NHLGameEvent GetNewEvent() { NHLGameEvent gameEvent = null; lock (mutex) { if (newEvents.Count > 0) { gameEvent = newEvents.Dequeue(); oldEvents.Add(gameEvent); goalDebug = false; } else if (goalDebug) { System.Console.WriteLine("GOAL DEBUGFFFFFFF"); goalDebug = false; } } return(gameEvent); }
public bool EventExists(NHLGameEvent newEvent) { bool exists = false; lock (mutex) { foreach (NHLGameEvent gameEvent in newEvents) { if ((newEvent.Type == gameEvent.Type) && (gameEvent.Type == NHLGameEvent.EventType.EVENT_GOAL)) { if (newEvent.AwayScore == gameEvent.AwayScore && newEvent.HomeScore == gameEvent.HomeScore) { exists = true; } } else if (newEvent.EventText.CompareTo(gameEvent.EventText) == 0) { exists = true; } } foreach (NHLGameEvent gameEvent in oldEvents) { if ((newEvent.TimeText == gameEvent.TimeText && newEvent.Type == gameEvent.Type) || (newEvent.Type == gameEvent.Type) && (gameEvent.Type == NHLGameEvent.EventType.EVENT_GOAL) && newEvent.AwayScore == gameEvent.AwayScore && newEvent.HomeScore == gameEvent.HomeScore) { exists = true; } else if (newEvent.EventText.CompareTo(gameEvent.EventText) == 0) { exists = true; } } } return(exists); }
void ParseNHLDotComTimeString(string s) { string[] elements = s.Split('|'); if (elements.Length > 1) { if (elements[1].Length > 0) { if (elements[1].CompareTo(time) != 0) { if (lastGoal != null && lastGoal.EventText.CompareTo(originalLastGoal.EventText) != 0) AddEvent(lastGoal); lastGoal = null; originalLastGoal = null; } //time = elements[1]; if (elements[1].Contains("PM") || elements[1].Contains("AM")) notStarted = true; else { notStarted = false; time = elements[1]; } } if (elements[2].Length > 0) period = elements[2]; if (time.CompareTo("Final") == 0) final = true; } }
NHLGameEvent ParseNHLDotComScoreString(string score) { NHLGameEvent newEvent = null; String eventText, timeText; NHLGameEvent.EventType type = NHLGameEvent.EventType.EVENT_OTHER; string []elements = score.Split('|'); int newHomeScore = -1, newAwayScore = -1; bool scoreChanged = false; if (elements.Length >= 22) { awayTeamName = FixCase(elements[5]); awayTeamCityShort = elements[3]; awayTeamCity = FixCase(elements[4]); if (elements[7].Length > 0) { newAwayScore = Convert.ToInt32(elements[7]); if (newAwayScore != awayScore) { scoreChanged = true; awayScore = newAwayScore; } } if (elements[6].Length > 0) awayShots = Convert.ToInt32(elements[6]); homeTeamName = FixCase(elements[15]); homeTeamCityShort = elements[13]; homeTeamCity = FixCase(elements[14]); if (elements[17].Length > 0) { newHomeScore = Convert.ToInt32(elements[17]); if (newHomeScore != homeScore) { scoreChanged = true; homeScore = newHomeScore; } } if (elements[16].Length > 0) homeShots = Convert.ToInt32(elements[16]); if (elements[1].CompareTo("SO") == 0) { shootout = true; if (elements[21].Length > 0 && elements[11].Length > 0) { int homeSOgoals = Convert.ToInt32(elements[21].Substring(0, 1)); int awaySOgoals = Convert.ToInt32(elements[11].Substring(0, 1)); /* Handle inconsistencey with nhl.com */ if (homeScore == awayScore && final) { if (homeSOgoals > awaySOgoals) homeScore++; else if (awaySOgoals > homeSOgoals) awayScore++; } } } else if (elements[21].Length > 0 && elements[11].Length > 0) { //Then it must have been/is only OT overtime = true; } if (elements.Length >= 23) { eventText = elements[22]; int s, e; s = eventText.IndexOf('('); e = eventText.IndexOf(')'); if (s != -1 && e != -1) { timeText = eventText.Substring(s).Substring(1, e - s - 1); if (eventText.StartsWith("Goal:") || eventText.StartsWith("Empty Net Goal")) type = NHLGameEvent.EventType.EVENT_GOAL; else if (eventText.StartsWith("Penalty:")) type = NHLGameEvent.EventType.EVENT_PENALTY; newEvent = new NHLGameEvent(eventText, type, timeText); newEvent.HomeScore = homeScore; newEvent.AwayScore = awayScore; newEvent.ScoreChanged = scoreChanged; } } } return newEvent; }
public void AddEvent(NHLGameEvent newEvent) { lock (mutex) { if (newEvent.Type == NHLGameEvent.EventType.EVENT_GOAL || newEvent.Type == NHLGameEvent.EventType.EVENT_DEBUG) { System.Console.WriteLine("Adding goal event (" + guid + ")"); goalDebug = true; } newEvents.Enqueue(newEvent); } }
public bool EventExists(NHLGameEvent newEvent) { bool exists = false; lock (mutex) { foreach (NHLGameEvent gameEvent in newEvents) { if ((newEvent.Type == gameEvent.Type) && (gameEvent.Type == NHLGameEvent.EventType.EVENT_GOAL)) { if (newEvent.AwayScore == gameEvent.AwayScore && newEvent.HomeScore == gameEvent.HomeScore) exists = true; } else if (newEvent.EventText.CompareTo(gameEvent.EventText) == 0) exists = true; } foreach (NHLGameEvent gameEvent in oldEvents) { if ((newEvent.TimeText == gameEvent.TimeText && newEvent.Type == gameEvent.Type) || (newEvent.Type == gameEvent.Type) && (gameEvent.Type == NHLGameEvent.EventType.EVENT_GOAL) && newEvent.AwayScore == gameEvent.AwayScore && newEvent.HomeScore == gameEvent.HomeScore) { exists = true; } else if (newEvent.EventText.CompareTo(gameEvent.EventText) == 0) exists = true; } } return exists; }
public string GetScoreString(NHLGameEvent gameEvent, bool showTime) { return GetScoreString(gameEvent.HomeScore, gameEvent.AwayScore, showTime); }
public NHLGame(int _gameId) { guid = System.Guid.NewGuid(); gameId = _gameId; homeScore = 0; homeShots = 0; awayScore = 0; awayShots = 0; period = String.Empty; //intermission = false; final = false; time = String.Empty; shootout = false; notStarted = true; overtime = false; mutex = new Object(); thread = new Thread(new ParameterizedThreadStart(LoopThread)); lastGoal = null; originalLastGoal = null; newEvents = new Queue<NHLGameEvent>(); oldEvents = new List<NHLGameEvent>(); nhlGameStats = null; nhlSeasonStats = null; goalDebug = false; }
static void LoopThread(object _game) { NHLGame game = (NHLGame)_game; String timeString, scoreString; bool wasOver; game.nhlFetch = new NHLDotComFetch(); NHLGameEvent gameEvent; for (; ;) { timeString = game.nhlFetch.FetchGameTimeString(game.GameId); scoreString = game.nhlFetch.FetchGameScoreString(game.GameId); int sleepTime = SLEEPY_UPDATE_DELAY; lock (game.mutex) { if (game.stopMonitoringStatsDelay > 0) { game.stopMonitoringStatsDelay--; if (game.stopMonitoringStatsDelay == 0) { System.Console.WriteLine("Stopped monitoring stats for: " + game.HomeTeamName + "/" + game.AwayTeamName); game.nhlGameStats.StopMonitoring(); break; } } else if (scoreString != null && timeString != null) { wasOver = game.final; game.ParseNHLDotComTimeString(timeString); gameEvent = game.ParseNHLDotComScoreString(scoreString); if (game.nhlGameStats == null) { game.nhlGameStats = new NHLGameStats(game.NhlSeasonStats, game.GameId, game.HomeTeamName, game.AwayTeamName, false); } if (game.final && !wasOver) { game.ParseNHLDotComScoreString(scoreString); gameEvent = new NHLGameEvent("Game Over.", NHLGameEvent.EventType.EVENT_GAMEOVER, String.Empty); game.AddEvent(gameEvent); System.Console.WriteLine("Game over: " + game.HomeTeamName + "/" + game.AwayTeamName); game.stopMonitoringStatsDelay = STOP_MONITORING_STATS_DELAY; } else { game.HandleGameEvent(gameEvent); } } NHLGameStatus status = game.GetStatus(); if (status == NHLGameStatus.GAME_NODATA || status == NHLGameStatus.GAME_PREVIEWDATA) { sleepTime = SLEEPY_UPDATE_DELAY; } else { sleepTime = GAMETIME_UPDATE_DELAY; } } if (game.nhlGameStats != null) { // an attempt to get quicker goal events NHLGameEvent lastGoalEventFromStats = game.nhlGameStats.getAndClearLastGoalEvent(); if (lastGoalEventFromStats != null) { game.HandleGameEvent(lastGoalEventFromStats); } } Thread.Sleep(sleepTime); if (!game.IsRunning()) { if (game.nhlGameStats != null) { game.nhlGameStats.StopMonitoring(); } break; } } }
private void HandleGameEvent(NHLGameEvent gameEvent) { if (gameEvent != null && gameEvent.Type == NHLGameEvent.EventType.EVENT_GOAL) { if (!EventExists(gameEvent)) System.Console.WriteLine("Goal: {0}", gameEvent.EventText); if (originalLastGoal == null) { if (!EventExists(gameEvent)) { if (!gameEvent.ScoreChanged) { if (gameEvent.TeamThatScored.CompareTo(HomeTeamCityShort) == 0) gameEvent.HomeScore++; else if (gameEvent.TeamThatScored.CompareTo(AwayTeamCityShort) == 0) gameEvent.AwayScore++; else Console.WriteLine("Couldn't find team that scored."); } AddEvent(gameEvent); originalLastGoal = gameEvent; } } else { if (gameEvent.ScoreChanged) System.Console.WriteLine("DISALLOWED GOAL?"); lastGoal = gameEvent; lastGoal.Updated = true; } } else if (gameEvent != null) { bool exists = EventExists(gameEvent); if (exists) gameEvent.Updated = true; else AddEvent(gameEvent); } }
NHLGameEvent ParseNHLDotComScoreString(string score) { NHLGameEvent newEvent = null; String eventText, timeText; NHLGameEvent.EventType type = NHLGameEvent.EventType.EVENT_OTHER; string [] elements = score.Split('|'); int newHomeScore = -1, newAwayScore = -1; bool scoreChanged = false; if (elements.Length >= 22) { awayTeamName = FixCase(elements[5]); awayTeamCityShort = elements[3]; awayTeamCity = FixCase(elements[4]); if (elements[7].Length > 0) { newAwayScore = Convert.ToInt32(elements[7]); if (newAwayScore != awayScore) { scoreChanged = true; awayScore = newAwayScore; } } if (elements[6].Length > 0) { awayShots = Convert.ToInt32(elements[6]); } homeTeamName = FixCase(elements[15]); homeTeamCityShort = elements[13]; homeTeamCity = FixCase(elements[14]); if (elements[17].Length > 0) { newHomeScore = Convert.ToInt32(elements[17]); if (newHomeScore != homeScore) { scoreChanged = true; homeScore = newHomeScore; } } if (elements[16].Length > 0) { homeShots = Convert.ToInt32(elements[16]); } if (elements[1].CompareTo("SO") == 0) { shootout = true; if (elements[21].Length > 0 && elements[11].Length > 0) { int homeSOgoals = Convert.ToInt32(elements[21].Substring(0, 1)); int awaySOgoals = Convert.ToInt32(elements[11].Substring(0, 1)); /* Handle inconsistencey with nhl.com */ if (homeScore == awayScore && final) { if (homeSOgoals > awaySOgoals) { homeScore++; } else if (awaySOgoals > homeSOgoals) { awayScore++; } } } } else if (elements[21].Length > 0 && elements[11].Length > 0) { //Then it must have been/is only OT overtime = true; } if (elements.Length >= 23) { eventText = elements[22]; int s, e; s = eventText.IndexOf('('); e = eventText.IndexOf(')'); if (s != -1 && e != -1) { timeText = eventText.Substring(s).Substring(1, e - s - 1); if (eventText.StartsWith("Goal:") || eventText.StartsWith("Empty Net Goal")) { type = NHLGameEvent.EventType.EVENT_GOAL; } else if (eventText.StartsWith("Penalty:")) { type = NHLGameEvent.EventType.EVENT_PENALTY; } newEvent = new NHLGameEvent(eventText, type, timeText); newEvent.HomeScore = homeScore; newEvent.AwayScore = awayScore; newEvent.ScoreChanged = scoreChanged; } } } return(newEvent); }
public string GetScoreString(NHLGameEvent gameEvent, bool showTime) { return(GetScoreString(gameEvent.HomeScore, gameEvent.AwayScore, showTime)); }
static void LoopThread(object _game) { NHLGame game = (NHLGame)_game; String timeString, scoreString; bool wasOver; game.nhlFetch = new NHLDotComFetch(); NHLGameEvent gameEvent; for (; ; ) { timeString = game.nhlFetch.FetchGameTimeString(game.GameId); scoreString = game.nhlFetch.FetchGameScoreString(game.GameId); int sleepTime = SLEEPY_UPDATE_DELAY; lock (game.mutex) { if (game.stopMonitoringStatsDelay > 0) { game.stopMonitoringStatsDelay--; if (game.stopMonitoringStatsDelay == 0) { System.Console.WriteLine("Stopped monitorng stats for: " + game.HomeTeamName + "/" + game.AwayTeamName); game.nhlGameStats.StopMonitoring(); break; } } else if (scoreString != null && timeString != null) { wasOver = game.final; game.ParseNHLDotComTimeString(timeString); gameEvent = game.ParseNHLDotComScoreString(scoreString); if (game.nhlGameStats == null) game.nhlGameStats = new NHLGameStats(game.NhlSeasonStats, game.GameId, game.HomeTeamName, game.AwayTeamName, false); if (game.final && !wasOver) { game.ParseNHLDotComScoreString(scoreString); gameEvent = new NHLGameEvent("Game Over.", NHLGameEvent.EventType.EVENT_GAMEOVER, String.Empty); game.AddEvent(gameEvent); System.Console.WriteLine("Game over: " + game.HomeTeamName + "/" + game.AwayTeamName); game.stopMonitoringStatsDelay = STOP_MONITORING_STATS_DELAY; } else { game.HandleGameEvent(gameEvent); } } NHLGameStatus status = game.GetStatus(); if (status == NHLGameStatus.GAME_NODATA || status == NHLGameStatus.GAME_PREVIEWDATA) sleepTime = SLEEPY_UPDATE_DELAY; else sleepTime = GAMETIME_UPDATE_DELAY; } if (game.nhlGameStats != null) { // an attempt to get quicker goal events NHLGameEvent lastGoalEventFromStats = game.nhlGameStats.getAndClearLastGoalEvent(); if (lastGoalEventFromStats != null) { game.HandleGameEvent(lastGoalEventFromStats); } } Thread.Sleep(sleepTime); if (!game.IsRunning()) { if (game.nhlGameStats != null) game.nhlGameStats.StopMonitoring(); break; } } }
public void CheckLastPlayByPlayEventForGoal() { lock (mutex) { if (lastGoalEvent != null) return; //{"eventid":215,"period":1,"type":"Goal","localtime":"8:41 PM","time":"18:40","teamid":22,"video": //"2_60_mtl_edm_0910_discrete_EDM215_goal_800K_16x9.flv","pid":8467964,"ycoord":5,"xcoord":56,"playername": //"Mike Comrie","sweater":"91","desc":"Mike Comrie GOAL on Carey Price"} XmlNodeList nodes = playByPlay.SelectNodes("//data/game/plays/play"); if (nodes.Count > 0) { XmlNode node = nodes[nodes.Count - 1]; string type = node.SelectNodes("type")[0].InnerText; string name = node.SelectNodes("playername")[0].InnerText; string description = node.SelectNodes("desc")[0].InnerText; string time = node.SelectNodes("time")[0].InnerText; string period = node.SelectNodes("period")[0].InnerText; string teamId = node.SelectNodes("teamid")[0].InnerText; if (string.Compare(type, "Goal", true) == 0) { string teamShort; string firstInitial, lastName; string periodWithSuffix; NHLTeam team = NHLStats.Instance.GetTeamFromTeamId(teamId); if (team != null) { teamShort = team.TeamId; firstInitial = name.Substring(0, 1); lastName = name.Substring(name.IndexOf(' ') + 1); periodWithSuffix = GetPeriodFormatted(period); // Convert to description to Goal: MTL HAMRLIK,R. 65' (16:38 3rd) or close string fakedDescription = string.Format("Goal: {0} {1},{2}. ({3} {4})", teamShort, firstInitial, lastName, time, periodWithSuffix); NHLGameEvent goalEvent = new NHLGameEvent(fakedDescription, NHLGameEvent.EventType.EVENT_GOAL, time); this.lastGoalEvent = goalEvent; System.Console.WriteLine("Set detected goal event from PBP: " + description); } else { System.Console.WriteLine("Couldn't find team from team id " + teamId); } } } } }