You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
8.0 KiB
246 lines
8.0 KiB
<?php
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Emoji;
|
|
use App\Entity\Rarity;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use App\Repository\RarityRepository;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use App\Repository\EmojiRepository;
|
|
|
|
#[Route('/emoji', name: 'emoji')]
|
|
class EmojiController extends AbstractController
|
|
{
|
|
private RarityRepository $rarityRepository;
|
|
private HttpClientInterface $httpClient;
|
|
|
|
public function __construct(RarityRepository $rarityRepository, HttpClientInterface $httpClient)
|
|
{
|
|
$this->rarityRepository = $rarityRepository;
|
|
$this->httpClient = $httpClient;
|
|
}
|
|
|
|
#[Route('/emoji', name: 'app_emoji')]
|
|
public function index(): Response
|
|
{
|
|
$this->testToMove();
|
|
return $this->render('emoji/index.html.twig', [
|
|
'controller_name' => 'EmojiController',
|
|
]);
|
|
}
|
|
|
|
#[Route('/count', name: 'count')]
|
|
public function count(EmojiRepository $emojiRepository): Response
|
|
{
|
|
$count = count($emojiRepository->findAll());
|
|
return new Response(['count' => $count]);
|
|
}
|
|
|
|
#[Route('/addRarity', name: 'addR')]
|
|
public function addRarityDebug(EntityManagerInterface $entityManager) {
|
|
$rarity = new Rarity();
|
|
$rarity->setName('Bip');
|
|
$rarity->setDropRate(42);
|
|
$entityManager->persist($rarity);
|
|
$entityManager->flush();
|
|
|
|
return new Response();
|
|
}
|
|
|
|
#[Route('/add/{code}', name: 'add')]
|
|
public function addEmojiDebug(string $code, EntityManagerInterface $entityManager) {
|
|
$emoji = new Emoji();
|
|
$emoji->setCode($code);
|
|
$emoji->setName('Default Name');
|
|
$emoji->setStrength(1.0);
|
|
$emoji->setToughness(1.0);
|
|
$emoji->setIntelligence(1.0);
|
|
$emoji->setSpeed(1.0);
|
|
$emoji->setFightsWon(0);
|
|
|
|
// On récupère une instance de Rarity existante (par exemple, la première)
|
|
$rarity = $this->getRarity();
|
|
if (!$rarity) {
|
|
throw new \RuntimeException('Aucun objet Rarity trouvé en base.');
|
|
}
|
|
$emoji->setRarity($rarity);
|
|
|
|
// Optionnel : définir parent1 et parent2 si tu veux tester avec des relations
|
|
// $emoji->setParent1(null);
|
|
// $emoji->setParent2(null);
|
|
|
|
$entityManager->persist($emoji);
|
|
$entityManager->flush();
|
|
|
|
return new Response();
|
|
}
|
|
|
|
private function getRarity(): Rarity {
|
|
$rarity = $this->rarityRepository->findAll();
|
|
$rand = mt_rand() / mt_getrandmax();
|
|
|
|
$sum = 0.0;
|
|
foreach($rarity as $r) {
|
|
$sum += $r->getDropRate();
|
|
if($sum > $rand) {
|
|
return $r;
|
|
}
|
|
}
|
|
|
|
return $rarity[0];
|
|
}
|
|
|
|
// Renvoi l'url de l'image issue de la fusion de deux emojis. Fonctionne avec un code et un emoji directement
|
|
private function getFusionUrl(string $emoji1, string $emoji2): string
|
|
{
|
|
$baseUrl = 'https://emojik.vercel.app/s/';
|
|
$size = 256;
|
|
|
|
// On encode les string pour l'url
|
|
$encodedEmoji1 = urlencode($emoji1);
|
|
$encodedEmoji2 = urlencode($emoji2);
|
|
|
|
return sprintf('%s%s_%s?size=%d', $baseUrl, $encodedEmoji1, $encodedEmoji2, $size);
|
|
}
|
|
|
|
#[Route('/fusion/{emoji1_id}/{emoji2_id}', name: 'fusion')]
|
|
public function reproduceEmoji(int $emoji1_id, int $emoji2_id, EntityManagerInterface $entityManager, EmojiRepository $emojiRepository): JsonResponse {
|
|
$emoji1 = $emojiRepository->find($emoji1_id);
|
|
$emoji2 = $emojiRepository->find($emoji2_id);
|
|
|
|
if (!$emoji1 || !$emoji2) {
|
|
return new JsonResponse(['error' => 'One or both emojis not found'], 404);
|
|
}
|
|
|
|
$child = new Emoji();
|
|
|
|
$child->setName("Testenfant");
|
|
|
|
// Chance de fusion
|
|
$fusionRand = mt_rand() / mt_getrandmax();
|
|
if($fusionRand > 0.8) {
|
|
// Si les emoji parents sont des fusions on remonte l'arbre généalogique jusqu'à trouver un smiley normal
|
|
// On ne peut pas fusionner un smiley déjà fusionné
|
|
$temp1 = $emoji1;
|
|
while(str_contains($temp1->getCode(), "vercel")) {
|
|
$temp1 = $temp1->getParent1();
|
|
}
|
|
|
|
$temp2 = $emoji2;
|
|
while(str_contains($temp2->getCode(), "vercel")) {
|
|
$temp2 = $temp2->getParent2();
|
|
}
|
|
|
|
$child->setCode($this->getFusionUrl($temp1->getCode(), $temp2->getCode()));
|
|
} else {
|
|
$rand = mt_rand() / mt_getrandmax();
|
|
if($rand > 0.5) {
|
|
$child->setCode($emoji1->getCode());
|
|
} else {
|
|
$child->setCode($emoji2->getCode());
|
|
}
|
|
}
|
|
|
|
// Lors d'une fusion un emoji récupère chaque stat d'un de ces deux parents de manière aléatoire
|
|
$rand = mt_rand() / mt_getrandmax();
|
|
if($rand > 0.5) {
|
|
$child->setStrength($emoji1->getStrength());
|
|
} else {
|
|
$child->setStrength($emoji2->getStrength());
|
|
}
|
|
|
|
$rand = mt_rand() / mt_getrandmax();
|
|
if($rand > 0.5) {
|
|
$child->setToughness($emoji1->getToughness());
|
|
} else {
|
|
$child->setToughness($emoji2->getToughness());
|
|
}
|
|
|
|
$rand = mt_rand() / mt_getrandmax();
|
|
if($rand > 0.5) {
|
|
$child->setIntelligence($emoji1->getIntelligence());
|
|
} else {
|
|
$child->setIntelligence($emoji2->getIntelligence());
|
|
}
|
|
|
|
$rand = mt_rand() / mt_getrandmax();
|
|
if($rand > 0.5) {
|
|
$child->setSpeed($emoji1->getSpeed());
|
|
} else {
|
|
$child->setSpeed($emoji2->getSpeed());
|
|
}
|
|
|
|
$child->setFightsWon(0);
|
|
|
|
$child->setRarity($this->getRarity());
|
|
|
|
$child->setParent1($emoji1);
|
|
$child->setParent2($emoji2);
|
|
|
|
$entityManager->persist($child);
|
|
$entityManager->flush();
|
|
|
|
return new JsonResponse([
|
|
'message' => 'Child created',
|
|
'childId' => $child->getId()
|
|
]);
|
|
}
|
|
|
|
#[Route('/startEmoji', name: 'get_starter_emoji')]
|
|
public function startEmoji(EntityManagerInterface $entityManager, EmojiRepository $emojiRepository): JsonResponse {
|
|
}
|
|
|
|
|
|
|
|
#[Route('/fight/{idEmoji1}/{idEmoji2}', name: 'fight_emoji')]
|
|
public function fightEmoji(int $idEmoji1, int $idEmoji2, EntityManagerInterface $entityManager, EmojiRepository $emojiRepository): JsonResponse {
|
|
$emoji1 = $emojiRepository->find($idEmoji1);
|
|
$emoji2 = $emojiRepository->find($idEmoji2);
|
|
|
|
if (!$emoji1 || !$emoji2) {
|
|
return new JsonResponse(['error' => 'Emoji not found'], 404);
|
|
}
|
|
$aleatoire = random_int(0,3);
|
|
$valEmoji1 = [$emoji1->getStrength(),$emoji1->getToughness(),$emoji1->getIntelligence(),$emoji1->getSpeed()];
|
|
$valEmoji2 = [$emoji2->getStrength(),$emoji2->getToughness(),$emoji2->getIntelligence(),$emoji2->getSpeed()];
|
|
$difference = $valEmoji1[$aleatoire] - $valEmoji2[$aleatoire];
|
|
if($difference > 0){
|
|
$emoji1->wonFight();
|
|
$entityManager->persist($emoji1);
|
|
$entityManager->remove($emoji2);
|
|
} else {
|
|
$emoji2->wonFight();
|
|
$entityManager->persist($emoji2);
|
|
$entityManager->remove($emoji1);
|
|
}
|
|
|
|
$entityManager->flush();
|
|
|
|
return new JsonResponse([
|
|
'message' => 'End of the fight',
|
|
]);
|
|
}
|
|
|
|
public function testToMove(){
|
|
$e = new Emoji();
|
|
$e->setName("ROBERT");
|
|
$e->setStrength(5);
|
|
$e->setIntelligence(2);
|
|
$e->setToughness(3);
|
|
$e->setSpeed(4);
|
|
$e2 = new Emoji();
|
|
$e2->setName("BIBOP");
|
|
$e2->setStrength(42);
|
|
$e2->setIntelligence(1);
|
|
$e2->setToughness(1);
|
|
$e2->setSpeed(1);
|
|
$vic = $this->fightEmoji($e,$e2);
|
|
echo $vic->getName();
|
|
}
|
|
}
|