Exemple #1
0
        private void RaiseGameEvent(string jsonString)
        {
            TimeOfLastRadarGameEventOrHeartbeat = DateTime.Now;
            bool            isHeartbeat  = jsonString.Contains("heartbeat");
            NcaabbGameEvent mlbGameEvent = null;

            if (isHeartbeat)
            {
                //  Logger.Info("Radar heartbeat");
            }

            if (!isHeartbeat)
            {
                mlbGameEvent = NcaabbGameEvent.FromJson(jsonString);
            }

            bool isGameEvent = mlbGameEvent != null;

            if (isGameEvent)
            {
                NcaabbGameEventEventArgs ncaabbGameEventEventArgs = new NcaabbGameEventEventArgs
                {
                    GameEvent = mlbGameEvent
                };
                OnRadarGameEvent(ncaabbGameEventEventArgs);
            }
        }
        private void HandleGameEvent(object sender, NcaabbGameEventEventArgs ncaabbGameEventEventArgs)
        {
            NcaabbGameEvent ncaabbGameEvent = ncaabbGameEventEventArgs.GameEvent;
            Guid            gameId          = ncaabbGameEvent.Payload.Game.Id;

            if (gameId == GameId)
            {
                string eventType = ncaabbGameEvent.Payload.Event.EventType;
                string message   = $"Handling Radar game event: GameId = {gameId}, EventType = {eventType}";
                Logger.Info(message);

                switch (eventType)
                {
                // todo add specific event
                default:
                    ProcessRadarGameEvent(ncaabbGameEvent);
                    ModelUpdateRequired = true;
                    break;
                }
            }
        }
        private void ProcessRadarGameEvent(NcaabbGameEvent ncaabbGameEvent)
        {
            try
            {
                if (ncaabbGameEvent.Metadata == null)
                {
                    return;
                }

                Dictionary <string, double> evsDictionary = ModelData[NcaabbModelDataKeys.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;
                }

                NcaabbGameState.Status = ncaabbGameEvent.Metadata.Status;
                Event payloadEvent = ncaabbGameEvent.Payload.Event;
                Game  payloadGame  = ncaabbGameEvent.Payload.Game;
                long  sequence     = payloadEvent.Sequence;

                if (sequence <= NcaabbGameState.Sequence)
                {
                    return;
                }

                NcaabbGameState.Sequence     = sequence;
                NcaabbGameState.Period       = $"Q{payloadEvent.Period.Sequence}";
                NcaabbGameState.PeriodNumber = payloadEvent.Period.Sequence;
                NcaabbGameState.Seconds      =
                    Utils.ConvertPeriodToGameString(payloadEvent.Period.Sequence, payloadEvent.Clock, 2400);
                NcaabbGameState.Clock     = payloadEvent.Clock;
                NcaabbGameState.AwayScore = payloadGame.Away.Points;
                NcaabbGameState.HomeScore = payloadGame.Home.Points;

                Dictionary <string, double> egtDictionary = ModelData[NcaabbModelDataKeys.Egt];
                egtDictionary["S"] = NcaabbGameState.Seconds;
                egtDictionary["F"] = NcaabbGameState.Foul;
                //egtDictionary["P"] = NcaabbGameState.Possession;

                int homeScore = NcaabbGameState.HomeScore;
                int awayScore = NcaabbGameState.AwayScore;
                evsDictionary["H,CG"] = homeScore;
                evsDictionary["V,CG"] = awayScore;

                switch (NcaabbGameState.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"":         ""{NcaabbGameState.Period}"",
                                        ""clock"":          ""{NcaabbGameState.Clock}"",
                                        ""possession"":     ""{NcaabbGameState.Possession}"",
                                        ""foul"":           {NcaabbGameState.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);
            }
        }