lib/IRC/Bot/Hangman.pm

filter: Statementstmt 30.5% 55 / 180 Branchbran 16.0% 9 / 56 Conditioncond 10.0% 3 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 45.1% 14 / 31 Podpod 100% 23 / 23 totaltotal 30.6% 104 / 339 CC 40 SCAR 31.5
CC
40
Cov
25%
said
110.0
tick
30.0
give_advice
20.0
CRAP 23.2
↑ Summary lib/IRC/Bot/Hangman/Command.pm »
1 =head1 NAME
2  
3 IRC::Bot::Hangman - An IRC hangman
4  
5 =head1 SYNOPSIS
6  
7 use IRC::Bot::Hangman;
8 IRC::Bot::Hangman->new(
9 channels => [ '#hangman' ],
10 nick => 'hangman',
11 server => 'irc.blablablablah.bla',
12 word_list_name => 'too_easy',
13 games => 3,
14 )->run;
15 print "Finished\n";
16  
17 =head1 COMMANDS
18  
19 <letter>? : guess a letter
20 guess <letter> : guess a letter
21 guess <word> : guess an entire word
22 <hangman> help - help instructions
23 <hangman> play : Start a new game or display current game
24 <hangman> quiet : keep quiet between guesses
25 <hangman> talk : Talk between guesses
26  
27 =head1 DESCRIPTION
28  
29 This module provides a useless IRC bot which
30 enables you to play hangman, the classic word game.
31 It comes shipped with a list of ~2000 english words by default.
32 The architecture is plugin based, words, commands and responses
33 can be extended at will by adding new modules.
34  
35 The main motivation was to provide a multi-player text based
36 game for children to help them practising writing.
37  
38 =head1 PLUGINS
39  
40 The plugins are managed by
41  
42 IRC::Bot::Hangman::WordList
43 IRC::Bot::Hangman::Command
44 IRC::Bot::Hangman::Response
45  
46 =cut
47  
48 package IRC::Bot::Hangman;
49 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use base qw( Bot::BasicBot );
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Games::GuessWord;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use IRC::Bot::Hangman::WordList;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use IRC::Bot::Hangman::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use IRC::Bot::Hangman::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 our $VERSION = '0.1';
59  
60 1 our $DEFAULT_WORD_LIST = 'default';
61 1 our $DEFAULT_DELAY = 30; # seconds
62  
63  
64 =head1 METHODS
65  
66 =head2 word_list( $list )
67  
68 Get or set the word list as an array ref.
69 A default word list of ~2000 english words is provided
70 if no list is set.
71  
72 =cut
73  
74 4 ▶sub word_list {
Subroutine word_list: called CC=4 SCAR=15.0
Pod documented
75 4 my $self = shift;
76 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 3 4
77 1 my $list = shift;
78 1 ▶ unless (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 0 1 1
79 0 carp "word_list should be an array ref";
80 0 return;
81 }
82 1 $self->{word_list} = $list;
83 1 return $self;
84 }
85 3 ▶ $self->{word_list} ||= $self->load_word_list();
Condition33%1/3
$self->{"word_list"} ||= $self->load_word_list
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"word_list"} ||= $self->load_word_list
$self->{"word_list"} $self->load_word_list
Truth table33%1/3
A ||= B
A= $self->{"word_list"}B= $self->load_word_list
ABresult
1X1
011
000
86 }
87  
88  
89 =head2 load_word_list( name )
90  
91 Returns a default english words list
92 from L<IRC::Bot::Hangman::WordList>
93  
94 =cut
95  
96 0 ▶sub load_word_list {
Subroutine load_word_list: not called CC=2 SCAR=17.9
Pod documented
97 0 my $self = shift;
98 0 ▶ my $name = shift || $self->word_list_name;
Condition0%0/3
shift() || $self->word_list_name
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->word_list_name
shift() $self->word_list_name
Truth table0%0/3
A || B
A= shift()B= $self->word_list_name
ABresult
1X1
011
000
99 0 IRC::Bot::Hangman::WordList->load( $name );
100 }
101  
102  
103 =head2 word_list_name( $name )
104  
105 Get or set the word list name.
106 It must be an installed module in IRC::Bot::Hangman::WordList::xxx
107 The default provided is 'default' = IRC::Bot::Hangman::WordList::Default
108  
109 =cut
110  
111 0 ▶sub word_list_name {
Subroutine word_list_name: not called CC=3 SCAR=24.8
Pod documented
112 0 my $self = shift;
113 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
114 0 $self->{word_list_name} = shift;
115 0 return $self;
116 }
117 0 ▶ $self->{word_list_name} ||= $DEFAULT_WORD_LIST;
Condition0%0/3
$self->{"word_list_name"} ||= $IRC::Bot::Hangman::DEFAULT_WORD_LIST
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"word_list_name"} ||= $IRC::Bot::Hangman::DEFAULT_WORD_LIST
$self->{"word_list_name"} $IRC::Bot::Hangman::DEFAULT_WORD_LIST
Truth table0%0/3
A ||= B
A= $self->{"word_list_name"}B= $IRC::Bot::Hangman::DEFAULT_WORD_LIST
ABresult
1X1
011
000
118 }
119  
120  
121 =head2 games( integer )
122  
123 Get or set the number of games before ending.
124 undef means infinity.
125  
126 =cut
127  
128 4 ▶sub games {
Subroutine games: called CC=2 SCAR=6.9
Pod documented
129 4 my $self = shift;
130 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
131 2 my $games = shift;
132 2 $self->{games} = $games;
133 2 return $self;
134 }
135 2 $self->{games};
136 }
137  
138  
139 =head2 game( $game )
140  
141 Get or set the hangman game.
142 The default value is a L<Games::GuessWord> instance
143 with word_list() word list.
144  
145 =cut
146  
147 11 ▶sub game {
Subroutine game: called CC=3 SCAR=11.0
Pod documented
148 11 my $self = shift;
149 11 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 9 11
150 2 my $game = shift;
151 2 $self->{game} = $game;
152 2 return $self;
153 }
154 9 ▶ $self->{game} ||= $self->load_game;
Condition66%2/3
$self->{"game"} ||= $self->load_game
l!l&&r!l&&!r
810
MC/DC0%0/2
$self->{"game"} ||= $self->load_game
$self->{"game"} $self->load_game
Truth table66%2/3
A ||= B
A= $self->{"game"}B= $self->load_game
ABresult
1X1
011
000
155 }
156  
157  
158 =head2 new_game()
159  
160 Reset the game
161  
162 =cut
163  
164 1 ▶sub new_game {
Subroutine new_game: called CC=2 SCAR=7.1
Pod documented
165 1 my $self = shift;
166 1 ▶ my $game = $self->game or return;
Branch: 1 branch
BranchTrueFalseTotal
my $game = $self->game or return 1 0 1
167 1 $self->game( ref($game)->new( words => $self->word_list ) );
168 }
169  
170  
171 =head2 replay()
172  
173 Reset the game unless it is the last game
174 as counted by games()
175  
176 =cut
177  
178 0 ▶sub replay {
Subroutine replay: not called CC=3 SCAR=24.8
Pod documented
179 0 my $self = shift;
180 0 my $games = $self->games;
181 0 ▶ if (defined $games) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $games) 0 0 0
182 0 $self->games($games - 1);
183 0 ▶ if ($self->games <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->games <= 0) 0 0 0
184 0 $self->schedule_tick(0);
185 0 return $self->get_a_msg('last_game');
186 }
187 }
188 0 $self->new_game();
189 0 $self->schedule_tick(5);
190 0 return;
191 }
192  
193  
194 =head2 can_talk()
195  
196 Get set C<can_talk>, used by C<tick> to display reminders.
197  
198 =cut
199  
200 1 ▶sub can_talk {
Subroutine can_talk: called CC=2 SCAR=8.4
Pod documented
201 1 my $self = shift;
202 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
203 0 $self->{can_talk} = shift;
204 0 return $self;
205 }
206 1 $self->{can_talk};
207 }
208  
209  
210 =head2 load_game()
211  
212 Returns a L<Games::GuessWord> instance
213  
214 =cut
215  
216 1 ▶sub load_game {
Subroutine load_game: called CC=1 SCAR=0.0
Pod documented
217 1 my $self = shift;
218 1 Games::GuessWord->new( words => $self->word_list );
219 }
220  
221  
222 =head2 msg_guess()
223  
224 Displays the word to guess
225  
226 =cut
227  
228 0 ▶sub msg_guess {
Subroutine msg_guess: not called CC=1 SCAR=6.9
Pod documented
229 0 my $self = shift;
230 0 'To guess: ' . $self->game->answer . ' - ' . $self->game->chances . " chances remaining";
231 }
232  
233  
234 =head2 get_delay()
235  
236 Returns a random time calculated:
237 delay() * (1 + rand(4)) seconds
238  
239 =cut
240  
241 0 ▶sub get_delay {
Subroutine get_delay: not called CC=1 SCAR=6.9
Pod documented
242 0 my $self = shift;
243 0 my $delay = $self->delay;
244 0 $delay *(1 + rand(4));
245 }
246  
247  
248 =head2 delay()
249  
250 Get set base delay in seconds.
251 Default is 30s.
252  
253 =cut
254  
255 0 ▶sub delay {
Subroutine delay: not called CC=3 SCAR=24.8
Pod documented
256 0 my $self = shift;
257 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
258 0 $self->{delay} = shift;
259 0 return $self;
260 }
261 0 ▶ $self->{delay} ||= $DEFAULT_DELAY;
Condition0%0/3
$self->{"delay"} ||= $IRC::Bot::Hangman::DEFAULT_DELAY
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"delay"} ||= $IRC::Bot::Hangman::DEFAULT_DELAY
$self->{"delay"} $IRC::Bot::Hangman::DEFAULT_DELAY
Truth table0%0/3
A ||= B
A= $self->{"delay"}B= $IRC::Bot::Hangman::DEFAULT_DELAY
ABresult
1X1
011
000
262 }
263  
264  
265 =head2 input()
266  
267 Get/set input
268  
269 =cut
270  
271 0 ▶sub input {
Subroutine input: not called CC=2 SCAR=17.9
Pod documented
272 0 my $self = shift;
273 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
274 0 $self->{input} = shift;
275 0 return $self;
276 }
277 0 $self->{input};
278 }
279  
280  
281 =head2 response()
282  
283 Get/set response
284  
285 =cut
286  
287 0 ▶sub response {
Subroutine response: not called CC=2 SCAR=17.9
Pod documented
288 0 my $self = shift;
289 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
290 0 $self->{response} = shift;
291 0 return $self;
292 }
293 0 $self->{response};
294 }
295  
296  
297 =head2 set_response( type )
298  
299 Sets the response from a response type
300  
301 =cut
302  
303 0 ▶sub set_response {
Subroutine set_response: not called CC=2 SCAR=17.9
Pod documented
304 0 my $self = shift;
305 0 my $type = shift;
306 0 ▶ my $msg = $self->get_a_msg( $type ) or carp "No message of type $type";
Branch: 1 branch
BranchTrueFalseTotal
my $msg = $self->get_a_msg($type) or carp("No message of type $type") 0 0 0
307 0 $self->response( $msg );
308 }
309  
310  
311 =head2 get_a_msg( type )
312  
313 Returns a msg of a given type
314  
315 =cut
316  
317 0 ▶sub get_a_msg {
Subroutine get_a_msg: not called CC=1 SCAR=6.9
Pod documented
318 0 my $self = shift;
319 0 my $type = shift;
320 0 IRC::Bot::Hangman::Response->get_a_msg( $type );
321 }
322  
323  
324 =head2 guess_word( word )
325  
326 Guess a word : success or one chance less
327  
328 =cut
329  
330 0 ▶sub guess_word {
Subroutine guess_word: not called CC=2 SCAR=17.9
Pod documented
331 0 my $self = shift;
332 0 my $guess = shift;
333 0 ▶ if ($guess eq $self->game->secret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guess eq $self->game->secret) { } 0 0 0
334 0 $self->game->guess($guess);
335 0 return $self->get_a_msg('good_guess');
336 }
337 else {
338 0 $self->game->{chances}--;
339 0 return $self->get_a_msg('bad_guess');
340 }
341 }
342  
343  
344 =head2 guess_letter( letter )
345  
346 Guess a letter : match or one chance less
347  
348 =cut
349  
350 0 ▶sub guess_letter {
Subroutine guess_letter: not called CC=3 SCAR=24.8
Pod documented
351 0 my $self = shift;
352 0 my $guess = shift;
353 0 my @guesses = $self->game->guesses;
354 0 my @msg;
355 0 ▶ if (grep { $_ eq $guess } @guesses) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $guess;} @guesses) { } 0 0 0
356 0 push @msg, $self->get_a_msg('already_guessed');
357 0 push @msg, 'Letters used: ' . join(', ', $self->game->guesses);
358 }
359 else {
360 0 my $chances = $self->game->chances;
361 0 $self->game->guess($guess);
362 0 ▶ if ($chances == $self->game->chances) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chances == $self->game->chances) { } 0 0 0
363 0 push @msg, $self->get_a_msg('good_guess');
364 }
365 else {
366 0 push @msg, $self->get_a_msg('bad_guess');
367 }
368 0 push @msg, $self->give_advice($guess);
369 }
370 0 @msg;
371 }
372  
373  
374 =head2 conclusion()
375  
376 Displays an end of game message : sucess or lost
377  
378 =cut
379  
380 0 ▶sub conclusion {
Subroutine conclusion: not called CC=3 SCAR=24.8
Pod documented
381 0 my $self = shift;
382 0 my @msg;
383 0 ▶ if ($self->game->won) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->game->won) { } 0 0 0
elsif ($self->game->lost) { } 0 0 0
384 0 push @msg, $self->get_a_msg('won');
385 0 push @msg, "The word was: " . $self->game->secret;
386 0 push @msg, "Your score: " . $self->game->score;
387 0 push @msg, $self->replay();
388 }
389 elsif ($self->game->lost) {
390 0 push @msg, $self->get_a_msg('lost');
391 0 push @msg, "The word was: " . $self->game->secret;
392 0 push @msg, "Your score: " . $self->game->score;
393 0 push @msg, $self->replay();
394 }
395 else {
396 0 push @msg, $self->msg_guess;
397 }
398 0 @msg;
399 }
400  
401  
402 =head2 give_advice( guess )
403  
404 =cut
405  
406 0 ▶sub give_advice {
Subroutine give_advice: not called CC=4 SCAR=30.0
Pod documented
407 0 my $self = shift;
408 0 my $guess = shift;
409 0 my @guesses = $self->game->guesses;
410 0 ▶ if ($guess =~ /[euioa]/ and grep(/[euioa]/, @guesses) >= 3 and @guesses < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guess =~ /[euioa]/ and grep(/[euioa]/, @guesses) >= 3 and @guesses < 6) 0 0 0
Condition0%0/6
$guess =~ /[euioa]/ and grep(/[euioa]/, @guesses) >= 3 and @guesses < 6
!ll&&!rl&&r
000
$guess =~ /[euioa]/ and grep(/[euioa]/, @guesses) >= 3
!ll&&!rl&&r
000
MC/DC0%0/3
$guess =~ /[euioa]/ and grep(/[euioa]/, @guesses) >= 3 and @guesses < 6
$guess =~ /[euioa]/ grep(/[euioa]/, @guesses) >= 3 @guesses < 6
Truth table0%0/4
A and B and C
A= $guess =~ /[euioa]/B= grep(/[euioa]/, @guesses) >= 3C= @guesses < 6
ABCresult
0XX0
10X0
1100
1111
411 0 return $self->get_a_msg('lack_imagination');
412 }
413 0 return;
414 }
415  
416  
417 =head1 Bot::BasicBot METHODS
418  
419 These are the L<Bot::BasicBot> overriden methods
420  
421 =head2 said( $args )
422  
423 This is the main method,
424 everything said is analysed to provide a reply
425 if appropriate
426  
427 =cut
428  
429 0 ▶sub said {
Subroutine said: not called CC=10 SCAR=47.0
Pod documented
430 0 my $self = shift;
431 0 my $args = shift;
432  
433 0 ▶ return if ($self->ignore_nick($args->{who}));
Branch: 1 branch
BranchTrueFalseTotal
if $self->ignore_nick($args->{"who"}) 0 0 0
434  
435 0 my $nick = $self->nick;
436 0 ▶ if ($args->{address} || '' eq $nick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"address"} or "" eq $nick) 0 0 0
Condition0%0/3
$args->{"address"} or "" eq $nick
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"address"} or "" eq $nick
$args->{"address"} "" eq $nick
Truth table0%0/3
A or B
A= $args->{"address"}B= "" eq $nick
ABresult
1X1
011
000
437 0 my $msg = $args->{body};
438 0 $msg =~ s/[\r\n\f]+$//;
439 0 $self->input( $msg );
440 0 $self->response('');
441 0 IRC::Bot::Hangman::Command->run( $self );
442 0 ▶ return $self->response if $self->response;
Branch: 1 branch
BranchTrueFalseTotal
if $self->response 0 0 0
443 }
444  
445 0 ▶ return if ($self->game->won or $self->game->lost);
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->won or $self->game->lost 0 0 0
Condition0%0/3
$self->game->won or $self->game->lost
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->game->won or $self->game->lost
$self->game->won $self->game->lost
Truth table0%0/3
A or B
A= $self->game->wonB= $self->game->lost
ABresult
1X1
011
000
446  
447 0 my ($guess) = ($args->{body} =~ /^\s*([a-z])\s*\?\s*$/);
448 0 ▶ ($guess) = ($args->{body} =~ /^\s*guess\s+([a-z]+)\s*$/) unless $guess;
Branch: 1 branch
BranchTrueFalseTotal
unless $guess 0 0 0
449 0 ▶ $guess or return;
Branch: 1 branch
BranchTrueFalseTotal
$guess or return 0 0 0
450  
451 0 $self->schedule_tick($self->get_delay);
452 0 $guess = lc $guess;
453  
454 0 my @msg;
455 0 ▶ if (length $guess > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $guess > 1) { } 0 0 0
456 0 push @msg, $self->guess_word($guess);
457 }
458 else {
459 0 push @msg, $self->guess_letter($guess);
460 }
461  
462 0 push @msg, $self->conclusion;
463 0 join "\r\n", @msg;
464 }
465  
466  
467 =head2 help()
468  
469 Displays help when called C<hangman help>
470  
471 =cut
472  
473 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
474 0 my $self = shift;
475 0 my $help = $self->get_a_msg('help');
476 0 my $nick = $self->nick;
477 0 $help =~ s/<hangman>/$nick/g;
478 0 $help;
479 }
480  
481  
482 =head2 tick()
483  
484 Called every now and then to display a reminder
485 if the game is active and C<can_talk> is on.
486  
487 =cut
488  
489 0 ▶sub tick {
Subroutine tick: not called CC=5 SCAR=34.0
Pod documented
490 0 my $self = shift;
491 0 ▶ return $self->get_delay if ($self->game->lost or $self->game->won);
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->lost or $self->game->won 0 0 0
Condition0%0/3
$self->game->lost or $self->game->won
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->game->lost or $self->game->won
$self->game->lost $self->game->won
Truth table0%0/3
A or B
A= $self->game->lostB= $self->game->won
ABresult
1X1
011
000
492 0 ▶ if ($self->can_talk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can_talk) 0 0 0
493 0 my @msg = ($self->get_a_msg('play'), $self->msg_guess);
494 0 $self->say( channel => $_, body => join "\r\n", @msg ) for (@{$self->{channels}});
495 }
496 0 $self->get_delay;
497 }
498  
499  
500 1 1;
501  
502  
503 =head1 SEE ALSO
504  
505 L<Bot::BasicBot>
506  
507 =head1 AUTHOR
508  
509 Pierre Denis <pierre@itrelease.net>
510  
511 http://www.itrelease.net/
512  
513 =head1 COPYRIGHT
514  
515 Copyright 2005 IT Release Ltd - All Rights Reserved.
516  
517 This module is released under the same license as Perl itself.
518  
519 =cut
↑ Summary lib/IRC/Bot/Hangman/Command.pm »