Exemple #1
0
        public async Task <FullGameStateOutVM> Play(PlayCardInVM move, int gameID, int playerID)
        {
            using var transaction = await _transactionManager.BeginTransactionAsync();

            Game game = await _gameManagementService.GetPlayableGame(gameID, playerID);

            FullGameStateOutVM output = await InternalPlayCard(move, game, playerID);

            await transaction.CommitAsync();

            return(output);
        }
        public async Task <FullGameStateOutVM> GetGame(int id)
        {
            using var transaction = await _transactionManager.BeginTransactionAsync();

            var game = await GetFullGame(id);

            if (game == null)
            {
                throw new ArgumentOutOfRangeException("No game with given id found");
            }
            return(_mapper.MapFullGameState(game));
        }
Exemple #3
0
        public async Task <int?> CheckLogin(PlayerInVM playerIn)
        {
            using var transaction = await _transactionManager.BeginTransactionAsync();

            var userData = await _context
                           .Players
                           .FirstOrDefaultAsync(p => p.Username == playerIn.Username);

            if (userData == null)
            {
                return(null);
            }
            var hashedPassword = _passwordService.HashPassword(playerIn.Password, userData.Salt);
            await transaction.CommitAsync();

            return(hashedPassword == userData.HashedPassword ? userData.ID : (int?)null);
        }
Exemple #4
0
        public async Task <FullGameStateOutVM> CreateGame(string[] playerList, int cardLimit)
        {
            if (cardLimit < 1)
            {
                throw new BadRequestException("Cannot create game without in-hand cards");
            }
            using var transaction = await _transactionManager.BeginTransactionAsync();

            var randomInstance = _randomGenerator.CreateRandom();

            await _gameManagementService.CloseExistingGames(playerList);

            var cards     = _deckService.CreateCards();
            var cardQueue = new Queue <Card>(cards);

            var empty = _cardSerializer.SerializeCards(new Card[0]);

            var game = new Game
            {
                GameStartedAt = DateTimeOffset.UtcNow,
                CardLimit     = cardLimit,
                Discarded     = empty,
            };
            var players = (await _context
                           .Players
                           .Where(p => playerList.Contains(p.Username))
                           .ToListAsync())
                          .Select(player => new
            {
                Player = player,
                Cards  = cardQueue.Dequeue(cardLimit)
            })
                          .ToList();

            if (players.Count != playerList.Length)
            {
                throw new ArgumentOutOfRangeException("Not all players exist");
            }
            var state = players.Select(e => new GamePlayerState
            {
                Game   = game,
                Player = e.Player,
                Hand   = _cardSerializer.SerializeCards(e.Cards),
                Table  = _mapper.SerializeQueues(new List <List <Card> > {
                    new List <Card>(), new List <Card>()
                }),
            }).ToList();
            var remaining = cardQueue.ToList();

            game.State      = state;
            game.Deck       = _cardSerializer.SerializeCards(remaining);
            game.NextPlayer = players[randomInstance.Next(players.Count)].Player;

            await _context.AddAsync(game);

            await _context.SaveChangesAsync();

            var output = _mapper.MapFullGameState(game);
            await transaction.CommitAsync();

            return(output);
        }
 public static Task StartTransactionAsync(this ITransactionManager transactionManager, TransactionProperties transactionProperties)
 {
     return(transactionManager.BeginTransactionAsync(transactionProperties, CancellationToken.None));
 }