line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::Cobalt::Plugin::Ohm; |
2
|
|
|
|
|
|
|
$Bot::Cobalt::Plugin::Ohm::VERSION = '0.001002'; |
3
|
|
|
|
|
|
|
# A simple Ohm's law calculator borrowed from SYMKAT: |
4
|
|
|
|
|
|
|
# https://gist.github.com/symkat/da287f0993e708b53701 |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
42504
|
use strictures 2; |
|
2
|
|
|
|
|
3560
|
|
|
2
|
|
|
|
|
111
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
2072
|
use Bot::Cobalt; |
|
2
|
|
|
|
|
22895
|
|
|
2
|
|
|
|
|
13
|
|
9
|
2
|
|
|
2
|
|
3987
|
use Bot::Cobalt::Common; |
|
2
|
|
|
|
|
490683
|
|
|
2
|
|
|
|
|
15
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
0
|
88
|
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
|
|
16290
|
no strict 'refs'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1759
|
|
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
|
0
|
|
|
|
|
0
|
$resp = "Parser failure, try input in the form of: a w o v"; |
52
|
|
|
|
|
|
|
last RESP |
53
|
0
|
|
|
|
|
0
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
$resp = |
56
|
0
|
|
|
0
|
|
0
|
try { $self->_calc(%parsed) } |
57
|
0
|
|
|
0
|
|
0
|
catch { "Calc failure; $_" }; |
|
0
|
|
|
|
|
0
|
|
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
0
|
unless (length $resp) { |
60
|
0
|
|
|
|
|
0
|
$resp = "Calc failure; malformed input from parser"; |
61
|
|
|
|
|
|
|
last RESP |
62
|
0
|
|
|
|
|
0
|
} |
63
|
|
|
|
|
|
|
} # RESP |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
0
|
broadcast message => $context, $msg->channel, "${src_nick}: $resp"; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
0
|
PLUGIN_EAT_NONE |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# These routines stolen directly from SYMKAT and then hacked to shreds: |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _parse_values { |
73
|
5
|
|
|
5
|
|
1775
|
my ($self, $message) = @_; |
74
|
5
|
|
|
|
|
10
|
my %values = (); |
75
|
|
|
|
|
|
|
|
76
|
5
|
100
|
|
|
|
24
|
if ( $message =~ /(\d+(?:\.\d+)?)o/i ) { |
77
|
4
|
|
|
|
|
13
|
$values{o} = $1; |
78
|
|
|
|
|
|
|
} |
79
|
5
|
100
|
|
|
|
18
|
if ( $message =~ /(\d+(?:\.\d+)?)w/i ) { |
80
|
2
|
|
|
|
|
7
|
$values{w} = $1; |
81
|
|
|
|
|
|
|
} |
82
|
5
|
100
|
|
|
|
19
|
if ( $message =~ /(\d+(?:\.\d+)?)a/i ) { |
83
|
1
|
|
|
|
|
3
|
$values{a} = $1; |
84
|
|
|
|
|
|
|
} |
85
|
5
|
100
|
|
|
|
15
|
if ( $message =~ /(\d+(?:\.\d+)?)v/i ) { |
86
|
3
|
|
|
|
|
7
|
$values{v} = $1; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
%values |
90
|
5
|
|
|
|
|
30
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _calc { |
93
|
2
|
|
|
2
|
|
371
|
my ($self, %values) = @_; |
94
|
|
|
|
|
|
|
# A = V / O |
95
|
|
|
|
|
|
|
# A = W / V |
96
|
|
|
|
|
|
|
# A = sqrt(W / O) |
97
|
2
|
50
|
|
|
|
7
|
unless (defined $values{a}) { |
98
|
|
|
|
|
|
|
$values{a} = |
99
|
|
|
|
|
|
|
$values{v} && $values{o} ? $values{v} / $values{o} |
100
|
|
|
|
|
|
|
: $values{w} && $values{v} ? $values{w} / $values{v} |
101
|
|
|
|
|
|
|
: $values{w} && $values{o} ? sqrt( $values{w} / $values{o} ) |
102
|
2
|
50
|
66
|
|
|
26
|
: undef |
|
|
50
|
33
|
|
|
|
|
|
|
100
|
33
|
|
|
|
|
103
|
|
|
|
|
|
|
; |
104
|
|
|
|
|
|
|
die "Not enough information to calculate amperage\n" |
105
|
|
|
|
|
|
|
unless defined $values{a} |
106
|
2
|
100
|
|
|
|
10
|
} |
107
|
|
|
|
|
|
|
# W = ( V * V ) / O |
108
|
|
|
|
|
|
|
# W = ( A * A ) * O |
109
|
|
|
|
|
|
|
# W = V * R |
110
|
1
|
50
|
|
|
|
4
|
unless (defined $values{w}) { |
111
|
|
|
|
|
|
|
$values{w} = |
112
|
|
|
|
|
|
|
$values{v} && $values{o} ? ($values{v} ** 2) / $values{o} |
113
|
|
|
|
|
|
|
: $values{a} && $values{o} ? ($values{a} ** 2) * $values{o} |
114
|
|
|
|
|
|
|
: $values{v} && $values{a} ? $values{v} * $values{a} |
115
|
|
|
|
|
|
|
: undef |
116
|
1
|
0
|
33
|
|
|
19
|
; |
|
|
0
|
0
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
117
|
|
|
|
|
|
|
die "Not enough information to calculate wattage\n" |
118
|
|
|
|
|
|
|
unless defined $values{w} |
119
|
1
|
50
|
|
|
|
4
|
} |
120
|
|
|
|
|
|
|
# O = V / A |
121
|
|
|
|
|
|
|
# O = ( V * V ) * W |
122
|
|
|
|
|
|
|
# O = W / ( A * A ) |
123
|
1
|
50
|
|
|
|
4
|
unless (defined $values{o}) { |
124
|
|
|
|
|
|
|
$values{o} = |
125
|
|
|
|
|
|
|
$values{v} && $values{a} ? $values{v} / $values{a} |
126
|
|
|
|
|
|
|
: $values{v} && $values{w} ? ($values{v} ** 2) * $values{w} |
127
|
0
|
0
|
0
|
|
|
0
|
: $values{w} && $values{a} ? $values{w} / ($values{a} ** 2) |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
128
|
|
|
|
|
|
|
: undef |
129
|
|
|
|
|
|
|
; |
130
|
|
|
|
|
|
|
die "Not enough information to calculate ohms\n" |
131
|
|
|
|
|
|
|
unless defined $values{o} |
132
|
0
|
0
|
|
|
|
0
|
} |
133
|
|
|
|
|
|
|
# V = sqrt( W * O ) |
134
|
|
|
|
|
|
|
# V = W / A |
135
|
|
|
|
|
|
|
# V = A * O |
136
|
1
|
50
|
|
|
|
4
|
unless (defined $values{v}) { |
137
|
|
|
|
|
|
|
$values{v} = |
138
|
|
|
|
|
|
|
$values{w} && $values{o} ? sqrt( $values{w} * $values{o} ) |
139
|
|
|
|
|
|
|
: $values{w} && $values{a} ? $values{w} / $values{a} |
140
|
|
|
|
|
|
|
: $values{a} && $values{o} ? $values{a} * $values{o} |
141
|
|
|
|
|
|
|
: undef |
142
|
0
|
0
|
0
|
|
|
0
|
; |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
143
|
|
|
|
|
|
|
die "Not enough information to calculate voltage\n" |
144
|
|
|
|
|
|
|
unless defined $values{v} |
145
|
0
|
0
|
|
|
|
0
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sprintf |
148
|
|
|
|
|
|
|
"%.2fw/%.2fv @ %.2famps against %.2fohm" => |
149
|
1
|
|
|
|
|
3
|
map {; $values{$_} } qw/ w v a o / |
|
4
|
|
|
|
|
21
|
|
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
1; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=pod |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 NAME |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Bot::Cobalt::Plugin::Ohm - Simple Ohm's law calculator for Bot::Cobalt |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 SYNOPSIS |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# What's my voltage and amperage firing my 0.87 Ohm coil at 25W? |
163
|
|
|
|
|
|
|
!ohm 0.87o 25w |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 DESCRIPTION |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
A simple Ohm's law calculator; given a string specifying parameters in the |
168
|
|
|
|
|
|
|
form of C<< a o w v >>, attempts to fill in the blanks. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 AUTHOR |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Kaitlyn Parkhurst (CPAN: C) wrote the calculator as an irssi script. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Adapted (with permission) to L by Jon Portnoy |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |