Files
barbosa-landerAPI/app/src/main/java/com/example/barbosalanderapi/MainActivity.java
2026-04-14 16:52:38 +01:00

218 lines
7.5 KiB
Java
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.example.barbosalanderapi;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import com.example.barbosalanderapi.R;
import com.example.barbosalanderapi.RandomApiService;
import com.example.barbosalanderapi.RandomRequest;
import com.example.barbosalanderapi.RandomResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private TextView slot1, slot2, slot3, txtSaldo;
private EditText editAposta;
private Button btnSortear;
private ProgressBar progress;
private long saldo = 100000;
private long apostaAtual = 0;
private final String API_KEY = "ebfb7ff0-b2f6-41c8-bef3-4fba17be410c";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slot1 = findViewById(R.id.slot1);
slot2 = findViewById(R.id.slot2);
slot3 = findViewById(R.id.slot3);
txtSaldo = findViewById(R.id.txtSaldo);
editAposta = findViewById(R.id.editAposta);
btnSortear = findViewById(R.id.btnSortear);
progress = findViewById(R.id.progress);
atualizarSaldoUI();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.random.org/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RandomApiService service = retrofit.create(RandomApiService.class);
btnSortear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
girarSlots(service);
}
});
}
private void girarSlots(RandomApiService service) {
String inputAposta = editAposta.getText().toString();
if (inputAposta.isEmpty()) {
Toast.makeText(this, "Insira um valor de aposta!", Toast.LENGTH_SHORT).show();
return;
}
apostaAtual = Long.parseLong(inputAposta);
if (apostaAtual <= 0) {
Toast.makeText(this, "Aposta deve ser maior que 0!", Toast.LENGTH_SHORT).show();
return;
}
if (apostaAtual > saldo) {
Toast.makeText(this, "Saldo insuficiente!", Toast.LENGTH_SHORT).show();
return;
}
// Subtrai a aposta do saldo
saldo -= apostaAtual;
atualizarSaldoUI();
progress.setVisibility(View.VISIBLE);
btnSortear.setEnabled(false);
editAposta.setEnabled(false);
RandomRequest request = new RandomRequest(API_KEY, 3, 1, 5);
service.generateIntegers(request).enqueue(new Callback<RandomResponse>() {
@Override
public void onResponse(Call<RandomResponse> call, Response<RandomResponse> response) {
progress.setVisibility(View.GONE);
btnSortear.setEnabled(true);
if (response.isSuccessful() && response.body() != null) {
RandomResponse body = response.body();
if (body.result != null && body.result.random != null && body.result.random.data != null) {
int[] numeros = body.result.random.data;
iniciarAnimacaoSlot(slot1, numeros[0], 1000, false);
iniciarAnimacaoSlot(slot2, numeros[1], 2000, false);
iniciarAnimacaoSlot(slot3, numeros[2], 3000, true);
} else if (body.error != null) {
Log.e("API_JSON_RPC_ERROR", "Erro: " + body.error.message);
usarSorteioLocal();
} else {
usarSorteioLocal();
}
} else {
Toast.makeText(MainActivity.this, "Erro de rede: " + response.code() + " - Usando Sorteio Local", Toast.LENGTH_SHORT).show();
Log.e("API_HTTP_ERROR", "Código: " + response.code());
usarSorteioLocal();
}
}
@Override
public void onFailure(Call<RandomResponse> call, Throwable t) {
progress.setVisibility(View.GONE);
btnSortear.setEnabled(true);
Toast.makeText(MainActivity.this, "Sem Internet - Usando Sorteio Local", Toast.LENGTH_SHORT).show();
Log.e("API_ERRO", "Erro: " + t.getMessage());
usarSorteioLocal();
}
});
}
private void usarSorteioLocal() {
java.util.Random random = new java.util.Random();
int[] numeros = {
random.nextInt(5) + 1,
random.nextInt(5) + 1,
random.nextInt(5) + 1
};
iniciarAnimacaoSlot(slot1, numeros[0], 1000, false);
iniciarAnimacaoSlot(slot2, numeros[1], 2000, false);
iniciarAnimacaoSlot(slot3, numeros[2], 3000, true);
}
private void iniciarAnimacaoSlot(final TextView slot, final int numeroFinal, final long tempoDeGiro, final boolean isUltimo) {
final long startTime = System.currentTimeMillis();
final Handler handler = new Handler();
final java.util.Random rand = new java.util.Random();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = System.currentTimeMillis() - startTime;
if (elapsed < tempoDeGiro) {
slot.setText(numeroParaEmoji(rand.nextInt(5) + 1));
slot.setTranslationY((float) (Math.random() * 10 - 5));
handler.postDelayed(this, 80);
} else {
slot.setText(numeroParaEmoji(numeroFinal));
slot.setTranslationY(0);
slot.setScaleX(1.3f);
slot.setScaleY(1.3f);
slot.animate().scaleX(1.0f).scaleY(1.0f).setDuration(300).start();
if (isUltimo) {
verificarGanhador();
btnSortear.setEnabled(true);
editAposta.setEnabled(true);
progress.setVisibility(View.GONE);
}
}
}
});
}
private void verificarGanhador() {
String s1 = slot1.getText().toString();
String s2 = slot2.getText().toString();
String s3 = slot3.getText().toString();
if (s1.equals(s2) && s2.equals(s3)) {
long premio = apostaAtual * 2;
saldo += premio;
Toast.makeText(MainActivity.this, "JACKPOT! 🎰💰 GANHOU " + premio + "€!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Não foi desta vez! Perdeu " + apostaAtual + "", Toast.LENGTH_SHORT).show();
}
atualizarSaldoUI();
}
private void atualizarSaldoUI() {
txtSaldo.setText("SALDO: " + String.format("%,d", saldo).replace(',', '.') + "");
}
private String numeroParaEmoji(int numero) {
switch (numero) {
case 1: return "🍒";
case 2: return "🍋";
case 3: return "🔔";
case 4: return "💎";
case 5: return "7";
default: return "";
}
}
}
//teste git