public async void UpdateWinnerSuccess()
        {
            var entity = new DatabaseModel.CoinFlip
            {
                Created       = DateTime.Now,
                CreatorUserId = _setup.User1.Id,
                CreatorIsHead = true,
                GameModeId    = _setup.GameMode.Id,
                Hash          = "HASh",
                Percentage    = 10.654.ToString(CultureInfo.InvariantCulture),
                RoundId       = Guid.NewGuid().ToString(),
                Salt          = "SALT",
                SettingId     = 0,
                Status        = 0,
                TimerStarted  = null,
                WinnerId      = null,
            };

            using (var trans = new TransactionWrapperWrapper(_setup.Database))
            {
                var insertedCoinFlip = await _coinFlipMatchRepoService.InsertAsync(entity, trans);

                insertedCoinFlip.WinnerId = _setup.User1.Id;
                trans.Commit();

                await _coinFlipMatchRepoService.UpdateAsync(insertedCoinFlip);

                var findEntiry = await _coinFlipMatchRepoService.FindAsync(entity.Id);

                Assert.Equal(_setup.User1.Id, findEntiry.WinnerId);
            }
        }
 public async Task UpdateAsync(DatabaseModel.CoinFlip coinflip)
 {
     using (var cn = _connection.GetNewOpenConnection())
     {
         await cn.UpdateAsync(coinflip);
     }
 }
        public async void InsertFailsAndRollBackIsDoneSuccess()
        {
            var entity = new DatabaseModel.CoinFlip
            {
                Created       = DateTime.Now,
                CreatorUserId = _setup.User1.Id,
                CreatorIsHead = true,
                GameModeId    = _setup.GameMode.Id,
                Hash          = "HASh",
                Percentage    = 10.654.ToString(CultureInfo.InvariantCulture),
                RoundId       = Guid.NewGuid().ToString(),
                Salt          = "SALT",
                SettingId     = 0,
                Status        = 0,
                TimerStarted  = null,
                WinnerId      = null,
            };

            using (var trans = new TransactionWrapperWrapper(_setup.Database))
            {
                Assert.True(entity.IsNew);
                var insertedCoinFlip = await _coinFlipMatchRepoService.InsertAsync(entity, trans);

                Assert.False(entity.IsNew);
                Assert.True(insertedCoinFlip.Id > 0);
                trans.Rollback();
                var res = await _coinFlipMatchRepoService.FindAsync(insertedCoinFlip.Id);

                Assert.Equal(null, res);
            }
        }
        public async Task SetWinnerTicketForMatch(DatabaseModel.CoinFlip match, int ticket)
        {
            var findFilter   = GetFindFilter().Where(m => m.LookUpId == match.Id);
            var updateFilter = GetUpdateFilter().Set(m => m.WinningTicket, ticket);

            await GetCollection().UpdateOneAsync(findFilter, updateFilter);
        }
 public async Task RemoveAsync(DatabaseModel.CoinFlip coinFlip)
 {
     using (var cn = _connection.GetNewOpenConnection())
     {
         await cn.ExecuteAsync("DELETE FROM CoinFlip WHERE Id = @Id", new { Id = coinFlip.Id });
     }
 }
        public async Task SetWinnerForMatch(DatabaseModel.CoinFlip match, UserWithQuote user)
        {
            var findFilter   = GetFindFilter().Where(m => m.LookUpId == match.Id);
            var updateFilter = GetUpdateFilter().Set(m => m.Winner, user);

            await GetCollection().UpdateOneAsync(findFilter, updateFilter);
        }
        public async Task UpdateTimmerStartedForMatch(DatabaseModel.CoinFlip match, DateTime timer)
        {
            var findFilter   = GetFindFilter().Where(m => m.LookUpId == match.Id);
            var updateFilter = GetUpdateFilter().Set(m => m.TimerStarted, timer);

            await GetCollection().UpdateOneAsync(findFilter, updateFilter);
        }
        public async Task UpdateStatusForMatch(DatabaseModel.CoinFlip match, MatchStatus status)
        {
            var findFilter   = GetFindFilter().Where(m => m.LookUpId == match.Id);
            var updateFilter = GetUpdateFilter().Set(m => m.Status, (int)status);

            await GetCollection().UpdateOneAsync(findFilter, updateFilter);
        }
