diff --git a/commands/pozitie.php b/commands/pozitie.php index efbd42c..d79f790 100644 --- a/commands/pozitie.php +++ b/commands/pozitie.php @@ -18,14 +18,20 @@ function normalizeSpecialNumber($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) { $options = $interaction->data->options; $inmatriculare = $options['inmatriculare']->value; $numarparc = $options['numarparc']->value; if (!$inmatriculare && !$numarparc) { - $interaction->respondWithMessage(MessageBuilder::new()->setContent('Musai trebuie sa ai ori un numar de inmatriculare, ori un numar de parc.')); - return true; + $inmatriculare = null; } else if (!$inmatriculare) { $stbdb = new PDO("sqlite:" . __DIR__ . "/../stbdb.db"); $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); $vehicle = null; - - foreach ($response as $v) { - if ($v['vehicle']['vehicle']['licensePlate'] == $inmatriculare) { - $vehicle = $v; - break; + $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; + } } } + + if (!$vehicle) { $interaction->respondWithMessage(MessageBuilder::new()->setContent('Vehiculul nu a putut fi gasit.')); return true; @@ -71,7 +84,21 @@ $discord->listenCommand('pozitie', function (Interaction $interaction) { ) ->GetImage() ->saveJPG(__DIR__ . '/../pozitie.jpg', 70); - - $interaction->respondWithMessage(MessageBuilder::new()->addFile(__DIR__ . '/../pozitie.jpg')); + 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')); + } }); ?>