Add karmaset, and fix the karma rankings
authorpdbogen <pdbogen@088b83a4-0077-4247-935c-42ec02c2848b>
Thu, 12 Mar 2009 22:49:49 +0000 (22:49 +0000)
committerpdbogen <pdbogen@088b83a4-0077-4247-935c-42ec02c2848b>
Thu, 12 Mar 2009 22:49:49 +0000 (22:49 +0000)
git-svn-id: https://www.cernu.us/~pdbogen/svn/destult2@87 088b83a4-0077-4247-935c-42ec02c2848b

cmdaccess.yaml
commands.yaml

index 05c94cc..38b0a79 100644 (file)
@@ -13,6 +13,7 @@ FLUSHURLS: 1
 IGNORE: 2
 JOIN: 1
 KARMACLEAN: 2
+KARMASET: 2
 MEMOLIST: 2
 MEMOMOVE: 2
 MEMOWIPE: 2
index 30de8b5..a31b1c2 100644 (file)
@@ -1725,6 +1725,28 @@ KARMAUP: |-
     print( "KARMA: <$who> $what++\n" );
   }
 
+KARMASET: |-
+  sub {
+    my( $kernel, $heap, $who, $what, $src, $dest, $replypath ) = @_;
+    if( !exists( $heap->{ "karma" } ) ) {
+      if( -e "karma.yaml" ) {
+        $heap->{ "karma" } = LoadFile( "karma.yaml" );
+      } else {
+        $heap->{ "karma" } = {};
+      }
+    }
+    if( $what =~ /^([^ ]+) (-?[0-9]+)$/ ) {
+      my $value = $2;
+      $what = $1;
+      $heap->{ "karma" }->{ uc( $what ) } = $value;
+      DumpFile( "karma.yaml", $heap->{ 'karma' } );
+      print( "KARMA: <$who> $what = $value\n" );
+      $kernel->post( $src, $replypath, "Set.", $dest );
+    } else {
+      $kernel->post( $src, $replypath, "Usage: KARMASET <what> <value>", $dest );
+    }
+  }
+
 KARMACLEAN: |-
   sub {
     my( $kernel, $heap, $who, $what, $src, $dest, $replypath ) = @_;
@@ -1766,8 +1788,6 @@ KARMA: |-
     }
     if( $what =~ /^([+-])([1-9][0-9]*)$/ ) {
       my $target = $2;
-      my $thresh = 0;
-      my $tItem = 0;
       my $mult = ($1 eq "-"?-1:1);
       my @response;
       my %karma = %{ $heap->{ "karma" } };
@@ -1781,30 +1801,16 @@ KARMA: |-
       }
       
       for my $key (keys %karma) {
-       my $slotted = 0;
-        INNER:
-        for( my $i = 0; $i < $target; $i++ ) {
-          if( !defined( $response[ $i ]->[1] ) ) {
-            $response[ $i ] = [ $karma{ $key }, $key ];
-            $slotted = 1;
-            last INNER;
-          }
-        }
-        if( $slotted == 0 ) {
-          INNER:
-          for( my $i = 0; $i < $target; $i++ ) {
-            if( $response[ $i ]->[0]*$mult < $karma{ $key }*$mult ) {
-            $response[ $i ] = [ $karma{ $key }, $key ];
-            last INNER;
-            }
+        if( !defined( $response[ 0 ]->[1] ) || $response[ 0 ]->[0]*$mult < $karma{ $key }*$mult ) {
+          $response[ 0 ] = [ $karma{ $key }, $key ];
+          if( $mult == 1 ) {
+            @response = sort { $a->[0] <=> $b->[0] } @response;
+          } else {
+            @response = sort { $b->[0] <=> $a->[0] } @response;
           }
         }
       }
-      if( $mult == 1 ) {
-        @response = sort { $b->[0] <=> $a->[0] } @response;
-      } else {
-        @response = sort { $a->[0] <=> $b->[0] } @response;
-      }
+      @response = reverse @response;
       my $resp = $response[0]->[1]." (".$response[0]->[0].")";
       for( my $i = 1; $i < scalar @response; $i++ ) {
        next unless defined $response[ $i ]->[1];