ソースを参照

Merge branch 'clement-dp-dh' into 'master'

ajout gestion dp et dh et url dans tweet

See merge request clement.krebs/twyrael!13
KREBS-CHEVRESSON CLEMENT 4 年 前
コミット
46e410a7cf

+ 62 - 1
src/Controller/HomeController.php

@@ -48,9 +48,26 @@ class HomeController extends AbstractController
                 $text = $form->get('text')->getData();
                 $mentions = array();
                 $hashtags = array();
+                $dp = array();
+                $dh = array();
+                $urls = array();
                 preg_match_all("~@([a-zA-Z0-9_]*)~", $text, $mentions);
                 preg_match_all("~#([a-zA-Z0-9_]*)~", $text, $hashtags);
-                for ($i = 0; $i < sizeof($mentions[0]); $i++) {
+                preg_match_all("!DP %(.+)(?:\s|$)!U", $text, $dp);
+                preg_match_all("!DH %(.+)(?:\s|$)!U", $text, $dh);
+                preg_match_all("~[a-z]+://\S+~", $text, $urls);
+                for($i = 0; $i < sizeof($urls[0]); $i++) {
+                    $url = $urls[0][$i];
+                    $urls[1][$i] = "<a href='$url'>$url</a>";
+                    $u = explode("/", $urls[0][$i]);
+                    $formatted_url = "";
+                    foreach ($u as $u_part) {
+                        $formatted_url .= $u_part . "\/";
+                    }
+                    $urls[0][$i] = "~[a-z]+://\S+~";
+                }
+                $text = preg_replace($urls[0], $urls[1], $text);
+                for($i=0; $i < sizeof($mentions[0]); $i++) {
                     $user = $repository_profile->findOneBy(array('username' => $mentions[1][$i]));
                     if ($user) {
                         $message->addMention($user);
@@ -73,6 +90,50 @@ class HomeController extends AbstractController
                     $hashtags[0][$i] = "~" . $hashtags[0][$i] . "~";
                     $em->persist($hashtag);
                 }
+                for($i=0; $i < sizeof($dp[0]); $i++) {
+                    $ch = curl_init("https://richie.u-strasbg.fr/~virgile/sf4/public/index.php/cours/diablo/");
+                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+                    $response = curl_exec($ch);
+                    curl_close($ch);
+                    $json_response = json_decode($response, true);
+                    $battletag = $json_response["battleTag"];
+                    if (str_replace("-", "#", $dp[1][$i]) == $battletag) {
+                        $parangon = $json_response["paragonLevel"];
+                        $parangon_season = $json_response["paragonLevelSeason"];
+                        $kills = $json_response["kills"]["monsters"];
+                        $kills_elites = $json_response["kills"]["elites"];
+                        $profile_name = $dp[1][$i];
+                        $dp[1][$i] = "<a href='#' data-toggle='popover' data-original-title='$profile_name' data-content='Parangon : $parangon ; Parangon saisonnier : $parangon_season ; Nombre de tués : $kills ; Nombre d élites tués : $kills_elites'>$profile_name</a>";
+                        $dp[0][$i] = "~" . $dp[0][$i] . "~";
+                        $text = preg_replace($dp[0], $dp[1], $text);
+                    }
+                }
+                for($i=0; $i < sizeof($dh[0]); $i++) {
+                    $values = explode("/", $dh[1][$i]);
+                    $ch = curl_init("https://richie.u-strasbg.fr/~virgile/sf4/public/index.php/cours/diablo/");
+                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+                    $response = curl_exec($ch);
+                    curl_close($ch);
+                    $json_response = json_decode($response, true);
+                    $battletag = $json_response["battleTag"];
+                    $found_hero = null;
+                    if (str_replace("-", "#", $values[0]) == $battletag) {
+                        foreach ($json_response["heroes"] as $hero) {
+                            if ($hero["name"] == $values[1]) {
+                                $found_hero = $hero;
+                                break;
+                            }
+                        }
+                        if ($found_hero) {
+                            $hero_name = $found_hero["name"];
+                            $hero_class = $found_hero["class"];
+                            $hero_level = $found_hero["level"];
+                            $dh[1][$i] = "<a href='#' data-toggle='popover' data-original-title='$hero_name' data-content='Classe : $hero_class Niveau : $hero_level'>$hero_name</a>";
+                            $dh[0][$i] = "~" . $dh[0][$i] . "~";
+                            $text = preg_replace($dh[0], $dh[1], $text);
+                        }
+                    }
+                }
                 $text = preg_replace($mentions[0], $mentions[1], $text);
                 $message->setText(preg_replace($hashtags[0], $hashtags[1], $text));
                 $message->setSender($profile);

+ 4 - 1
templates/base.html.twig

@@ -8,7 +8,10 @@
 		</title>
 		{# Run `composer require symfony/webpack-encore-bundle` and uncomment the following Encore helpers to start using Symfony UX #}
 		{% block stylesheets %}
+			<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
+			<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
 			<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
+			<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
 		{% endblock %}
 
 		{% block javascripts %}{% endblock %}
@@ -55,4 +58,4 @@
 			</div>
 		</body>
 	</body>
-</html></nav>{% block body %}{% endblock %}</body></html></body></html>
+</html>

+ 8 - 0
templates/home/index.html.twig

@@ -4,6 +4,7 @@
 {% endblock %}
 
 {% block body %}
+
 	<div class="d-flex flex-column align-items-center">
 		<div class="card" style="width: 75rem;">
 			{{ form_start(form) }}
@@ -27,3 +28,10 @@
 		{% endfor %}
 	</div>
 {% endblock %}
+{% block javascripts %}
+	<script>
+		$(document).ready(() => {
+			$('[data-toggle="popover"]').popover();
+		});
+	</script>
+{% endblock %}