Exemple #1
0
        public void SendMarkets(Dictionary <int, List <Market> > periodMarketsDictionary, Guid gameId, string match)
        {
            if (periodMarketsDictionary.Count == 0)
            {
                return;
            }

            if (periodMarketsDictionary[1].Count == 0)
            {
                return;
            }

            List <Markets> formattedMarketList = new List <Markets>();

            foreach (KeyValuePair <int, List <Market> > keyValuePair in periodMarketsDictionary)
            {
                int           key        = keyValuePair.Key;
                List <Market> marketList = keyValuePair.Value;
                string        period     = _periods[key];

                //------------------------------------------------------------------------

                int id = _altSpreadLines[key];

                Markets handicapAlts = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "SP" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                    s.MarketRunnerList[0].Probability < 0.965)
                };

                if (handicapAlts.MarketList.Count > 0)
                {
                    SendOddsMessage(SetMarketActive(handicapAlts), gameId, formattedMarketList);

                    // center line fix to create 1.5 run line standard for full game


                    Market market = handicapAlts.MarketList.OrderBy(v => v.Weight).First();

                    if (market != null)
                    {
                        int id2 = _spreadLines[key];
                        market.Active = true;

                        Markets handicap = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        handicap.MarketList.Add(market);
                        SendOddsMessage(handicap, gameId, formattedMarketList);
                    }
                }

                Markets handicapAltsInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "SP" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                     s.MarketRunnerList[0].Probability> 0.965))
                };

                if (handicapAltsInactive.MarketList.Count > 0)
                {
                    SendOddsMessage2(SetMarketInactive(handicapAltsInactive), gameId);

                    if (handicapAltsInactive.MarketList.Count == 0)
                    {
                        Market market = new Market
                        {
                            Active = false,
                            Tp     = "SP",
                            Target = 0
                        };

                        int id2 = _spreadLines[key];

                        Markets handicap = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        handicap.MarketList.Add(market);
                        SendOddsMessage2(handicap, gameId);
                    }
                }

                //------------------------------------------------------------------------

                id = _moneyLines[key];

                Markets moneyline = new Markets
                {
                    Game   = gameId.ToString(),
                    Period = period,
                    Id     = id,
                    Name   = _marketList[id]
                };

                Market market1 = marketList.Find(s =>
                                                 s.Tp == "ML" && s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                 s.MarketRunnerList[0].Probability < 0.965);

                if (market1 != null)
                {
                    market1.Active = true;
                    market1.Tp     = "ML";
                    moneyline.MarketList.Add(market1);
                    SendOddsMessage(moneyline, gameId, formattedMarketList);
                }
                else
                {
                    Market market = new Market
                    {
                        Active = false,
                        Tp     = "ML",
                        Target = 0
                    };
                    moneyline.MarketList.Add(market);
                    SendOddsMessage2(moneyline, gameId);
                }

                //------------------------------------------------------------------------

                id = _altTotalLines[key];

                Markets totallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "TL" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                    s.MarketRunnerList[0].Probability < 0.965)
                };

                Markets totallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "TL" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                     s.MarketRunnerList[0].Probability> 0.965))
                };



                if (totallineAlt.MarketList.Count > 0)
                {
                    SendOddsMessage(SetMarketActive(totallineAlt), gameId, formattedMarketList);
                    Market market2 = totallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market2 != null)
                    {
                        int id2 = _totalLines[key];
                        market2.Active = true;
                        Markets totalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        totalline.MarketList.Add(market2);
                        SendOddsMessage(totalline, gameId, formattedMarketList);
                    }
                }

                if (totallineAltInactive.MarketList.Count > 0)
                {
                    SendOddsMessage2(SetMarketInactive(totallineAltInactive), gameId);
                    Market market2 = totallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market2 != null)
                    {
                        int id2 = _totalLines[key];
                        market2.Active = false;
                        Markets totalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        totalline.MarketList.Add(market2);
                        SendOddsMessage2(totalline, gameId);
                    }
                }


                //------------------------------------------------------------------------

                id = _altATeamTotalLines[key];

                Markets awayTotallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "ATTL" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 && s.MarketRunnerList[0].Probability < 0.965)
                };

                if (awayTotallineAlt.MarketList.Count > 0)
                {
                    SendOddsMessage(SetMarketActive(awayTotallineAlt), gameId, formattedMarketList);
                    Market market3 = awayTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market3 != null)
                    {
                        int id2 = _aTeamTotalLines[key];
                        market3.Active = true;
                        Markets awayTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        awayTotalline.MarketList.Add(market3);
                        SendOddsMessage(awayTotalline, gameId, formattedMarketList);
                    }
                }

                Markets awayTotallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "ATTL" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                       s.MarketRunnerList[0].Probability> 0.965))
                };

                if (awayTotallineAltInactive.MarketList.Count > 0)
                {
                    SendOddsMessage2(SetMarketInactive(awayTotallineAltInactive), gameId);
                    Market market3 = awayTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market3 != null)
                    {
                        int id2 = _aTeamTotalLines[key];
                        market3.Active = false;
                        Markets awayTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        awayTotalline.MarketList.Add(market3);
                        SendOddsMessage2(awayTotalline, gameId);
                    }
                }

                //------------------------------------------------------------------------

                id = _altHTeamTotalLines[key];

                Markets homeTotallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "HTTL" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 && s.MarketRunnerList[0].Probability < 0.965)
                };

                if (homeTotallineAlt.MarketList.Count > 0)
                {
                    SendOddsMessage(SetMarketActive(homeTotallineAlt), gameId, formattedMarketList);
                    Market market4 = homeTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market4 != null)
                    {
                        int id2 = _hTeamTotalLines[key];
                        market4.Active = true;
                        Markets homeTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        homeTotalline.MarketList.Add(market4);
                        SendOddsMessage(homeTotalline, gameId, formattedMarketList);
                    }
                }

                //inactive
                Markets homeTotallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "HTTL" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                       s.MarketRunnerList[0].Probability> 0.965))
                };

                if (homeTotallineAltInactive.MarketList.Count > 0)
                {
                    SendOddsMessage2(SetMarketInactive(homeTotallineAltInactive), gameId);
                    Market market4 = homeTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market4 != null)
                    {
                        int id2 = _hTeamTotalLines[key];
                        market4.Active = false;
                        Markets homeTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        homeTotalline.MarketList.Add(market4);
                        SendOddsMessage2(homeTotalline, gameId);
                    }
                }

                //------------------------to do send center line-----------------------------------------------

                id = _totalLines3W[key];

                Markets totalline3W = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = _totalLines3W[key],
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "TL3W" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                    s.MarketRunnerList[0].Probability < 0.965)
                };

                if (totalline3W.MarketList.Count > 0)
                {
                    SendOddsMessage(SetMarketActive(totalline3W), gameId, formattedMarketList);
                }


                Markets totalline3WInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = _totalLines3W[key],
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "TL3W" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                       s.MarketRunnerList[0].Probability> 0.965))
                };

                if (totalline3WInactive.MarketList.Count > 0)
                {
                    SendOddsMessage2(SetMarketInactive(totalline3WInactive), gameId);
                }

                //------------------------------------------------------------------------

                id = _spread3WLines[key];

                Markets handicap3W = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => s.Tp == "SP3W" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                    s.MarketRunnerList[0].Probability < 0.965)
                };

                if (handicap3W.MarketList.Count > 0)
                {
                    SendOddsMessage(handicap3W, gameId, formattedMarketList);
                }

                Markets handicap3WInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "SP3W" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                       s.MarketRunnerList[0].Probability> 0.965))
                };

                if (handicap3WInactive.MarketList.Count > 0)
                {
                    SendOddsMessage2(handicap3WInactive, gameId);
                }
            }

            FormattedGame formattedGame = new FormattedGame
            {
                Id         = gameId.ToString(),
                Match      = match,
                MarketList = formattedMarketList
            };

            string formattedGameJsonString = JsonConvert.SerializeObject(formattedGame);

            _datastore.AddFinishedOdds(gameId, formattedGameJsonString);
        }
        public void SendMarkets(Dictionary <int, List <Market> > periodMarketsDictionary, Guid gameId, string match, Dictionary <string, double> gameState)
        {
            // todo divide into smaller functions?
            if (periodMarketsDictionary.Count == 0)
            {
                Logger.Info("SendMarkets(): periodMarketsDictionary.Count is zero");
                return;
            }

            if (periodMarketsDictionary[1].Count == 0)
            {
                Logger.Info("SendMarkets(): periodMarketsDictionary[1].Count is zero");
                return;
            }

            const string   eventName           = "WNBATEAM";
            List <Markets> formattedMarketList = new List <Markets>();

            foreach (KeyValuePair <int, List <Market> > keyValuePair in periodMarketsDictionary)
            {
                try
                {
                    int           key        = keyValuePair.Key;
                    List <Market> marketList = keyValuePair.Value;
                    string        period     = _periods[key];

                    //------------------------------------- 2 way spread -----------------------------------

                    int id = _altSpreadLines[key];

                    Markets handicapAlts = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "SP"))
                    };

                    if (handicapAlts.MarketList.Count > 0)
                    {
                        Markets activeHandicapAlts = SetMarketActiveNew(handicapAlts, true, gameState);
                        SendOddsMessage(activeHandicapAlts, gameId, formattedMarketList, eventName);

                        if (handicapAlts.MarketList.Count > 0)
                        {
                            Market market = handicapAlts.MarketList.OrderBy(v => v.Weight).First();

                            if (market != null)
                            {
                                int id2 = _spreadLines[key];
                                market.Active = CheckActive(period, gameState, true);

                                Markets handicap = new Markets
                                {
                                    Game   = gameId.ToString(),
                                    Period = period,
                                    Id     = id2,
                                    Name   = _marketList[id2]
                                };

                                handicap.MarketList.Add(market);
                                SendOddsMessage(handicap, gameId, formattedMarketList, eventName);
                            }
                        }
                    }

                    //inactive
                    Markets handicapAltsInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "SP"))
                    };

                    if (handicapAltsInactive.MarketList.Count > 0)
                    {
                        Markets inActiveHandicapAlts = SetMarketInactive(handicapAlts);
                        SendOddsMessage2(inActiveHandicapAlts, gameId, eventName);

                        if (handicapAlts.MarketList.Count == 0)
                        {
                            Market market = handicapAltsInactive.MarketList.OrderBy(v => v.Weight).First();

                            if (market != null)
                            {
                                int id2 = _spreadLines[key];
                                market.Active = false;

                                Markets handicap = new Markets
                                {
                                    Game   = gameId.ToString(),
                                    Period = period,
                                    Id     = id2,
                                    Name   = _marketList[id2]
                                };

                                handicap.MarketList.Add(market);
                                SendOddsMessage2(handicap, gameId, eventName);
                            }
                        }
                    }

                    //--------------------------------------------- 2 way moneyline ---------------------------

                    id = _moneyLines[key];

                    Markets moneyline = new Markets
                    {
                        Game   = gameId.ToString(),
                        Period = period,
                        Id     = id,
                        Name   = _marketList[id]
                    };

                    Market market1 = marketList.Find(s => SelectInsideLimitsWithZeroTarget(s, "ML"));

                    if (market1 != null)
                    {
                        market1.Active = CheckActive(period, gameState, true);
                        market1.Tp     = "ML";
                        moneyline.MarketList.Add(market1);
                        SendOddsMessage(moneyline, gameId, formattedMarketList, eventName);
                    }
                    else
                    {  //inactive
                        Market market = new Market
                        {
                            Active = false,
                            Tp     = "ML"
                        };
                        moneyline.MarketList.Add(market);
                        SendOddsMessage2(moneyline, gameId, eventName);
                    }

                    //----------------------------------------- 2 way total -------------------------------

                    id = _altTotalLines[key];

                    Markets totallineAlt = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "TL"))
                    };

                    if (totallineAlt.MarketList.Count > 0)
                    {
                        Markets activeTotallineAlt = SetMarketActiveNew(totallineAlt, true, gameState); //SetMarketActive(totallineAlt);
                        SendOddsMessage(activeTotallineAlt, gameId, formattedMarketList, eventName);
                        Market market2 = totallineAlt.MarketList.OrderBy(v => v.Weight).First();

                        if (market2 != null)
                        {
                            int id2 = _totalLines[key];
                            market2.Active = CheckActive(period, gameState, true);
                            Markets totalline = new Markets
                            {
                                Game   = gameId.ToString(),
                                Period = period,
                                Id     = id2,
                                Name   = _marketList[id2]
                            };

                            totalline.MarketList.Add(market2);
                            SendOddsMessage(totalline, gameId, formattedMarketList, eventName);
                        }
                    }

                    Markets totallineAltInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "TL"))
                    };

                    if (totallineAltInactive.MarketList.Count > 0)
                    {
                        Markets activeTotallineAlt = SetMarketInactive(totallineAltInactive);
                        SendOddsMessage(activeTotallineAlt, gameId, formattedMarketList, eventName);

                        if (totallineAlt.MarketList.Count == 0)
                        {
                            Market market2 = totallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                            if (market2 != null)
                            {
                                int id2 = _totalLines[key];
                                market2.Active = true;
                                Markets totalline = new Markets
                                {
                                    Game   = gameId.ToString(),
                                    Period = period,
                                    Id     = id2,
                                    Name   = _marketList[id2]
                                };

                                totalline.MarketList.Add(market2);
                                SendOddsMessage(totalline, gameId, formattedMarketList, eventName);
                            }
                        }
                    }

                    //------------------------------------------------2 way away team total ------------------------

                    id = _altATeamTotalLines[key];

                    Markets awayTotallineAlt = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "ATTL"))
                    };

                    if (awayTotallineAlt.MarketList.Count > 0)
                    {
                        Markets activeAwayTotallineAlt = SetMarketActiveNew(awayTotallineAlt, true, gameState); //SetMarketActive(awayTotallineAlt);
                        SendOddsMessage(activeAwayTotallineAlt, gameId, formattedMarketList, eventName);
                        Market market3 = awayTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                        if (market3 != null)
                        {
                            int id2 = _aTeamTotalLines[key];
                            market3.Active = CheckActive(period, gameState, true);
                            Markets awayTotalline = new Markets
                            {
                                Game   = gameId.ToString(),
                                Period = period,
                                Id     = id2,
                                Name   = _marketList[id2]
                            };

                            awayTotalline.MarketList.Add(market3);
                            SendOddsMessage(awayTotalline, gameId, formattedMarketList, eventName);
                        }
                    }

                    Markets awayTotallineAltInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "ATTL"))
                    };

                    if (awayTotallineAltInactive.MarketList.Count > 0)
                    {
                        Markets activeAwayTotallineAlt = SetMarketInactive(awayTotallineAltInactive);
                        SendOddsMessage2(activeAwayTotallineAlt, gameId, eventName);

                        if (awayTotallineAlt.MarketList.Count == 0)
                        {
                            Market market3 = awayTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                            if (market3 != null)
                            {
                                int id2 = _aTeamTotalLines[key];
                                market3.Active = false;
                                Markets awayTotalline = new Markets
                                {
                                    Game   = gameId.ToString(),
                                    Period = period,
                                    Id     = id2,
                                    Name   = _marketList[id2]
                                };

                                awayTotalline.MarketList.Add(market3);
                                SendOddsMessage2(awayTotalline, gameId, eventName);
                            }
                        }
                    }

                    //--------------------------------------------2 way home team total ----------------------------

                    id = _altHTeamTotalLines[key];

                    Markets homeTotallineAlt = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "HTTL"))
                    };

                    if (homeTotallineAlt.MarketList.Count > 0)
                    {
                        Markets activeHomeTotallineAlt = SetMarketActiveNew(homeTotallineAlt, true, gameState);
                        //SetMarketActive(homeTotallineAlt);
                        SendOddsMessage(activeHomeTotallineAlt, gameId, formattedMarketList, eventName);
                        Market market4 = homeTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                        if (market4 != null)
                        {
                            int id2 = _hTeamTotalLines[key];
                            market4.Active = CheckActive(period, gameState, true);
                            Markets homeTotalline = new Markets
                            {
                                Game   = gameId.ToString(),
                                Period = period,
                                Id     = id2,
                                Name   = _marketList[id2]
                            };

                            homeTotalline.MarketList.Add(market4);
                            SendOddsMessage(homeTotalline, gameId, formattedMarketList, eventName);
                        }
                    }

                    Markets homeTotallineAltInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "HTTL"))
                    };

                    if (homeTotallineAltInactive.MarketList.Count > 0)
                    {
                        Markets activeHomeTotallineAlt = SetMarketInactive(homeTotallineAltInactive);
                        SendOddsMessage2(activeHomeTotallineAlt, gameId, eventName);

                        if (homeTotallineAlt.MarketList.Count == 0)
                        {
                            Market market3 = homeTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                            if (market3 != null)
                            {
                                int id2 = _hTeamTotalLines[key];
                                market3.Active = false;
                                Markets homeTotalline = new Markets
                                {
                                    Game   = gameId.ToString(),
                                    Period = period,
                                    Id     = id2,
                                    Name   = _marketList[id2]
                                };

                                homeTotalline.MarketList.Add(market3);
                                SendOddsMessage2(homeTotalline, gameId, eventName);
                            }
                        }
                    }

                    //-------------------------------------three way total -----------------------------------

                    id = _totalLines3W[key];

                    Markets totalline3W = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = _totalLines3W[key],
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "TL3W"))
                    };

                    Markets activeTotalline3W = SetMarketActiveNew(totalline3W, true, gameState);  //SetMarketActive(totalline3W);
                    SendOddsMessage(activeTotalline3W, gameId, formattedMarketList, eventName);

                    Markets totalline3WInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = _totalLines3W[key],
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "TL3W"))
                    };

                    activeTotalline3W = SetMarketInactive(totalline3WInactive);
                    SendOddsMessage2(activeTotalline3W, gameId, eventName);

                    //-------------------------------------three way away team total -----------------------------------

                    id = _altATeamTotal3WLines[key];

                    Markets awayTotalline3W = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = _totalLines3W[key],
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "ATTL3W"))
                    };

                    Markets activeAwayTotalline3W = SetMarketActive(awayTotalline3W);
                    SendOddsMessage(activeAwayTotalline3W, gameId, formattedMarketList, eventName);

                    Markets awayTotalline3WInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = _totalLines3W[key],
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "ATTL3W"))
                    };

                    activeAwayTotalline3W = SetMarketInactive(awayTotalline3WInactive);
                    SendOddsMessage(activeAwayTotalline3W, gameId, formattedMarketList, eventName);

                    //-------------------------------------three way home team total -----------------------------------

                    id = _altHTeamTotal3WLines[key];

                    Markets homeTotalline3W = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = _totalLines3W[key],
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "HTTL3W"))
                    };

                    Markets activeHomeTotalline3W = SetMarketActiveNew(homeTotalline3W, true, gameState);
                    //SetMarketActive(homeTotalline3W);
                    SendOddsMessage(activeHomeTotalline3W, gameId, formattedMarketList, eventName);

                    Markets homeTotalline3WInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = _totalLines3W[key],
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "HTTL3W"))
                    };

                    activeHomeTotalline3W = SetMarketActiveNew(homeTotalline3WInactive, true, gameState);
                    SendOddsMessage2(activeHomeTotalline3W, gameId, eventName);

                    //--------------------------------------- three way spread ---------------------------------

                    id = _spread3WLines[key];

                    Markets handicap3W = new Markets
                    {
                        Game   = gameId.ToString(),
                        Period = period,
                        Id     = id,
                        Name   = _marketList[id],
                        // todo why is the target check missing? !s.Target.IsEqualToZero()
                        MarketList = marketList.FindAll(s =>
                                                        s.Tp == "SP3W" && s.MarketRunnerList[0].Probability > 0.035 &&
                                                        s.MarketRunnerList[0].Probability < 0.965)
                                     //MarketList = marketList.FindAll(s => s.Tp == "SP3W" && s.MarketRunnerList[0].Probability > 0.035 &&
                                     //                                     s.MarketRunnerList[0].Probability < 0.965)
                    };
                    Markets activehandicap3W = SetMarketActiveNew(handicap3W, true, gameState);
                    SendOddsMessage(activehandicap3W, gameId, formattedMarketList, eventName);

                    Markets handicap3WInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithoutTarget(s, "SP3W"))
                    };

                    activehandicap3W = SetMarketInactive(handicap3WInactive);
                    SendOddsMessage2(activehandicap3W, gameId, eventName);

                    //--------------------------------------- three way money ---------------------------------

                    id = _moneyLines3W[key];

                    Markets moneyLine3W = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithoutTarget(s, "ML3"))
                    };

                    Markets activeMoneyLine3W = SetMarketActiveNew(moneyLine3W, true, gameState); //SetMarketActive(moneyLine3W);
                    SendOddsMessage(activeMoneyLine3W, gameId, formattedMarketList, eventName);

                    Markets moneyLine3WInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithoutTarget(s, "ML3"))
                    };

                    activeMoneyLine3W = SetMarketActiveNew(moneyLine3WInactive, true, gameState);
                    SendOddsMessage2(activeMoneyLine3W, gameId, eventName);

                    //--------------------------------------------winning margin REG ----------------------------

                    id = _winningMarginR[key];

                    Markets winningMargin = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithoutTarget(s, "WMR"))
                    };
                    Markets activeWinningMargin = SetMarketActiveNew(winningMargin, true, gameState);
                    SendOddsMessage(activeWinningMargin, gameId, formattedMarketList, eventName);

                    Markets winningMarginInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithoutTarget(s, "WMP"))
                    };
                    activeWinningMargin = SetMarketInactive(winningMarginInactive);
                    SendOddsMessage2(activeWinningMargin, gameId, eventName);

                    //--------------------------------------------winning margin CG ----------------------------

                    id = _winningMarginOt[key];

                    Markets winningMarginCg = new Markets  // CG BECAUSE ITS C (complete) and G(game)
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithoutTarget(s, "WMCG"))
                    };
                    Markets activewinningMarginCg = SetMarketActiveNew(winningMarginCg, true, gameState); //SetMarketActive(winningMarginCg);
                    SendOddsMessage(activewinningMarginCg, gameId, formattedMarketList, eventName);

                    Markets winningMarginCgInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithoutTarget(s, "WMCG"))
                    };

                    activewinningMarginCg = SetMarketInactive(winningMarginCgInactive);
                    SendOddsMessage(activewinningMarginCg, gameId, formattedMarketList, eventName);

                    //--------------------------------------------combo 6 ----------------------------

                    id = _comb6[key];

                    Markets winnerTotal = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithoutTarget(s, "C6W"))
                    };
                    Markets activeWinnerTotal = SetMarketActiveNew(winnerTotal, true, gameState); //SetMarketActive(winnerTotal);
                    SendOddsMessage(activeWinnerTotal, gameId, formattedMarketList, eventName);

                    Markets winnerTotalInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectOutsideLimitsWithoutTarget(s, "C6W"))
                    };
                    activeWinnerTotal = SetMarketInactive(winnerTotal);
                    SendOddsMessage2(activeWinnerTotal, gameId, eventName);

                    //--------------------------------------------combo 4 ----------------------------

                    id = _comb4[key];

                    Markets spreadTotal = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithoutTarget(s, "C4W"))
                    };
                    Markets activeSpreadTotal = SetMarketActiveNew(spreadTotal, true, gameState); //SetMarketActive(spreadTotal);
                    SendOddsMessage(activeSpreadTotal, gameId, formattedMarketList, eventName);

                    // todo why is this unused? Typo?
                    Markets spreadTotalInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id],
                        MarketList = marketList.FindAll(s => SelectInsideLimitsWithoutTarget(s, "C4W"))
                    };

                    activeSpreadTotal = SetMarketInactive(spreadTotalInactive);
                    SendOddsMessage2(activeSpreadTotal, gameId, eventName);
                }
                catch (Exception exception)
                {
                    Logger.Error(exception);
                }
            }

            try
            {
                FormattedGame formattedGame = new FormattedGame
                {
                    Id         = gameId.ToString(),
                    Match      = match,
                    MarketList = formattedMarketList
                };

                string formattedGameJsonString = JsonConvert.SerializeObject(formattedGame);
                _datastore.AddFinishedOdds(gameId, formattedGameJsonString);
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
            }
        }
        public void SendMarkets(Dictionary <int, List <Market> > periodMarketsDictionary, Guid gameId, string match, Dictionary <string, double> gameState)
        {
            if (periodMarketsDictionary.Count == 0)
            {
                return;
            }

            if (periodMarketsDictionary[1].Count == 0)
            {
                return;
            }

            /*  testing
             * gameState = new Dictionary<string, double>();
             * gameState["I"] = 5;
             * gameState["T"] = 0;
             */

            List <Markets> formattedMarketList = new List <Markets>();

            foreach (KeyValuePair <int, List <Market> > keyValuePair in periodMarketsDictionary)
            {
                int           key        = keyValuePair.Key;
                List <Market> marketList = keyValuePair.Value;
                string        period     = _periods[key];

                //------------------------------------------------------------------------

                //center handicap exists if false send takedown
                bool cHandicap  = false;
                bool cTotal     = false;
                bool cHomeTotal = false;
                bool cAwayTotal = false;

                int id = _altRunLines[key];
                try
                {
                    Markets handicapAlts = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id].Name,
                        MarketList = marketList.FindAll(s =>
                                                        s.Tp == "SP" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                        s.MarketRunnerList[0].Probability < 0.965)
                    };

                    if (handicapAlts.MarketList.Count > 0)
                    {
                        Markets handicapAltsSet = SetMarketActiveNew(handicapAlts, true, gameState);
                        SendOddsMessage(handicapAltsSet, gameId, formattedMarketList);

                        // center line fix to create 1.5 run line standard for full game

                        List <Market> marketListRunLine =
                            handicapAlts.MarketList.FindAll(v => Math.Abs(v.Target).IsEqualTo(1.5));

                        if (marketListRunLine.Count > 0 && key == 4)
                        {
                            Market market = marketListRunLine.OrderBy(v => v.Weight).First();

                            if (market != null)
                            {
                                cHandicap = true;
                                int id2 = _periodRunLines[key];
                                market.Active = CheckActive(period, gameState, true);

                                Markets handicap = new Markets
                                {
                                    Game   = gameId.ToString(),
                                    Period = period,
                                    Id     = id2,
                                    Name   = _marketList[id2].Name
                                };

                                handicap.MarketList.Add(market);
                                SendOddsMessage(handicap, gameId, formattedMarketList);
                            }
                        }
                        else
                        {
                            Market market = handicapAlts.MarketList.OrderBy(v => v.Weight).First();

                            if (market != null)
                            {
                                cHandicap = true;
                                int id2 = _periodRunLines[key];
                                market.Active = CheckActive(period, gameState, true);

                                Markets handicap = new Markets
                                {
                                    Game   = gameId.ToString(),
                                    Period = period,
                                    Id     = id2,
                                    Name   = _marketList[id2].Name
                                };

                                handicap.MarketList.Add(market);
                                SendOddsMessage(handicap, gameId, formattedMarketList);
                            }
                        }
                    }

                    Markets handicapAltsInactive = new Markets
                    {
                        Game       = gameId.ToString(),
                        Period     = period,
                        Id         = id,
                        Name       = _marketList[id].Name,
                        MarketList = marketList.FindAll(s =>
                                                        s.Tp == "SP" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                         s.MarketRunnerList[0].Probability> 0.965))
                    };

                    if (handicapAltsInactive.MarketList.Count > 0 && handicapAlts.MarketList.Count == 0)
                    {
                        SendOddsMessage2(SetMarketInactive(handicapAltsInactive), gameId);

                        if (handicapAltsInactive.MarketList.Count == 0)
                        {
                            Market market = new Market
                            {
                                Active = false,
                                Tp     = "SP",
                                Target = 0
                            };

                            int id2 = _periodRunLines[key];

                            Markets handicap = new Markets
                            {
                                Game   = gameId.ToString(),
                                Period = period,
                                Id     = id2,
                                Name   = _marketList[id2].Name
                            };

                            handicap.MarketList.Add(market);
                            if (!cHandicap)
                            {
                                SendOddsMessage2(handicap, gameId);
                            }
                        }
                    }

                    //------------------------------------------------------------------------

                    id = _periodMoneyLines[key];

                    Markets moneyline = new Markets
                    {
                        Game   = gameId.ToString(),
                        Period = period,
                        Id     = id,
                        Name   = _marketList[id].Name
                    };

                    Market market1 = marketList.Find(s =>
                                                     s.Tp == "SP" && s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                     s.MarketRunnerList[0].Probability < 0.965);

                    if (market1 != null)
                    {
                        market1.Active = CheckActive(period, gameState, true);
                        market1.Tp     = "ML";
                        moneyline.MarketList.Add(market1);
                        SendOddsMessage(moneyline, gameId, formattedMarketList);
                    }
                    else
                    {
                        Market market = new Market
                        {
                            Active = false,
                            Tp     = "ML",
                            Target = 0
                        };
                        moneyline.MarketList.Add(market);
                        SendOddsMessage2(moneyline, gameId);
                    }
                }
                catch (Exception e)
                {
                    Logger.Info(e);
                }



                id = _altTotalLines[key];

                Markets totallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id].Name,
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "TL" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                                                    s.MarketRunnerList[0].Probability < 0.965)
                };

                Markets totallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id].Name,
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "TL" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                     s.MarketRunnerList[0].Probability> 0.965))
                };

                if (totallineAlt.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(totallineAlt, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList);
                    Market market2 = totallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market2 != null)
                    {
                        cTotal = true;
                        int id2 = _totalLines[key];
                        market2.Active = CheckActive(period, gameState, true);
                        Markets totalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2].Name
                        };

                        totalline.MarketList.Add(market2);
                        SendOddsMessage(totalline, gameId, formattedMarketList);
                    }
                }

                if (totallineAltInactive.MarketList.Count > 0 && totallineAlt.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(totallineAltInactive), gameId);
                    Market market2 = totallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market2 != null)
                    {
                        int id2 = _totalLines[key];
                        market2.Active = false;
                        Markets totalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2].Name
                        };

                        totalline.MarketList.Add(market2);
                        if (!cTotal)
                        {
                            SendOddsMessage2(totalline, gameId);
                        }
                    }
                }

                //------------------------------------------------------------------------

                id = _altATeamTotalLines[key];

                Markets awayTotallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id].Name,
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "ATTL" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 && s.MarketRunnerList[0].Probability < 0.965)
                };

                if (awayTotallineAlt.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(awayTotallineAlt, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList);
                    Market market3 = awayTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market3 != null)
                    {
                        cAwayTotal = true;
                        int id2 = _aTeamTotalLines[key];
                        market3.Active = CheckActive(period, gameState, true);
                        Markets awayTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2].Name
                        };

                        awayTotalline.MarketList.Add(market3);
                        SendOddsMessage(awayTotalline, gameId, formattedMarketList);
                    }
                }

                Markets awayTotallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id].Name,
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "ATTL" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                       s.MarketRunnerList[0].Probability> 0.965))
                };

                if (awayTotallineAltInactive.MarketList.Count > 0 && awayTotallineAlt.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(awayTotallineAltInactive), gameId);
                    Market market3 = awayTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market3 != null)
                    {
                        int id2 = _aTeamTotalLines[key];
                        market3.Active = false;
                        Markets awayTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2].Name
                        };

                        awayTotalline.MarketList.Add(market3);
                        if (!cAwayTotal)
                        {
                            SendOddsMessage2(awayTotalline, gameId);
                        }
                    }
                }

                //------------------------------------------------------------------------

                id = _altHTeamTotalLines[key];

                Markets homeTotallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id].Name,
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "HTTL" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 && s.MarketRunnerList[0].Probability < 0.965)
                };

                if (homeTotallineAlt.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(homeTotallineAlt, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList);
                    Market market4 = homeTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market4 != null)
                    {
                        cHomeTotal = true;
                        int id2 = _hTeamTotalLines[key];
                        market4.Active = CheckActive(period, gameState, true);
                        Markets homeTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2].Name
                        };

                        homeTotalline.MarketList.Add(market4);
                        SendOddsMessage(homeTotalline, gameId, formattedMarketList);
                    }
                }

                //inactive
                Markets homeTotallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id].Name,
                    MarketList = marketList.FindAll(s =>
                                                    s.Tp == "HTTL" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability <0.035 ||
                                                                                                                                       s.MarketRunnerList[0].Probability> 0.965))
                };

                if (homeTotallineAltInactive.MarketList.Count > 0 && homeTotallineAlt.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(homeTotallineAltInactive), gameId);
                    Market market4 = homeTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market4 != null)
                    {
                        int id2 = _hTeamTotalLines[key];
                        market4.Active = false;
                        Markets homeTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2].Name
                        };

                        homeTotalline.MarketList.Add(market4);
                        if (!cHomeTotal)
                        {
                            SendOddsMessage2(homeTotalline, gameId);
                        }
                    }
                }

                /*
                 * //------------------------to do send center line-----------------------------------------------
                 *
                 * id = _totalLines3W[key];
                 *
                 * Markets totalline3W = new Markets
                 * {
                 *  Game = gameId.ToString(),
                 *  Period = period,
                 *  Id = _totalLines3W[key],
                 *  Name = _marketList[id].Name,
                 *  MarketList = marketList.FindAll(s =>
                 *      s.Tp == "TL3W" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                 *      s.MarketRunnerList[0].Probability < 0.965)
                 * };
                 *
                 * if (totalline3W.MarketList.Count > 0)
                 * {
                 *  Markets activeMarkets = SetMarketActiveNew(totalline3W, true, gameState);
                 *  SendOddsMessage(activeMarkets, gameId, formattedMarketList);
                 * }
                 *
                 *
                 * Markets totalline3WInactive = new Markets
                 * {
                 *  Game = gameId.ToString(),
                 *  Period = period,
                 *  Id = _totalLines3W[key],
                 *  Name = _marketList[id].Name,
                 *  MarketList = marketList.FindAll(s =>
                 *      s.Tp == "TL3W" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability < 0.035 ||
                 *      s.MarketRunnerList[0].Probability > 0.965))
                 * };
                 *
                 * if (totalline3WInactive.MarketList.Count > 0)
                 * {
                 *  SendOddsMessage2(SetMarketInactive(totalline3WInactive), gameId);
                 * }
                 *
                 * //------------------------------------------------------------------------
                 *
                 * id = _altRunLines3W[key];
                 *
                 * Markets handicap3W = new Markets
                 * {
                 *  Game = gameId.ToString(),
                 *  Period = period,
                 *  Id = id,
                 *  Name = _marketList[id].Name,
                 *  MarketList = marketList.FindAll(s => s.Tp == "SP3W" && !s.Target.IsEqualToZero() && s.MarketRunnerList[0].Probability > 0.035 &&
                 *                                       s.MarketRunnerList[0].Probability < 0.965)
                 * };
                 *
                 * if (handicap3W.MarketList.Count > 0)
                 * {
                 *  Markets activeMarkets = SetMarketActiveNew(handicap3W, true, gameState);
                 *  SendOddsMessage(activeMarkets, gameId, formattedMarketList);
                 * }
                 *
                 * Markets handicap3WInactive = new Markets
                 * {
                 *  Game = gameId.ToString(),
                 *  Period = period,
                 *  Id = id,
                 *  Name = _marketList[id].Name,
                 *  MarketList = marketList.FindAll(s =>
                 *      s.Tp == "SP3W" && !s.Target.IsEqualToZero() && (s.MarketRunnerList[0].Probability < 0.035 ||
                 *      s.MarketRunnerList[0].Probability > 0.965))
                 * };
                 *
                 * if (handicap3WInactive.MarketList.Count > 0 && handicap3W.MarketList.Count == 0)
                 * {
                 *  SendOddsMessage2(SetMarketInactive(handicap3WInactive), gameId);
                 * }
                 */
            }

            FormattedGame formattedGame = new FormattedGame
            {
                Id         = gameId.ToString(),
                Match      = match,
                MarketList = formattedMarketList
            };

            string formattedGameJsonString = JsonConvert.SerializeObject(formattedGame);

            _datastore.AddFinishedOdds(gameId, formattedGameJsonString);
        }
