ikarusbot/commands/pozitie.php

105 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2025-01-10 19:52:34 +02:00
<?php
use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\WebSockets\Event;
use Discord\Parts\Interactions\Command\Command;
use Discord\Parts\Interactions\Interaction;
use Discord\Builders\MessageBuilder;
use \DantSu\OpenStreetMapStaticAPI\OpenStreetMap;
use \DantSu\OpenStreetMapStaticAPI\LatLng;
use \DantSu\OpenStreetMapStaticAPI\Polygon;
use \DantSu\OpenStreetMapStaticAPI\Markers;
function normalizeSpecialNumber($input) {
if (preg_match('/^B-(\d{5})$/', $input, $matches)) {
return 'B' . $matches[1];
}
return $input;
}
2025-01-11 08:22:12 +02:00
function reverseNormalizeSpecialNumber($input) {
if (preg_match('/^B(\d{5})$/', $input, $matches)) {
return 'B-' . $matches[1];
}
return $input;
}
2025-01-10 19:52:34 +02:00
$discord->listenCommand('pozitie', function (Interaction $interaction) {
$options = $interaction->data->options;
$inmatriculare = $options['inmatriculare']->value;
$numarparc = $options['numarparc']->value;
if (!$inmatriculare && !$numarparc) {
2025-01-11 08:22:12 +02:00
$inmatriculare = null;
} else if (!$inmatriculare) {
$stbdb = new PDO("sqlite:" . __DIR__ . "/../stbdb.db");
$stmt = $stbdb->prepare("SELECT inmatriculare FROM inmatriculari WHERE parc = :parc");
$stmt->bindParam(':parc', $numarparc);
$stmt->execute();
$inmatriculare = normalizeSpecialNumber($stmt->fetch(PDO::FETCH_COLUMN));
}
echo $inmatriculare;
2025-01-10 19:52:34 +02:00
$client = new \GuzzleHttp\Client();
$request = $client->request('GET', 'https://maps.mo-bi.ro/api/busData');
if ($request->getStatusCode() !== 200) {
$interaction->respondWithMessage(
MessageBuilder::new()->setContent(
'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The code monkeys at our headquarters are working VEWY HAWD to fix this'
)
);
return true;
}
$response = json_decode($request->getBody(), true);
$vehicle = null;
2025-01-11 08:22:12 +02:00
$random = false;
if (!$inmatriculare && !$numarparc) {
$vehicle = $response[array_rand($response)];
$random = true;
} else {
foreach ($response as $v) {
if ($v['vehicle']['vehicle']['licensePlate'] == $inmatriculare) {
$vehicle = $v;
break;
}
2025-01-10 19:52:34 +02:00
}
}
2025-01-11 08:22:12 +02:00
2025-01-10 19:52:34 +02:00
if (!$vehicle) {
$interaction->respondWithMessage(MessageBuilder::new()->setContent('Vehiculul nu a putut fi gasit.'));
return true;
}
$latitude = $vehicle['vehicle']['position']['latitude'];
$longitude = $vehicle['vehicle']['position']['longitude'];
(new OpenStreetMap(new LatLng($vehicle['vehicle']['position']['latitude'], $vehicle['vehicle']['position']['longitude']), 16, 600, 300))
->addMarkers(
(new Markers(__DIR__ . '/../resources/marker.png'))
->setAnchor(Markers::ANCHOR_CENTER, Markers::ANCHOR_BOTTOM)
->addMarker(new LatLng($vehicle['vehicle']['position']['latitude'], $vehicle['vehicle']['position']['longitude']))
)
->GetImage()
->saveJPG(__DIR__ . '/../pozitie.jpg', 70);
2025-01-11 08:22:12 +02:00
if ($random) {
$stbdb = new PDO("sqlite:" . __DIR__ . "/../stbdb.db");
$stmt = $stbdb->prepare("SELECT parc FROM inmatriculari WHERE inmatriculare = :inmatriculare");
$stmt->bindParam(':inmatriculare', reverseNormalizeSpecialNumber($vehicle['vehicle']['vehicle']['licensePlate']));
$stmt->execute();
$vehParc = $stmt->fetch(PDO::FETCH_COLUMN);
if (!$vehParc && $vehicle['vehicle']['vehicle']['licensePlate']) {
$vehParc = $vehicle['vehicle']['vehicle']['licensePlate'];
} else if (!$vehParc && !$vehicle['vehicle']['vehicle']['licensePlate']) {
$vehParc = "Nu am putut identifica acest vehicul.";
}
$interaction->respondWithMessage(MessageBuilder::new()->setContent("**" . $vehParc . "**")->addFile(__DIR__ . '/../pozitie.jpg'));
} else {
$interaction->respondWithMessage(MessageBuilder::new()->addFile(__DIR__ . '/../pozitie.jpg'));
}
2025-01-10 19:52:34 +02:00
});
?>