using SuccessfulCode.BetfairApi; using SuccessfulCode.BetfairApi.BettingModels; using SwiftyMessageHub.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace AutoBet.DataRecorded.MessageControllers { public class OddsDataCollectorController : MessageController { [MessageRoute] public async Task GenerateSnapshot(OddsSnapshotDetails details) { var auth = new Authentication("TheRocker1987", "reSharper1036!", "H5TT4zKqOlRCpNOr"); var client = new BettingApiClient(auth); var horseRacingQueries = new BetfairHorseRacingQueries(client); var horseWins = await horseRacingQueries.GetHorseWinCatalogue(DateTime.UtcNow.AddHours(-2), DateTime.UtcNow); var marketDetails = horseWins.ToDictionary(e => e.MarketId, e => e); var horseDetails = horseWins.SelectMany(e => e.Runners.Select(e1 => new { Key = (e.MarketId, e1.SelectionId), Value = e1 })).ToDictionary(e => e.Key, e => e.Value); var prices = await horseRacingQueries.GetBestPrices(horseWins.Select(e => e.MarketId).ToArray()); var snapShots = prices.SelectMany(e => e.Runners.Select(e1 => GenerateOddsSnapshots(marketDetails, horseDetails, e1, e))); foreach (var snapshot in snapShots) { await SendResponse(snapshot); } } private static Db.OddsSnapshot GenerateOddsSnapshots(Dictionary marketDetails, Dictionary<(string MarketId, long SelectionId), RunnerCatalog> horseDetails, Runner runner, MarketBook book) { var marketDetail = marketDetails[book.MarketId]; var horseDetail = horseDetails[(marketDetail.MarketId, runner.SelectionId)]; var runnerName = Regex.Match(horseDetail.RunnerName, "^[0-9]*\\.? ?(.*)", RegexOptions.Compiled).Groups[1].Value; var oddsSnapshot = new Db.OddsSnapshot() { EventName = marketDetail.Event.Name, HorseName = runnerName, JockeyName = horseDetail.Metadata.JockeyName, Odds = GenerateOdds(runner), SnapshotDate = DateTime.UtcNow }; return oddsSnapshot; } private static List GenerateOdds(Runner e1) { var backOdds = e1.ExchangePrices.AvailableToBack.Select(e => new Db.BetOdds() { OddsType = Db.OddsType.Back, Available = (decimal)e.Size, Odds = e.Price }).ToList(); var layOdds = e1.ExchangePrices.AvailableToBack.Select(e => new Db.BetOdds() { OddsType = Db.OddsType.Lay, Available = (decimal)e.Size, Odds = e.Price }).ToList(); backOdds.AddRange(layOdds); return backOdds; } } }