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.
249 lines
9.4 KiB
249 lines
9.4 KiB
<?php
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Emoji;
|
|
use App\Entity\Rarity;
|
|
use App\Service\EmojiService;
|
|
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;
|
|
use App\Entity\UserEmojis;
|
|
use App\Repository\UserRepository;
|
|
use App\Repository\UserEmojisRepository;
|
|
use App\Entity\StockEmoji;
|
|
|
|
#[Route('/emojis', name: 'app_emoji_')]
|
|
class EmojiController extends AbstractController
|
|
{
|
|
private RarityRepository $rarityRepository;
|
|
private HttpClientInterface $httpClient;
|
|
private EmojiService $emojiService;
|
|
private UserRepository $userRepository;
|
|
private UserEmojisRepository $userEmojisRepository;
|
|
private EntityManagerInterface $em;
|
|
|
|
public function __construct(RarityRepository $rarityRepository, HttpClientInterface $httpClient, EmojiService $emojiService,
|
|
UserRepository $userRepository, UserEmojisRepository $userEmojisRepository, EntityManagerInterface $em)
|
|
{
|
|
$this->rarityRepository = $rarityRepository;
|
|
$this->httpClient = $httpClient;
|
|
$this->emojiService = $emojiService;
|
|
$this->userRepository = $userRepository;
|
|
$this->userEmojisRepository = $userEmojisRepository;
|
|
$this->em = $em;
|
|
}
|
|
|
|
#[Route('/', name: 'emojis')]
|
|
public function index(): Response
|
|
{
|
|
return $this->render('emoji/index.html.twig', [
|
|
'controller_name' => 'EmojiController',
|
|
]);
|
|
}
|
|
|
|
// 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/{userId}/{emoji1_id}/{emoji2_id}', name: 'fusion', methods: ['GET'], requirements: ['userId' => '\d+', 'emoji1_id' => '\d+', 'emoji2_id' => '\d+'])]
|
|
public function reproduceEmoji(int $userId, int $emoji1_id, int $emoji2_id, EntityManagerInterface $entityManager, EmojiRepository $emojiRepository): JsonResponse {
|
|
|
|
$user = $this->userRepository->findOneBy(['id' => $userId]);
|
|
|
|
if (!$user) {
|
|
return new JsonResponse(['error' => 'User not found'], 404);
|
|
}
|
|
|
|
$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();
|
|
|
|
$names = [
|
|
'Zabbo', 'Snorky', 'Wiblo', 'Kipzi', 'Gribz', 'Flimbo', 'Plinko', 'Raffu', 'Mibzo', 'Dazzo',
|
|
'Flinko', 'Groopi', 'Zimba', 'Wocky', 'Trizzi', 'Lumpo', 'Yabbo', 'Glinky', 'Zuffo', 'Brimbo',
|
|
'Chompa', 'Snizzle', 'Wibzi', 'Paffy', 'Tizzle', 'Nokko', 'Lazzo', 'Flumzi', 'Wozzy', 'Blimpo',
|
|
'Zuppi', 'Glinty', 'Fobzi', 'Muggo', 'Twibzy', 'Crungo', 'Jibbi', 'Snoffy', 'Glimzo', 'Daffo',
|
|
'Nibzi', 'Kranko', 'Yibba', 'Bloopo', 'Taffli', 'Zagga', 'Whizzo', 'Plobbi', 'Ruffli', 'Snibbo',
|
|
'Fuzzu', 'Gozzi', 'Trabbo', 'Flonzi', 'Whimpy', 'Klipzo', 'Zombzi', 'Hiffa', 'Fruggo', 'Luzzli',
|
|
'Zilpo', 'Baffi', 'Ramboo', 'Dibzo', 'Muffli', 'Kaffo', 'Gobzi', 'Tobbu', 'Xibzi', 'Pufflo',
|
|
'Jabbo', 'Zinky', 'Baffro', 'Tizzi', 'Wombo', 'Snuxi', 'Lompa', 'Criffy', 'Mebzi', 'Zoffo',
|
|
'Quimbo', 'Trubzi', 'Hozzy', 'Kabbo', 'Snooki', 'Laffo', 'Glumpi', 'Vibzo', 'Druffo', 'Whirli',
|
|
'Torko', 'Gropzi', 'Plibzo', 'Shaffi', 'Zabboz', 'Wubzi', 'Cloppo', 'Nibzo', 'Flunzi', 'Krabbo'
|
|
];
|
|
$randomName = $names[array_rand($names)];
|
|
$child->setName($randomName);
|
|
|
|
// Chance de fusion
|
|
$fusionRand = mt_rand() / mt_getrandmax();
|
|
if($fusionRand > 0.5) {
|
|
// 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 {
|
|
$repository = $this->em->getRepository(StockEmoji::class);
|
|
$allEmojis = $repository->findAll();
|
|
$randomKey = array_rand($allEmojis);
|
|
$randomEmoji = $allEmojis[$randomKey]->getCode();
|
|
$child->setCode($randomEmoji);
|
|
}
|
|
|
|
// 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->emojiService->generateRarity());
|
|
|
|
$child->setParent1($emoji1);
|
|
$child->setParent2($emoji2);
|
|
|
|
$entityManager->persist($child);
|
|
|
|
$userEmoji = new UserEmojis();
|
|
$userEmoji->setUser($user);
|
|
$userEmoji->setEmoji($child);
|
|
|
|
$entityManager->persist($userEmoji);
|
|
|
|
$entityManager->flush();
|
|
|
|
return new JsonResponse([
|
|
'message' => 'Child created',
|
|
'mommy' => $child->getParent1()->getCode(),
|
|
'daddy' => $child->getParent2()->getCode(),
|
|
'baby' => $child->getCode()
|
|
]);
|
|
}
|
|
|
|
|
|
#[Route('/fight/{idEmoji1}/{idEmoji2}', name: 'fight_emoji', methods: ['GET'], requirements: ['idEmoji1' => '\d+', 'idEmoji2' => '\d+'])]
|
|
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) {
|
|
$winner = $emoji1;
|
|
$loser = $emoji2;
|
|
$wonFight = 'left';
|
|
} else {
|
|
$winner = $emoji2;
|
|
$loser = $emoji1;
|
|
$wonFight = 'right';
|
|
}
|
|
|
|
$winner->wonFight();
|
|
|
|
$userEmojiLoser = $this->userEmojisRepository->findOneBy(['emoji' => $loser]);
|
|
|
|
$entityManager->persist($winner);
|
|
$entityManager->remove($loser);
|
|
$entityManager->remove($userEmojiLoser);
|
|
$entityManager->flush();
|
|
|
|
return new JsonResponse([
|
|
'leftEmoji' => $emoji1->getCode(),
|
|
'rightEmoji' => $emoji2->getCode(),
|
|
'winner' => $wonFight
|
|
]);
|
|
}
|
|
|
|
#[Route('/{emojiID}/getParents/', name: 'get_parents_emoji', methods: ['GET'])]
|
|
public function getParentsEmoji(int $emojiID, EmojiRepository $emojiRepository): JsonResponse {
|
|
$emoji = $emojiRepository->find($emojiID);
|
|
|
|
if (!$emoji) {
|
|
return new JsonResponse(['error' => 'Emoji not found'], 404);
|
|
}
|
|
|
|
$codeParent1 = null;
|
|
$codeParent2 = null;
|
|
$nameParent1 = 'inconnu';
|
|
$nameParent2 = 'inconnu';
|
|
|
|
|
|
if ($emoji->getParent1()) {
|
|
$codeParent1 = $emoji->getParent1()->getCode();
|
|
$nameParent1 = $emoji->getParent1()->getName();
|
|
}
|
|
if ($emoji->getParent2()) {
|
|
$codeParent2 = $emoji->getParent2()->getCode();
|
|
$nameParent2 = $emoji->getParent2()->getName();
|
|
}
|
|
|
|
|
|
return new JsonResponse([
|
|
'codeParent1' => $codeParent1,
|
|
'nameParent1' => $nameParent1,
|
|
'codeParent2' => $codeParent2,
|
|
'nameParent2' => $nameParent2,
|
|
'codeEnfant' => $emoji->getCode(),
|
|
'nameEnfant' => $emoji->getName(),
|
|
]);
|
|
}
|
|
}
|