-
Notifications
You must be signed in to change notification settings - Fork 3
/
Points.pm
261 lines (195 loc) · 9.27 KB
/
Points.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package plugins::Points;
#---------------------------------------------------------------------------
# Copyright (C) 2013 [email protected]
# https://github.com/egretsareherons/RocksBot
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------
use strict;
use warnings;
use base qw (modules::PluginBaseClass);
use modules::PluginBaseClass;
use Data::Dumper;
sub plugin_init{
my $self = shift;
$self->returnType("text");
$self->outputDelimiter($self->BULLET);
$self->suppressNick("true");
return $self;
}
sub getOutput {
my $self = shift;
my $output = "";
my $options = $self->{'options'};
my $cmd = $self->{'command'};
#return $self->help(["plugin_description"]) if $self->hasFlag("help");
my $c = $self->getCollection(__PACKAGE__, $self->accountNick());
if ($cmd eq 'points'){
if ($self->hasFlag("all")){
if ($options){
my $c = $self->getCollection(__PACKAGE__, '%');
my @records = $c->matchRecords({val1=>$options});
return ("No one has rated $options. Yet.") if (@records == 0);
my @sorted = sort { $b->{val2} <=> $a->{val2} } @records;
foreach my $rec (@sorted){
$self->addToList($rec->{'collection_name'} .": " . $rec->{'val2'}, $self->BULLET);
}
return "How $options rates: " . $self->getList();
}else{
return "Usage: points -all <thing>";
}
}
if ($self->hasFlag("posneg") || $self->hasFlag("toprankers") ||$self->hasFlag("total") ) {
my $c = $self->getCollection(__PACKAGE__, '%');
my @records = $c->getAllRecords();
return ("No data. Yet.") if (@records == 0);
my (%total, %num, %ratio);
foreach my $rec (@records){
$total{$rec->{collection_name}} += $rec->{val2};
$num{$rec->{collection_name}}++;
}
foreach my $n (keys %num){
print "$n: $total{$n} / $num{$n} \n";
$ratio{$n} = $total{$n} / $num{$n};
}
my %data;
if ($self->hasFlag("posneg")){
$output = "Positivity Rankings: ";
%data = %ratio;
}elsif ($self->hasFlag("toprankers")){
$output = "People Rankings the Most Things: ";
%data = %num;
}elsif ($self->hasFlag("total")){
$output = "Points given out: ";
%data = %total;
}
foreach (sort { ($data{$b} <=> $data{$a}) } keys %data){
if ($self->hasFlag("posneg")){
$self->addToList( "$_: ". sprintf("%.2f",$data{$_}), $self->BULLET);;
}else{
$self->addToList( "$_: ". sprintf("%d",$data{$_}), $self->BULLET );;
}
}
return $output . $self->getList();
}
if($self->hasFlag("delete")){
# Make sure people who have an account have their stuff protected
return ("You don't have permission to do that.") if (!$self->hasPermission($self->accountNick()));
my $what;
if (! ($what = $self->hasFlagValue("delete"))){
return "Usage: $cmd -delete=<thing>. Or do -deleteeverything to delete everything.";
}
my @records = $c->matchRecords({val1=>$what});
if (@records == 1){
$c->delete($records[0]->{'row_id'});
return ("$self->{'nick'}: deleted $what from your leaderboard");
}else{
return ("$self->{'nick'}: \"$what\" doesn't currently appear in your points list.");
}
}
if ($self->hasFlag("deleteeverything")){
# Make sure people who have an account have their stuff protected
return ("You don't have permission to do that.") if (!$self->hasPermission($self->accountNick()));
my @records = $c->getAllRecords();
my @sorted = sort { $b->{display_id} <=> $a->{display_id} } @records;
foreach my $rec (@sorted){
$c->delete($rec->{'row_id'});
}
return "$self->{'nick'}: ALL of your points have been deleted. I hope you're happy.";
}
if ($self->hasFlagValue("nick") || $options){
my $user = $self->hasFlagValue("nick") || $options;
my $c = $self->getCollection(__PACKAGE__, $user);
my @records = $c->getAllRecords();
if (@records == 0){
return ("$user hasn't assigned anything any points yet.");
}
$output = "According to $user: ";
my @sorted = sort { $b->{val2} <=> $a->{val2} } @records;
my $comma = "";
foreach my $rec (@sorted){
$self->addToList( $rec->{'val1'} .": " . $rec->{'val2'}, $self->BULLET);
}
return $output . $self->getList();
}else{
my @records = $c->getAllRecords();
if (@records == 0){
return ("You haven't assigned anything any points yet. Use addpoint & rmpoint to do that.");
}
$output = "$self->{'nick'}'s view: ";
my @sorted = sort { $b->{val2} <=> $a->{val2} } @records;
foreach my $rec (@sorted){
$self->addToList( $rec->{'val1'} .": " . $rec->{'val2'}, $self->BULLET);
}
return $output . $self->getList();
}
}
# Make sure people who have an account have their stuff protected
return ("You don't have permission to do that.") if (!$self->hasPermission($self->accountNick()));
if ($cmd eq 'addpoint' || ($cmd eq 'rmpoint') && (lc($options) eq lc($self->{BotName}))){
return "Usage: $cmd <whatever>" if ($options eq "");
my @records = $c->matchRecords({val1=>$options});
if (@records == 0){
$c->add($options, 1);
return ("$options is now worth 1 in $self->{'nick'}'s eyes.");
}else{
my $counter_val = $records[0]->{'val2'};
$counter_val++;
if ($c->updateRecord($records[0]->{row_id}, {val2 => $counter_val} )){
return ("$options is now worth $counter_val in $self->{'nick'}'s eyes.");
}else{
return ("Something went wrong. Let's just pretend this didn't happen.");
}
}
}elsif($cmd eq 'rmpoint'){
return "Usage: $cmd <whatever>" if ($options eq "");
my @records = $c->matchRecords({val1=>$options});
if (@records == 0){
$c->add($options, -1);
return ("$options is now worth -1 in $self->{'nick'}'s eyes.");
}else{
my $counter_val = $records[0]->{'val2'};
$counter_val--;
if ($c->updateRecord($records[0]->{row_id}, {val2 => $counter_val} )){
return ("$options is now worth $counter_val in $self->{'nick'}'s eyes.");
}else{
return ("Something went wrong. Let's just pretend this didn't happen.");
}
}
}
return $output;
}
sub listeners{
my $self = shift;
my @commands = [qw(addpoint rmpoint points)];
my @irc_events = [qw () ];
my @preg_matches = [qw () ];
my $default_permissions =[ ];
return {commands=>@commands, permissions=>$default_permissions,
irc_events=>@irc_events, preg_matches=>@preg_matches};
}
sub addHelp{
my $self = shift;
$self->addHelpItem("[plugin_description]", "$self->{BotName} points system: This is your personal points collection. You can assign and remove points as you see fit. Commands: addpoint, rmpoint, points.");
$self->addHelpItem("[addpoint]", "Give something a point. Usage: addpoint <something>");
$self->addHelpItem("[rmpoint]", "Take a point away. Usage: rmpoint <something>");
$self->addHelpItem("[points]", "See the points you've assigned. Flags: -nick=<nick> -all -delete=<thing> -deleteeverything -posneg -total ");
$self->addHelpItem("[points][-delete]", "Delete a single item from your leaderboard. Usage: points -delete=\"<the thing>\"");
$self->addHelpItem("[points][-deleteeverything]", "Delete everything from your leaderboard. Usage: points -deleteeverything");
$self->addHelpItem("[points][-total]", "Rank the rankers by total points given out. Usage: points -total");
$self->addHelpItem("[points][-posneg]", "Rank the rankers by positivity. Usage: points -posneg");
# $self->addHelpItem("[points][-most]", "Rank the rankers by number of things ranked. Usage: points -most");
}
1;
__END__