public async Task InitTest() { var itemDesc1 = new DatabaseModel.ItemDescription("item1", new decimal(10.20), "730", "2", "imageUrl", true); var itemDesc2 = new DatabaseModel.ItemDescription("item2", new decimal(11.40), "730", "2", "imageUrl", true); ItemDescription1 = await new ItemDescriptionRepoService(DatabaseConnectionFactory, new ItemDescriptionQueries()).InsertAsync(itemDesc1); ItemDescription2 = await new ItemDescriptionRepoService(DatabaseConnectionFactory, new ItemDescriptionQueries()).InsertAsync(itemDesc2); var gameMode = new DatabaseModel.GameMode("Jackpot", 1); var gameMode2 = new DatabaseModel.GameMode("Jackpot1", 2); var gameMode3 = new DatabaseModel.GameMode("Jackpot2", 3); var gameMode4 = new DatabaseModel.GameMode("Jackpot3", 4); GameMode = await new GameModeRepoService(DatabaseConnectionFactory).Insert(gameMode); GameMode2 = await new GameModeRepoService(DatabaseConnectionFactory).Insert(gameMode2); GameMode3 = await new GameModeRepoService(DatabaseConnectionFactory).Insert(gameMode3); GameMode4 = await new GameModeRepoService(DatabaseConnectionFactory).Insert(gameMode4); var bot = new DatabaseModel.Bot(",anotherBot", "Bot 2"); Bot1 = await new BotRepoService(DatabaseConnectionFactory, new BotQueries()).InsertAsync(bot); var match = new DatabaseModel.Match(1, "salt", "hash", 55.ToString(CultureInfo.InvariantCulture), 1, null, null, 1, GameMode.Id, DateTime.Now); Match1 = await new MatchRepoService(DatabaseConnectionFactory, new MatchQueries()).InsertAsync(match); var match1 = new DatabaseModel.Match(2, "salt", "hash", 55.ToString(CultureInfo.InvariantCulture), 1, null, null, 1, GameMode.Id, DateTime.Now); Match2 = await new MatchRepoService(DatabaseConnectionFactory, new MatchQueries()).InsertAsync(match1); }
public async Task CreateMatchFailDueToNoItemsPlaced() { var percentage = 12.4587; var percentageAsString = percentage.ToString(CultureInfo.InvariantCulture); var salt = "randomSalt"; var hash = "ranomdHash"; var creatorUser = new DatabaseModel.User("steamId", "name", "imageURl", null, DateTime.Today, DateTime.Today, false, null, 10); var gameMode = new DatabaseModel.GameMode { CurrentSettingId = 1, IsEnabled = true, Type = GameModeHelper.GetStringFromType(GameModeType.CoinFlip), Id = 2 }; A.CallTo(() => _fakedRandomService.GeneratePercentage()).Returns(percentageAsString); A.CallTo(() => _fakedRandomService.GenerateSalt()).Returns(salt); A.CallTo(() => _fakedHashService.CreateBase64Sha512Hash(percentageAsString, salt)).Returns(hash); A.CallTo(() => _fakedUserRepoService.FindAsync(creatorUser.SteamId)).Returns(creatorUser); A.CallTo(() => _fakedGameModeRepoSerivce.Find(GameModeHelper.GetStringFromType(GameModeType.CoinFlip))).Returns(gameMode); var itemList = new List <AssetAndDescriptionId>(); await Assert.ThrowsAsync <InvalidBetException>(async() => await _coinFlipService.CreateMatch(creatorUser.SteamId, true, itemList, _defaultSetting)); A.CallTo(() => _fakedCoinFlipMatchRepoService.InsertAsync(A <DatabaseModel.CoinFlip> ._, A <ITransactionWrapper> ._)).MustNotHaveHappened(); A.CallTo(() => _fakedUserRepoService.FindAsync(creatorUser.SteamId)).MustHaveHappened(); }
protected override async Task InitTest() { await base.InitTest(); User1 = await new UserRepoService(FakedFactory, new UserQueries()).InsertAsync("randomSteamId", "randomName", "imageUr"); GameMode = await new GameModeRepoService(FakedFactory).Insert(new DatabaseModel.GameMode("CoinFlip", 1)); }
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 CreateMatchRollBackWhenPlaceBetFailsSuccess() { var percentage = 12.4587; var percentageAsString = percentage.ToString(CultureInfo.InvariantCulture); var salt = "randomSalt"; var hash = "ranomdHash"; var creatorUser = new DatabaseModel.User("steamId", "name", "imageURl", null, DateTime.Today, DateTime.Today, false, null, 10); var gameMode = new DatabaseModel.GameMode { CurrentSettingId = 1, IsEnabled = true, Type = GameModeHelper.GetStringFromType(GameModeType.CoinFlip), Id = 2 }; A.CallTo(() => _fakedUserRepoService.FindAsync(creatorUser.SteamId)).Returns(creatorUser); A.CallTo(() => _fakedRandomService.GeneratePercentage()).Returns(percentageAsString); A.CallTo(() => _fakedRandomService.GenerateSalt()).Returns(salt); A.CallTo(() => _fakedHashService.CreateBase64Sha512Hash(percentageAsString, salt)).Returns(hash); A.CallTo(() => _fakedGameModeRepoSerivce.Find(GameModeHelper.GetStringFromType(GameModeType.CoinFlip))).Returns(gameMode); A.CallTo(() => _fakedBetService.PlaceBetOnCoinFlipMatch ( A <DatabaseModel.CoinFlip> ._, A <JackpotMatchSetting> ._, A <DatabaseModel.GameMode> ._, A <int> ._, A <List <DatabaseModel.Item> > ._, A <DatabaseModel.User> ._, A <List <DatabaseModel.ItemDescription> > ._ )).Throws <Exception>(); var itemList = new List <AssetAndDescriptionId> { new AssetAndDescriptionId { AssetId = "assetId1", DescriptionId = 1 } }; var didFail = false; try { await _coinFlipService.CreateMatch(creatorUser.SteamId, true, itemList, _defaultSetting); } catch (Exception) { didFail = true; } Assert.True(didFail, "The method did not throw!"); A.CallTo(() => _fakedCoinFlipMatchRepoService.RemoveAsync(A <DatabaseModel.CoinFlip> ._)).MustHaveHappened(); A.CallTo(() => _fakedJackpotSettingRepoService.RemoveAsync(A <DatabaseModel.JackpotSetting> ._)).MustHaveHappened(); A.CallTo(() => _fakedUserRepoService.FindAsync(creatorUser.SteamId)).MustHaveHappened(); }
protected override async Task InitTest() { await base.InitTest(); var gameModeRepoService = new GameModeRepoService(FakedFactory); GameMode = new DatabaseModel.GameMode("GameMode1", 1); await gameModeRepoService.Insert(GameMode); }
public async Task <DatabaseModel.GameMode> Insert(DatabaseModel.GameMode mode) { using (var cn = DatabaseConnection.GetNewOpenConnection()) { await cn.InsertAsync(mode); cn.Close(); } return(mode); }
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); }
private async Task SetUpGameModeAndSetting() { var currentMatch = await _repoServiceFactory.MatchRepoService.GetCurrentMatch(); if (currentMatch != null) //there is no matches, this is the first time we are starting the server. { _currentMatchSettings = (DatabaseModel.JackpotSetting) await _gameModeSettingService.Find(currentMatch.SettingId, GameModeType.JackpotCsgo); } _gameMode = await _repoServiceFactory.GameModeRepoService.Find(GameModeHelper.GetStringFromType(GameModeType.JackpotCsgo)); }
protected async Task <List <Bet> > GetBets(List <Item> items, int matchId, DatabaseModel.GameMode gameMode) { var uniqueUsers = UniqueUsers(items); var databaseBets = await _repoServiceFactory.BetRepoService.FindAsync(matchId, gameMode.Id); var databaseUsers = await _repoServiceFactory.UserRepoService.FindAsync(uniqueUsers.Select(u => u.SteamId).ToList()); var bets = uniqueUsers.Select(user => new Bet { Items = items.Where(item => item.Owner.SteamId == user.SteamId).ToList(), User = user, }) .ToList(); foreach (var databaseUser in databaseUsers) { foreach (var databaseBet in databaseBets) { if (databaseBet.UserId != databaseUser.Id) { continue; } foreach (var bet in bets) { if (bet.User.SteamId != databaseUser.SteamId) { continue; } bet.DateTime = databaseBet.Created; } } } bets = bets.OrderBy(o => o.DateTime).ToList(); decimal totalValue = 0; foreach (var bet in bets) { var betValue = bet.Items.Sum(a => a.Value); var ticket = new RoundTicket(); ticket.Start = ((int)(totalValue * 100)) + 1; ticket.End = (int)((betValue + totalValue) * 100); bet.Tickets = ticket; totalValue += betValue; } return(bets); }
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); }
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); }
private async Task PlaceBetOnMatch(int matchId, JackpotMatchSetting setting, DatabaseModel.GameMode gameMode, List <AssetAndDescriptionId> assetAndDescriptionIds, string userSteamId) { var itemDescs = assetAndDescriptionIds.Select(item => item.DescriptionId).Distinct().ToList(); var itemsTask = _repoServiceFactory.ItemRepoService.FindAsync(assetAndDescriptionIds); var userTask = _repoServiceFactory.UserRepoService.FindAsync(userSteamId); var itemDescriptionTask = _repoServiceFactory.ItemDescriptionRepoService.FindAsync(itemDescs); await Task.WhenAll(itemsTask, userTask, itemDescriptionTask); var items = itemsTask.Result; var user = userTask.Result; var itemDescriptions = itemDescriptionTask.Result; await PlaceBetOnMatch(matchId, setting, gameMode, assetAndDescriptionIds.Count, items, user, itemDescriptions); }
private async void Tick() { while (_isRunning) { await Task.Delay(200); if (_isClosed) { continue; } _gameMode = await _repoServiceFactory.GameModeRepoService.Find(GameModeHelper.GetStringFromType(GameModeType.JackpotCsgo)); var match = await _repoServiceFactory.MatchRepoService.GetCurrentMatch(); var betsOnMatch = await _repoServiceFactory.BetRepoService.FindAsync(match); var itemBetsOnMatch = await _repoServiceFactory.ItemBettedRepoService.FindAsync(betsOnMatch); await ProcessQueueUntillTimeSpanIsMet(TimeSpan.Zero); if (ShouldStartTimmer(betsOnMatch, match)) { await StartTimmer(match.RoundId); } if (!ShouldCloseMatch(match, itemBetsOnMatch, _betQueue.Items.Count)) { continue; } _isClosed = true; await CloseMatch(match.RoundId); await ProcessQueueUntillDone(); await DraftWinner(match); await Task.Delay(_currentMatchSettings.DraftingTimeInMilliSec); await CreateNewMatchAsync(match.RoundId + 1); } }
public CoinFlipManagerTest() { _fakedRepoServiceFactory = A.Fake <IRepoServiceFactory>(); _fakedCoinFlipMatchRepoService = A.Fake <ICoinFlipMatchRepoService>(); _fakedBetRepoService = A.Fake <IBetRepoService>(); _fakedJackpotSettingRepo = A.Fake <IJackpotSettingRepo>(); _fakedGameModeRepoService = A.Fake <IGameModeRepoService>(); _fakedUserRepoService = A.Fake <IUserRepoService>(); _fakedCoinFlipService = A.Fake <ICoinFlipService>(); _fakedDraftingService = A.Fake <IJackpotDraftService>(); _gameMode = new DatabaseModel.GameMode { CurrentSettingId = 1, Id = 2, IsEnabled = true, Type = "CoinFlip" }; _coinFlipSocketSender = A.Dummy <ICoinFlipHubConnections>(); A.CallTo(() => _fakedRepoServiceFactory.UserRepoService).Returns(_fakedUserRepoService); A.CallTo(() => _fakedRepoServiceFactory.CoinFlipMatchRepoService).Returns(_fakedCoinFlipMatchRepoService); A.CallTo(() => _fakedRepoServiceFactory.BetRepoService).Returns(_fakedBetRepoService); A.CallTo(() => _fakedRepoServiceFactory.JackpotSettingRepo).Returns(_fakedJackpotSettingRepo); A.CallTo(() => _fakedRepoServiceFactory.GameModeRepoService).Returns(_fakedGameModeRepoService); A.CallTo(() => _fakedGameModeRepoService.Find(GameModeHelper.GetStringFromType(GameModeType.CoinFlip))).Returns(_gameMode); _dummyBetOrWithdrawQueueManager = A.Dummy <IBetOrWithdrawQueueManager>(); _dummyBetHubConnection = A.Dummy <IBetHubConnections>(); _dummyBetService = A.Dummy <IBetService>(); _dummyLogServiceFactory = A.Dummy <ILogServiceFactory>(); _dummyMongoJackpotRepoService = A.Dummy <IMongoJackpotRepoService>(); _dummyHotStatusManager = A.Dummy <IHotStatusManager>(); _dummyDiscordService = A.Dummy <IDiscordService>(); }
public async Task <DatabaseModel.Match> CreateNewMatchAsync(int roundId) { var settingRes = await _gameModeSettingService.GetSettingForType(GameModeType.JackpotCsgo); _currentMatchSettings = (DatabaseModel.JackpotSetting)settingRes ?? throw new NullReferenceException("The match setting is NULL!"); _gameMode = await _repoServiceFactory.GameModeRepoService.Find(GameModeHelper.GetStringFromType(GameModeType.JackpotCsgo)); var res = await base.CreateNewMatchAsync(roundId, _gameMode); var jackpotMatch = new JackpotMatch( roundId.ToString(), res.Hash, res.Salt, res.Percentage, MatchStatusHelper.GetMatchStatusFromInt(res.Status), new List <Bet>(), res.TimerStarted, new JackpotMatchSetting( _currentMatchSettings.Rake, TimeSpan.FromMilliseconds(_currentMatchSettings.TimmerInMilliSec), _currentMatchSettings.ItemsLimit, _currentMatchSettings.MaxItemAUserCanBet, _currentMatchSettings.MinItemAUserCanBet, _currentMatchSettings.MaxValueAUserCanBet, _currentMatchSettings.MinValueAUserCanBet, _currentMatchSettings.AllowCsgo, _currentMatchSettings.AllowPubg, TimeSpan.FromMilliseconds(_currentMatchSettings.DraftingTimeInMilliSec), _currentMatchSettings.DraftingGraph ) ); await _matchHubConnections.NewMatchCreated(jackpotMatch); _isClosed = false; return(res); }
private async Task InsertBetWithTransaction ( DatabaseModel.User user, int matchId, List <DatabaseModel.Item> items, DatabaseModel.GameMode gameMode, List <DatabaseModel.ItemDescription> itemDescriptions ) { List <DatabaseModel.ItemBetted> itemBets; DatabaseModel.Bet insertedBet; List <Item> itemModels; using (var transaction = _transactionFactory.BeginTransaction()) { try { var bet = new DatabaseModel.Bet(user.Id, matchId, gameMode.Id, DateTime.Now); insertedBet = await _betRepoService.InsertAsync(bet, transaction); itemBets = CreateItemBets(insertedBet.Id, itemDescriptions, items, out itemModels); await _itemBettedRepoService.InsertAsync(itemBets, transaction); transaction.Commit(); } catch (SqlException e) { throw new InvalidBetException("Can't inset bet due to sql error", e); } } if (gameMode.Type == GameModeHelper.GetStringFromType(GameModeType.JackpotCsgo)) { await SendWebsocketMatchEvent(user, matchId, itemModels, insertedBet); } }
protected async Task <DatabaseModel.Match> CreateNewMatchAsync(int roundId, DatabaseModel.GameMode currentGameMode) { var percentage = _randomService.GeneratePercentage(); var salt = _randomService.GenerateSalt(); var hash = _hashService.CreateBase64Sha512Hash(percentage, salt); var status = MatchStatusHelper.GetIntFromMatchStatus(MatchStatus.Open); var match = new DatabaseModel.Match ( roundId, salt, hash, percentage, status, null, null, currentGameMode.CurrentSettingId, currentGameMode.Id, DateTime.Now ); return(await _matchRepoService.InsertAsync(match)); }
protected async Task <JackpotMatch> GetMatchAsync(int id, DatabaseModel.JackpotSetting settings, DatabaseModel.GameMode gameMode) { var match = await _matchRepoService.FindAsync(id); return(await GetMatch(settings, match, gameMode)); }
private async void Tick() { while (_running) { await Task.Delay(200); _currentGamemode = await _repoServiceFactory.GameModeRepoService.Find(GameModeHelper.GetStringFromType(GameModeType.CoinFlip)); _hotStatusManager.RemoveExperiedHotStatuses(); var matches = await _repoServiceFactory.CoinFlipMatchRepoService.FindAllNotClosedMatches(); foreach (var coinFlip in matches) { var currentMatchSetting = await _repoServiceFactory.JackpotSettingRepo.Find(coinFlip.SettingId); var bets = await _repoServiceFactory.BetRepoService.FindAsync(coinFlip.Id, _currentGamemode.Id); if (bets.Count > 2) { var e = new Exception("BETS SHOULD NEVER BE MORE THEN 2!"); e.Data.Add("Match", coinFlip); throw e; } if (bets.Count == 1) { continue; // we are still wating for someone to join. } if (bets.Count == 0) { continue; // The create match and insert bet is done on two diffrent transactions sadly... } if (coinFlip.Status == (int)MatchStatus.Open) { var timer = DateTime.Now; coinFlip.Status = (int)MatchStatus.TimerCountdown; coinFlip.TimerStarted = timer; await _repoServiceFactory.CoinFlipMatchRepoService.UpdateAsync(coinFlip); await _jackpotRepoService.UpdateStatusForMatch(coinFlip, MatchStatus.TimerCountdown); await _jackpotRepoService.UpdateTimmerStartedForMatch(coinFlip, timer); await SendWebSocketEventPlayerJoinedAndInsertCoinFlipBetToMongoDb(coinFlip, currentMatchSetting, bets); continue; } if (!coinFlip.TimerStarted.HasValue) { var e = new Exception("Timmer should always have a value here!"); e.Data.Add("match", coinFlip); throw e; } if (coinFlip.Status == (int)MatchStatus.TimerCountdown) { if (!IsTimerDone(currentMatchSetting, coinFlip)) { continue; } //Timer is done, start drafting! await DraftWinner(coinFlip, currentMatchSetting, bets); continue; } if (coinFlip.Status == (int)MatchStatus.Drafting) { if (!IsDraftingDone(currentMatchSetting, coinFlip)) { continue; } coinFlip.Status = (int)MatchStatus.Closed; await _repoServiceFactory.CoinFlipMatchRepoService.UpdateAsync(coinFlip); await _jackpotRepoService.UpdateStatusForMatch(coinFlip, MatchStatus.Closed); await _coinFlipHubConnections.MatchClosed(coinFlip); continue; } } } _didStopGracefully = true; }
private async Task Setup() { _currentGamemode = await _repoServiceFactory.GameModeRepoService.Find(GameModeHelper.GetStringFromType(GameModeType.CoinFlip)); }
public async Task CreateMatchSuccess() { var percentage = 12.4587; var percentageAsString = percentage.ToString(CultureInfo.InvariantCulture); var salt = "randomSalt"; var hash = "ranomdHash"; var creatorUser = new DatabaseModel.User("steamId", "name", "imageURl", null, DateTime.Today, DateTime.Today, false, null, 10); var gameMode = new DatabaseModel.GameMode { CurrentSettingId = 1, IsEnabled = true, Type = GameModeHelper.GetStringFromType(GameModeType.CoinFlip), Id = 2 }; A.CallTo(() => _fakedMongoDbPreHashRepoService.Find(hash, creatorUser.SteamId)).Returns(new MongoDbModels.PreHash { Created = DateTime.Today, Hash = hash, Percentage = percentageAsString, Salt = salt, UserSteamId = creatorUser.SteamId }); A.CallTo(() => _fakedRandomService.GenerateNewGuidAsString()).Returns("GUID"); A.CallTo(() => _fakedJackpotSettingRepoService.InsertAsync(A <DatabaseModel.JackpotSetting> ._, A <ITransactionWrapper> ._)) .Returns(new DatabaseModel.JackpotSetting { Id = 1 }); A.CallTo(() => _fakedUserRepoService.FindAsync(creatorUser.SteamId)).Returns(creatorUser); A.CallTo(() => _fakedGameModeRepoSerivce.Find(GameModeHelper.GetStringFromType(GameModeType.CoinFlip))).Returns(gameMode); var itemList = new List <AssetAndDescriptionId> { new AssetAndDescriptionId { AssetId = "assetId1", DescriptionId = 1 } }; await _coinFlipService.CreateMatch(creatorUser.SteamId, true, itemList, _defaultSetting); A.CallTo(() => _fakedCoinFlipMatchRepoService.InsertAsync(A <DatabaseModel.CoinFlip> .That.Matches (m => m.Created > DateTime.Today && m.CreatorUserId == 10 && m.CreatorIsHead == true && m.Hash == hash && m.Salt == salt && m.Percentage == percentageAsString && m.RoundId == "GUID" && m.Status == (int)MatchStatus.Open && m.TimerStarted == null && m.WinnerId == null && m.GameModeId == 2 && m.SettingId == 1 ), A <ITransactionWrapper> ._ )).MustHaveHappened(); A.CallTo(() => _fakedMongoDbPreHashRepoService.Find(hash, creatorUser.SteamId)).MustHaveHappened(); A.CallTo(() => _fakedUserRepoService.FindAsync(creatorUser.SteamId)).MustHaveHappened(); A.CallTo(() => _fakedTransactionWrapper.Commit()).MustHaveHappened(); A.CallTo(() => _fakedBetService.PlaceBetOnCoinFlipMatch ( A <DatabaseModel.CoinFlip> ._, A <JackpotMatchSetting> ._, A <DatabaseModel.GameMode> ._, A <int> ._, A <List <DatabaseModel.Item> > ._, A <DatabaseModel.User> ._, A <List <DatabaseModel.ItemDescription> > ._ )).MustHaveHappened(); A.CallTo(() => _fakedMongoDbJackpotRepoService.InsertAsync(A <MongoDbModels.JackpotMatch> ._)).MustHaveHappened(); }
public async Task InitTest() { var userRepoService = new UserRepoService(DatabaseConnectionFactory, new UserQueries()); var matchRepoService = new MatchRepoService(DatabaseConnectionFactory, new MatchQueries()); var betRepoService = new BetRepoService(DatabaseConnectionFactory, new BetQueries()); var itemRepoService = new ItemRepoService(DatabaseConnectionFactory, new ItemQueries()); GameMode = new DatabaseModel.GameMode("Jackpot", 1); await new GameModeRepoService(DatabaseConnectionFactory).Insert(GameMode); User1 = await userRepoService.InsertAsync(new DatabaseModel.User( "987654321", "Kalle", " ba/bab154f01140ec39b2986d97838f0127534ec82d_full.jpg ", "?partner=117688384&token=mn347bmb ", DateTime.Now, DateTime.Now, false )); User2 = await userRepoService.InsertAsync(new DatabaseModel.User( "4477654321", "Kalle", " ba/bab154f01140ec39b2986d97838f0127534ec82d_full.jpg ", "?partner=117688384&token=mn347bmb ", DateTime.Now, DateTime.Now, false )); User3 = await userRepoService.InsertAsync(new DatabaseModel.User( "444784", "Kalle", " ba/bab154f01140ec39b2986d97838f0127534ec82d_full.jpg ", "?partner=117688384&token=mn347bmb ", DateTime.Now, DateTime.Now, false )); Match1 = await matchRepoService.InsertAsync(new DatabaseModel.Match( 11, "salt", "hash", 47.5484.ToString(CultureInfo.InvariantCulture), 1, null, null, 1, GameMode.Id, DateTime.Now )); Match2 = await matchRepoService.InsertAsync(new DatabaseModel.Match( 101, "salt", "hash", 47.5484.ToString(CultureInfo.InvariantCulture), 1, null, null, 1, GameMode.Id, DateTime.Now )); var model1 = new DatabaseModel.ItemDescription("itemDescription itembet 2", new decimal(13.37), "730", "2", "imageUrl", true); var model2 = new DatabaseModel.ItemDescription("itemDescription itembet 1", new decimal(13.37), "730", "2", "imageUrl", true); var itemDescriptionRepoService = new ItemDescriptionRepoService(DatabaseConnectionFactory, new ItemDescriptionQueries()); BotId = await new BotRepoService(DatabaseConnectionFactory, new BotQueries()).InsertAsync(new DatabaseModel.Bot("47489", "Bot ItemBet")); ItemDescription1 = await itemDescriptionRepoService.InsertAsync(model2); ItemDescription2 = await itemDescriptionRepoService.InsertAsync(model1); Item1 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 1", ItemDescription1.Id, BotId.Id, User1.Id, DateTimeOffset.Now)); Item2 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 2", ItemDescription1.Id, BotId.Id, User1.Id, DateTimeOffset.Now)); Item3 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 3", ItemDescription2.Id, BotId.Id, User1.Id, DateTimeOffset.Now)); Item4 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 4", ItemDescription1.Id, BotId.Id, User1.Id, DateTimeOffset.Now)); Item5 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 5", ItemDescription2.Id, BotId.Id, User1.Id, DateTimeOffset.Now)); Item6 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 6", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item7 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 7", ItemDescription2.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item8 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 8", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item9 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 9", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item10 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 10", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item11 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 11", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item12 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 12", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item13 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 13", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item14 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 14", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Item15 = await itemRepoService.InsertAsync(new DatabaseModel.Item("itemBet 15", ItemDescription1.Id, BotId.Id, User2.Id, DateTimeOffset.Now)); Bet1 = await betRepoService.InsertAsync(new DatabaseModel.Bet(User1.Id, Match1.Id, GameMode.Id, DateTime.Now)); Bet2 = await betRepoService.InsertAsync(new DatabaseModel.Bet(User2.Id, Match1.Id, GameMode.Id, DateTime.Now)); Bet3 = await betRepoService.InsertAsync(new DatabaseModel.Bet(User3.Id, Match1.Id, GameMode.Id, DateTime.Now)); Bet4 = await betRepoService.InsertAsync(new DatabaseModel.Bet(User3.Id, Match2.Id, GameMode.Id, DateTime.Now)); Bet5 = await betRepoService.InsertAsync(new DatabaseModel.Bet(User1.Id, Match2.Id, GameMode.Id, DateTime.Now)); }
private async Task <JackpotMatch> GetMatch(DatabaseModel.JackpotSetting settings, DatabaseModel.Match match, DatabaseModel.GameMode gameMode) { var items = await _betService.GetBettedItemsOnMatch(match.Id, gameMode.Id) ?? new List <Item>(); return(new JackpotMatch( match.RoundId.ToString(), match.Hash, match.Salt, match.Percentage, MatchStatusHelper.GetMatchStatusFromInt(match.Status), await GetBets(items, match.Id, gameMode), match.TimerStarted, settings.ToJackpotMatchSetting() )); }
private async Task PlaceBetOnMatch ( int matchId, JackpotMatchSetting setting, DatabaseModel.GameMode gameMode, int assetAndDescriptionIdsCount, List <DatabaseModel.Item> items, DatabaseModel.User user, List <DatabaseModel.ItemDescription> uniqueItemDescriptions ) { if (!gameMode.IsEnabled) { throw new GameModeIsNotEnabledException($"The current gamemode {gameMode.Type} is not enabled"); } if ((setting.AllowCsgo && setting.AllowPubg) == false) { var oneOrMoreItemsIsPubg = uniqueItemDescriptions.Any(AppIdToGameConverterHelper.IsPubgItem); var oneOrMoreItemsIsCsgo = uniqueItemDescriptions.Any(AppIdToGameConverterHelper.IsCsgoItem); if (setting.AllowCsgo && oneOrMoreItemsIsPubg) { throw new NotAllowedAppIdOnMatchException("You tried to bet a PUBG item on a match that only accepts CSGO"); } if (setting.AllowPubg && oneOrMoreItemsIsCsgo) { throw new NotAllowedAppIdOnMatchException("You tried to bet a CSGO item on a match that only accepts PUBG"); } } if (uniqueItemDescriptions.Any(description => !description.Valid)) { throw new InvalidItemException("One or more items is not allowed to be used for betting at the moment, refresh the page and try again"); } if (user == null) { throw new UserDoesNotExistException("Can't find a user"); } if (items.Any(item => item.OwnerId != user.Id)) { throw new UserDoesNotOwnThisItemsException("The user does not own this item!"); } if (items.Count == 0) { throw new InvalidAssetAndDecriptionIdException("No items with that assetId or descriptionId was found"); } if (items.Count != assetAndDescriptionIdsCount) { throw new NotSameCountAsExpectedException("assetAndDescriptionIds count != avalibleItems"); } if (items.Count > setting.MaxItemAUserCanBet) { throw new ToManyItemsOnBetException($"You can max bet {setting.MaxItemAUserCanBet} items, you tried to bet {items.Count}"); } if (items.Count < setting.MinItemAUserCanBet) { throw new ToFewItemsOnBetException($"You can min bet {setting.MinItemAUserCanBet} items, you tried to bet {items.Count}"); } var sumOfBet = await _itemService.GetSumOfItems(items); if (sumOfBet > setting.MaxValueAUserCanBet) { throw new ToMuchValueOnBetException($"You can max bet {setting.MaxValueAUserCanBet} value, you tried to bet {sumOfBet}"); } if (sumOfBet < setting.MinValueAUserCanBet) { throw new ToLittleValueOnBetException($"You can min bet {setting.MinValueAUserCanBet} value, you tried to bet {sumOfBet}"); } var avalibleItems = await _itemService.GetAvalibleItemsForUser(user); if (avalibleItems.Count == 0) { throw new NoAvalibleItemsForBettingException("No avalible item for betting"); } if (IsSameItemsInAvalibleItemsAsInItem(items, avalibleItems)) { throw new ItemNotAvalibleException("This item can not be betted now! Is this items already betted on an open match?)"); } await InsertBetWithTransaction(user, matchId, items, gameMode, uniqueItemDescriptions); //we have now successfully placed a bet if (gameMode.Type == GameModeHelper.GetStringFromType(GameModeType.JackpotCsgo)) { _discordService.JackpotBetAsync(matchId, user.SteamId, sumOfBet); } }
public async Task InitTest() { ConnectionString = new ConnectionStringsForTest().GetConnectionString(DatabaseName); Database = new DatabaseConnection(ConnectionString); var fakedFactory = A.Fake <IDatabaseConnectionFactory>(); A.CallTo(() => fakedFactory.GetDatabaseConnection(Factories.Database.Main)).Returns(Database); var userRepo = new UserRepoService(fakedFactory, new UserQueries()); var matchRepoService = new MatchRepoService(fakedFactory, new MatchQueries()); var coinFlipMatchRepoService = new CoinFlipMatchRepoService(fakedFactory); var gameModeRepoService = new GameModeRepoService(fakedFactory); var gameMode = new DatabaseModel.GameMode(GameModeHelper.GetStringFromType(GameModeType.JackpotCsgo), 1); var gameMode1 = new DatabaseModel.GameMode(GameModeHelper.GetStringFromType(GameModeType.CoinFlip), 1); GameMode = await gameModeRepoService.Insert(gameMode); GameMode1 = await gameModeRepoService.Insert(gameMode1); User1 = await userRepo.InsertAsync(new DatabaseModel.User( "987654321", "Kalle", " ba/bab154f01140ec39b2986d97838f0127534ec82d_full.jpg ", "?partner=117688384&token=mn347bmb ", DateTime.Now, DateTime.Now, false )); User2 = await userRepo.InsertAsync(new DatabaseModel.User( "987154321", "Kalle", " ba/bab154f01140ec39b2986d97838f0127534ec82d_full.jpg ", "?partner=117688384&token=mn347bmb ", DateTime.Now, DateTime.Now, false )); User3 = await userRepo.InsertAsync(new DatabaseModel.User( "983654321", "Kalle", " ba/bab154f01140ec39b2986d97838f0127534ec82d_full.jpg ", "?partner=117688384&token=mn347bmb ", DateTime.Now, DateTime.Now, false )); User4 = await userRepo.InsertAsync(new DatabaseModel.User( "983656584", "Kalle", " ba/bab154f01140ec39b2986d97838f0127534ec82d_full.jpg ", "?partner=117688384&token=mn347bmb ", DateTime.Now, DateTime.Now, false )); Match1 = await matchRepoService.InsertAsync(new DatabaseModel.Match( 11, "salt", "hash", 47.5484.ToString(CultureInfo.InvariantCulture), 1, null, null, 1, GameMode.Id, DateTime.Now )); Match2 = await matchRepoService.InsertAsync(new DatabaseModel.Match( 12, "salt", "hash", 47.5484.ToString(CultureInfo.InvariantCulture), 1, null, null, 1, GameMode.Id, DateTime.Now )); Match3 = await matchRepoService.InsertAsync(new DatabaseModel.Match( 13, "salt", "hash", 47.5484.ToString(CultureInfo.InvariantCulture), 1, null, null, 1, GameMode.Id, DateTime.Now )); using (var transaction = new TransactionWrapperWrapper(Database)) { CoinFlip1 = await coinFlipMatchRepoService.InsertAsync(new DatabaseModel.CoinFlip { Created = DateTime.Today, CreatorIsHead = true, CreatorUserId = User1.Id, GameModeId = gameMode1.Id, Hash = "", Percentage = "", Salt = "", RoundId = "xF!", SettingId = 0, Status = 1, TimerStarted = null, WinnerId = null }, transaction); CoinFlip2 = await coinFlipMatchRepoService.InsertAsync(new DatabaseModel.CoinFlip { Created = DateTime.Today, CreatorIsHead = true, CreatorUserId = User1.Id, GameModeId = gameMode1.Id, Hash = "", Percentage = "", Salt = "", RoundId = "xD1", SettingId = 0, Status = 1, TimerStarted = null, WinnerId = null }, transaction); CoinFlip3 = await coinFlipMatchRepoService.InsertAsync(new DatabaseModel.CoinFlip { Created = DateTime.Today, CreatorIsHead = true, CreatorUserId = User1.Id, GameModeId = gameMode1.Id, Hash = "", Percentage = "", Salt = "", RoundId = "xD", SettingId = 0, Status = 1, TimerStarted = null, WinnerId = null }, transaction); transaction.Commit(); } }
protected async Task <JackpotMatch> GetCurrentMatch(DatabaseModel.JackpotSetting settings, DatabaseModel.GameMode gameMode) { var match = await _matchRepoService.GetCurrentMatch(); return(await GetMatch(settings, match, gameMode)); }