public async override Task <ValidadeReferenceModel> ValidadeReference(ValidadeReferenceLookuoModel request, ServerCallContext context)
        {
            ValidadeReferenceModel output = new ValidadeReferenceModel();

            Models.User user = _context.User.FirstOrDefault(u => u.SessionID == request.SessionId);
            if (user == null)
            {
                output.IsItValid   = 0;
                output.BoughtGames = -1;
                return(await Task.FromResult(output));
            }

            ResponseValidateReference rvr = await APIServerCommunication.validateReference(request.Reference, user.Id);

            if (rvr.status.Equals("error"))
            {
                output.IsItValid   = 0;
                output.BoughtGames = -1;
                return(await Task.FromResult(output));
            }

            if (rvr.valid.Equals("true"))
            {
                // TODO: Executar a transação.

                String rvtp = await APIServerCommunication.transactionToServerByAccountId(user.Id, request.Reference);

                if (rvtp.Contains("success"))
                {
                    // Depois de ser feita a transação, do cliente para o servidor, transforma-se o amount em jogos. Para já, cara crédito pago, corresponde a um jogo.


                    string numberString = new String(rvtp.Where(Char.IsDigit).ToArray());

                    user.GamesToPlay += Int32.Parse(numberString);

                    _context.SaveChanges();



                    output.IsItValid = 1;
                }
                else
                {
                    output.IsItValid = 0;
                }
            }
            else
            {
                output.IsItValid = 0;
            }


            // TODO: Enviar para o cliente os dados do ValidadeReferenceModel, de acordo com aquilo que recebe da API (tem de receber o número de jogos e se é válido)


            return(await Task.FromResult(output));
        }
Exemple #2
0
        // Tratamento do evento de validação de referencia. Este evento foi chamado no AuthView.
        private async void AuthView_APIValidateReference(string reference)
        {
            ValidadeReferenceModel outcome = null;

            try
            {
                // Modelo com as informações do pedido para o servidor tratar.
                ValidadeReferenceLookuoModel transactionData = new ValidadeReferenceLookuoModel
                {
                    SessionId = Program.AuthUser.SessionID,
                    Reference = reference
                };

                // Pedido assincrono de validação da referência passada pelo servidor.
                outcome = await GameClient.ValidadeReferenceAsync(transactionData);
            }
            // No caso de a conexão com o servidor falhar, é chamado o método de ConnectController para finalizar todas as conexões
            catch (Grpc.Core.RpcException)
            {
                Program.ConnectController.ConnectionError();
                return;
            }
            catch (JsonException jsonEx)
            {
                Program.CreditBankMenurView.ShowMessageBox("Unable to validate the reference in the API due to json parse exception:\n" + jsonEx + "\nCreditBankController: AuthView_APIValidateReference");
                return;
            }
            catch (ArgumentNullException nullEx)
            {
                Program.CreditBankMenurView.ShowMessageBox("Unable to validate the reference in the API due to null exception:\n" + nullEx + "\nCreditBankController: AuthView_APIValidateReference");
                return;
            }
            catch (HttpRequestException httpEx)
            {
                Program.CreditBankMenurView.ShowMessageBox("Unable to validate the reference in the API due to Http request exception:\n" + httpEx + "\nCreditBankController: AuthView_APIValidateReference");
                return;
            }

            if (outcome.IsItValid == 1 && outcome != null) // Referencia introduzida válida.
            {
                Program.AuthView.ValidateReference();
            }
            else // Referencia introduzida inválida.
            {
                Program.AuthView.InvalidateReference();
            }
        }