From 5c1583d69efcd6e65715721bc0013c8afe43b705 Mon Sep 17 00:00:00 2001 From: octo Date: Tue, 12 Apr 2005 09:30:58 +0000 Subject: [PATCH] Fixed Bignumbers Plugin Fixed Quotes --- lib/Onis/Plugins/Bignumbers.pm | 3 -- lib/Onis/Plugins/Core.pm | 69 +++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/lib/Onis/Plugins/Bignumbers.pm b/lib/Onis/Plugins/Bignumbers.pm index 7e37935..46e5634 100644 --- a/lib/Onis/Plugins/Bignumbers.pm +++ b/lib/Onis/Plugins/Bignumbers.pm @@ -105,9 +105,6 @@ sub calculate $CalcData->{$main}{'uppercase'} += $uppercase; $CalcData->{$main}{'smiley_happy'} += $smiley_happy; $CalcData->{$main}{'smiley_sad'} += $smiley_sad; - - require Data::Dumper; - print STDOUT Data::Dumper->Dump ([$CalcData->{$main}], ["CalcData->{$main}"]); } } diff --git a/lib/Onis/Plugins/Core.pm b/lib/Onis/Plugins/Core.pm index fc8e3db..565b893 100644 --- a/lib/Onis/Plugins/Core.pm +++ b/lib/Onis/Plugins/Core.pm @@ -46,7 +46,9 @@ our $NickCharsCounter = Onis::Data::Persistent->new ('NickCharsCounter', 'nick', ) ); -our $QuoteCache = {}; # Saves per-nick information without any modification +our $QuoteCache = Onis::Data::Persistent->new ('QuoteCache', 'key', qw(epoch text)); +our $QuotePtr = Onis::Data::Persistent->new ('QuotePtr', 'nick', qw(pointer)); + our $QuoteData = {}; # Is generated before output. Nicks are merged according to Data::Core. our $NickData = {}; # Same as above, but for nicks rather than quotes. our $SortedNicklist = []; @@ -300,21 +302,16 @@ sub add if ((length ($text) >= $QuoteMin) and (length ($text) <= $QuoteMax)) { - if (!defined ($QuoteCache->{$nick})) - { - $QuoteCache->{$nick} = []; - } - push (@{$QuoteCache->{$nick}}, [$time, $text]); - } + my ($pointer) = $QuotePtr->get ($nick); + $pointer ||= 0; - if (defined ($QuoteCache->{$nick})) - { - while (scalar (@{$QuoteCache->{$nick}}) > $QuoteCacheSize) - { - shift (@{$QuoteCache->{$nick}}); - } - } + my $key = sprintf ("%s:%02i", $nick, $pointer); + + $QuoteCache->put ($key, $time, $text); + $pointer = ($pointer + 1) % $QuoteCacheSize; + $QuotePtr->put ($nick, $pointer); + } return (1); } @@ -378,16 +375,43 @@ sub calculate { $QuoteData->{$main} = []; } - if (defined ($QuoteCache->{$nick})) + } + + for ($QuoteCache->keys ()) + { + my $key = $_; + my ($nick, $num) = split (m/:/, $key); + my $main = get_main_nick ($nick); + + my ($epoch, $text) = $QuoteCache->get ($key); + die unless (defined ($text)); + + if (!defined ($QuoteData->{$main})) + { + die; + } + elsif (scalar (@{$QuoteData->{$main}}) < $QuoteCacheSize) + { + push (@{$QuoteData->{$main}}, [$epoch, $text]); + } + else { - my @new = (); - push (@new, @{$QuoteData->{$main}}) if (@{$QuoteData->{$main}}); - push (@new, @{$QuoteCache->{$nick}}) if (@{$QuoteCache->{$nick}}); + my $insert = -1; + my $min = $epoch; - @new = sort { $b->[0] <=> $a->[0] } (@new); - splice (@new, $QuoteCacheSize) if (scalar (@new) > $QuoteCacheSize); + for (my $i = 0; $i < $QuoteCacheSize; $i++) + { + if ($QuoteData->{$main}[$i][0] < $min) + { + $insert = $i; + $min = $QuoteData->{$main}[$i][0]; + } + } - $QuoteData->{$main} = \@new; + if ($insert != -1) + { + $QuoteData->{$main}[$insert] = [$epoch, $text]; + } } } } @@ -591,9 +615,6 @@ EOF my $num = scalar (@{$QuoteData->{$nick}}); my $rand = int (rand ($num)); - require Data::Dumper; - print STDOUT Data::Dumper->Dump ([$rand, $QuoteData->{$nick}], ['rand', "QuoteData->{$nick}"]); - $quote = html_escape ($QuoteData->{$nick}[$rand][1]); } -- 2.11.0