Example #1
0
        public async Task <ActionResult> UpdateScore(Guid scoreId, UpdateScoreDto updateScoreDto)
        {
            var scoreToUpdate = await _beachBuddyRepository.GetScore(scoreId);

            if (scoreToUpdate == null)
            {
                return(NotFound());
            }

            if (updateScoreDto.WinCount < 0)
            {
                return(BadRequest("You can't be that bad! Win count must be greater than 0."));
            }

            _mapper.Map(updateScoreDto, scoreToUpdate);
            _beachBuddyRepository.UpdateScore(scoreToUpdate);
            await _beachBuddyRepository.Save();

            await _notificationService.sendNotification(null, NotificationType.ScoreUpdated, null, null, true);

            return(NoContent());
        }
Example #2
0
        private async Task ProcessScoreUpdate(String text,
                                              User userWhoSentMessage,
                                              string fromNumber,
                                              string toNumber)
        {
            var words = text.Split(" ");

            if (words.Length < 2)
            {
                await _twilioService.SendSms(fromNumber,
                                             "I'm not sure what game you are referring to... Send 'h' for help.");

                return;
            }

            var firstWordOfMessage = words[0].ToLower();
            var firstChar          = firstWordOfMessage.ToCharArray()[0];

            var secondWordOfMessage = text.Split(" ")[1].ToLower();

            var userWhoseScoreToManipulate = userWhoSentMessage;

            var gameName = text.Substring(firstWordOfMessage.Length).Trim();

            var users = await _beachBuddyRepository.GetUsers(new UserResourceParameters()
            {
                Name = secondWordOfMessage
            });

            var userMentionedInMessage = users.FirstOrDefault();

            if (userMentionedInMessage != null)
            {
                userWhoseScoreToManipulate = userMentionedInMessage;

                // We need to recalculate the Game Name so we can take out the Users name
                gameName = text.Substring(firstWordOfMessage.Length + " ".Length + secondWordOfMessage.Length).Trim();
            }

            var scoreToEdit = await _beachBuddyRepository.GetScore(userWhoseScoreToManipulate.Id, gameName);

            if (scoreToEdit == null)
            {
                await _twilioService.SendSms(fromNumber,
                                             $"Sorry, I could not find the game, {gameName}. You may need to add it first. Send 'h' for help.");

                return;
            }

            switch (firstChar)
            {
            case '+':
            {
                Double.TryParse(firstWordOfMessage, out var scoreChange);

                scoreToEdit.WinCount += Convert.ToInt32(scoreChange);
                break;
            }

            case '-':
            {
                Double.TryParse(firstWordOfMessage, out var score);
                var scoreChange = Math.Abs(score);

                var winCount = Convert.ToInt32(scoreChange);

                if (winCount <= 0)
                {
                    await _twilioService.SendSms(fromNumber,
                                                 $"{userWhoseScoreToManipulate.FirstName} already has 0 wins in {gameName}");

                    return;
                }

                scoreToEdit.WinCount -= winCount;
                break;
            }
            }

            _beachBuddyRepository.UpdateScore(scoreToEdit);
            await _beachBuddyRepository.Save();

            await _notificationService.sendNotification(null, NotificationType.ScoreUpdated, null, null, true);

            await _twilioService.SendSms(fromNumber,
                                         $"{userWhoseScoreToManipulate.FirstName} now has {scoreToEdit.WinCount} win(s) in {gameName}!");
        }