-
Notifications
You must be signed in to change notification settings - Fork 3
/
Badges.pm
561 lines (406 loc) · 16.4 KB
/
Badges.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package plugins::Badges;
#---------------------------------------------------------------------------
# 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/>.
#-----------------------------------------------------------------------------
# This plugin is a mess. Sorry about that.
use strict;
use warnings;
use base qw (modules::PluginBaseClass);
use modules::PluginBaseClass;
use Data::Dumper;
use Date::Manip;
sub getOutput {
my $self = shift;
my $options = $self->{options};
my $output;
my $c = $self->getCollection(__PACKAGE__, $self->accountNick());
##
## No Arguments - print Badges
##
if ( ($self->numFlags() == 0 && $options eq '')|| $self->hasFlag("list")){
my @records = $c->getAllRecords();
$output = "Your badges: ";
foreach my $badge (@records){
my $badge_name = $badge->{'val1'};
my $badge_date = $badge->{'val2'};
my $badge_dollar = $badge->{'val3'};
if ($self->hasTime($badge_date)){
$output .= $self->timeSince($badge_date). " with $badge_name";
if ($badge_dollar){
my $saved = $badge_dollar * $self->daysSince($badge_date);
$saved = commify($saved);
$output .= ' ($'.$saved. ' saved)';
}
$output .=". ";
}else{
$output .= "On day ".$self->daysSince($badge_date). " with $badge_name";
if ($badge_dollar){
my $saved = $badge_dollar * $self->daysSince($badge_date);
$saved = commify($saved);
$output.= ' ($'.$saved. ' saved)';
}
$output .=". ";
}
}
if (@records){
return ($output);
}else{
return ("You don't have any badges. " . $self->help('badge'));
}
##
## add a badge
##
}elsif($self->hasFlag("add")){
if (! $self->hasPermission($self->accountNick()) ){
return ("You don't have permission to do that.");
}
my ($badge_date, $badge_name);
if (!($badge_name = $self->hasFlagValue("name"))){
return ("A badge name is required. Example: -name = QuitSmoking Example: -name=\"Quit Smoking\"");
}
if (! ($badge_date = $self->hasFlagValue("date"))){
return ("A date is required. Example: -date=\"10/31/2012\" Example: -date=\"January 4, 2013\" Example: -date=\"4 days ago\" Example: -date=\"last tuesday\"");
}
my $date = new Date::Manip::Date;
my $err;
eval{
$err = $date->parse($badge_date);
};
if ($@){
return ("Whoops. An error occurred.");
}
if ($err){
print "ERROR PARSING DATE : $err\n";
return ("There was an error parsing that date. Badge not added");
}
my $date_db = $date->value();
#print "Date value is " . $date_db . "\n";
my $date_printable = $date->printf("%m/%d/%Y");
my @records = $c->matchRecords({val1=>$badge_name});
if (@records > 0){
return("Looks like you already have a badge by that name. Use delete or reset.");
}
my $temp = $badge_name;
$temp=~s/[A-Za-z0-9 ]//g;
if ($temp){
return "'$temp' You can't use special characters in your badge name. Only letters and numbers, and spaces if you must."
}
$c->add($badge_name, $date_db);
return "added badge '$badge_name' with a date of $date_printable for user ". $self->{'nick'}.".";
##
## all - see all badges of a particular type
##
}elsif($self->hasFlag("all")){
my $badge_name;
my $oc = $self->getCollection(__PACKAGE__, '%');
if ($badge_name = $self->hasFlagValue("name")){
my @records = $oc->matchRecords({val1=>$badge_name});
my $ret = "$badge_name badges: ";
foreach my $badge (@records){
my $badge_user = $badge->{'collection_name'};
my $badge_name = $badge->{'val1'};
my $badge_date = $badge->{'val2'};
if ($self->hasTime($badge_date)){
$ret .= "$badge_user: " . $self->timeSince($badge_date). ". ";
}else{
$ret .= "$badge_user: " . $self->daysSince($badge_date). " Days. ";
}
}
return ($ret) if (@records);
return ("No one has a badge called $badge_name.");
}else{
my @records = $oc->getAllRecords();
my %badge_list;
foreach my $badge (@records){
my $badge_name = $badge->{'val1'};
$badge_list{$badge_name}++;
}
my $ret = "All user badges: ";
foreach my $k (sort keys %badge_list){
$ret.= $k . "(".$badge_list{$k}.") ";
}
return ($ret) if (@records);
return ("No one has a badge! Man, you guys are L-A-M-E.");
}
##
## user - see another user's badge
##
}elsif( (my $nick = $self->hasFlagValue("nick"))){
my $oc = $self->getCollection(__PACKAGE__, $nick);
my @records = $oc->getAllRecords();
my $ret = $nick."'s badges: ";
foreach my $badge (@records){
my $badge_name = $badge->{'val1'};
my $badge_date = $badge->{'val2'};
my $badge_dollar = $badge->{'val3'};
if ($self->hasTime($badge_date)){
$ret .= $self->timeSince($badge_date). " with $badge_name";
if ($badge_dollar){
my $saved = $badge_dollar * $self->daysSince($badge_date);
$saved = commify($saved);
$ret .= ' ($'.$saved. ' saved)';
}
$ret .= ". ";
}else{
$ret .= "On day ".$self->daysSince($badge_date). " with $badge_name";
if ($badge_dollar){
my $saved = $badge_dollar * $self->daysSince($badge_date);
$saved = commify($saved);
$ret .= ' ($'.$saved. ' saved)';
}
$ret .= ". ";
}
}
return ($ret) if (@records);
return ($nick. " doesn't have any badges.");
##
## dollar amounts
##
}elsif($self->hasFlag("cost")){
if (! $self->hasPermission($self->accountNick()) ){
return ("You don't have permission to do that.");
}
my $ret = "";
my ($badge_name, $badge_cost);
if (!($badge_name=$self->hasFlagValue("name"))){
return "You must specify a badge name. Example: -name = \"QuitSmoking\" ";
}
if (!($badge_cost=$self->hasFlagValue("cost"))){
return "You must specify a daily cost. Example: -cost = 15.99. To clear the cost, use -cost = none";
}
$badge_cost =~ s/\$//;
if ($badge_cost eq "none"){
$badge_cost = 0;
}
my @records = $c->matchRecords({val1=>$badge_name});
if (@records == 1){
if ($c->updateRecord($records[0]->{'row_id'}, {val3 => $badge_cost} )){
return "Badge $badge_name updated.";
}else{
return "Whoops. Something went wrong. (#3pr)";
}
}elsif(@records > 1){
print "POSSIBLE ERROR - MORE THAN ONE RECORD\n";
return ("Whoops, something went wrong.");
}else{
return ("Can't find a badge for you with that name." );
}
##
## delete a badge
##
}elsif($self->hasFlag("delete")){
if (! $self->hasPermission($self->accountNick()) ){
return ("You don't have permission to do that.");
}
my $ret = "";
my $badge_name;
if (!($badge_name=$self->hasFlagValue("name"))){
return "You must specify a badge name. Example: -name = \"QuitSmoking\" ";
}
my @records = $c->matchRecords({val1=>$badge_name});
if (@records == 1){
$c->delete($records[0]->{'row_id'});
return "Deleted badge $badge_name";
}elsif(@records > 1){
print "POSSIBLE ERROR - MORE THAN ONE RECORD\n";
return ("Whoops, something went wrong.");
}else{
return ("Can't find a badge for you with that name. Use '".$self->{BotCommandPrefix}."badge list' to list your current badges.");
}
##
## Reset Badge
##
}elsif( $self->hasFlag("update")){
if (! $self->hasPermission($self->accountNick()) ){
return ("You don't have permission to do that.");
}
my ($badge_name, $badge_date);
if (!($badge_name = $self->hasFlagValue("name"))){
return ("A badge name is required. Example: -name = QuitSmoking Example: -name=\"Quit Smoking\"");
}
if (!($badge_date = $self->hasFlagValue("date"))){
return ("A date is required. Example: -date=\"10/31/2012\" Example: -date=\"January 4, 2013\" Example: -date=\"4 days ago\" Example: -date=\"last tuesday\"");
}
my @records = $c->matchRecords({val1=>$badge_name});
if (@records == 1){
my $date = new Date::Manip::Date;
if ($badge_date){
my $err;
eval{ $err = $date->parse($badge_date); };
if ($@){ return ("Whoops. An error occurred."); }
if ($err){
print "ERROR PARSING DATE : $err\n";
return ("There was an error parsing that date. Badge not updated");
}
}else{
if ($self->hasTime($records[0]->{'val2'})){
$date->parse("now");
}else{
$date->parse("today");
}
}
my $date_db = $date->value();
my $date_printable = $date->printf("%m/%d/%Y");
if ($c->updateRecord($records[0]->{'row_id'}, {val2 => $date_db} )){
return ("Badge $badge_name updated to $date_printable.");
}else{
return ("There was an error updating that badge.");
}
}elsif(@records > 1){
print "POSSIBLE ERROR - MORE THAN ONE RECORD\n";
return ("Whoops, something went wrong. #4rpo");
}else{
return ("Can't find a badge for you with that name.");
}
##
## show single badge by name
##
}elsif($self->hasFlag("name") || $self->{'options'}=~/^(\w+)\b/){
my $badge_name;
if (!($badge_name = $self->hasFlagValue("name"))){
$badge_name = $1;
}
my $ret="";
my @records = $c->getAllRecords();
foreach my $badge (@records){
if ($badge->{'val1'} eq $badge_name){
my $badge_date = $badge->{'val2'};
my $badge_dollar = $badge->{'val3'};
if ($self->hasTime($badge_date)){
$ret .= $self->timeSince($badge_date). " with $badge_name";
if ($badge_dollar){
my $saved = $badge_dollar * $self->daysSince($badge_date);
$saved = commify($saved);
$ret .= ' ($'.$saved. ' saved)';
}
$ret .= ". ";
}else{
$ret .= "On day ".$self->daysSince($badge_date). " with $badge_name";
if ($badge_dollar){
my $saved = $badge_dollar * $self->daysSince($badge_date);
$saved = commify($saved);
$ret .= ' ($'.$saved. ' saved)';
}
$ret .= ". ";
}
}
}
if ($ret){
return ($ret);
}else{
return ("You don't have a badge by that name. " . $self->help('badge'));
}
}
}
sub hasTime{
my $self = shift;
my $date = shift;
if ($date=~/00:00:00/){
return 0;
}else{
return 1;
}
}
sub daysSince{
my $self = shift;
my $date_str = shift;
my $date = new Date::Manip::Date;
my $err = $date->parse($date_str);
my $now = new Date::Manip::Date;
$now->parse("today");
my $date_printable = $date->printf("%m/%d/%Y");
my $delta = $date->calc($now);
my @dv = $delta->value();
my $days = int($dv[4] / 24);
my $ret;
if ($days >=0){
$ret = $days + 1;
}else{
$ret = $days;
}
return $ret;
}
sub timeSince{
my $self = shift;
my $date_str = shift;
my $date = new Date::Manip::Date;
my $err = $date->parse($date_str);
my $now = new Date::Manip::Date;
$now->parse("now");
my $date_printable = $date->printf("%m/%d/%Y");
my $delta = $date->calc($now);
my @dv = $delta->value();
my $hours = $dv[4];
my $minutes = $dv[5];
my $seconds = $dv[6];
my $days = int($dv[4] / 24);
if ($days > 2){
return $days + 1 . " days";
}elsif ($days >= 1){
return "$days days, ".($hours % 24) ." hours";
}elsif ($hours >= 1){
return "$hours hours";
}elsif ($minutes >= 1){
return "$minutes minutes";
}elsif($seconds > 0){
return "$seconds seconds";
}elsif($hours > -48){
return "$hours hours ";
}else{
return $days . " days ";
}
my $ret = "hours: $hours min: $minutes sec: $seconds";
return $ret;
}
sub commify {
local $_ = shift;
s{(?<!\d|\.)(\d{4,})}
{my $n = $1;
$n=~s/(?<=.)(?=(?:.{3})+$)/,/g;
$n;
}eg;
return $_;
}
sub listeners{
my $self = shift;
my @commands = [qw(badge)];
## Values: irc_join
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};
}
##
## addHelp()
## The help system will pull from here using PluginBaseClass->help(key).
##
sub addHelp{
my $self = shift;
$self->addHelpItem("[plugin_description]", "Badges. Get a date-based \"days since\" badge for whatever.");
$self->addHelpItem("[badge]", "Use $self->{BotCommandPrefix}badge to list your badges. Flags: -add -delete -update -cost, with -name=\"Badge Name\" -date=\"a date string\". Use -all to see all users' badges, use -all -name=\"Badge Name\" to see all badges of a particular type for all users. Use -nick=SomeGuy to see SomeGuy's badges");
$self->addHelpItem("[badge][-add]", "Create a badge. Usage: $self->{BotCommandPrefix}badge -add -name=\"Badge Name\" -date=\"some date\"");
$self->addHelpItem("[badge][-update]", "Change the date on a badge. Usage: $self->{BotCommandPrefix}badge -update -name=\"Badge Name\" -date=\"some date\"");
$self->addHelpItem("[badge][-delete]", "Usage: $self->{BotCommandPrefix}badge -delete -name=\"Badge Name\"");
$self->addHelpItem("[badge][-cost]", "Set a daily cost for a badge. Example: $self->{BotCommandPrefix}badge -cost = 5.25 -name=\"Badge Name\". To clear a cost, use -cost = none");
$self->addHelpItem("[badge][-all]", "See system wide badges. Usage: $self->{BotCommandPrefix}badge -all. To see all badges of a particular type, use $self->{BotCommandPrefix}badge -all -name=\"Badge Name\"");
$self->addHelpItem("[badge][-nick]", "See another users badges. Usage: $self->{BotCommandPrefix}badge -nick = someguy");
$self->addHelpItem("[badge][-name]", "See a particular badge. Usage: $self->{BotCommandPrefix}badge -name = \"Badge Name\"");
}
1;
__END__