コード例 #1
0
        protected override void HandleEvent(MatchUpdate matchUpdate, ulong deliveryTag)
        {
            if (matchUpdate.IsFinished)
            {
                return;
            }

            _logger.LogInformation($"Received matchUpdate update for {matchUpdate.Id}, calculating stakes.".AddTimestamp());

            using (var dbContext = new BetsDbContext(_configuration.ConnectionString))
            {
                using (var transaction = dbContext.Database.BeginTransaction(IsolationLevel.RepeatableRead))
                {
                    var stakes    = dbContext.Stakes.Where(s => s.MatchId == matchUpdate.Id && s.IsBettable).ToList();
                    var newStakes = stakes.Select(s => CalculateNewStake(s, matchUpdate));
                    dbContext.Stakes.AddRange(newStakes);

                    var match = dbContext.Matches.Single(m => m.Id == matchUpdate.Id);
                    match.BlueScore = matchUpdate.BlueScore;
                    match.RedScore  = matchUpdate.RedScore;

                    dbContext.SaveChanges();
                    transaction.Commit();
                }
            }
        }
コード例 #2
0
ファイル: NewBetWorker.cs プロジェクト: V0ldek/LeagueOfBets
        private async Task ProcessBatchesAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"Sleeping for {_configuration.BatchFrequencyInMilliseconds} ms.".AddTimestamp());
                await Task.Delay(_configuration.BatchFrequencyInMilliseconds, cancellationToken);

                List <Bet> batch;
                ulong      deliveryTag;
                lock (_betsBatch)
                {
                    batch       = _betsBatch;
                    _betsBatch  = new List <Bet>();
                    deliveryTag = _lastDeliveryTag;
                }

                if (!batch.Any())
                {
                    _logger.LogInformation("No batch to process.".AddTimestamp());
                    continue;
                }

                _logger.LogInformation($"Processing batch of {batch.Count} bets.".AddTimestamp());

                var stakeIds       = batch.Select(b => b.StakeId);
                var betsByStakeIds = batch.GroupBy(b => b.StakeId).ToDictionary(g => g.Key, g => g.Count());

                using (var dbContext = new BetsDbContext(_configuration.ConnectionString))
                    using (var transaction = await dbContext.Database.BeginTransactionAsync(IsolationLevel.RepeatableRead, cancellationToken))
                    {
                        var matches = await dbContext.Matches.Include(m => m.Stakes).Where(m => m.Stakes.Any(s => stakeIds.Contains(s.Id)))
                                      .ToListAsync(cancellationToken);

                        _logger.LogInformation($"Got {matches.Count} matches".AddTimestamp());

                        var newStakes = matches.SelectMany(m => m.Stakes.Where(s => s.IsBettable).Select(s => CalculateNewStake(s, m, betsByStakeIds))).ToList();

                        foreach (var oldStake in matches.SelectMany(m => m.Stakes.Where(s => s.IsBettable)))
                        {
                            oldStake.IsBettable = false;
                        }
                        _logger.LogInformation("Calculated stakes.");
                        dbContext.Stakes.AddRange(newStakes);
                        await dbContext.SaveChangesAsync(cancellationToken);

                        transaction.Commit();
                    }

                EventConsumer.Ack(deliveryTag);
            }
        }
コード例 #3
0
ファイル: NewMatchWorker.cs プロジェクト: V0ldek/LeagueOfBets
        protected override void HandleEvent(Match match, ulong deliveryTag)
        {
            var stakes = new List <Stake>();

            for (var losersScore = 0; losersScore < match.BestOf; ++losersScore)
            {
                stakes.Add(
                    new Stake
                {
                    BlueScore  = match.BestOf,
                    RedScore   = losersScore,
                    MatchId    = match.Id,
                    Ratio      = _configuration.DefaultRatio,
                    IsBettable = true
                });
                stakes.Add(
                    new Stake
                {
                    BlueScore  = losersScore,
                    RedScore   = match.BestOf,
                    MatchId    = match.Id,
                    Ratio      = _configuration.DefaultRatio,
                    IsBettable = true
                });
            }

            _logger.LogInformation($"Received new match {match.Id}, adding stakes.".AddTimestamp());

            using (var dbContext = new BetsDbContext(_configuration.ConnectionString))
            {
                dbContext.Matches.Add(match);
                dbContext.Stakes.AddRange(stakes);

                dbContext.SaveChanges();
            }
        }
コード例 #4
0
 public StakeController(BetsDbContext betsDbContext)
 {
     _betsDbContext = betsDbContext;
 }
コード例 #5
0
 public AccountController(BetsDbContext betsDbContext, ILogger <AccountController> logger)
 {
     _betsDbContext = betsDbContext;
     _logger        = logger;
 }
コード例 #6
0
ファイル: BetController.cs プロジェクト: V0ldek/LeagueOfBets
 public BetController(BetsDbContext betsDbContext, ILogger <BetController> logger, IBetEventProducer betEventProducer)
 {
     _betsDbContext    = betsDbContext;
     _betEventProducer = betEventProducer;
     _logger           = logger;
 }