Beispiel #9
0
        private DatabaseModel.CoinFlip CreateCoinFlipMatch
        (
            bool isHead,
            DatabaseModel.User creatorUser,
            DatabaseModel.GameMode gameMode,
            MongoDbModels.PreHash preHash
        )
        {
            var percentage = preHash.Percentage;
            var salt       = preHash.Salt;
            var hash       = preHash.Hash;

            var roundId = _randomService.GenerateNewGuidAsString();

            var match = new DatabaseModel.CoinFlip
            {
                Created       = DateTime.Now,
                CreatorUserId = creatorUser.Id,
                CreatorIsHead = isHead,
                Hash          = hash,
                Percentage    = percentage,
                Salt          = salt,
                RoundId       = roundId,
                Status        = (int)MatchStatus.Open,
                WinnerId      = null,
                TimerStarted  = null,
                SettingId     = gameMode.CurrentSettingId,
                GameModeId    = gameMode.Id,
            };

            return(match);
        }
        public async Task AddBetToMatch(DatabaseModel.CoinFlip match, CoinFlipBet bet)
        {
            var findFilter   = GetFindFilter().Where(m => m.LookUpId == match.Id);
            var updateFilter = GetUpdateFilter().Push(m => m.Bets, bet);

            await GetCollection().UpdateOneAsync(findFilter, updateFilter);
        }
        public async Task <DatabaseModel.CoinFlip> InsertAsync(DatabaseModel.CoinFlip coinFlip, ITransactionWrapper transactionWrapper)
        {
            var cn          = transactionWrapper.SqlConnection;
            var transaction = transactionWrapper.Transaction;

            var insertedId = await cn.InsertAsync(coinFlip, transaction);

            coinFlip.Id = insertedId;
            return(coinFlip);
        }
Beispiel #12
0
 public async Task PlaceBetOnCoinFlipMatch
 (
     DatabaseModel.CoinFlip match,
     JackpotMatchSetting setting,
     DatabaseModel.GameMode gameMode,
     List <AssetAndDescriptionId> assetAndDescriptionIds,
     string userSteamId
 )
 {
     await PlaceBetOnMatch(match.Id, setting, gameMode, assetAndDescriptionIds, userSteamId);
 }
Beispiel #13
0
 public async Task PlaceBetOnCoinFlipMatch
 (
     DatabaseModel.CoinFlip match,
     JackpotMatchSetting setting,
     DatabaseModel.GameMode gameMode,
     int assetAndDescriptionIdsCount,
     List <DatabaseModel.Item> items,
     DatabaseModel.User user,
     List <DatabaseModel.ItemDescription> uniqueItemDescriptions
 )
 {
     await PlaceBetOnMatch(match.Id, setting, gameMode, assetAndDescriptionIdsCount, items, user, uniqueItemDescriptions);
 }
Beispiel #14
0
        private async Task <DatabaseModel.CoinFlip> InsertedMatchAndBetWithTransaction
        (
            List <AssetAndDescriptionId> assetAndDescriptionIds,
            DatabaseModel.CoinFlip match,
            DatabaseModel.JackpotSetting jackpotSetting,
            DatabaseModel.GameMode gameMode,
            List <DatabaseModel.Item> items,
            DatabaseModel.User user,
            List <DatabaseModel.ItemDescription> itemDescription,
            JackpotMatchSetting jackpotMatchSetting
        )
        {
            DatabaseModel.CoinFlip       insertedMatch;
            DatabaseModel.JackpotSetting insertedJackpotSetting;

            using (var transaction = _transactionFactory.BeginTransaction())
            {
                try
                {
                    insertedJackpotSetting = await _repoServiceFactory.JackpotSettingRepo.InsertAsync(jackpotSetting, transaction);

                    match.SettingId = insertedJackpotSetting.Id;
                    insertedMatch   = await _repoServiceFactory.CoinFlipMatchRepoService.InsertAsync(match, transaction);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }

                transaction.Commit();
            }

            try
            {
                var count = assetAndDescriptionIds.Count;
                await _betService.PlaceBetOnCoinFlipMatch(insertedMatch, jackpotMatchSetting, gameMode, count, items, user, itemDescription);
            }
            catch (Exception)
            {
                await _repoServiceFactory.CoinFlipMatchRepoService.RemoveAsync(insertedMatch);

                await _repoServiceFactory.JackpotSettingRepo.RemoveAsync(insertedJackpotSetting);

                throw;
            }

            return(insertedMatch);
        }
