public Task <bool> Handle(ReviveCommand request, CancellationToken cancellationToken)
        {
            ValidationHelper.ValidateUser(request.Request, out var userGuid, out var userRole);
            var user = _userRepository.GetById(userGuid);

            var cardsOfUser = _cardRepository.GetByUserAndCardType(userGuid, CardType.REVIVE);

            if (!cardsOfUser.Any())
            {
                throw new InvalidOperationException("No user cards of specified type have been found");
            }

            var graveyardGems = _gemRepository.GetByUser(Guid.Parse("0a9c40cd-34f5-439c-ad3c-0946aea1e5ea"));

            if (graveyardGems.Any())
            {
                var firstGemFromGraveyard = graveyardGems.FirstOrDefault();

                firstGemFromGraveyard.To.Id = user.Id;

                _gemRepository.Save(firstGemFromGraveyard);

                var card = cardsOfUser.FirstOrDefault();
                _cardRepository.Delete(card.Id);

                return(Task.FromResult(true));
            }
            else
            {
                return(Task.FromResult(false));
            }
        }
        public Task <bool> Handle(StealGemCommand request, CancellationToken cancellationToken)
        {
            var gems = _gemRepository.GetByUser(request.Target);

            var gem = gems.FirstOrDefault();

            if (gem != null)
            {
                ValidationHelper.ValidateUser(request.Request, out var userGuid, out var userRole);

                var user = _userRepository.GetById(userGuid);

                var cardsInPossession = _cardRepository.GetByUserAndCardType(userGuid, CardType.STEAL_GEM);
                if (!cardsInPossession.Any())
                {
                    throw new InvalidOperationException("No user cards of specified type have been found");
                }

                gem.Message += "\n Stolen from: " + gem.To.FirstName;
                gem.To       = user;

                _gemRepository.Save(gem);

                _cardRepository.Delete(cardsInPossession.FirstOrDefault().Id);

                return(Task.FromResult(true));
            }
            else
            {
                return(Task.FromResult(false));
            }
        }
        public Task <bool> Handle(MaledictionCommand request, CancellationToken cancellationToken)
        {
            ValidationHelper.ValidateUser(request.Request, out var userGuid, out var userRole);

            var cardsOfUser = _cardRepository.GetByUserAndCardType(userGuid, CardType.MALEDICTION);

            if (!cardsOfUser.Any())
            {
                throw new InvalidOperationException("No user cards of specified type have been found");
            }

            var card = cardsOfUser.FirstOrDefault();

            var targetUserGemsFromCurrentWeek = _gemRepository.GetByUser(request.Target).Where(x => x.Week == request.Week).ToList();

            if (targetUserGemsFromCurrentWeek.Any())
            {
                foreach (var gem in targetUserGemsFromCurrentWeek)
                {
                    gem.Message += "[Malediction - Originally to " + gem.To.Name + "]";
                    gem.To.Id    = Guid.Parse("0a9c40cd-34f5-439c-ad3c-0946aea1e5ea");

                    _gemRepository.Save(gem);
                }
            }

            card.IsActive         = true;
            card.TargetPlayerGuid = request.Target;
            card.Week             = request.Week;
            _cardRepository.Save(card);
            return(Task.FromResult(true));
        }