Exemple #4
0
        public void SendMarkets(Dictionary <int, List <Market> > periodMarketsDictionary, Guid gameId, string match, Dictionary <string, double> gameState)
        {
            // todo divide into smaller functions
            if (periodMarketsDictionary.Count == 0)
            {
                return;
            }

            if (periodMarketsDictionary[1].Count == 0)
            {
                return;
            }

            const string   eventName           = "MLB";
            List <Markets> formattedMarketList = new List <Markets>();

            foreach (KeyValuePair <int, List <Market> > keyValuePair in periodMarketsDictionary)
            {
                int           key        = keyValuePair.Key;
                List <Market> marketList = keyValuePair.Value;
                string        period     = _periods[key];

                //------------------------------------------------------------------------

                //center handicap exists if false send takedown
                bool cHandicap  = false;
                bool cTotal     = false;
                bool cHomeTotal = false;
                bool cAwayTotal = false;

                int id = _altRunLines[key];

                Markets handicapAlts = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(m => SelectInsideLimitsWithNonZeroTarget(m, "SP"))
                };

                if (handicapAlts.MarketList.Count > 0)
                {
                    Markets handicapAltsSet = SetMarketActiveNew(handicapAlts, true, gameState);
                    SendOddsMessage(handicapAltsSet, gameId, formattedMarketList, eventName);

                    // center line fix to create 1.5 run line standard for full game

                    List <Market> marketListRunLine =
                        handicapAlts.MarketList.FindAll(v => Math.Abs(v.Target).IsEqualTo(1.5));

                    if (marketListRunLine.Count > 0 && key == 4 && gameState["I"] < 2)
                    {
                        Market market = marketListRunLine.OrderBy(v => v.Weight).First();

                        if (market != null)
                        {
                            cHandicap = true;
                            int id2 = _periodRunLines[key];
                            market.Active = CheckActive(period, gameState, true);

                            Markets handicap = new Markets
                            {
                                Game   = gameId.ToString(),
                                Period = period,
                                Id     = id2,
                                Name   = _marketList[id2]
                            };

                            handicap.MarketList.Add(market);
                            SendOddsMessage(handicap, gameId, formattedMarketList, eventName);
                        }
                    }
                    else
                    {
                        Market market = handicapAlts.MarketList.OrderBy(v => v.Weight).First();

                        if (market != null)
                        {
                            cHandicap = true;
                            int id2 = _periodRunLines[key];
                            market.Active = CheckActive(period, gameState, true);

                            Markets handicap = new Markets
                            {
                                Game   = gameId.ToString(),
                                Period = period,
                                Id     = id2,
                                Name   = _marketList[id2]
                            };

                            handicap.MarketList.Add(market);
                            SendOddsMessage(handicap, gameId, formattedMarketList, eventName);
                        }
                    }
                }

                Markets handicapAltsInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(m => SelectOutsideLimitsWithNonZeroTarget(m, "SP"))
                };

                if (handicapAltsInactive.MarketList.Count > 0 && handicapAlts.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(handicapAltsInactive), gameId, eventName);

                    if (handicapAltsInactive.MarketList.Count == 0)
                    {
                        Market market = new Market
                        {
                            Active = false,
                            Tp     = "SP",
                            Target = 0
                        };

                        int id2 = _periodRunLines[key];

                        Markets handicap = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        handicap.MarketList.Add(market);
                        if (!cHandicap)
                        {
                            SendOddsMessage2(handicap, gameId, eventName);
                        }
                    }
                }

                //------------------------------------------------------------------------

                id = _periodMoneyLines[key];

                Markets moneyline = new Markets
                {
                    Game   = gameId.ToString(),
                    Period = period,
                    Id     = id,
                    Name   = _marketList[id]
                };

                Market market1 = marketList.Find(s => SelectInsideLimitsWithZeroTarget(s, "SP"));

                if (market1 != null)
                {
                    market1.Active = CheckActive(period, gameState, true);
                    market1.Tp     = "ML";
                    moneyline.MarketList.Add(market1);
                    SendOddsMessage(moneyline, gameId, formattedMarketList, eventName);
                }
                else
                {
                    Market market = new Market
                    {
                        Active = false,
                        Tp     = "ML",
                        Target = 0
                    };
                    moneyline.MarketList.Add(market);
                    SendOddsMessage2(moneyline, gameId, eventName);
                }

                //------------------------------------------------------------------------

                id = _altTotalLines[key];

                Markets totallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "TL"))
                };

                Markets totallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "TL"))
                };

                if (totallineAlt.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(totallineAlt, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList, eventName);
                    Market market2 = totallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market2 != null)
                    {
                        cTotal = true;
                        int id2 = _totalLines[key];
                        market2.Active = CheckActive(period, gameState, true);
                        Markets totalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        totalline.MarketList.Add(market2);
                        SendOddsMessage(totalline, gameId, formattedMarketList, eventName);
                    }
                }

                if (totallineAltInactive.MarketList.Count > 0 && totallineAlt.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(totallineAltInactive), gameId, eventName);
                    Market market2 = totallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market2 != null)
                    {
                        int id2 = _totalLines[key];
                        market2.Active = false;
                        Markets totalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        totalline.MarketList.Add(market2);
                        if (!cTotal)
                        {
                            SendOddsMessage2(totalline, gameId, eventName);
                        }
                    }
                }

                //------------------------------------------------------------------------

                id = _altATeamTotalLines[key];

                Markets awayTotallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "ATTL"))
                };

                if (awayTotallineAlt.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(awayTotallineAlt, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList, eventName);
                    Market market3 = awayTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market3 != null)
                    {
                        cAwayTotal = true;
                        int id2 = _aTeamTotalLines[key];
                        market3.Active = CheckActive(period, gameState, true);
                        Markets awayTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        awayTotalline.MarketList.Add(market3);
                        SendOddsMessage(awayTotalline, gameId, formattedMarketList, eventName);
                    }
                }

                Markets awayTotallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "ATTL"))
                };

                if (awayTotallineAltInactive.MarketList.Count > 0 && awayTotallineAlt.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(awayTotallineAltInactive), gameId, eventName);
                    Market market3 = awayTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market3 != null)
                    {
                        int id2 = _aTeamTotalLines[key];
                        market3.Active = false;
                        Markets awayTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        awayTotalline.MarketList.Add(market3);
                        if (!cAwayTotal)
                        {
                            SendOddsMessage2(awayTotalline, gameId, eventName);
                        }
                    }
                }

                //------------------------------------------------------------------------

                id = _altHTeamTotalLines[key];

                Markets homeTotallineAlt = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "HTTL"))
                };

                if (homeTotallineAlt.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(homeTotallineAlt, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList, eventName);
                    Market market4 = homeTotallineAlt.MarketList.OrderBy(v => v.Weight).First();

                    if (market4 != null)
                    {
                        cHomeTotal = true;
                        int id2 = _hTeamTotalLines[key];
                        market4.Active = CheckActive(period, gameState, true);
                        Markets homeTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        homeTotalline.MarketList.Add(market4);
                        SendOddsMessage(homeTotalline, gameId, formattedMarketList, eventName);
                    }
                }

                //inactive
                Markets homeTotallineAltInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "HTTL"))
                };

                if (homeTotallineAltInactive.MarketList.Count > 0 && homeTotallineAlt.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(homeTotallineAltInactive), gameId, eventName);
                    Market market4 = homeTotallineAltInactive.MarketList.OrderBy(v => v.Weight).First();

                    if (market4 != null)
                    {
                        int id2 = _hTeamTotalLines[key];
                        market4.Active = false;
                        Markets homeTotalline = new Markets
                        {
                            Game   = gameId.ToString(),
                            Period = period,
                            Id     = id2,
                            Name   = _marketList[id2]
                        };

                        homeTotalline.MarketList.Add(market4);
                        if (!cHomeTotal)
                        {
                            SendOddsMessage2(homeTotalline, gameId, eventName);
                        }
                    }
                }

                //------------------------to do send center line-----------------------------------------------

                id = _totalLines3W[key];

                Markets totalline3W = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = _totalLines3W[key],
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectInsideLimitsWithNonZeroTarget(s, "TL3W"))
                };

                if (totalline3W.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(totalline3W, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList, eventName);
                }


                Markets totalline3WInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = _totalLines3W[key],
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectOutsideLimitsWithNonZeroTarget(s, "TL3W"))
                };

                if (totalline3WInactive.MarketList.Count > 0)
                {
                    SendOddsMessage2(SetMarketInactive(totalline3WInactive), gameId, eventName);
                }

                //------------------------------------------------------------------------

                id = _altRunLines3W[key];

                Markets handicap3W = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectInsideLimitsWithoutTarget(s, "SP3W"))
                };

                if (handicap3W.MarketList.Count > 0)
                {
                    Markets activeMarkets = SetMarketActiveNew(handicap3W, true, gameState);
                    SendOddsMessage(activeMarkets, gameId, formattedMarketList, eventName);
                }

                Markets handicap3WInactive = new Markets
                {
                    Game       = gameId.ToString(),
                    Period     = period,
                    Id         = id,
                    Name       = _marketList[id],
                    MarketList = marketList.FindAll(s => SelectOutsideLimitsWithoutTarget(s, "SP3W"))
                };

                if (handicap3WInactive.MarketList.Count > 0 && handicap3W.MarketList.Count == 0)
                {
                    SendOddsMessage2(SetMarketInactive(handicap3WInactive), gameId, eventName);
                }
            }

            FormattedGame formattedGame = new FormattedGame
            {
                Id         = gameId.ToString(),
                Match      = match,
                MarketList = formattedMarketList
            };

            string formattedGameJsonString = JsonConvert.SerializeObject(formattedGame);

            _datastore.AddFinishedOdds(gameId, formattedGameJsonString);
        }