Beispiel #15
0
        private async Task <MongoDbModels.JackpotMatch> InsertIntoMongoDb
        (
            DatabaseModel.CoinFlip insertedMatch,
            List <DatabaseModel.Item> items,
            List <DatabaseModel.ItemDescription> itemDescription,
            JackpotMatchSetting jackpotMatchSetting,
            DatabaseModel.User user
        )
        {
            var mapedItems         = items.MapItemsToItemsDescription(itemDescription);
            var coinFlipMatchModel = new MongoDbModels.JackpotMatch
            {
                LookUpId     = insertedMatch.Id,
                RoundId      = insertedMatch.RoundId,
                Hash         = insertedMatch.Hash,
                Salt         = insertedMatch.Salt,
                Percentage   = insertedMatch.Percentage,
                Status       = insertedMatch.Status,
                TimerStarted = insertedMatch.TimerStarted,
                Bets         = new List <CoinFlipBet>
                {
                    new CoinFlipBet
                    {
                        User = new User
                        {
                            ImageUrl = user.ImageUrl,
                            Name     = user.Name,
                            SteamId  = user.SteamId,
                        },
                        DateTime = DateTime.Now,
                        Items    = mapedItems,
                        Tickets  = new RoundTicket
                        {
                            Start = 1,
                            End   = (int)(mapedItems.Sum(item => item.Value) * 100)
                        },
                        IsHead = insertedMatch.CreatorIsHead
                    }
                },
                Setting = jackpotMatchSetting
            };

            await _mongoJackpotRepoService.InsertAsync(coinFlipMatchModel);

            return(coinFlipMatchModel);
        }
Beispiel #16
0
        private async Task DraftWinner(DatabaseModel.CoinFlip match, DatabaseModel.JackpotSetting matchSetting, List <DatabaseModel.Bet> betsOnMatch)
        {
            var itemBetsOnMatch = await _repoServiceFactory.ItemBettedRepoService.FindAsync(betsOnMatch);

            var percentage  = Convert.ToDouble(match.Percentage, CultureInfo.InvariantCulture);
            var winningBet  = _draftService.GetWinningBet(percentage, betsOnMatch, itemBetsOnMatch);
            var winningUser = await _repoServiceFactory.UserRepoService.FindAsync(winningBet.Bet.UserId);

            match.WinnerId = winningUser.Id;
            match.Status   = (int)MatchStatus.Drafting;
            await _repoServiceFactory.CoinFlipMatchRepoService.UpdateAsync(match);

            await _jackpotRepoService.UpdateStatusForMatch(match, MatchStatus.Drafting);

            await _jackpotRepoService.SetWinnerForMatch(match, new UserWithQuote
            {
                ImageUrl = winningUser.ImageUrl,
                Name     = winningUser.Name,
                SteamId  = winningUser.SteamId,
                Quote    = winningUser.Quote
            });

            await _jackpotRepoService.SetWinnerTicketForMatch(match, winningBet.WinningTicket + 1);

            var potValue = itemBetsOnMatch.Sum(itemBet => itemBet.Value);

            await _coinFlipHubConnections.MatchDrafting(new CoinFlipWinnerSelected
            {
                Winner = new UserWithQuote
                {
                    ImageUrl = winningUser.ImageUrl,
                    Name     = winningUser.Name,
                    SteamId  = winningUser.SteamId,
                    Quote    = winningUser.Quote,
                },
                Percentage    = match.Percentage,
                PotValue      = potValue,
                RoundId       = match.RoundId,
                WinningTicket = winningBet.WinningTicket + 1,
                Salt          = match.Salt,
            });

            await _draftService.ChangeOwnerOfItems(betsOnMatch, itemBetsOnMatch, winningUser, match.Id, matchSetting.Rake, _currentGamemode.Id);

            _discordService.CoinFlipWinnerAsync(match.RoundId, potValue);
        }
