Skip to content

Commit

Permalink
Support new DB layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadyita committed May 22, 2021
1 parent a71b076 commit 42bfabb
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 207 deletions.
21 changes: 21 additions & 0 deletions Migrations/20210518155642_CreateSpawntimeTable.shared.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Nadybot\User\Modules\SPAWNTIME_MODULE\Migrations;

use Illuminate\Database\Schema\Blueprint;
use Nadybot\Core\DB;
use Nadybot\Core\LoggerWrapper;
use Nadybot\Core\SchemaMigration;

class CreateSpawntimeTable implements SchemaMigration {
public function migrate(LoggerWrapper $logger, DB $db): void {
$table = "spawntime";
$db->schema()->dropIfExists($table);
$db->schema()->create($table, function(Blueprint $table) {
$table->string("mob", 50)->primary();
$table->string("placeholder", 50)->nullable();
$table->boolean("can_skip_spawn")->nullable();
$table->integer("spawntime")->nullable();
});
}
}
5 changes: 3 additions & 2 deletions Spawntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nadybot\User\Modules\SPAWNTIME_MODULE;

use Illuminate\Support\Collection;
use Nadybot\Core\DBRow;

class Spawntime extends DBRow {
Expand All @@ -10,6 +11,6 @@ class Spawntime extends DBRow {
public ?bool $can_skip_spawn = null;
public ?int $spawntime = null;

/** @var WhereisCoordinates[] */
public $coordinates = [];
/** @var Collection<WhereisCoordinates> */
public Collection $coordinates;
}
117 changes: 42 additions & 75 deletions SpawntimeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nadybot\User\Modules\SPAWNTIME_MODULE;

use Illuminate\Support\Collection;
use Nadybot\Core\CommandReply;
use Nadybot\Core\DB;
use Nadybot\Core\LoggerWrapper;
Expand Down Expand Up @@ -44,7 +45,8 @@ class SpawntimeController {
*/
public function setup(): void {
// load database tables from .sql-files
$this->db->loadSQLFile($this->moduleName, 'spawntime');
$this->db->loadMigrations($this->moduleName, __DIR__ . '/Migrations');
$this->db->loadCSVFile($this->moduleName, __DIR__ . '/spawntime.csv');
}

/**
Expand All @@ -66,7 +68,7 @@ public function getLocationBlob(Spawntime $spawntime): string {
* Return the formatted entry for one mob
*/
protected function getMobLine(Spawntime $row, bool $displayDirectly): string {
$line = "<highlight>" . $row->mob . "<end>: ";
$line = "<highlight>{$row->mob}<end>: ";
if ($row->spawntime !== null) {
$line .= "<orange>" . strftime('%Hh%Mm%Ss', $row->spawntime) . "<end>";
} else {
Expand All @@ -84,17 +86,17 @@ protected function getMobLine(Spawntime $row, bool $displayDirectly): string {
if (count($flags)) {
$line .= ' (' . join(', ', $flags) . ')';
}
if ($displayDirectly === true && count($row->coordinates)) {
if ($displayDirectly === true && $row->coordinates->count()) {
$line .= " [" . $this->getLocationBlob($row) . "]";
} elseif (count($row->coordinates) > 1) {
} elseif ($row->coordinates->count() > 1) {
$line .= " [" .
$this->text->makeChatcmd(
"locations (" . count($row->coordinates) . ")",
"/tell <myname> whereis " . $row->mob
).
"]";
} elseif (count($row->coordinates) === 1) {
$coords = $row->coordinates[0];
} elseif ($row->coordinates->count() === 1) {
$coords = $row->coordinates->first();
if ($coords->playfield_id != 0 && $coords->xcoord != 0 && $coords->ycoord != 0) {
$line .= " [".
$this->text->makeChatcmd(
Expand All @@ -111,26 +113,17 @@ protected function getMobLine(Spawntime $row, bool $displayDirectly): string {
* Command to list all Spawntimes
*
* @HandlesCommand("spawntime")
* @Matches("/^spawntime?$/i")
* @Matches("/^spawntime$/i")
*/
public function spawntimeListCommand(string $message, string $channel, string $sender, CommandReply $sendto, array $args): void {
$sql = "SELECT * FROM spawntime s ".
"LEFT JOIN whereis w ON ";
if ($this->db->getType() === $this->db::MYSQL) {
$sql .= "(LOWER(w.name) LIKE CONCAT(LOWER(s.mob), '%'))";
} else {
$sql .= "(LOWER(w.name) LIKE LOWER(s.mob) || '%')";
}
$sql .= " LEFT JOIN playfields p ON (p.id=w.playfield_id) ORDER BY mob ASC";
/** @var Spawntime[] */
$allTimes = $this->db->fetchAll(Spawntime::class, $sql);
if (!count($allTimes)) {
$spawnTimes = $this->db->table("spawntime")->asObj(Spawntime::class);
if ($spawnTimes->isEmpty()) {
$msg = 'There are currently no spawntimes in the database.';
$sendto->reply($msg);
return;
}
$timeLines = $this->spawntimesToLines($allTimes);
$msg = $this->text->makeBlob('All known spawntimes', join("\n", $timeLines));
$timeLines = $this->spawntimesToLines($spawnTimes);
$msg = $this->text->makeBlob('All known spawntimes', $timeLines->join("\n"));
$sendto->reply($msg);
}

Expand All @@ -142,74 +135,48 @@ public function spawntimeListCommand(string $message, string $channel, string $s
*/
public function spawntimeSearchCommand(string $message, string $channel, string $sender, CommandReply $sendto, array $args): void {
$args[1] = trim($args[1]);
$tokens = array_map(
function($token) {
return "%$token%";
},
explode(" ", $args[1])
);
$sql = "SELECT s.*, w.*, p.short_name, p.long_name ".
"FROM spawntime s ".
"LEFT JOIN whereis w ON ";
if ($this->db->getType() === $this->db::MYSQL) {
$sql .= "(LOWER(w.name) LIKE CONCAT(LOWER(s.mob), '%'))";
} else {
$sql .= "(LOWER(w.name) LIKE LOWER(s.mob) || '%')";
}
$sql .= " LEFT JOIN playfields p ON (p.id=w.playfield_id) ".
"WHERE ";
$partsMob = array_fill(0, count($tokens), "mob LIKE ?");
$partsPlaceholder = array_fill(0, count($tokens), "placeholder LIKE ?");
$sql .= "(" . join(" AND ", $partsMob).")".
" OR ".
"(" . join(" AND ", $partsPlaceholder) . ") ".
"ORDER BY mob ASC";
$allTimes = $this->db->fetchAll(Spawntime::class, $sql, ...[...$tokens, ...$tokens]);
if (!count($allTimes)) {
$tokens = explode(" ", $args[1]);
$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);
return;
}
$timeLines = $this->spawntimesToLines($allTimes);
$count = count($timeLines);
$timeLines = $this->spawntimesToLines($spawnTimes);
$count = $timeLines->count();
if ($count === 1) {
$msg = $timeLines[0];
$msg = $timeLines->first();
} elseif ($count < 4) {
$msg = "Spawntimes matching <highlight>{$args[1]}<end>:\n".
join("\n", $timeLines);
$timeLines->join("\n");
} else {
$msg = $this->text->makeBlob("Spawntimes for \"{$args[1]}\" ($count)", join("\n", $timeLines));
$msg = $this->text->makeBlob(
"Spawntimes for \"{$args[1]}\" ($count)",
$timeLines->join("\n")
);
}
$sendto->reply($msg);
}

/**
* @param Spawntime[] $spawntimes
* @return string[]
* @param Collection<Spawntime> $spawnTimes
* @return Collection<string>
*/
protected function spawntimesToLines(array $spawntimes): array {
$oldMob = null;
$allData = [];
foreach ($spawntimes as $spawntime) {
if ($oldMob !== null && $oldMob->mob !== $spawntime->mob) {
$allData []= $oldMob;
$oldMob = null;
}
if ($oldMob === null) {
$oldMob = $spawntime;
}
if (isset($spawntime->answer)) {
$oldMob->coordinates []= new WhereisCoordinates($spawntime);
}
}
$allData []= $oldMob;
$spawntimes = $allData;
$displayDirectly = count($spawntimes) < 4;
$timeLines = array_map(
[$this,'getMobLine'],
$spawntimes,
array_fill(0, count($spawntimes), $displayDirectly)
);
return $timeLines;
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;
});
});
$displayDirectly = $spawnTimes->count() < 4;
return $spawnTimes->map(function(Spawntime $spawn) use ($displayDirectly) {
return $this->getMobLine($spawn, $displayDirectly);
});
}
}
13 changes: 0 additions & 13 deletions WhereisCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,4 @@

class WhereisCoordinates extends Playfield {
use WhereisTrait;

public function __construct(Spawntime $spawn) {
$this->answer = $spawn->answer;
$this->keywords = $spawn->keywords;
if (isset($spawn->long_name)) {
$this->long_name = $spawn->long_name;
}
$this->name = $spawn->name;
$this->playfield_id = (int)$spawn->playfield_id;
$this->short_name = $spawn->short_name;
$this->xcoord = (int)$spawn->xcoord;
$this->ycoord = (int)$spawn->ycoord;
}
}
4 changes: 2 additions & 2 deletions aopkg.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name = "SPAWNTIME_MODULE"
description = "Query respawn-timers for mobs"
version = "0.1.2"
version = "0.1.3"
github = "Nadybot/SPAWNTIME_MODULE"
author = "Nadyita"
bot_type = "Nadybot"
bot_version = "^5.0.0-beta1"
bot_version = "^5.1.0"

[requires]
ext-Reflection = "*"
Expand Down
107 changes: 107 additions & 0 deletions spawntime.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
mob,placeholder,can_skip_spawn,spawntime
"A Sapling",,0,7200
"Abandoned Hope",,0,3600
"Acidic Prisoner",,0,900
"Assault Commander Pax",,0,7200
"Assault Commander Sleek",,0,7200
"Best in Brass",,0,1200
"Brigadier Crux",,0,7200
"Burning Prisoner",,0,900
"Carillion Thunder",,0,7200
"Cenobite Shadow",,0,3600
"Chief Gunner Kyle Swan",,0,7200
"Clan Modified A-4000",,0,
"Clan Modified A-4001",,0,
"Colonel Frank Kaehler",,1,1200
"Commander Jocasta",,0,3600
"Commander Kelly Frederickson",,1,1200
Cuty,,0,1200
"Cyborg Executioner",,0,600
"Dangerous Prisoner",,0,900
"Daria Marie Walzer",,1,1200
"Deadly Prisoner",,0,900
"Defender of the Three",,0,600
"Desert Nomad Commando",,0,7200
"Diamondine Soldier","Diamondine Trainee",0,1200
"Diseased Prisoner",,0,900
"Electro Unique",,0,1200
"Elian Zuwadza",,1,1200
"Explosive Prisoner",,0,900
"Fiery Soldier","Fiery Trainee",0,900
"Force Recon Commander 191",,0,7200
Galvano,,0,1200
"Ganking Uncle Pumpkin-Head",,0,21600
"Gartua the Doorkeeper",,0,600
"General Kronillis",,0,600
"General Nirtox",,0,600
"General Vivyan",,0,300
George,,0,1200
"Greasy Joints",,0,1200
"Guardian of Tomorrow",,0,600
"High Commander Fredrickson",,0,10800
"High Commander Jameson",,0,600
"High Commander Riker",,0,600
"Jack ""Leg-Chopper"" Menendez",,0,65400
"Jack Legchopper Clone","Soul Swapper",0,900
Joo,,0,1200
"Lab Director",,1,420
"Lien the Memorystalker",,0,600
"Live Metal",,0,1200
"Ljotur the Lunatic","Polymorphed Lunatic",0,900
"Lord of Sand",,0,1200
"Loren Warr",,0,32400
M.A.G.S.,,0,600
"Major Woon",,0,7200
"Mantis Queen",,0,2100
"Marcus Robicheaux",,1,1200
Metalomania,,0,1200
"Mick Nugget McMullet",,1,1200
Morty,,0,1200
"Murderous Prisoner",,0,900
"Notum Soldier","Notum Trainee",0,1200
"Nuts & Bolts",,0,1200
"Obediency Enforcer","Obediency Inspector",0,900
"Omni-Pol Command Juggernaut",,0,10800
Oscar,,0,1200
Otacustes,"Primus &lt;something&gt;",0,900
Ownz,,0,1200
"Peal Thunder",,0,7200
"Poisonous Prisoner",,0,900
Polly,,0,1200
Powa,,0,1200
"Prime Evolution Huzzum",,0,120
"Professor Van Horn",,1,1200
"Prototype A-9000 Guardbot",,0,
"Prototype Inferno",,0,600
"R-2000 Vermin Disposal Unit",,1,1200
"Razor the Battletoad",,0,7200
"Research Dome 1 Warden",,0,3600
"Research Dome 2 Warden",,0,3600
"Research Dome 3 Warden",,0,3600
"Resurrecting Swamp Hag",,0,7200
"Silenced Prisoner",,0,900
"Special Agent Deko",,0,7200
"Special Agent Lamb","Lamb's Apprentice",0,900
"Special Agent Moxy",,0,7200
"Spiritual Prisoner",,0,900
"Stinking Scorpid",,0,1800
Stumpy,,0,1200
"Supply Master Eel",,0,10800
"Supply Master Smug",,0,10800
Tarasque,,0,32400
"Temporal Prisoner",,0,900
"The Curator",,0,600
"The Escaped Gargantula",,1,1200
"The Hollow Reaper",,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
"Trash King",,0,1200
"Trash King Lackey",,1,420
"Tri Plumbo",,0,600
"Umbral Prisoner",,0,900
"Unicorn Access Supervisor",,0,3600
"Warchief Skawt",,0,600
Wardog,,0,600
"Xark the Battletoad",,0,7200
Loading

0 comments on commit 42bfabb

Please sign in to comment.