blib/lib/Bot/BasicBot/Pluggable/Module/Tea.pm

filter: Statementstmt 76.8% 53 / 69 Branchbran 50.0% 12 / 24 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 7 / 8 Podpod 60.0% 3 / 5 totaltotal 66.3% 77 / 116 CC 16 SCAR 24.6
CC
16
Cov
68%
told
21.5
chanjoin
4.3
tidy_lists
3.4
CRAP 11.7
↑ Summary
1 # ABSTRACT: Tea round organising IRC bot
2 package Bot::BasicBot::Pluggable::Module::Tea;
3  
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 extends 'Bot::BasicBot::Pluggable::Module';
9  
10 2 our $VERSION = "0.0011";
11  
12  
13 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
14  
15 0 return <<HELPMSG;
16 This plugin helps facilitae tea making within a team. Simply
17 issue the `!tea` command when you fancy a brew and a new tea round
18 will begin.
19  
20 If the user selected is away, issue `!tea away` and another user
21 will be selected.
22  
23 If you want to jump in the queue and make a round of tea, issue the
24 `!tea volunteer` command.
25  
26 If you want to see the current tea round status, issue `!tea status`.
27  
28 If you want to live on the edge issue the `!tea random` command to have
29 someone at random selected to make the tea
30 HELPMSG
31  
32 }
33  
34 {
35 2 my @nick_list;
36 2 my $last_used = DateTime->now;
37  
38 2 ▶ sub select_brew_maker {
Subroutine select_brew_maker: called CC=1 SCAR=0.0
Pod undocumented
39 2 my $self = shift;
40 2 my $chan = shift;
41  
42 2 my $brew_maker = $nick_list[0];
43  
44 # take the first nick and put them to the back of the list
45 2 push @nick_list, shift @nick_list;
46  
47 2 $last_used = DateTime->now;
48  
49 2 return $brew_maker;
50 }
51  
52 1 ▶ sub chanjoin {
Subroutine chanjoin: called CC=4 SCAR=14.6
Pod documented
53 1 my ( $self, $msg ) = @_;
54 # someone joined look at the current array of nicks and add the new person in a random place
55 1 my @all_nicks = $self->bot->pocoirc->channel_list( $msg->{channel} );
56  
57 1 for my $nick (@all_nicks) {
58 # insert new nick if not already in list and isn't the bot itself
59 3 ▶ unless (scalar(grep {$_ eq $nick} @nick_list) || ($nick eq $self->bot->nick)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({$_ eq $nick;} @nick_list) or $nick eq $self->bot->nick) 3 0 3
Condition33%1/3
scalar grep({$_ eq $nick;} @nick_list) or $nick eq $self->bot->nick
l!l&&r!l&&!r
003
MC/DC0%0/2
scalar grep({$_ eq $nick;} @nick_list) or $nick eq $self->bot->nick
scalar grep({$_ eq $nick;} @nick_list) $nick eq $self->bot->nick
Truth table33%1/3
A or B
A= scalar grep({$_ eq $nick;} @nick_list)B= $nick eq $self->bot->nick
ABresult
1X1
011
000
60 3 splice(@nick_list, int(rand(@nick_list - 1)), 0, $nick);
61 }
62 }
63 }
64  
65 4 ▶ sub tidy_lists {
Subroutine tidy_lists: called CC=3 SCAR=12.3
Pod undocumented
66 4 my ( $self, $chan ) = @_;
67 4 my @all_nicks = $self->bot->pocoirc->channel_list( $chan );
68 # don't tell Gianni I used ~~
69 4 ▶ @nick_list = grep { $_ ~~ @all_nicks && $_ ne $self->bot->nick } @nick_list;
Condition33%1/3
$_ ~~ \@all_nicks && $_ ne $self->bot->nick
!ll&&!rl&&r
0012
MC/DC0%0/2
$_ ~~ \@all_nicks && $_ ne $self->bot->nick
$_ ~~ \@all_nicks $_ ne $self->bot->nick
Truth table33%1/3
A && B
A= $_ ~~ \@all_nicksB= $_ ne $self->bot->nick
ABresult
0X0
100
111
70  
71 4 my $extra = '';
72 4 ▶ if (DateTime->now > $last_used->clone->add(hours => 8)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("DateTime"->now > $last_used->clone->add("hours", 8)) 0 4 4
73 0 @nick_list = shuffle(@nick_list);
74 0 $extra = ' (the rota was rewritten due to inactivity)';
75 }
76 4 return $extra;
77 }
78  
79 4 ▶ sub told {
Subroutine told: called CC=11 SCAR=30.7
Pod documented
80 4 my ( $self, $msg ) = @_;
81  
82 4 my $body = $msg->{body};
83 4 my $who = $msg->{who};
84 4 my $chan = $msg->{channel};
85  
86 4 my @all_nicks = $self->bot->pocoirc->channel_list( $chan );
87 #init the nick list if we don't have it already
88 4 ▶ $self->chanjoin( $msg ) unless $#nick_list > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $#nick_list > 0 1 3 4
89 4 my $extra = $self->tidy_lists( $msg->{channel} );
90  
91 4 ▶ if ( $body =~ /^!tea$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^!tea$/) 1 3 4
92  
93 1 my $brew_maker = $self->select_brew_maker( $chan );
94  
95 1 my $resp = "$who would like a brew! $brew_maker: your turn!$extra";
96 1 return $resp;
97 }
98  
99 # Process tea commands
100 3 ▶ if ( $body =~ /^!tea.*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^!tea.*$/) 3 0 3
101  
102 3 my @commands = split /\s/, $body;
103  
104 3 ▶ if ( $commands[1] eq 'away' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($commands[1] eq 'away') { } 1 2 3
elsif ($commands[1] eq 'volunteer') { } 0 2 2
elsif ($commands[1] eq 'random') { } 0 2 2
elsif ($commands[1] eq 'status') { } 2 0 2
elsif ($commands[1] eq 'version') { } 0 0 0
105 # Choose a new tea maker and pop the previous maker back
106 1 my $person_away = pop @nick_list;
107 1 my $brew_maker = $self->select_brew_maker( $chan );
108 1 unshift @nick_list, $person_away;
109 1 return "$who says $person_away is AWOL. $brew_maker, take over!";
110 }
111 elsif ( $commands[1] eq 'volunteer' ) {
112 # If you volunteer, go to back of list
113 0 @nick_list = grep {!/$who/} @nick_list;
114 0 push @nick_list, $who;
115 0 return "$who has volunteered to make a round. $who++";
116 }
117 elsif ( $commands[1] eq 'random' ) {
118 # Choose a random nick from the channel
119 0 my $brew_maker;
120 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$brew_maker ne $self->bot->nick or do { do { $brew_maker = $all_nicks[int rand scalar @all_nicks - 1] } } 0 0 0
121 0 $brew_maker = $all_nicks[int(rand(scalar @all_nicks - 1))];
122 } until $brew_maker ne $self->bot->nick;
123  
124 0 return "$who would like a brew! $brew_maker: your turn!";
125 }
126 elsif ( $commands[1] eq 'status' ) {
127 2 return "Tea round status is: " . join ',', @nick_list;
128 }
129 elsif ( $commands[1] eq 'version' ) {
130 0 return "This is teabot version $VERSION";
131 }
132 else {
133 0 return "$who-- # Imbecile! [unknown command]";
134 }
135  
136 }
137  
138 # No coffee allowed!
139 0 ▶ if ( $body =~ /^!coffee/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^!coffee/) 0 0 0
140 0 return "$who-- # no coffee here!";
141 }
142  
143 0 return;
144 }
145 }
146  
147 2 1;
148  
149  
150 __END__
↑ Summary