From 219851ffaf09cb923bb0b2f82be4003d7131332b Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 7 Sep 2022 17:52:48 -0700 Subject: [PATCH] Plugin/WordMorph: add `neighbors` subcommand; improve `custom` error messages --- lib/PBot/Plugin/WordMorph.pm | 25 ++++++++++++++++++++++--- lib/PBot/VERSION.pm | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/PBot/Plugin/WordMorph.pm b/lib/PBot/Plugin/WordMorph.pm index 0130c78a..f3af680f 100644 --- a/lib/PBot/Plugin/WordMorph.pm +++ b/lib/PBot/Plugin/WordMorph.pm @@ -35,7 +35,7 @@ sub unload { } use constant { - USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom | solve | hint [from direction] | check | show | giveup', + USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom | solve | hint [from direction] | check | neighbors | show | giveup', NO_MORPH_AVAILABLE => "There is no word morph available. Use `wordmorph start [steps to solve [word length]]` to create one.", DB_UNAVAILABLE => "Word morph database not available.", LEFT => 0, @@ -56,6 +56,22 @@ sub wordmorph { my $channel = $context->{from}; given ($command) { + when ('neighbors') { + if (!@args || @args > 1) { + return 'Usage: wordmorph neighbors ; list the neighbors of a given word'; + } + + return DB_UNAVAILABLE if not $self->{db}; + + if (not exists $self->{db}->{length $args[0]}->{$args[0]}) { + return "I do not know this word `$args[0]`."; + } + + my @neighbors = @{$self->{db}->{length $args[0]}->{$args[0]}}; + my $count = @neighbors; + return "`$args[0]` has $count neighbor" . ($count != 1 ? 's' : '') . ": " . join(', ', sort @neighbors); + } + when ('check') { if (!@args || @args > 1) { return 'Usage: wordmorph check ; check if a word exists in the Word Morph database'; @@ -400,11 +416,14 @@ sub transform { my @path; my (%leftstarts, %rightstarts); + die "I do not know this word `$left`.\n" if not exists $list->{$left}; + die "I do not know this word `$right`.\n" if not exists $list->{$right}; + SEARCH: for (;;) { my @left_ids = $leftstart..$#left; # choose array of indices of new words $leftstart = $#left; - die "Cannot create word morph! Bad word '$left' :(\n" if $leftstarts{$leftstart}++ > 2; # finish search if the path could not be found + die "Cannot find a path from `$left` to `$right`.\n" if $leftstarts{$leftstart}++ > 2; # finish search if the path could not be found for my $id (@left_ids) { # come through all new words my @prefix = @{$left[$id]}; my $searched = pop @prefix; @@ -425,7 +444,7 @@ sub transform { my @right_ids = $rightstart..$#right; # all the same :) the tree is build from both ends to speed up the process $rightstart = $#right; - die "Cannot create word morph! Bad word '$right'\n" if $rightstarts{$rightstart}++ > 2; + die "Cannot find a path from `$left` to `$right`.\n" if $rightstarts{$rightstart}++ > 2; # finish search if the path could not be found for my $id (@right_ids) { # build right relational table my @prefix = @{$right[$id]}; my $searched = pop @prefix; diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index a0e0da63..ea2c9c4a 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4582, - BUILD_DATE => "2022-09-06", + BUILD_REVISION => 4584, + BUILD_DATE => "2022-09-07", }; sub initialize {}