private void RaiseGameEvent(string jsonString) { try { TimeOfLastRadarGameEventOrHeartbeat = DateTime.Now; bool isHeartbeat = jsonString.Contains("heartbeat"); WnbaGameEvent mlbGameEvent = null; if (isHeartbeat) { // Logger.Info("Radar heartbeat"); } if (!isHeartbeat) { mlbGameEvent = WnbaGameEvent.FromJson(jsonString); } bool isGameEvent = mlbGameEvent != null; if (isGameEvent) { WnbaGameEventEventArgs wnbaGameEventEventArgs = new WnbaGameEventEventArgs { GameEvent = mlbGameEvent }; OnRadarGameEvent(wnbaGameEventEventArgs); } } catch (Exception e) { Logger.Info(e); } }
private void HandleGameEvent(object sender, WnbaGameEventEventArgs wnbaGameEventEventArgs) { WnbaGameEvent wnbaGameEvent = wnbaGameEventEventArgs.GameEvent; Guid gameId = wnbaGameEvent.Payload.Game.Id; if (gameId == GameId) { string eventType = wnbaGameEvent.Payload.Event.EventType; string message = $"Handling Radar game event: GameId = {gameId}, EventType = {eventType}"; Logger.Info(message); switch (eventType) { // todo add specific event default: ProcessRadarGameEvent(wnbaGameEvent); ModelUpdateRequired = true; break; } } }
private void ProcessRadarGameEvent(WnbaGameEvent wnbaGameEvent) { try { if (wnbaGameEvent.Metadata == null) { return; } Dictionary <string, double> evsDictionary = ModelData[WnbaModelDataKeys.Evs]; // todo are these if statements necessary since we are writing and not reading? if (!evsDictionary.ContainsKey("H,Q1")) { evsDictionary["V,Q1"] = 0; evsDictionary["H,Q1"] = 0; } if (!evsDictionary.ContainsKey("H,H1")) { evsDictionary["V,H1"] = 0; evsDictionary["H,H1"] = 0; } if (!evsDictionary.ContainsKey("H,Q2")) { evsDictionary["V,Q2"] = 0; evsDictionary["H,Q2"] = 0; } if (!evsDictionary.ContainsKey("H,H2")) { evsDictionary["V,H2"] = 0; evsDictionary["H,H2"] = 0; } if (!evsDictionary.ContainsKey("H,Q3")) { evsDictionary["V,Q3"] = 0; evsDictionary["H,Q3"] = 0; } if (!evsDictionary.ContainsKey("H,Q4")) { evsDictionary["V,Q4"] = 0; evsDictionary["H,Q4"] = 0; } WnbaGameState.Status = wnbaGameEvent.Metadata.Status; Event payloadEvent = wnbaGameEvent.Payload.Event; Game payloadGame = wnbaGameEvent.Payload.Game; long sequence = payloadEvent.Sequence; if (sequence <= WnbaGameState.Sequence) { return; } WnbaGameState.Sequence = sequence; WnbaGameState.Period = $"Q{payloadEvent.Period.Sequence}"; WnbaGameState.PeriodNumber = payloadEvent.Period.Sequence; WnbaGameState.Seconds = Utils.ConvertPeriodToGameString(payloadEvent.Period.Sequence, payloadEvent.Clock, 2400); WnbaGameState.Clock = payloadEvent.Clock; WnbaGameState.AwayScore = payloadGame.Away.Points; WnbaGameState.HomeScore = payloadGame.Home.Points; Dictionary <string, double> egtDictionary = ModelData[WnbaModelDataKeys.Egt]; egtDictionary["S"] = WnbaGameState.Seconds; egtDictionary["F"] = WnbaGameState.Foul; //egtDictionary["P"] = WnbaGameState.Possession; int homeScore = WnbaGameState.HomeScore; int awayScore = WnbaGameState.AwayScore; evsDictionary["H,CG"] = homeScore; evsDictionary["V,CG"] = awayScore; switch (WnbaGameState.PeriodNumber) { case 1: evsDictionary["V,Q1"] = awayScore; evsDictionary["H,Q1"] = homeScore; break; case 2: evsDictionary["V,Q2"] = awayScore - evsDictionary["V,Q1"]; evsDictionary["H,Q2"] = homeScore - evsDictionary["H,Q1"]; break; case 3: evsDictionary["V,Q3"] = awayScore - (evsDictionary["V,Q1"] + evsDictionary["V,Q2"]); evsDictionary["H,Q3"] = homeScore - (evsDictionary["H,Q1"] + evsDictionary["H,Q2"]); break; case 4: evsDictionary["V,Q4"] = awayScore - (evsDictionary["V,Q1"] + evsDictionary["V,Q2"] + evsDictionary["V,Q3"]); evsDictionary["H,Q4"] = homeScore - (evsDictionary["H,Q1"] + evsDictionary["H,Q2"] + evsDictionary["H,Q3"]); break; } evsDictionary["V,H1"] = evsDictionary["V,Q1"] + evsDictionary["V,Q2"]; evsDictionary["H,H1"] = evsDictionary["H,Q1"] + evsDictionary["H,Q2"]; evsDictionary["V,H2"] = evsDictionary["V,Q3"] + evsDictionary["V,Q4"]; evsDictionary["H,H2"] = evsDictionary["H,Q3"] + evsDictionary["H,Q4"]; string messageJson = $@"{{ ""game"": ""{GameId}"", ""away_score"": {awayScore}, ""home_score"": {homeScore}, ""period"": ""{WnbaGameState.Period}"", ""clock"": ""{WnbaGameState.Clock}"", ""possession"": ""{WnbaGameState.Possession}"", ""foul"": {WnbaGameState.Foul}, ""pause"": false }}"; if (!Utils.IsValidJson(messageJson)) { throw new Exception("JSON is invalid: {jsonString}"); } string messageKey = GameId + "score"; const string eventName = "WNBATEAM"; _pusherUtil.SendScoreMessage(messageJson, eventName, messageKey); } catch (Exception exception) { Logger.Error(exception); } }