adauga modul 'random la /pozitie'

This commit is contained in:
fzorb 2025-01-11 08:22:12 +02:00
parent 2b904abc48
commit 1564d26df2

View File

@ -18,14 +18,20 @@ function normalizeSpecialNumber($input) {
return $input; return $input;
} }
function reverseNormalizeSpecialNumber($input) {
if (preg_match('/^B(\d{5})$/', $input, $matches)) {
return 'B-' . $matches[1];
}
return $input;
}
$discord->listenCommand('pozitie', function (Interaction $interaction) { $discord->listenCommand('pozitie', function (Interaction $interaction) {
$options = $interaction->data->options; $options = $interaction->data->options;
$inmatriculare = $options['inmatriculare']->value; $inmatriculare = $options['inmatriculare']->value;
$numarparc = $options['numarparc']->value; $numarparc = $options['numarparc']->value;
if (!$inmatriculare && !$numarparc) { if (!$inmatriculare && !$numarparc) {
$interaction->respondWithMessage(MessageBuilder::new()->setContent('Musai trebuie sa ai ori un numar de inmatriculare, ori un numar de parc.')); $inmatriculare = null;
return true;
} else if (!$inmatriculare) { } else if (!$inmatriculare) {
$stbdb = new PDO("sqlite:" . __DIR__ . "/../stbdb.db"); $stbdb = new PDO("sqlite:" . __DIR__ . "/../stbdb.db");
$stmt = $stbdb->prepare("SELECT inmatriculare FROM inmatriculari WHERE parc = :parc"); $stmt = $stbdb->prepare("SELECT inmatriculare FROM inmatriculari WHERE parc = :parc");
@ -47,14 +53,21 @@ $discord->listenCommand('pozitie', function (Interaction $interaction) {
$response = json_decode($request->getBody(), true); $response = json_decode($request->getBody(), true);
$vehicle = null; $vehicle = null;
$random = false;
foreach ($response as $v) { if (!$inmatriculare && !$numarparc) {
if ($v['vehicle']['vehicle']['licensePlate'] == $inmatriculare) { $vehicle = $response[array_rand($response)];
$vehicle = $v; $random = true;
break; } else {
foreach ($response as $v) {
if ($v['vehicle']['vehicle']['licensePlate'] == $inmatriculare) {
$vehicle = $v;
break;
}
} }
} }
if (!$vehicle) { if (!$vehicle) {
$interaction->respondWithMessage(MessageBuilder::new()->setContent('Vehiculul nu a putut fi gasit.')); $interaction->respondWithMessage(MessageBuilder::new()->setContent('Vehiculul nu a putut fi gasit.'));
return true; return true;
@ -71,7 +84,21 @@ $discord->listenCommand('pozitie', function (Interaction $interaction) {
) )
->GetImage() ->GetImage()
->saveJPG(__DIR__ . '/../pozitie.jpg', 70); ->saveJPG(__DIR__ . '/../pozitie.jpg', 70);
if ($random) {
$interaction->respondWithMessage(MessageBuilder::new()->addFile(__DIR__ . '/../pozitie.jpg')); $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'));
}
}); });
?> ?>