diff --git a/src/Controller/EmojiController.php b/src/Controller/EmojiController.php index c8a6b83..02c0434 100644 --- a/src/Controller/EmojiController.php +++ b/src/Controller/EmojiController.php @@ -2,12 +2,29 @@ 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 { @@ -16,7 +33,114 @@ class EmojiController extends AbstractController ]); } - public function reproduceEmoji(Emoji $emoji1, Emoji $emoji2): Emoji { - return new Emoji(); + 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() + ]); } -} +} \ No newline at end of file diff --git a/src/Entity/Emoji.php b/src/Entity/Emoji.php index 56ffb08..f3bb04a 100644 --- a/src/Entity/Emoji.php +++ b/src/Entity/Emoji.php @@ -38,6 +38,36 @@ class Emoji #[ORM\JoinColumn(nullable: false)] private ?Rarity $rarity = null; + #[ORM\ManyToOne(targetEntity: self::class)] + #[ORM\JoinColumn(nullable: true)] + private ?Emoji $parent1 = null; + + #[ORM\ManyToOne(targetEntity: self::class)] + #[ORM\JoinColumn(nullable: true)] + private ?Emoji $parent2 = null; + + public function getParent1(): ?self + { + return $this->parent1; + } + + public function setParent1(?self $parent1): self + { + $this->parent1 = $parent1; + return $this; + } + + public function getParent2(): ?self + { + return $this->parent2; + } + + public function setParent2(?self $parent2): self + { + $this->parent2 = $parent2; + return $this; + } + public function getId(): ?int { return $this->id;