From ca7670da3d42c72dade29cec14cc29cf85b16340 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 9 Mar 2024 15:42:59 -0800 Subject: [PATCH] Plugin/Wordle: add `letters` subcommand to display good/unknown letters --- data/plugin_autoload | 1 + lib/PBot/Plugin/Wordle.pm | 45 ++++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/data/plugin_autoload b/data/plugin_autoload index d1972513..979d065b 100644 --- a/data/plugin_autoload +++ b/data/plugin_autoload @@ -20,5 +20,6 @@ TypoSub UrlTitles Weather Wolfram +Wordle WordMorph Wttr diff --git a/lib/PBot/Plugin/Wordle.pm b/lib/PBot/Plugin/Wordle.pm index 3fcdced0..43d4b0c4 100644 --- a/lib/PBot/Plugin/Wordle.pm +++ b/lib/PBot/Plugin/Wordle.pm @@ -24,12 +24,15 @@ sub unload($self) { } use constant { - USAGE => 'Usage: wordle start [word length] | custom | guess | show | giveup', + USAGE => 'Usage: wordle start [word length] | custom | guess | letters | show | giveup', NO_WORDLE => 'There is no Wordle yet. Use `wordle start` to begin a game.', DEFAULT_LENGTH => 5, MIN_LENGTH => 3, MAX_LENGTH => 10, WORDLIST => '/usr/share/dict/words', + LETTER_CORRECT => 1, + LETTER_PRESENT => 2, + LETTER_INVALID => 3, }; sub wordle($self, $context) { @@ -125,7 +128,7 @@ sub wordle($self, $context) { hostmask => "$botnick!wordle\@localhost", command => 'wordle', checkflood => 1, - message => "$context->{nick} started a custom $result"; + message => "$context->{nick} started a custom $result", }; $self->{pbot}->{interpreter}->add_message_to_output_queue($custom_channel, $message, 0); @@ -149,6 +152,30 @@ sub wordle($self, $context) { return $self->guess_wordle($channel, $args[0]); } + when ('letters') { + if (@args > 1) { + return "Usage: wordle letters"; + } + + if (not defined $self->{$channel}->{wordle}) { + return NO_WORDLE; + } + + my $result = 'Good/unknown letters: '; + + foreach my $letter (sort keys $self->{$channel}->{letters}->%*) { + if ($self->{$channel}->{letters}->{$letter} == LETTER_CORRECT) { + $result .= "*$letter* "; + } elsif ($self->{$channel}->{letters}->{$letter} == LETTER_PRESENT) { + $result .= "?$letter? "; + } elsif ($self->{$channel}->{letters}->{$letter} == 0) { + $result .= "$letter "; + } + } + + return $result; + } + default { return "Unknown command `$command`; " . USAGE; } @@ -198,10 +225,15 @@ sub make_wordle($self, $channel, $length, $word = undef) { @wordle = split //, $words[rand @words]; } - $self->{$channel}->{wordle} = \@wordle; - $self->{$channel}->{guesses} = []; - $self->{$channel}->{correct} = 0; + $self->{$channel}->{wordle} = \@wordle; + $self->{$channel}->{guesses} = []; + $self->{$channel}->{correct} = 0; $self->{$channel}->{guess_count} = 0; + $self->{$channel}->{letters} = {}; + + foreach my $letter ('A'..'Z') { + $self->{$channel}->{letters}->{$letter} = 0; + } push $self->{$channel}->{guesses}->@*, '? ' x $self->{$channel}->{wordle}->@*; @@ -255,6 +287,7 @@ sub guess_wordle($self, $channel, $guess) { $seen{$guess[$i]}++; $correct++; push @result, "*$guess[$i]*"; + $self->{$channel}->{letters}->{$guess[$i]} = LETTER_CORRECT; } else { my $present = 0; @@ -271,8 +304,10 @@ sub guess_wordle($self, $channel, $guess) { if ($present) { push @result, "?$guess[$i]?"; + $self->{$channel}->{letters}->{$guess[$i]} = LETTER_PRESENT; } else { push @result, "$guess[$i]"; + $self->{$channel}->{letters}->{$guess[$i]} = LETTER_INVALID; } } }