]> ruin.nu Git - ndwebbie.git/blobdiff - ND/Include.pm
ignore xp when both factors are negative
[ndwebbie.git] / ND / Include.pm
index 53e7a4c17580a48c1e26c2c0d6d36b0690547d51..992f978d9b28ff733bff43d06b2db0aebeafe732 100644 (file)
@@ -25,7 +25,7 @@ require Exporter;
 
 our @ISA = qw/Exporter/;
 
-our @EXPORT = qw/min max log_message intel_log unread_query/;
+our @EXPORT = qw/min max parseValue prettyValue log_message intel_log unread_query pa_xp/;
 
 sub min {
     my ($x,$y) = @_;
@@ -37,6 +37,36 @@ sub max {
     return ($x < $y ? $y : $x);
 }
 
+
+sub parseValue {
+       if (defined $_[0] && $_[0] =~ /^(-?\d+(?:\.\d+)?)([khMG])?$/){
+               return $1 unless defined $2;
+               return $1*100 if $2 eq 'h';
+               return $1*1000 if $2 eq 'k';
+               return $1*1000000 if $2 eq 'M';
+               return $1*1000000000 if $2 eq 'G';
+       }
+       return $_[0];
+}
+
+sub prettyValue {
+       my ($value,$decimals) = @_;
+       my $unit = '';
+       my @units = ('k','M','G','T');
+       for (my $i = 0; $value >= 1000;$i++){
+               $value /= 1000;
+               $unit = $units[$i];
+       }
+       unless (defined $decimals){
+               $decimals = '.0';
+               $decimals = '.1' if $value < 100 && $unit;
+               $decimals = '.2' if $value < 10 && $unit;
+       }
+
+       return sprintf('%'.$decimals.'f%s', $value,$unit);
+}
+
+
 sub log_message {
        my ($uid, $message) = @_;
        my $log = $ND::DBH->prepare_cached(q{INSERT INTO forum_posts (ftid,uid,message) VALUES(
@@ -51,6 +81,13 @@ sub intel_log {
        $log->execute($uid,$message,$planet) or $ND::ERROR .= p($ND::DBH->errstr);
 }
 
+sub pa_xp {
+       my ($roids,$ascore,$avalue,$tscore,$tvalue) = @_;
+       my $bravery = (max(0,min(2,$tscore/$ascore)-0.6)) * (min(2,$tvalue/$avalue)-0.4);
+       return int(max($roids * 10 * $bravery,0))
+
+}
+
 sub unread_query {
        return $ND::DBH->prepare_cached(q{
                        SELECT count(*) AS unread, count(NULLIF(fp.time > $2,FALSE)) AS new