From 886705537c563f7c4de7d77248c54359fddc332a Mon Sep 17 00:00:00 2001 From: Nadyita Date: Fri, 25 Feb 2022 11:04:51 +0100 Subject: [PATCH] Migrate to Nadybot 6 --- .github/workflows/vendor.yml | 38 +++++++++ README.txt | 2 + Spawntime.php | 3 +- SpawntimeController.php | 144 +++++++++++++++++------------------ WhereisCoordinates.php | 10 --- aopkg.toml | 5 +- spawntime.csv | 2 +- spawntime.txt | 6 -- 8 files changed, 113 insertions(+), 97 deletions(-) create mode 100644 .github/workflows/vendor.yml create mode 100644 README.txt delete mode 100644 WhereisCoordinates.php delete mode 100644 spawntime.txt diff --git a/.github/workflows/vendor.yml b/.github/workflows/vendor.yml new file mode 100644 index 0000000..eb2b679 --- /dev/null +++ b/.github/workflows/vendor.yml @@ -0,0 +1,38 @@ +name: Create release bundle +on: + release: + types: [published] + +jobs: + build: + name: Create vendor bundle + runs-on: ubuntu-20.04 + steps: + - name: Checkout sources + uses: actions/checkout@v2 + - name: Setup PHP 8.0 with composer + uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + - name: Create directory zip + shell: bash + run: | + cd .. + zip -r spawntime-bundle-${{ github.event.release.tag_name }}.zip SPAWNTIME_MODULE/ -x "*.git*" + mv spawntime-bundle-${{ github.event.release.tag_name }}.zip SPAWNTIME_MODULE/ + - name: Upload Release Asset + id: upload-release-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ github.event.release.upload_url }} + asset_path: ./spawntime-bundle-${{ github.event.release.tag_name }}.zip + asset_name: spawntime-bundle-${{ github.event.release.tag_name }}.zip + asset_content_type: application/zip + - name: Notify aopkg + uses: distributhor/workflow-webhook@v1 + env: + webhook_type: 'json-extended' + webhook_url: 'https://pkg.aobots.org/webhook' + webhook_secret: ${{ secrets.WEBHOOK_SECRET }} diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..35e3d51 --- /dev/null +++ b/README.txt @@ -0,0 +1,2 @@ +Allows you to query respawn-timers for mobs and check +if they can skip a spawn and/or have placeholders. diff --git a/Spawntime.php b/Spawntime.php index 17f5b58..a1e36c6 100644 --- a/Spawntime.php +++ b/Spawntime.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Nadybot\Core\DBRow; +use Nadybot\Modules\WHEREIS_MODULE\WhereisResult; class Spawntime extends DBRow { public string $mob; @@ -11,6 +12,6 @@ class Spawntime extends DBRow { public ?bool $can_skip_spawn = null; public ?int $spawntime = null; - /** @var Collection */ + /** @var Collection */ public Collection $coordinates; } diff --git a/SpawntimeController.php b/SpawntimeController.php index 3b094c5..ae0e8b1 100644 --- a/SpawntimeController.php +++ b/SpawntimeController.php @@ -2,75 +2,74 @@ namespace Nadybot\User\Modules\SPAWNTIME_MODULE; +use DateTime; +use DateTimeZone; +use Exception; use Illuminate\Support\Collection; -use Nadybot\Core\CommandReply; -use Nadybot\Core\DB; -use Nadybot\Core\LoggerWrapper; -use Nadybot\Core\Text; -use Nadybot\Core\Util; +use Nadybot\Core\{ + Attributes as NCA, + CmdContext, + DB, + ModuleInstance, + Text, +}; +use Nadybot\Modules\WHEREIS_MODULE\WhereisController; +use Nadybot\Modules\WHEREIS_MODULE\WhereisResult; /** * @author Nadyita (RK5) - * - * @Instance - * - * Commands this controller contains: - * @DefineCommand( - * command = 'spawntime', - * accessLevel = 'all', - * description = 'Show (re)spawntimers', - * alias = 'spawn', - * help = 'spawntime.txt' - * ) */ -class SpawntimeController { - public string $moduleName; - - /** @Inject */ +#[ + NCA\Instance, + NCA\HasMigrations, + NCA\DefineCommand( + command: 'spawntime', + accessLevel: 'guest', + description: 'Show (re)spawntimers', + alias: 'spawn', + ) +] +class SpawntimeController extends ModuleInstance { + #[NCA\Inject] public DB $db; - /** @Inject */ + #[NCA\Inject] public Text $text; - /** @Inject */ - public Util $util; - - /** @Logger */ - public LoggerWrapper $logger; + #[NCA\Inject] + public WhereisController $whereisController; - /** - * @Setup - * This handler is called on bot startup. - */ + #[NCA\Setup] public function setup(): void { - // load database tables from .sql-files - $this->db->loadMigrations($this->moduleName, __DIR__ . '/Migrations'); $this->db->loadCSVFile($this->moduleName, __DIR__ . '/spawntime.csv'); } - /** - * @return string[] - */ public function getLocationBlob(Spawntime $spawntime): string { $blob = ''; foreach ($spawntime->coordinates as $row) { - $blob .= "$row->name\n$row->answer"; + $blob .= "{$row->name}\n". + "{$row->answer}"; if ($row->playfield_id !== 0 && $row->xcoord !== 0 && $row->ycoord !== 0) { - $blob .= " " . $this->text->makeChatcmd("waypoint: {$row->xcoord}x{$row->ycoord} {$row->short_name}", "/waypoint {$row->xcoord} {$row->ycoord} {$row->playfield_id}"); + $blob .= " [" . $row->toWaypoint() . "]"; } $blob .= "\n\n"; } - return $this->text->makeBlob("locations (" . count($spawntime->coordinates).")", $blob); + $msg = $this->text->makeBlob("locations (" . count($spawntime->coordinates).")", $blob); + if (is_array($msg)) { + throw new Exception("Too many spawn locations for {$spawntime->mob}."); + } + return $msg; } /** * Return the formatted entry for one mob */ protected function getMobLine(Spawntime $row, bool $displayDirectly): string { - $line = "{$row->mob}: "; + $line = "{$row->mob}: "; if ($row->spawntime !== null) { - $line .= "" . strftime('%Hh%Mm%Ss', $row->spawntime) . ""; + $time = DateTime::createFromFormat("U", (string)$row->spawntime, new DateTimeZone("UTC")); + $line .= "" . $time->format('H\hi\ms\s') . ""; } else { $line .= "<unknown>"; } @@ -80,11 +79,11 @@ protected function getMobLine(Spawntime $row, bool $displayDirectly): string { if ($row->can_skip_spawn) { $flags[] = 'can skip spawn'; } - if (strlen($row->placeholder??"")) { + if (isset($row->placeholder) && strlen($row->placeholder)) { $flags[] = "placeholder: " . $row->placeholder; } if (count($flags)) { - $line .= ' (' . join(', ', $flags) . ')'; + $line .= ' (' . join(', ', $flags) . ')'; } if ($displayDirectly === true && $row->coordinates->count()) { $line .= " [" . $this->getLocationBlob($row) . "]"; @@ -96,53 +95,44 @@ protected function getMobLine(Spawntime $row, bool $displayDirectly): string { ). "]"; } elseif ($row->coordinates->count() === 1) { - $coords = $row->coordinates->first(); + /** @var WhereisResult */ + $coords = $row->coordinates->firstOrFail(); if ($coords->playfield_id != 0 && $coords->xcoord != 0 && $coords->ycoord != 0) { - $line .= " [". - $this->text->makeChatcmd( - "{$coords->xcoord}x{$coords->ycoord} {$coords->short_name}", - "/waypoint {$coords->xcoord} {$coords->ycoord} {$coords->playfield_id}" - ). - "]"; + $line .= " [". $coords->toWaypoint() . "]"; } } return $line; } /** - * Command to list all Spawntimes - * - * @HandlesCommand("spawntime") - * @Matches("/^spawntime$/i") + * List all spawn times */ - public function spawntimeListCommand(string $message, string $channel, string $sender, CommandReply $sendto, array $args): void { + #[NCA\HandlesCommand("spawntime")] + public function spawntimeListCommand(CmdContext $context): void { $spawnTimes = $this->db->table("spawntime")->asObj(Spawntime::class); if ($spawnTimes->isEmpty()) { $msg = 'There are currently no spawntimes in the database.'; - $sendto->reply($msg); + $context->reply($msg); return; } $timeLines = $this->spawntimesToLines($spawnTimes); $msg = $this->text->makeBlob('All known spawntimes', $timeLines->join("\n")); - $sendto->reply($msg); + $context->reply($msg); } /** - * Command to list all Spawntimes - * - * @HandlesCommand("spawntime") - * @Matches("/^spawntime (.+)$/i") + * Search for spawn times */ - public function spawntimeSearchCommand(string $message, string $channel, string $sender, CommandReply $sendto, array $args): void { - $args[1] = trim($args[1]); - $tokens = explode(" ", $args[1]); + #[NCA\HandlesCommand("spawntime")] + public function spawntimeSearchCommand(CmdContext $context, string $search): void { + $tokens = explode(" ", $search); $query = $this->db->table("spawntime"); $this->db->addWhereFromParams($query, $tokens, "mob"); $this->db->addWhereFromParams($query, $tokens, "placeholder", "or"); $spawnTimes = $query->asObj(Spawntime::class); if ($spawnTimes->isEmpty()) { - $msg = "No spawntime matching {$args[1]}."; - $sendto->reply($msg); + $msg = "No spawntime matching {$search}."; + $context->reply($msg); return; } $timeLines = $this->spawntimesToLines($spawnTimes); @@ -150,15 +140,15 @@ public function spawntimeSearchCommand(string $message, string $channel, string if ($count === 1) { $msg = $timeLines->first(); } elseif ($count < 4) { - $msg = "Spawntimes matching {$args[1]}:\n". + $msg = "Spawntimes matching {$search}:\n". $timeLines->join("\n"); } else { $msg = $this->text->makeBlob( - "Spawntimes for \"{$args[1]}\" ($count)", + "Spawntimes for \"{$search}\" ($count)", $timeLines->join("\n") ); } - $sendto->reply($msg); + $context->reply($msg); } /** @@ -166,17 +156,19 @@ public function spawntimeSearchCommand(string $message, string $channel, string * @return Collection */ protected function spawntimesToLines(Collection $spawnTimes): Collection { - $locations = $this->db->table("whereis as w") - ->join("playfields as p", "p.id", "w.playfield_id") - ->asObj(WhereisCoordinates::class); - $spawnTimes->each(function (Spawntime $spawn) use ($locations) { - $spawn->coordinates = $locations->filter(function (WhereisCoordinates $coords) use ($spawn): bool { - return strncasecmp($coords->name, $spawn->mob, strlen($spawn->mob)) === 0; - }); + $mobs = $this->whereisController->getAll(); + $spawnTimes->each(function (Spawntime $spawn) use ($mobs) { + $spawn->coordinates = $mobs->filter( + function (WhereisResult $row) use ($spawn): bool { + return strncasecmp($row->name, $spawn->mob, strlen($spawn->mob)) === 0; + } + )->values(); }); $displayDirectly = $spawnTimes->count() < 4; - return $spawnTimes->map(function(Spawntime $spawn) use ($displayDirectly) { + /** @var Collection */ + $result = $spawnTimes->map(function(Spawntime $spawn) use ($displayDirectly): string { return $this->getMobLine($spawn, $displayDirectly); }); + return $result; } } diff --git a/WhereisCoordinates.php b/WhereisCoordinates.php deleted file mode 100644 index d4a29ee..0000000 --- a/WhereisCoordinates.php +++ /dev/null @@ -1,10 +0,0 @@ -spawn <name of the mob>. -spawn wardog - -
Result: -Spawntimes matching Wardog: -Wardog: 10m \ No newline at end of file