public IActionResult Click(string name, int row, int column)
        {
            Grid grid = inMemoryGridFactory.GetOrCreate(name);

            List <SquareChangedEventArgs> squareChanged = new List <SquareChangedEventArgs>();
            List <SequenceFoundEventArgs> sequenceFound = new List <SequenceFoundEventArgs>();

            grid.SquareChanged += (sender, args) =>
            {
                squareChanged.Add(args);
            };

            grid.SequenceFound += (sender, args) =>
            {
                sequenceFound.Add(args);
            };

            grid.Click(new SquareIndex(row, column));

            ClickResponseModel result = new ClickResponseModel
            {
                ChangedSquares = squareChanged,
                SequencesFound = sequenceFound
            };

            return(Ok(result));
        }
Beispiel #2
0
        public async Task <ClickResponseModel> Click(ClickModel clickModel, Guid userId)
        {
            var errors   = new List <string>();
            var response = new ClickResponseModel(errors);

            using var transaction = context.Database.BeginTransaction(IsolationLevel.RepeatableRead);
            var now = DateTime.UtcNow;
            Tag?tag = null;

            if (Guid.TryParse(clickModel.TagId, out Guid tagGuid))
            {
                tag = await context.Tags.Include(x => x.User).SingleOrDefaultAsync(x => x.Id == tagGuid);

                if (tag == null)
                {
                    errors.Add(InvalidTagIdError);
                }
            }
            else
            {
                tag = await context.Tags.Include(x => x.User).SingleOrDefaultAsync(x => x.User.Username == clickModel.TagId);

                if (tag == null)
                {
                    errors.Add(InvlaidUsernameError);
                }
            }
            if (tag == null)
            {
                errors.Add(NoUsernameOrTagId);
            }
            var user = await context.Users.SingleAsync(x => x.Id == userId);

            if (user.Balance <= 0 && user.Repurchase && !string.IsNullOrWhiteSpace(user.BillingId) && !string.IsNullOrWhiteSpace(user.CardId) && !string.IsNullOrWhiteSpace(user.RepurchaseAmount))
            {
                await purchaseManager.Purchase(new PurchaseModel
                {
                    Amount   = user.RepurchaseAmount,
                    Refill   = true,
                    UseSaved = true,
                }, user);
            }
            if (!errors.Any() && user.Balance <= 0)
            {
                errors.Add(InsufficientFundsError);
            }
            if (errors.Any())
            {
                await transaction.RollbackAsync();

                return(response);
            }
            user.Balance       -= 1;
            tag !.User.Balance += 1;
            context.Clicks.Add(new Click
            {
                Id        = Guid.NewGuid(),
                TagId     = tag !.Id,
                UserId    = userId,
                Timestamp = now,
                ViewId    = clickModel.viewId,
                PageId    = clickModel.PageId,
                Page      = clickModel.Page,
                Referrer  = clickModel.Referrer
            });