Skip to content

Commit

Permalink
Migrate to Nadybot 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadyita committed Feb 25, 2022
1 parent 42bfabb commit 8867055
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 97 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/vendor.yml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allows you to query respawn-timers for mobs and check
if they can skip a spawn and/or have placeholders.
3 changes: 2 additions & 1 deletion Spawntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Illuminate\Support\Collection;
use Nadybot\Core\DBRow;
use Nadybot\Modules\WHEREIS_MODULE\WhereisResult;

class Spawntime extends DBRow {
public string $mob;
public ?string $placeholder = null;
public ?bool $can_skip_spawn = null;
public ?int $spawntime = null;

/** @var Collection<WhereisCoordinates> */
/** @var Collection<WhereisResult> */
public Collection $coordinates;
}
144 changes: 68 additions & 76 deletions SpawntimeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) <nadyita@hodorraid.org>
*
* @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 .= "<header2>$row->name<end>\n$row->answer";
$blob .= "<header2>{$row->name}<end>\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 = "<highlight>{$row->mob}<end>: ";
$line = "{$row->mob}: ";
if ($row->spawntime !== null) {
$line .= "<orange>" . strftime('%Hh%Mm%Ss', $row->spawntime) . "<end>";
$time = DateTime::createFromFormat("U", (string)$row->spawntime, new DateTimeZone("UTC"));
$line .= "<orange>" . $time->format('H\hi\ms\s') . "<end>";
} else {
$line .= "<orange>&lt;unknown&gt;<end>";
}
Expand All @@ -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 .= ' (<highlight>' . join(', ', $flags) . '<end>)';
}
if ($displayDirectly === true && $row->coordinates->count()) {
$line .= " [" . $this->getLocationBlob($row) . "]";
Expand All @@ -96,87 +95,80 @@ 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 <highlight>{$args[1]}<end>.";
$sendto->reply($msg);
$msg = "No spawntime matching <highlight>{$search}<end>.";
$context->reply($msg);
return;
}
$timeLines = $this->spawntimesToLines($spawnTimes);
$count = $timeLines->count();
if ($count === 1) {
$msg = $timeLines->first();
} elseif ($count < 4) {
$msg = "Spawntimes matching <highlight>{$args[1]}<end>:\n".
$msg = "Spawntimes matching <highlight>{$search}<end>:\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);
}

/**
* @param Collection<Spawntime> $spawnTimes
* @return Collection<string>
*/
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<string> */
$result = $spawnTimes->map(function(Spawntime $spawn) use ($displayDirectly): string {
return $this->getMobLine($spawn, $displayDirectly);
});
return $result;
}
}
10 changes: 0 additions & 10 deletions WhereisCoordinates.php

This file was deleted.

5 changes: 2 additions & 3 deletions aopkg.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
name = "SPAWNTIME_MODULE"
description = "Query respawn-timers for mobs"
version = "0.1.3"
version = "0.1.4"
github = "Nadybot/SPAWNTIME_MODULE"
author = "Nadyita"
bot_type = "Nadybot"
bot_version = "^5.1.0"
bot_version = "^6.0.0-alpha.1"

[requires]
ext-Reflection = "*"
ext-pcre = "*"
ext-date = "*"
2 changes: 1 addition & 1 deletion spawntime.csv
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Tarasque,,0,32400
"The Iron Reet",,1,1200
"The One","Harbinger of the One",1,1200
"The Pest","Rotten Plague",0,900
"Torrith the Ancient",,1,1200
"Torrith The Ancient",,1,1200
"Trash King",,0,1200
"Trash King Lackey",,1,420
"Tri Plumbo",,0,600
Expand Down
6 changes: 0 additions & 6 deletions spawntime.txt

This file was deleted.

0 comments on commit 8867055

Please sign in to comment.