Beispiel #17
0
 public async Task MatchClosed(DatabaseModel.CoinFlip match)
 {
     await SendToAll(match.RoundId, "onMatchDone");
 }
Beispiel #18
0
        private async Task SendWebSocketEventPlayerJoinedAndInsertCoinFlipBetToMongoDb
        (
            DatabaseModel.CoinFlip coinFlip,
            DatabaseModel.JackpotSetting currentMatchSetting,
            List <DatabaseModel.Bet> bets
        )
        {
            var creatorBet        = bets.First(bet => bet.UserId == coinFlip.CreatorUserId);
            var creatorItemBetted = await _repoServiceFactory.ItemBettedRepoService.FindAsync(creatorBet);

            var creatorValueBet = creatorItemBetted.Sum(betted => betted.Value);

            var newBet     = bets.First(bet => bet.UserId != coinFlip.CreatorUserId);
            var itemBetted = await _repoServiceFactory.ItemBettedRepoService.FindAsync(newBet);

            var valueBet = itemBetted.Sum(betted => betted.Value);

            var dbUser = await _repoServiceFactory.UserRepoService.FindAsync(newBet.UserId);

            var user = new User
            {
                ImageUrl = dbUser.ImageUrl,
                Name     = dbUser.Name,
                SteamId  = dbUser.SteamId
            };

            var unquieItemDescriptions = itemBetted.Select(item => item.DescriptionId).Distinct().ToList();

            var itemDescriptions = await _repoServiceFactory.ItemDescriptionRepoService.FindAsync(unquieItemDescriptions);

            var items = new List <Item>();

            foreach (var itemDescription in itemDescriptions)
            {
                foreach (var betted in itemBetted)
                {
                    if (betted.DescriptionId != itemDescription.Id)
                    {
                        continue;
                    }

                    items.Add(new Item
                    {
                        AssetId       = betted.AssetId,
                        DescriptionId = itemDescription.Id,
                        IconUrl       = itemDescription.ImageUrl,
                        Name          = itemDescription.Name,
                        Value         = itemDescription.Value,
                        TookAsRake    = false,
                        Owner         = user,
                    });
                }
            }

            var coinflipBet = new CoinFlipBet
            {
                IsHead   = !coinFlip.CreatorIsHead,
                Items    = items,
                User     = user,
                DateTime = newBet.Created,
                Tickets  = new RoundTicket
                {
                    Start = ((int)(creatorValueBet * 100)) + 1,
                    End   = (int)((valueBet + creatorValueBet) * 100)
                }
            };

            var websocketData = new TimerStartedWebsocketModel
            {
                RoundId = coinFlip.RoundId,
                Bet     = coinflipBet,
                DraftingTimeInMilliSec = currentMatchSetting.DraftingTimeInMilliSec
            };
            await _jackpotRepoService.AddBetToMatch(coinFlip, coinflipBet);

            await _coinFlipHubConnections.MatchTimmerStarted(websocketData);

            _discordService.CoinFlipJoinAsync(creatorValueBet, coinFlip.RoundId, user.SteamId);
        }
Beispiel #19
0
        private bool IsTimerDone(DatabaseModel.JackpotSetting currentMatchSetting, DatabaseModel.CoinFlip coinFlip)
        {
            var timeSpan = TimeSpan.FromMilliseconds(currentMatchSetting.TimmerInMilliSec);

            return((DateTime.Now - (coinFlip.TimerStarted.Value + timeSpan)).TotalMilliseconds > 0);
        }
Beispiel #20
0
        private bool IsDraftingDone(DatabaseModel.JackpotSetting currentMatchSetting, DatabaseModel.CoinFlip coinFlip)
        {
            var countdownTimeSpan = TimeSpan.FromMilliseconds(currentMatchSetting.TimmerInMilliSec);
            var draftingTimeSpan  = TimeSpan.FromMilliseconds(currentMatchSetting.DraftingTimeInMilliSec);

            return((DateTime.Now - (coinFlip.TimerStarted.Value + countdownTimeSpan + draftingTimeSpan)).TotalMilliseconds > 0);
        }