public void Test_Unsort_Deck()
        {
            var newDeck = new LogicDeck(4);

            newDeck.Unsort();
            var expectedDeck = new LogicDeck()
            {
                Size    = 4,
                Score   = 0,
                Victory = false,
                Tiles   = new List <LogicTile>()
                {
                    new LogicTile(0, 4),
                    new LogicTile(1, 4),
                    new LogicTile(2, 4),
                    new LogicTile(3, 4),
                    new LogicTile(4, 4),
                    new LogicTile(5, 4),
                    new LogicTile(6, 4),
                    new LogicTile(7, 4),
                    new LogicTile(8, 4),
                    new LogicTile(9, 4),
                    new LogicTile(10, 4),
                    new LogicTile(11, 4),
                    new LogicTile(12, 4),
                    new LogicTile(13, 4),
                    new LogicTile(14, 4),
                    new LogicTile(15, 4),
                }
            };

            Assert.IsFalse(Equals(expectedDeck, newDeck));
        }
Exemple #2
0
        public async Task <Result <Deck> > Handle(NewDeckCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.UserDbs
                       .Include(u => u.Deck)
                       .Include(d => d.Deck.Tiles)
                       .AsNoTracking()
                       .FirstOrDefaultAsync(
                u => u.Email == request.Email, cancellationToken);

            if (user == null)
            {
                //create user and deck
                var logicDeck = new LogicDeck(4);

                do
                {
                    logicDeck.Unsort();
                } while (!logicDeck.CheckWinIsPossible());
                logicDeck.Tiles         = logicDeck.Tiles.OrderBy(t => t.Pos).ToList();
                logicDeck.User.Email    = request.Email;
                logicDeck.User.UserName = request.UserName;

                var deckDb = _mapper.Map <DeckDb>(logicDeck);
                _context.Add(deckDb);
                try
                {
                    await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                    return(Result.Ok <Deck>(_mapper.Map <Deck>(deckDb)));
                }
                catch (DbUpdateException ex)
                {
                    return(Result.Fail <Deck>(ex.Message));
                }
            }

            //reset deck
            try
            {
                var deckDb = user.Deck;

                var logicDeck = _mapper.Map <LogicDeck>(deckDb);
                logicDeck.User.Email    = request.Email;
                logicDeck.User.UserName = request.UserName;
                do
                {
                    logicDeck.Unsort();
                } while (!logicDeck.CheckWinIsPossible());
                logicDeck.Tiles   = logicDeck.Tiles.OrderBy(t => t.Pos).ToList();
                logicDeck.Victory = false;
                logicDeck.Score   = 0;
                _context.Update(_mapper.Map <DeckDb>(logicDeck));
                await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                return(Result.Ok <Deck>(_mapper.Map <Deck>(logicDeck)));
            }
            catch (DbUpdateException ex)
            {
                return(Result.Fail <Deck>(ex.Message));
            }
        }