line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::Cobalt::Plugin::Ohm; |
2
|
|
|
|
|
|
|
$Bot::Cobalt::Plugin::Ohm::VERSION = '0.001001'; |
3
|
|
|
|
|
|
|
# A simple Ohm's law calculator borrowed from SYMKAT: |
4
|
|
|
|
|
|
|
# https://gist.github.com/symkat/da287f0993e708b53701 |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
54168
|
use strictures 2; |
|
2
|
|
|
|
|
3159
|
|
|
2
|
|
|
|
|
95
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1691
|
use Bot::Cobalt; |
|
2
|
|
|
|
|
19289
|
|
|
2
|
|
|
|
|
13
|
|
9
|
2
|
|
|
2
|
|
3358
|
use Bot::Cobalt::Common; |
|
2
|
|
|
|
|
533372
|
|
|
2
|
|
|
|
|
16
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
0
|
96
|
sub new { bless [], shift } |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub Cobalt_register { |
14
|
0
|
|
|
0
|
0
|
0
|
my ($self, $core) = splice @_, 0, 2; |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
0
|
my @events = map {; 'public_cmd_'.$_ } qw/ ohm watt amp volt /; |
|
0
|
|
|
|
|
0
|
|
17
|
0
|
|
|
|
|
0
|
register $self, SERVER => [ @events ]; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
0
|
$core->log->info("Loaded Ohm"); |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
0
|
PLUGIN_EAT_NONE |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub Cobalt_unregister { |
25
|
0
|
|
|
0
|
0
|
0
|
my ($self, $core) = splice @_, 0, 2; |
26
|
0
|
|
|
|
|
0
|
$core->log->info("Unloaded Ohm"); |
27
|
0
|
|
|
|
|
0
|
PLUGIN_EAT_NONE |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
{ |
31
|
2
|
|
|
2
|
|
16108
|
no strict 'refs'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1734
|
|
32
|
|
|
|
|
|
|
for (qw/watt amp volt/) { |
33
|
|
|
|
|
|
|
my $meth = 'Bot_public_cmd_'.$_; |
34
|
|
|
|
|
|
|
*{__PACKAGE__.'::'.$meth} = *Bot_public_cmd_ohm |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub Bot_public_cmd_ohm { |
39
|
0
|
|
|
0
|
0
|
0
|
my ($self, $core) = splice @_, 0, 2; |
40
|
0
|
|
|
|
|
0
|
my $msg = ${ $_[0] }; |
|
0
|
|
|
|
|
0
|
|
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
my $context = $msg->context; |
43
|
0
|
|
|
|
|
0
|
my $src_nick = $msg->src_nick; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
0
|
my $str = join '', @{ $msg->message_array }; |
|
0
|
|
|
|
|
0
|
|
46
|
0
|
|
|
|
|
0
|
my %parsed = $self->_parse_values($str); |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
0
|
my $resp; |
49
|
|
|
|
|
|
|
RESP: { |
50
|
0
|
0
|
|
|
|
0
|
unless (keys %parsed) { |
|
0
|
|
|
|
|
0
|
|
51
|
|
|
|
|
|
|
# FIXME better errors |
52
|
0
|
|
|
|
|
0
|
$resp = "Parser failure; malformed input"; |
53
|
|
|
|
|
|
|
last RESP |
54
|
0
|
|
|
|
|
0
|
} |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
0
|
$resp = $self->_calc(%parsed); |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
0
|
unless (length $resp) { |
59
|
0
|
|
|
|
|
0
|
$resp = "Calc failure; malformed input from parser"; |
60
|
|
|
|
|
|
|
last RESP |
61
|
0
|
|
|
|
|
0
|
} |
62
|
|
|
|
|
|
|
} # RESP |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
broadcast message => $context, $msg->channel, "${src_nick}: $resp"; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
PLUGIN_EAT_NONE |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# These routines stolen directly from SYMKAT and then hacked to shreds: |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _parse_values { |
72
|
5
|
|
|
5
|
|
1808
|
my ($self, $message) = @_; |
73
|
5
|
|
|
|
|
24
|
my %values = (); |
74
|
|
|
|
|
|
|
|
75
|
5
|
100
|
|
|
|
23
|
if ( $message =~ /(\d+(?:\.\d+)?)o/i ) { |
76
|
4
|
|
|
|
|
13
|
$values{o} = $1; |
77
|
|
|
|
|
|
|
} |
78
|
5
|
100
|
|
|
|
17
|
if ( $message =~ /(\d+(?:\.\d+)?)w/i ) { |
79
|
2
|
|
|
|
|
6
|
$values{w} = $1; |
80
|
|
|
|
|
|
|
} |
81
|
5
|
100
|
|
|
|
19
|
if ( $message =~ /(\d+(?:\.\d+)?)a/i ) { |
82
|
1
|
|
|
|
|
4
|
$values{a} = $1; |
83
|
|
|
|
|
|
|
} |
84
|
5
|
100
|
|
|
|
17
|
if ( $message =~ /(\d+(?:\.\d+)?)v/i ) { |
85
|
3
|
|
|
|
|
7
|
$values{v} = $1; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
%values |
89
|
5
|
|
|
|
|
28
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _calc { |
92
|
1
|
|
|
1
|
|
9
|
my ($self, %values) = @_; |
93
|
|
|
|
|
|
|
# A = V / O |
94
|
|
|
|
|
|
|
# A = W / V |
95
|
|
|
|
|
|
|
# A = sqrt(W / O) |
96
|
1
|
50
|
|
|
|
4
|
unless (defined $values{a}) { |
97
|
|
|
|
|
|
|
$values{a} = |
98
|
|
|
|
|
|
|
$values{v} && $values{o} ? $values{v} / $values{o} |
99
|
|
|
|
|
|
|
: $values{w} && $values{v} ? $values{w} / $values{v} |
100
|
|
|
|
|
|
|
: $values{w} && $values{o} ? sqrt( $values{w} / $values{o} ) |
101
|
1
|
0
|
33
|
|
|
9
|
: undef |
|
|
0
|
0
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
102
|
|
|
|
|
|
|
; |
103
|
|
|
|
|
|
|
return '' unless defined $values{a} |
104
|
1
|
50
|
|
|
|
4
|
} |
105
|
|
|
|
|
|
|
# W = ( V * V ) / O |
106
|
|
|
|
|
|
|
# W = ( A * A ) * O |
107
|
|
|
|
|
|
|
# W = V * R |
108
|
1
|
50
|
|
|
|
4
|
unless (defined $values{w}) { |
109
|
|
|
|
|
|
|
$values{w} = |
110
|
|
|
|
|
|
|
$values{v} && $values{o} ? ($values{v} ** 2) / $values{o} |
111
|
|
|
|
|
|
|
: $values{a} && $values{o} ? ($values{a} ** 2) * $values{o} |
112
|
|
|
|
|
|
|
: $values{v} && $values{a} ? $values{v} * $values{a} |
113
|
|
|
|
|
|
|
: undef |
114
|
1
|
0
|
33
|
|
|
17
|
; |
|
|
0
|
0
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
115
|
|
|
|
|
|
|
return '' unless defined $values{w} |
116
|
1
|
50
|
|
|
|
4
|
} |
117
|
|
|
|
|
|
|
# O = V / A |
118
|
|
|
|
|
|
|
# O = ( V * V ) * W |
119
|
|
|
|
|
|
|
# O = W / ( A * A ) |
120
|
1
|
50
|
|
|
|
10
|
unless (defined $values{o}) { |
121
|
|
|
|
|
|
|
$values{o} = |
122
|
|
|
|
|
|
|
$values{v} && $values{a} ? $values{v} / $values{a} |
123
|
|
|
|
|
|
|
: $values{v} && $values{w} ? ($values{v} ** 2) * $values{w} |
124
|
0
|
0
|
0
|
|
|
0
|
: $values{w} && $values{a} ? $values{w} / ($values{a} ** 2) |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
125
|
|
|
|
|
|
|
: undef |
126
|
|
|
|
|
|
|
; |
127
|
|
|
|
|
|
|
return '' unless defined $values{o} |
128
|
0
|
0
|
|
|
|
0
|
} |
129
|
|
|
|
|
|
|
# V = sqrt( W * O ) |
130
|
|
|
|
|
|
|
# V = W / A |
131
|
|
|
|
|
|
|
# V = A * O |
132
|
1
|
50
|
|
|
|
4
|
unless (defined $values{v}) { |
133
|
|
|
|
|
|
|
$values{v} = |
134
|
|
|
|
|
|
|
$values{w} && $values{o} ? sqrt( $values{w} * $values{o} ) |
135
|
|
|
|
|
|
|
: $values{w} && $values{a} ? $values{w} / $values{a} |
136
|
|
|
|
|
|
|
: $values{a} && $values{o} ? $values{a} * $values{o} |
137
|
|
|
|
|
|
|
: undef |
138
|
0
|
0
|
0
|
|
|
0
|
; |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
139
|
|
|
|
|
|
|
return '' unless defined $values{v} |
140
|
0
|
0
|
|
|
|
0
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sprintf |
143
|
|
|
|
|
|
|
"%.2fw/%.2fv @ %.2famps against %.2fohm" => |
144
|
1
|
|
|
|
|
3
|
map {; $values{$_} } qw/ w v a o / |
|
4
|
|
|
|
|
22
|
|
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=pod |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 NAME |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Bot::Cobalt::Plugin::Ohm - Simple Ohm's law calculator for Bot::Cobalt |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 SYNOPSIS |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
# What's my voltage and amperage firing my 0.87 Ohm coil at 25W? |
158
|
|
|
|
|
|
|
!ohm 0.87o 25w |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 DESCRIPTION |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
A simple Ohm's law calculator; given a string specifying parameters in the |
163
|
|
|
|
|
|
|
form of C<< a o w v >>, attempts to fill in the blanks. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 AUTHOR |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Kaitlyn Parkhurst (CPAN: C) wrote the calculator as an irssi script. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Adapted (with permission) to L by Jon Portnoy |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=cut |