70% of features done!
This commit is contained in:
@@ -45,6 +45,7 @@ import com.example.quiz.data.model.enums.PariStatut;
|
||||
import com.example.quiz.databinding.FragmentBetValidationBinding;
|
||||
import com.example.quiz.utils.BluetoothUtils;
|
||||
import com.example.quiz.utils.Result;
|
||||
import com.example.quiz.utils.SharedPrefsHelper;
|
||||
import com.example.quiz.viewModel.PariViewModel;
|
||||
import com.example.quiz.viewModel.SharedViewModel;
|
||||
import com.google.android.material.appbar.MaterialToolbar;
|
||||
@@ -77,20 +78,24 @@ public class BetValidation extends Fragment {
|
||||
|
||||
private AlertDialog dialog;
|
||||
|
||||
private int nombreX;
|
||||
|
||||
|
||||
private boolean order;
|
||||
|
||||
private long mise;
|
||||
|
||||
|
||||
private TypeOfBet typeOfBet;
|
||||
|
||||
SharedPrefsHelper prefsHelper;
|
||||
|
||||
private Reunion reunion;
|
||||
|
||||
private Course course;
|
||||
|
||||
private int coeff;
|
||||
|
||||
private int mise;
|
||||
|
||||
private ActivityResultLauncher<Intent> enableBluetoothLauncher;
|
||||
|
||||
|
||||
@@ -114,6 +119,7 @@ public class BetValidation extends Fragment {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
prefsHelper = SharedPrefsHelper.getInstance(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -139,8 +145,8 @@ public class BetValidation extends Fragment {
|
||||
return;
|
||||
}
|
||||
coeff = Integer.parseInt(charSequence.toString());
|
||||
calculateMise(selectedHorses.getValue().size());
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
calculateMise(selectedHorses.getValue());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -180,7 +186,15 @@ public class BetValidation extends Fragment {
|
||||
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
||||
textView.setOnClickListener(v -> {
|
||||
final List<String> horses = new ArrayList<>(selectedHorses.getValue());
|
||||
if (horses.contains(horse)) {
|
||||
if(horses.size() >= shared.typeOfBet.getValue().getNumberOfHorse() && horse.equals("X")){
|
||||
Toast.makeText(getContext(), "Vous ne pouvez plus sélectionner X", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
if(_notClickable(horses) && horse.equals("X")){
|
||||
Toast.makeText(getContext(), "Vous ne pouvez plus sélectionner X", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
if (horses.contains(horse) && !horse.equals("X")) {
|
||||
horses.remove(horse);
|
||||
selectedHorses.setValue(horses);
|
||||
v.setSelected(false);
|
||||
@@ -196,15 +210,27 @@ public class BetValidation extends Fragment {
|
||||
.collect(Collectors.joining("-"));
|
||||
binding.combination.setText( combinationText);
|
||||
});
|
||||
|
||||
|
||||
return textView;
|
||||
}
|
||||
|
||||
|
||||
boolean _notClickable(List<String> selectedHorses){
|
||||
int numberOfElement = 0;
|
||||
if(shared.typeOfBet.getValue().getNumberOfHorse() < 2){
|
||||
return true;
|
||||
}
|
||||
for(String horse: selectedHorses){
|
||||
numberOfElement = horse.equals("X")?numberOfElement+1:numberOfElement;
|
||||
}
|
||||
nombreX = numberOfElement;
|
||||
return numberOfElement == shared.typeOfBet.getValue().getNumberOfHorse() - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
|
||||
setSelectedTypeOfBetImage();
|
||||
pariViewModel = new ViewModelProvider(this).get(PariViewModel.class);
|
||||
typeOfBet = shared.typeOfBet.getValue();
|
||||
reunion = shared.selectedReunion.getValue();
|
||||
@@ -241,8 +267,12 @@ public class BetValidation extends Fragment {
|
||||
selectedHorses.observe(getViewLifecycleOwner(), new Observer<List<String>>() {
|
||||
@Override
|
||||
public void onChanged(List<String> horses) {
|
||||
calculateMise(horses.size());
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
calculateMise(horses);
|
||||
if(horses.size() > shared.typeOfBet.getValue().getNumberOfHorse()){
|
||||
binding.elargie.setVisibility(View.VISIBLE);
|
||||
}else{
|
||||
binding.elargie.setVisibility(View.GONE);
|
||||
}
|
||||
if(shared.typeOfBet.getValue().getNumberOfHorse() > 2 && horses.size() >= shared.typeOfBet.getValue().getNumberOfHorse()){
|
||||
binding.order.setVisibility(View.VISIBLE);
|
||||
}else{
|
||||
@@ -252,8 +282,7 @@ public class BetValidation extends Fragment {
|
||||
});
|
||||
binding.order.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
order = isChecked;
|
||||
calculateMise(selectedHorses.getValue().size());
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
calculateMise(selectedHorses.getValue());
|
||||
});
|
||||
|
||||
binding.betValidateBtn.setOnClickListener(v->{
|
||||
@@ -274,10 +303,10 @@ public class BetValidation extends Fragment {
|
||||
}
|
||||
|
||||
|
||||
|
||||
Pari pari = new Pari(
|
||||
generate12Digits(),
|
||||
shared.typeOfBet.getValue().getName(),
|
||||
"GAGNANT",
|
||||
mise,
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").format(LocalDateTime.now()),
|
||||
true,
|
||||
@@ -285,25 +314,11 @@ public class BetValidation extends Fragment {
|
||||
new PariCourseDto(shared.selectedCourse.getValue().getId()),
|
||||
selectedHorses.getValue(),
|
||||
order?new ArrayList<String>(): selectedHorses.getValue(),
|
||||
order,
|
||||
"MULTI_4"
|
||||
!order,
|
||||
"MULTI_4",
|
||||
prefsHelper.get("code")
|
||||
);
|
||||
pariViewModel.createPari(pari).observe(getViewLifecycleOwner(),new Observer<Result<Pari>>() {
|
||||
@Override
|
||||
public void onChanged(Result<Pari> pariResult) {
|
||||
switch (pariResult.status){
|
||||
case ERROR:
|
||||
Toast.makeText(getContext(), pariResult.message, Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case LOADING:
|
||||
Toast.makeText(getContext(), "En cours", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case SUCCESS:
|
||||
_showPariDialog(pariResult.data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
_showPariDialog(pari);
|
||||
});
|
||||
|
||||
binding.backBtn.setOnClickListener(v->{
|
||||
@@ -315,6 +330,26 @@ public class BetValidation extends Fragment {
|
||||
});
|
||||
}
|
||||
|
||||
private void setSelectedTypeOfBetImage(){
|
||||
switch (shared.typeOfBet.getValue().getName()){
|
||||
case "COUPLE_PLACE":
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_couple_place);
|
||||
break;
|
||||
case "COUPLE_GAGNANT":
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_couple_gagnant);
|
||||
break;
|
||||
case "TIERCE":
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_tierce);
|
||||
break;
|
||||
case "QUARTE":
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_quarte);
|
||||
break;
|
||||
case "QUINTE":
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_quinte_plus);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void printPari(Pari pari) throws WriterException {
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pmu_logo);
|
||||
Bitmap barcode = generateBarcodeBitmap(pari.getNumeroTicket(), 384, 100);
|
||||
@@ -332,15 +367,16 @@ public class BetValidation extends Fragment {
|
||||
tspl.append("--------------------------------\n");
|
||||
boolean isElargie = selectedHorses.getValue().size()>shared.typeOfBet.getValue().getNumberOfHorse();
|
||||
tspl.append(isElargie?pari.getTypePari()+"/Elargie":pari.getTypePari()).append("\n");
|
||||
tspl.append(order?"COMBINAISON COMPLETE"+"\n":"");
|
||||
String combinationText = selectedHorses.getValue().stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining("-"));
|
||||
tspl.append(combinationText).append("\n");
|
||||
tspl.append("COEF: "+String.valueOf(coeff)).append(".0").append("\n");
|
||||
tspl.append("--------------------------------\n");
|
||||
tspl.append("MONTANT: ").append(mise).append(" XOF\n");
|
||||
tspl.append("MONTANT: ").append(pari.getMise()).append(" XOF\n");
|
||||
tspl.append("-------------------------------\n");
|
||||
tspl.append("AGENT: ").append(" 3332\n");
|
||||
tspl.append("AGENT: ").append(prefsHelper.get("code")).append("\n");
|
||||
tspl.append("DATE: ").append(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss").format(LocalDateTime.now())).append("\n");
|
||||
if (BluetoothUtils.needsBluetoothPermissions()) {
|
||||
if (!BluetoothUtils.hasBluetoothPermission(requireContext())) {
|
||||
@@ -378,7 +414,7 @@ public class BetValidation extends Fragment {
|
||||
TextView aler_coeff = (TextView) view.findViewById(R.id.alert_coeff);
|
||||
aler_coeff.setText("Coef:"+String.valueOf(coeff));
|
||||
TextView alert_montant = (TextView) view.findViewById(R.id.alert_montant);
|
||||
alert_montant.setText("Montant: "+String.valueOf(mise)+" CFA");
|
||||
alert_montant.setText("Montant: "+String.valueOf(pari.getMise())+" CFA");
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
builder.setView(view);
|
||||
builder.setCancelable(false);
|
||||
@@ -387,22 +423,35 @@ public class BetValidation extends Fragment {
|
||||
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||
|
||||
view.findViewById(R.id.alert_validate).setOnClickListener(v->{
|
||||
try {
|
||||
printPari(pari);
|
||||
dialog.dismiss();
|
||||
} catch (WriterException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
pariViewModel.createPari(pari).observe(getViewLifecycleOwner(),new Observer<Result<Pari>>() {
|
||||
@Override
|
||||
public void onChanged(Result<Pari> pariResult) {
|
||||
switch (pariResult.status){
|
||||
case ERROR:
|
||||
Toast.makeText(getContext(), pariResult.message, Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case LOADING:
|
||||
Toast.makeText(getContext(), "En cours", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case SUCCESS:
|
||||
try {
|
||||
printPari(pari);
|
||||
dialog.dismiss();
|
||||
} catch (WriterException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
view.findViewById(R.id.alert_cancel).setOnClickListener(v -> {
|
||||
dialog.dismiss();
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
void _initializeToZero(){
|
||||
mise = 0;
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
selectedHorses.setValue(List.of());
|
||||
binding.combination.setText("");
|
||||
binding.order.setChecked(false);
|
||||
@@ -413,56 +462,67 @@ public class BetValidation extends Fragment {
|
||||
setupNumberGrid(binding.gridNumbers);
|
||||
}
|
||||
|
||||
public void calculateMise(int nombreChevauxSelectionnes){
|
||||
if(typeOfBet.getNumberOfHorse() == 1){
|
||||
if(nombreChevauxSelectionnes==1){
|
||||
public void calculateMise(List<String> selectedHorses){
|
||||
_notClickable(selectedHorses);
|
||||
int nombreChevauxSelectionnes = selectedHorses.size();
|
||||
long mise = 0;
|
||||
if(shared.typeOfBet.getValue() == null || shared.selectedCourse.getValue() == null){
|
||||
return;
|
||||
}
|
||||
int typeOfBetHorses = shared.typeOfBet.getValue().getNumberOfHorse();
|
||||
int partants = shared.selectedCourse.getValue().getPartants();
|
||||
if(shared.typeOfBet.getValue().getNumberOfHorse() > nombreChevauxSelectionnes){
|
||||
binding.mise.setText(mise+" CFA");
|
||||
return;
|
||||
}
|
||||
if(typeOfBetHorses == 2){
|
||||
mise = 500;
|
||||
}else{
|
||||
if(typeOfBetHorses == 5){
|
||||
mise = 300;
|
||||
}else{
|
||||
mise = 200;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.getNumberOfHorse() == 2){
|
||||
if(nombreChevauxSelectionnes == 2){
|
||||
mise = 400;
|
||||
mise = mise * coeff;
|
||||
if(nombreX>0){
|
||||
if(nombreChevauxSelectionnes == typeOfBetHorses){
|
||||
mise = mise * _calculateArrangement((partants - (typeOfBetHorses-nombreX)), nombreX);
|
||||
}else{
|
||||
mise = mise * _calculateArrangement(nombreChevauxSelectionnes - typeOfBetHorses , nombreX);
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 2){
|
||||
mise = 400 + (nombreChevauxSelectionnes - 2)*300;
|
||||
if(order){
|
||||
mise = mise * _calculateArrangement(typeOfBetHorses, (typeOfBetHorses-nombreX));
|
||||
}
|
||||
this.mise = mise;
|
||||
binding.mise.setText(mise+" CFA");
|
||||
return;
|
||||
}
|
||||
if(typeOfBet.getNumberOfHorse() == 3){
|
||||
if(nombreChevauxSelectionnes == 3){
|
||||
mise = 500;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 3){
|
||||
mise = 500 + (nombreChevauxSelectionnes - 3)*500;
|
||||
}
|
||||
mise = mise * _calculateCombinaison(nombreChevauxSelectionnes, typeOfBetHorses);
|
||||
if(order){
|
||||
mise = mise * _calculateArrangement(nombreChevauxSelectionnes, typeOfBetHorses);
|
||||
}
|
||||
if(typeOfBet.getNumberOfHorse() == 4){
|
||||
if(nombreChevauxSelectionnes == 4){
|
||||
mise = 600;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 4){
|
||||
mise = 600 + (nombreChevauxSelectionnes - 4)*600;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.getNumberOfHorse() == 5){
|
||||
if(nombreChevauxSelectionnes == 5){
|
||||
mise = 1200;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 5){
|
||||
mise = 1200 + (nombreChevauxSelectionnes - 5)*1200;
|
||||
}
|
||||
}
|
||||
mise = order?mise * coeff * _calculateFactorial(shared.typeOfBet.getValue().getNumberOfHorse()):mise * coeff;
|
||||
this.mise = mise;
|
||||
binding.mise.setText(mise+" CFA");
|
||||
}
|
||||
|
||||
Integer _calculateFactorial(int n){
|
||||
int f = 1;
|
||||
Long _calculateArrangement(int n, int k){
|
||||
return _calculateFactorial(n) / _calculateFactorial(n-k);
|
||||
}
|
||||
|
||||
Long _calculateCombinaison(int n, int k){
|
||||
return _calculateFactorial(n) / (_calculateFactorial(k) * _calculateFactorial(n - k));
|
||||
}
|
||||
|
||||
Long _calculateFactorial(int n){
|
||||
long f = 1;
|
||||
if(n == 0){
|
||||
return f;
|
||||
return f;
|
||||
}
|
||||
for(int i = 1; i <=n; i++){
|
||||
f *= i;
|
||||
}
|
||||
return f;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user