public async Task <IActionResult> ClaimCredits(ClaimCreditsViewModel model)
        {
            var accessToken = HttpContext.Request.Headers["Authorization"].ToString().GetAccessTokenFromHeaderString();
            var userId      = await _identityService.GetUserId(accessToken);

            await _loyaltySyncService.AddCredit(new CreditsViewModel(userId, model.Credits), accessToken);

            if (model.NotificationId == null)
            {
                return(Ok(true));
            }

            var notification             = _notificationService.Get(model.NotificationId.GetValueOrDefault());
            var notificationEntityParams = _mapper.Map <NotificationEntityParams>(notification);

            notificationEntityParams.IsActionTaken = true;

            _notificationService.CreateOrUpdate(notificationEntityParams);
            var notificationModel = _mapper.Map <NotificationModel>(notificationEntityParams);

            return(Ok(notificationModel));
        }
Exemple #2
0
        public async Task <RoundEndModel> SendChoice(SendChoiceViewModel model, string userId, string accessToken)
        {
            var roundEndModel = new RoundEndModel(model.GameId);

            var answerCount = _roundAnswerService.RoundAnswerCount(model.GameRoundId, userId);

            if (answerCount > 1)
            {
                throw new AlreadyAnsweredException(_localizer.GetString("AlreadyAnswered").Value);
            }

            var roundAnswerEntity = _roundAnswerService.CreateOrUpdate(
                new RoundAnswerParams()
            {
                Choice      = model.Choice,
                GameRoundId = model.GameRoundId,
                UserId      = userId
            });

            if (_gameRoundService.IsRoundOver(model.GameRoundId))
            {
                var roundEnd = _gameRoundService.CalculateRoundEnd(model.GameRoundId);

                var gameRound       = _gameRoundService.Get(model.GameRoundId);
                var gameRoundParams = _mapper.Map <GameRoundParams>(gameRound);
                gameRoundParams.WinnerId = roundEnd.WinnerId;

                _gameRoundService.CreateOrUpdate(gameRoundParams);

                var game = _gameService.Get(model.GameId);

                if (!_gameRoundService.IsGameOver(model.GameId))
                {
                    var newRound = model.Round + 1;
                    await _hubContext.Clients
                    .Users(new List <string>() { game.HostUserId, game.GuestUserId })
                    .SendAsync("RoundEnds", JsonConvert.SerializeObject(roundEnd));

                    var newGameRoundEntity = _gameRoundService.CreateOrUpdate(new GameRoundParams(model.GameId, 0, newRound));
                    roundEndModel.Round       = newRound;
                    roundEndModel.GameRoundId = newGameRoundEntity.Id;


                    if (game.IsBotActivated)
                    {
                        var botUser     = _userService.GetUserByUserId(game.GuestUserId);
                        var credentials = await _userService.Login(new Services.Models.LoginModel()
                        {
                            Username = botUser.Nickname, Password = "******"
                        });

                        await BotSendAnswer(game, newGameRoundEntity, credentials.AccessToken);
                    }
                    return(roundEndModel);
                }
            }

            if (_gameRoundService.IsGameOver(model.GameId))
            {
                var gameEndResult = _gameRoundService.CalculateGameEnd(model.GameId);
                var game          = _gameService.Get(model.GameId);
                var gameParams    = _mapper.Map <GameParams>(game);
                gameParams.WinnerId = gameEndResult.UserId;
                var loserId = gameParams.WinnerId == game.HostUserId ? game.GuestUserId : game.HostUserId;
                _gameService.CreateOrUpdate(gameParams);

                var totalCreditsToAdd = game.Credit + gameEndResult.GainedCredits;

                await _loyaltySyncService.AddCredit(new CreditsModel(gameParams.WinnerId, totalCreditsToAdd), accessToken);

                await _loyaltySyncService.ChargeCredit(new CreditsModel(loserId, game.Credit), accessToken);

                await _loyaltySyncService.AddScores(new ScoreModel(gameParams.WinnerId, gameEndResult.TotalPoints), accessToken);

                if (!game.IsRematch)
                {
                    await _loyaltySyncService.AddWin(gameParams.WinnerId, accessToken);

                    await _loyaltySyncService.ResetWin(loserId, accessToken);
                }

                await _hubContext.Clients
                .Users(new List <string>() { game.HostUserId, game.GuestUserId })
                .SendAsync("GameEnds", JsonConvert.SerializeObject(gameEndResult));
            }
            else
            {
                await _bluffService.AskBluff(userId, model.GameId, model.GameRoundId);
            }

            roundEndModel.Round       = model.Round;
            roundEndModel.GameRoundId = roundAnswerEntity.GameRoundId;
            return(roundEndModel);
        }