File Coverage

blib/lib/Bot/Cobalt/Plugin/Extras/TempConv.pm
Criterion Covered Total %
statement 13 47 27.6
branch 0 12 0.0
condition 0 11 0.0
subroutine 5 15 33.3
pod 0 5 0.0
total 18 90 20.0


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Plugin::Extras::TempConv;
2             $Bot::Cobalt::Plugin::Extras::TempConv::VERSION = '0.021003';
3             ## RECEIVES AND EATS:
4             ## _public_cmd_tempconv ( !tempconv )
5             ## _public_cmd_temp ( !temp )
6              
7 1     1   1310 use strictures 2;
  1         8  
  1         54  
8              
9 1     1   209 use Object::Pluggable::Constants qw/ :ALL /;
  1         2  
  1         119  
10 1     1   5 use Bot::Cobalt::Utils qw/ color /;
  1         2  
  1         9  
11              
12 1     1   200 use constant MAX_TEMP => 100_000_000_000;
  1         2  
  1         603  
13              
14 1     1 0 336 sub new { bless [], shift }
15              
16             sub Cobalt_register {
17 0     0 0   my ($self, $core) = splice @_, 0, 2;
18 0           $core->plugin_register( $self, 'SERVER',
19             qw/
20             public_cmd_temp
21             public_cmd_tempconv
22             /,
23             );
24              
25 0           $core->log->info("Registered, cmds: temp tempconv");
26              
27 0           return PLUGIN_EAT_NONE
28             }
29              
30             sub Cobalt_unregister {
31 0     0 0   my ($self, $core) = splice @_, 0, 2;
32              
33 0           $core->log->info("Unregistering");
34              
35 0           return PLUGIN_EAT_NONE
36             }
37              
38             ## !temp(conv):
39 0     0 0   sub Bot_public_cmd_tempconv { Bot_public_cmd_temp(@_) }
40             sub Bot_public_cmd_temp {
41 0     0 0   my ($self, $core) = splice @_, 0, 2;
42 0           my $msg = ${ $_[0] };
  0            
43 0           my $context = $msg->context;
44              
45 0   0       my $str = $msg->message_array->[0] || '';
46 0           my ($temp, $type) = $str =~ /(-?\d+\.?\d*)?(\w)?/;
47              
48 0 0         $temp = 0 unless $temp;
49 0 0         $temp = MAX_TEMP if $temp > MAX_TEMP;
50 0 0 0       $type = 'F' unless $type and grep { $_ eq uc($type) } qw/F C K/;
  0            
51              
52 0           my ($f, $k, $c);
53 0           for my $upper (uc $type) {
54 0 0 0       ( ($f, $k, $c) = ( $temp, _f2k($temp), _f2c($temp) ) and last )
55             if $upper eq 'F';
56              
57 0 0 0       ( ($f, $k, $c) = ( _c2f($temp), _c2k($temp), $temp ) and last )
58             if $upper eq 'C';
59              
60 0 0 0       ( ($f, $k, $c) = ( _k2f($temp), $temp, _k2c($temp) ) and last )
61             if $upper eq 'K';
62             }
63              
64 0           $_ = sprintf("%.2f", $_) for ($f, $k, $c);
65              
66 0           my $resp = color( 'bold', "(${f}F)" )
67             . " == " .
68             color( 'bold', "(${c}C)" )
69             . " == " .
70             color( 'bold', "(${k}K)" );
71              
72 0           my $channel = $msg->channel;
73              
74 0           $core->send_event( 'message', $context, $channel, $resp );
75              
76 0           return PLUGIN_EAT_ALL
77             }
78              
79             ## Conversion functions:
80 0     0     sub _f2c { (shift(@_) - 32 ) * (5/9) }
81 0     0     sub _f2k { (shift(@_) + 459.67) * (5/9) }
82              
83 0     0     sub _c2f { shift(@_) * (9/5) + 32 }
84 0     0     sub _c2k { shift(@_) + 273.15 }
85              
86 0     0     sub _k2f { shift(@_) * (9/5) - 459.67 }
87 0     0     sub _k2c { shift(@_) - 273.15 }
88              
89             1;
90             __END__