Esempio n. 4
0
        public ActionResult Post(Gem gems)
        {
            ValidationHelper.ValidateUser(Request, out var userGuid, out var userRole);
            string msg = "You have successfully sent the gem(s) to ";

            if (userGuid == gems.From.Id)
            {
                var curUser     = _userRepository.GetById(gems.From.Id);
                var targetUser  = _userRepository.GetById(gems.To.Id);
                var titleString = "";

                if (targetUser.Name.Equals("Graveyard"))
                {
                    titleString = curUser.Name + " dumped the gem into the graveyard.";
                }
                else
                {
                    titleString = curUser.Name + " gave a gem to " + targetUser.Name;
                }

                var today = DateTime.Today;
                var week  = ((int)(today - _startDateTime).TotalDays / 7) + 1;

                var messageLog = new Message
                {
                    Id    = Guid.NewGuid(),
                    Title = titleString,
                    Body  = gems.Message,
                    Week  = week
                };

                gems.Week = week;

                _messageRepository.Save(messageLog);
                msg = MaledictionCardGemsDivert(gems, msg);

                if (curUser.GemsToGive > 0)
                {
                    _gemRepository.Save(gems);
                }
                else
                {
                    return(BadRequest("You don't have enough gems to give"));
                }
            }
            else
            {
                return(BadRequest("The operation is not allowed"));
            }

            return(Ok(new { message = msg }));
        }
        public Task <bool> Handle(SelfHugCommand request, CancellationToken cancellationToken)
        {
            var gemsList = new List <Gem>();

            ValidationHelper.ValidateUser(request.Request, out var userGuid, out var userRole);

            var user = _userRepository.GetById(userGuid);

            var cardsOfUser = _cardRepository.GetByUserAndCardType(userGuid, CardType.SELF_HUG);

            if (!cardsOfUser.Any())
            {
                throw new InvalidOperationException("No user cards of specified type have been found");
            }

            if (user.GemsToGive >= request.GemsToGive)
            {
                for (int i = 0; i < request.GemsToGive; i++)
                {
                    var newGuid = Guid.NewGuid();
                    var gem     = new Gem()
                    {
                        Id      = newGuid,
                        From    = user,
                        To      = user,
                        Message = user.FirstName + " gave a gem to themselves"
                    };

                    gemsList.Add(gem);

                    user.GemsToGive = user.GemsToGive - 1;
                }

                _gemRepository.Save(gemsList);
                _userRepository.Save(user);

                var card = cardsOfUser.FirstOrDefault();
                _cardRepository.Delete(card.Id);

                return(Task.FromResult(true));
            }
            else
            {
                return(Task.FromResult(false));
            }
        }
        public Task <bool> Handle(DoubleReceiveCommand request, CancellationToken cancellationToken)
        {
            var gemsList = new List <Gem>();

            ValidationHelper.ValidateUser(request.Request, out var userGuid, out var userRole);
            var user       = _userRepository.GetById(userGuid);
            var userTarget = _userRepository.GetById(request.Target);

            var cardsOfUser = _cardRepository.GetByUserAndCardType(userGuid, CardType.DOUBLE_RECEIVE);

            if (!cardsOfUser.Any())
            {
                throw new InvalidOperationException("No user cards of specified type have been found");
            }

            var receivedGemsFromTargetThisWeek = _gemRepository.GetByWeek(request.Week).Where(x => x.From != null).Where(x => x.From.Id == userTarget.Id);

            if (receivedGemsFromTargetThisWeek.Any())
            {
                for (int i = 0; i < receivedGemsFromTargetThisWeek.Count(); i++)
                {
                    var newGuid = Guid.NewGuid();
                    var gem     = new Gem()
                    {
                        Id      = newGuid,
                        From    = userTarget,
                        To      = user,
                        Message = user.Name + " doubled the number of gems he/she was given from " + userTarget.Name
                    };

                    gemsList.Add(gem);
                }

                _gemRepository.Save(gemsList);
                _userRepository.Save(user);

                var card = cardsOfUser.FirstOrDefault();
                _cardRepository.Delete(card.Id);

                return(Task.FromResult(true));
            }
            else
            {
                return(Task.FromResult(false));
            }
        }
        public Task <bool> Handle(RobinHoodCommand request, CancellationToken cancellationToken)
        {
            var sourceUserGems = _gemRepository.GetByUser(request.Source);
            var targetUserGems = _gemRepository.GetByUser(request.Target);

            ValidationHelper.ValidateUser(request.Request, out var userGuid, out var userRole);

            if (!sourceUserGems.Any())
            {
                throw new InvalidOperationException("The user from whom you want to steal the gems has none available");
            }

            var cardsInHand = _cardRepository.GetByUserAndCardType(userGuid, CardType.ROBIN_HOOD);

            if (!cardsInHand.Any())
            {
                throw new InvalidOperationException("No user cards of specified type have been found");
            }

            var sourceUserGemsList = sourceUserGems.ToList();
            var targetUserGemsList = targetUserGems.ToList();

            if (sourceUserGemsList.Count > targetUserGemsList.Count)
            {
                var gem = sourceUserGemsList[0];
                gem.Message += " \n [Originally to " + gem.To.FirstName + "]";
                gem.To.Id    = request.Target;
                _gemRepository.Save(gem);

                // Use card
                _cardRepository.Delete(cardsInHand.FirstOrDefault().Id);

                return(Task.FromResult(true));
            }

            return(Task.FromResult(false));
        }