First karma run fixes

Thanks Ilya Savin for very special utf8 test case for karma.
This commit is contained in:
eleksir
2020-11-12 02:15:00 +03:00
parent c71ba14bdc
commit c6c53d6ebf
4 changed files with 55 additions and 19 deletions

View File

@@ -3,7 +3,7 @@ requires 'Cwd::utf8';
requires 'DB_File'; requires 'DB_File';
requires 'Digest::MD5'; requires 'Digest::MD5';
requires 'Data::Dumper'; requires 'Data::Dumper';
requires 'Digest::SHA1'; requires 'Digest::SHA';
requires 'File::Basename'; requires 'File::Basename';
requires 'File::Path'; requires 'File::Path';
requires 'Hailo'; requires 'Hailo';

View File

@@ -15,5 +15,8 @@
}, },
"lat" : { "lat" : {
"brain": "data/lat/lat.sqlite" "brain": "data/lat/lat.sqlite"
},
"karma" : {
"dir": "data/karma"
} }
} }

View File

@@ -6,7 +6,7 @@ use warnings;
use utf8; use utf8;
use open qw(:std :utf8); use open qw(:std :utf8);
use English qw( -no_match_vars ); use English qw( -no_match_vars );
use Digest::SHA1; use Digest::SHA qw(sha1_base64);
use DB_File; use DB_File;
use vars qw/$VERSION/; use vars qw/$VERSION/;
@@ -15,8 +15,6 @@ our @EXPORT_OK = qw(karmaSet karmaGet);
$VERSION = '1.0'; $VERSION = '1.0';
use File::Basename qw(dirname);
use File::Path qw(mkpath);
use conf qw(loadConf); use conf qw(loadConf);
use botlib qw(logger); use botlib qw(logger);
@@ -30,14 +28,25 @@ sub karmaSet (@) {
my $phrase = shift; my $phrase = shift;
my $action = shift; my $action = shift;
my $score = 0; my $score = 0;
my $karmafile = sprintf "$karmadir/%s.db", sha1_base64 ($chatid); # sha1* does not understand utf8, so explicitly construct string cost of decimal numbers only
my $karmafile = join ('', unpack ('C*', $chatid));
$karmafile = sha1_base64 ($karmafile);
# sha1 string can contain slashes, so make a bit longer string consist of only decimal numbers
$karmafile = join ('', unpack ('C*', $karmafile));
$karmafile = sprintf '%s/%s.db', $karmadir, $karmafile;
if (($phrase eq '') || ($phrase =~ /^ +$/)) {
$phrase = '';
}
# init hash, store phrase and score # init hash, store phrase and score
tie my %karma, 'DB_File', $karmafile or do { tie my %karma, 'DB_File', $karmafile || do {
logger "Something nasty happen when cachedata ties to its data: $OS_ERROR"; logger "Something nasty happen when cachedata ties to its data: $OS_ERROR";
return sprintf 'Карма %s составляет 0', $phrase; return sprintf 'Карма %s составляет 0', $phrase;
}; };
my $sha1_phrase = sha1($phrase); # bdb does not understand utf8, so tied hash too, we do not want store original phrase anyway
my $sha1_phrase = sha1_base64 (join ('', unpack ('C*', $phrase)));
if (defined $karma{$sha1_phrase}) { if (defined $karma{$sha1_phrase}) {
if ($action eq '++') { if ($action eq '++') {
@@ -50,17 +59,27 @@ sub karmaSet (@) {
} else { } else {
if ($action eq '++') { if ($action eq '++') {
$karma{$sha1_phrase} = 1; $karma{$sha1_phrase} = 1;
$score = 1;
} else { } else {
$karma{$sha1_phrase} = -1; $karma{$sha1_phrase} = -1;
$score = -1;
} }
} }
untie %karma; untie %karma;
if ($score < 0 && (($score % $max) - 1) == 0)) { if ($score < -1 && (($score % (0 - $max)) + 1) == 0) {
return sprintf 'Зарегистрировано пробитие дна, карма %s составляет %d', $phrase, $score; if ($phrase eq '') {
return sprintf 'Зарегистрировано пробитие дна, карма пустоты составляет %d', $score;
} else {
return sprintf 'Зарегистрировано пробитие дна, карма %s составляет %d', $phrase, $score;
}
} else { } else {
return sprintf 'Карма %s составляет %d', $phrase, $score; if ($phrase eq '') {
return sprintf 'Карма пустоты составляет %d', $score;
} else {
return sprintf 'Карма %s составляет %d', $phrase, $score;
}
} }
} }
@@ -68,15 +87,25 @@ sub karmaSet (@) {
sub karmaGet (@) { sub karmaGet (@) {
my $chatid = shift; my $chatid = shift;
my $phrase = shift; my $phrase = shift;
# sha1* does not understand utf8, so explicitly construct string cost of decimal numbers only
my $karmafile = join ('', unpack ('C*', $chatid));
$karmafile = sha1_base64 ($karmafile);
# sha1 string can contain slashes, so make a bit longer string consist of only decimal numbers
$karmafile = join ('', unpack ('C*', $karmafile));
$karmafile = sprintf '%s/%s.db', $karmadir, $karmafile;
if (($phrase eq '') || ($phrase =~ /^ +$/)) {
$phrase = '';
}
my $karmafile = sprintf "$karmadir/%s.db", sha1_base64 ($chatid);
# init hash, store phrase and score # init hash, store phrase and score
tie my %karma, 'DB_File', $karmafile or do { tie my %karma, 'DB_File', $karmafile || do {
logger "Something nasty happen when karma ties to its data: $OS_ERROR"; logger "Something nasty happen when karma ties to its data: $OS_ERROR";
return sprintf 'Карма %s составляет 0', $phrase; return sprintf 'Карма %s составляет 0', $phrase;
}; };
my $sha1_phrase = sha1 ($phrase); # bdb does not understand utf8, so tied hash too, we do not want store original phrase anyway
my $sha1_phrase = sha1_base64 (join ('', unpack ('C*', $phrase)));
my $score = $karma{$sha1_phrase}; my $score = $karma{$sha1_phrase};
untie %karma; untie %karma;
@@ -84,7 +113,11 @@ sub karmaGet (@) {
$score = 0; $score = 0;
} }
return sprintf 'Карма %s составляет %d', $phrase, $score; if ($phrase eq '') {
return sprintf 'Карма пустоты составляет %d', $score;
} else {
return sprintf 'Карма %s составляет %d', $phrase, $score;
}
} }
1; 1;

View File

@@ -16,6 +16,7 @@ use conf qw(loadConf);
use botlib qw(weather logger trim randomCommonPhrase); use botlib qw(weather logger trim randomCommonPhrase);
use telegramlib qw(visavi); use telegramlib qw(visavi);
use lat qw(latAnswer); use lat qw(latAnswer);
use karma qw(karmaSet karmaGet);
use Exporter qw(import); use Exporter qw(import);
our @EXPORT_OK = qw(run_telegrambot); our @EXPORT_OK = qw(run_telegrambot);
@@ -357,12 +358,11 @@ MYHELP
$reply = weather ($city); $reply = weather ($city);
} elsif (length ($text) == 4 && (substr ($text, 1, 3) eq 'lat' || substr ($text, 1, 3) eq 'лат')) { } elsif (length ($text) == 4 && (substr ($text, 1, 3) eq 'lat' || substr ($text, 1, 3) eq 'лат')) {
$reply = latAnswer(); $reply = latAnswer();
} elsif ((length($text) > 8) || (substr ($text, 1, 6) eq 'karma ') || (substr ($text, 1, 6) eq 'карма ')) { } elsif ((length ($text) >= 7) && ((substr ($text, 1, 6) eq 'karma ') || (substr ($text, 1, 6) eq 'карма '))) {
my $mytext = substr ($text, 7); my $mytext = substr ($text, 7);
chomp ($mytext);
$mytext = trim ($mytext); $mytext = trim ($mytext);
$reply = karmaGet ($chatid, $mytext); $reply = karmaGet ($chatid, $mytext);
} elsif ((length($text) > 8) || (substr ($text, 1, 6) eq 'karma ') || (substr ($text, 1, 6) eq 'карма ')) {
$reply = 'Простите, что? Карма пустоты?';
} }
} elsif ( } elsif (
($text eq $qname) or ($text eq $qname) or
@@ -384,11 +384,11 @@ MYHELP
} elsif ((lc ($text) =~ /.+ ${qtname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qtname$/)) { } elsif ((lc ($text) =~ /.+ ${qtname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qtname$/)) {
$phrase = $text; $phrase = $text;
$reply = $hailo->{$msg->chat->id}->reply ($phrase); $reply = $hailo->{$msg->chat->id}->reply ($phrase);
} elsif ((substr ($text, -2) eq '++') && (substr ($text, -2) eq '--')) { } elsif ((substr ($text, -2) eq '++') || (substr ($text, -2) eq '--')) {
my @arr = split(/\n/, $text); my @arr = split(/\n/, $text);
if ($#arr < 1) { if ($#arr < 1) {
$reply = kermaSet ($chatid, $phrase, substr ($text, -2)); $reply = karmaSet ($chatid, substr ($text, 0, -2), substr ($text, -2));
} else { } else {
# just message in chat # just message in chat
$hailo->{$msg->chat->id}->learn ($text); $hailo->{$msg->chat->id}->learn ($text);