<?php
namespace App\Controller;
use App\Entity\Prediction;
use App\Entity\PredictionGenerator;
use App\Entity\Offre;
use App\Entity\Participants;
use App\Entity\Predictionfast;
use App\Entity\PredictionMeilleuresCombinaisons;
use App\Form\PredictionGeneratorType;
use App\Repository\AbonnementRepository;
use App\Repository\CoursesRepository;
use App\Repository\OffreRepository;
use App\Repository\ParticipantsRepository;
use App\Repository\PerformanceRepository;
use App\Repository\PredictionMeilleuresCombinaisonsRepository;
use App\Repository\PredictionRepository;
use App\Repository\ReunionsRepository;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\JsonResponse;
// "/{reactRouting}", name="index", defaults={"reactRouting": null}, requirements={"reactRouting"=".+"}
class IndexController extends AbstractCoreController
{
/**
* @Route("/", name="index")
*/
public function index(ManagerRegistry $doctrine, PerformanceRepository $performanceRepository): Response
{
/* $slides_right = $doctrine->getManager()->getRepository(Prediction::class)->findWinningPredictions(10); */
// dd($slides_right);
/* $performancesData = $performanceRepository->findPerformances('1'); */
$tPariTypesKeys = explode('|', $_ENV['PARI_TYPES_KEYS']);
$tPariTypesValues = explode('|', $_ENV['PARI_TYPES_VALUES']);
$pari_types = array_combine($tPariTypesKeys, $tPariTypesValues);
$categories = array();
/* foreach ($performancesData as $key => $performance) {
foreach ($pari_types as $keyPari => $pari_type) {
$categorie = [];
$categorie['nom'] = $pari_type;
$categorie['details'] = [];
switch ($keyPari) {
case 'SP':
for ($i = 2; $i <= 4; ++$i) {
$detail = [];
$method = 'getSPen' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.1) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'SG':
for ($i = 3; $i <= 4; ++$i) {
$detail = [];
$method = 'getSGen' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.15) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'CG':
for ($i = 3; $i <= 4; ++$i) {
$detail = [];
$method = 'getCGen' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.05) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'CP':
for ($i = 2; $i <= 4; ++$i) {
$detail = [];
$method = 'getCPen' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.005) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'deuxsur4':
for ($i = 2; $i <= 4; ++$i) {
$detail = [];
$method = 'getdeuxsur4en' . $i . 'chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.025) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'trioDesordre':
for ($i = 4; $i <= 4; ++$i) {
$detail = [];
$method = 'getTrioDesordreEn' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.05) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'trioOrdre':
for ($i = 4; $i <= 4; ++$i) {
$detail = [];
$method = 'getTrioOrdreEn' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.005) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'quarteDesordre':
for ($i = 5; $i <= 4; ++$i) {
$detail = [];
$method = 'getQuarteDesordreEn' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.00005) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'quarteOrdre':
for ($i = 5; $i <= 4; ++$i) {
$detail = [];
$method = 'getQuarteOrdreEn' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.00005) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'quinteDesordre':
for ($i = 6; $i <= 4; ++$i) {
$detail = [];
$method = 'getQuinteDesordrEn' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.000005) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
case 'quinteOrdre':
for ($i = 6; $i <= 4; ++$i) {
$detail = [];
$method = 'getQuinteOrdreEn' . $i . 'Chev';
if (method_exists($performance, $method)) {
$perfData = $performance->$method();
if ($perfData < 0.000005) {
break; // Sort de la boucle
}
$detail['valeur'] = round($perfData * 100, 2);
$detail['nbChevaux'] = $i;
$categorie['details'][] = $detail;
}
}
break;
default:
// Si le type de pari n'est pas géré, vous pouvez soit ignorer, soit ajouter une valeur par défaut
break;
}
if (!empty($categorie['details'])) {
$categories[] = $categorie;
}
}
}
*/
/*
$performances = array();
foreach ($performancesData as $key => $performance) {
foreach ($pari_types as $keyPari => $pari_type) {
$performances[$keyPari]['type'] = $pari_type;
for ($i = 0; $i < 4; ++$i) {
switch ($keyPari) {
case 'SP':
switch ($i) {
case 0:
$perfData = $performance->getSPen1Chev();
break;
case 1:
$perfData = $performance->getSPen2Chev();
break;
case 2:
$perfData = $performance->getSPen3Chev();
break;
case 3:
$perfData = $performance->getSPen4Chev();
break;
case 4:
$perfData = $performance->getSPen5Chev();
break;
case 5:
$perfData = $performance->getSPen6Chev();
break;
case 6:
$perfData = $performance->getSPen7Chev();
break;
case 7:
$perfData = $performance->getSPen8Chev();
break;
default:
$perfData = 0.00;
break;
}
break;
default:
$perfData = 0.00;
break;
}
$performances[$keyPari]['data'][$i] = round($perfData*100, 2);
}
}
}
*/
return $this->render('index/index.html.twig', [
"categories" => $categories,
/* 'slides_right' => $slides_right, */
]);
}
/**
* @Route("/program", name="program")
*/
public function program(): Response
{
return $this->render('programme/index.html.twig');
}
/**
* @Route("/program/{date}/{course}", name="program_course")
*/
public function programCourse($date, $course, PredictionMeilleuresCombinaisonsRepository $predictionMeilleuresCombinaisonsRepository, CoursesRepository $coursesRepository, ReunionsRepository $reunionsRepository, AbonnementRepository $abonnementRepository, Request $request): Response
{
try {
$dateObj = new \DateTime($date);
} catch (\Exception $e) {
// Si la date est invalide, utiliser aujourd'hui
$dateObj = new \DateTime();
}
// Récupérer les réunions pour cette date
$reunions = $reunionsRepository->findByDate($dateObj);
// Récupérer les courses pour cette réunion spécifique (si $course est un ID de course)
$courseDetails = null;
if ($course) {
$courseDetails = $coursesRepository->find($course);
}
// Récupérer les détails de la course sélectionnée
$courseDetails = null;
$predictions = [];
// initialisation du variable pour la subscription
$hasActiveSubscription = false;
// Vérifier si l'utilisateur est connecté
$user = $this->getUser();
if ($user) {
$userPlatform = $user->retrieveUserPlatform();
// Vérifier si l'utilisateur a un abonnement actif
$activeSubscriptions = $abonnementRepository->findActiveSubscriptions($userPlatform);
$hasActiveSubscription = !empty($activeSubscriptions);
}
/* dd($activeSubscriptions); */
if ($course && is_numeric($course)) {
$courseDetails = $coursesRepository->find($course);
// Récupérer les prédictions UNIQUEMENT si l'utilisateur est connecté et abonné
if ($courseDetails && $hasActiveSubscription) {
$predictions = $predictionMeilleuresCombinaisonsRepository->findByCourseId($course);
}
}
return $this->render('programme/course.html.twig', [
'date' => $dateObj,
'selectedCourseId' => $course,
'courseDetails' => $courseDetails,
'reunions' => $reunions,
'predictions' => $predictions,
'hasActiveSubscription' => $hasActiveSubscription,
'isUserLoggedIn' => $user !== null,
]);
/* $predictionGenerator = new Predictionfast();
$form = $this->createForm(PredictionGeneratorType::class, $predictionGenerator);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if($this->getUser()) {
$predictionGenerator->setIdUser($this->getUser()->getId());
}
$predictionGenerator->setIdCoursePredict($course);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($predictionGenerator);
$entityManager->flush();
$this->addFlash('success', 'Votre demande a bien été soumise.');
return $this->redirectToRoute('program_course', ['date' => $date, 'course' => $course]);
}
$myPredictions = [];
if($this->getUser()) {
$myPredictions = $predictionRepository->findBy(['idUser' => $this->getUser()->getId()]);
} */
/* return $this->render('programme/course.html.twig', [
'form' => $form->createView(),
'myPredictions' => $myPredictions,
]); */
}
/**
* @Route("/api/reunion/{idReunion}/courses", name="api_reunion_courses")
*/
public function getCoursesByReunion(int $idReunion, CoursesRepository $coursesRepository, ReunionsRepository $reunionsRepository): JsonResponse
{
// Récupérer la réunion
$reunion = $reunionsRepository->find($idReunion);
if (!$reunion) {
return $this->json(['error' => 'Réunion non trouvée'], 404);
}
// Récupérer les courses pour cette réunion
$courses = $coursesRepository->findBy(
['reunion' => $idReunion],
['numeroCourse' => 'ASC']
);
$data = [];
foreach ($courses as $course) {
$data[] = [
'id' => $course->getIdCourse(),
'numeroCourse' => $course->getNumeroCourse(),
'nom' => $course->getNom(),
'heureDepart' => $course->getHeureDepart() ? $course->getHeureDepart()->format('H:i') : null,
'discipline' => $course->getDiscipline(),
'distance' => $course->getDistance(),
'montant' => $course->getMontant(),
'montantdevise' => $course->getMontantdevise(),
'nbDeclaresPartants' => $course->getNbDeclaresPartants(),
'etatTerrain' => $course->getEtatTerrain(),
'specialite' => $course->getSpecialite(),
'conditionsAge' => $course->getConditionsAge(),
'corde' => $course->getCorde(),
];
}
return $this->json([
'reunion' => [
'id' => $reunion->getIdReunion(),
'date' => $reunion->getDate() ? $reunion->getDate()->format('Y-m-d H:i:s') : null,
'hippodrome' => $reunion->getHippodrome(),
'numeroReunion' => $reunion->getNumeroReunion(),
'pays' => $reunion->getPays(),
],
'courses' => $data,
'coursesExemple' => $coursesRepository->findOneBy([], ['idCourse' => 'ASC'])
]);
}
/**
* @Route("/api/course/{courseId}/predictions", name="api_course_predictions")
*/
public function getPredictionsByCourse(string $courseId, PredictionMeilleuresCombinaisonsRepository $predictionsRepository, AbonnementRepository $abonnementRepository, OffreRepository $offreRepository): JsonResponse
{
// Vérifier si l'utilisateur est connecté
$user = $this->getUser();
$userPlatform = null;
$hasActiveSubscription = false;
if ($user) {
$userPlatform = $user->retrieveUserPlatform();
// Vérifier si l'utilisateur a un abonnement actif
if ($userPlatform) {
$activeSubscriptions = $abonnementRepository->findActiveSubscriptions($userPlatform);
$hasActiveSubscription = !empty($activeSubscriptions);
}
}
// Récupérer les prédictions (maintenant accessible à tous)
$predictions = $predictionsRepository->findByFkCourseIdGroupedByType($courseId);
/* $predictions = $predictionsRepository->findByCourseIdGroupedByType($courseId); */
$formattedPredictions = [];
foreach ($predictions as $typePari => $typePredictions) {
$formattedPredictions[$typePari] = [];
foreach ($typePredictions as $prediction) {
// Préparer les données de base
$predictionData = [
'id' => $prediction->getIdPrediction(),
'typePari' => $prediction->getTypePari(),
'risque' => $prediction->getRisque(),
'methodeCalcul' => $prediction->getMethodecalcul(),
'batchSize' => $prediction->getBatchSize(),
'combinaison' => $prediction->getCombinaison(),
'nbPropositions' => $prediction->getNbPropositions(),
'success' => $prediction->isSuccess(),
'dateCreation' => $prediction->getDateCreation() ? $prediction->getDateCreation()->format('Y-m-d H:i:s') : null,
'tauxReussite' => $prediction->getTauxReussite()
];
// Gérer les positions selon le statut d'abonnement
if ($hasActiveSubscription && $userPlatform) {
// Utilisateur abonné : renvoyer les vraies positions
$predictionData['positions'] = [
'p1' => $prediction->getP1(),
'p2' => $prediction->getP2(),
'p3' => $prediction->getP3(),
'p4' => $prediction->getP4(),
'p5' => $prediction->getP5(),
'p6' => $prediction->getP6(),
'p7' => $prediction->getP7(),
'p8' => $prediction->getP8(),
];
} else {
// Non-abonné : renvoyer des positions masquées
// Créer des positions masquées avec "x" pour chaque position non-nulle
$positions = [
'p1' => $prediction->getP1() ? 'x' : null,
'p2' => $prediction->getP2() ? 'x' : null,
'p3' => $prediction->getP3() ? 'x' : null,
'p4' => $prediction->getP4() ? 'x' : null,
'p5' => $prediction->getP5() ? 'x' : null,
'p6' => $prediction->getP6() ? 'x' : null,
'p7' => $prediction->getP7() ? 'x' : null,
'p8' => $prediction->getP8() ? 'x' : null,
];
// Calculer combien de positions sont non-nulles pour garder le même nombre de "x"
$positionCount = 0;
for ($i = 1; $i <= 8; $i++) {
$getter = 'getP' . $i;
if ($prediction->$getter()) {
$positionCount++;
}
}
$predictionData['positions'] = $positions;
$predictionData['maskedPositionsCount'] = $positionCount; // Pour information si besoin
}
$formattedPredictions[$typePari][] = $predictionData;
}
}
// Ajouter le statut d'abonnement dans la réponse pour le frontend
$responseData = [
'predictions' => $formattedPredictions,
'userStatus' => [
'isLoggedIn' => ($userPlatform !== null),
'hasActiveSubscription' => $hasActiveSubscription,
/* 'userPlatform' => [
'id' => $userPlatform?->getId()
],
'offre' => $offreRepository->findAll() */
],
/* 'Allprediction' => $predictionsRepository->findOneBy([], ['idPrediction' => 'ASC']), */
/* 'prediction_pour_course_409241 ' => $predictionsRepository->findByFkCourseIdGroupedByType(409241) */
];
return $this->json($responseData);
}
/**
* @Route("/api/course/{courseId}/participants", name="api_course_participants")
*/
public function getParticipantsByCourse(int $courseId, ParticipantsRepository $participantsRepository, OffreRepository $offreRepository, PredictionMeilleuresCombinaisonsRepository $predictionMeilleuresCombinaisonsRepository): JsonResponse
{
// Récupérer les participants pour cette course
$participants = $participantsRepository->findBy(
['course' => $courseId],
['numero' => 'ASC']
);
/* $predictions = $predictionMeilleuresCombinaisonsRepository->findBy(
[], // Critères (vide pour tous)
['id' => 'DESC'], // Tri
12, // Limite
0 // Offset
); */
/* $predictionTest = $predictionMeilleuresCombinaisonsRepository->findAll(); */
// Tri numérique manuel
usort($participants, function ($a, $b) {
return (int)$a->getNumero() <=> (int)$b->getNumero();
});
$formattedParticipants = [];
foreach ($participants as $participant) {
$formattedParticipants[] = [
'id' => $participant->getIdParticipant(),
'numero' => $participant->getNumero(),
'nom' => $participant->getNom(),
'jockey' => $participant->getJockey(),
'entraineur' => $participant->getEntraineur(),
'sexe' => $participant->getSexe(),
'age' => $participant->getAge(),
'distance' => $participant->getDistance(),
'dateNaissance' => $participant->getDateNaissance() ? $participant->getDateNaissance()->format('Y-m-d') : null,
'poids' => $participant->getPoids(),
'poidsUnite' => $participant->getPoidsUnite(),
'oeilleres' => $participant->getOeilleres(),
'deferes' => $participant->getDeferes(),
'corde' => $participant->getCorde(),
'cote' => $participant->getCote(),
'statutDepart' => $participant->getStatutDepart(),
'arrivee' => $participant->getArrivee(),
'musique' => $participant->getMusique(),
'tempsRecord' => $participant->getTempsRecord(),
'gains' => $participant->getGains(),
'confianceEntourage' => $participant->getConfianceEntourage(),
];
}
return $this->json([
'courseId' => $courseId,
'participants' => $formattedParticipants,
'count' => count($formattedParticipants),
/* 'predictionTest' => $predictionTest */
]);
}
}