]> ruin.nu Git - ND.git/blob - Include.pm
9a42d97b092f062599c628e7e72e7257bbde2de3
[ND.git] / Include.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 package ND::Include;
21 use strict;
22 use warnings;
23 use CGI qw{:standard};
24 require Exporter;
25
26 our @ISA = qw/Exporter/;
27
28 our @EXPORT = qw/min max parseValue prettyValue log_message intel_log unread_query pa_xp/;
29
30 sub min {
31     my ($x,$y) = @_;
32     return ($x > $y ? $y : $x);
33 }
34
35 sub max {
36     my ($x,$y) = @_;
37     return ($x < $y ? $y : $x);
38 }
39
40
41 sub parseValue {
42         if (defined $_[0] && $_[0] =~ /^(-?\d+(?:\.\d+)?)([khMG])?$/){
43                 return $1 unless defined $2;
44                 return $1*100 if $2 eq 'h';
45                 return $1*1000 if $2 eq 'k';
46                 return $1*1000000 if $2 eq 'M';
47                 return $1*1000000000 if $2 eq 'G';
48         }
49         return $_[0];
50 }
51
52 sub prettyValue {
53         my ($value,$decimals) = @_;
54         return sprintf('%.3e',$value) if $value > 1000000000000000;
55         my $unit = '';
56         my @units = ('k','M','G','T');
57         for (my $i = 0; $value >= 1000 && $i < 4;$i++){
58                 $value /= 1000;
59                 $unit = $units[$i];
60         }
61         unless (defined $decimals){
62                 $decimals = '.0';
63                 $decimals = '.1' if $value < 100 && $unit;
64                 $decimals = '.2' if $value < 10 && $unit;
65         }
66
67         return sprintf('%'.$decimals.'f%s', $value,$unit);
68 }
69
70
71 sub log_message {
72         my ($uid, $message) = @_;
73         my $log = $ND::DBH->prepare_cached(q{INSERT INTO forum_posts (ftid,uid,message) VALUES(
74                 (SELECT ftid FROM users WHERE uid = $1),$1,$2)});
75         $log->execute($uid,$message) or $ND::ERROR .= p($ND::DBH->errstr);
76 }
77
78 sub intel_log {
79         my ($uid,$planet, $message) = @_;
80         my $log = $ND::DBH->prepare_cached(q{INSERT INTO forum_posts (ftid,uid,message) VALUES(
81                 (SELECT ftid FROM planets WHERE id = $3),$1,$2)});
82         $log->execute($uid,$message,$planet) or $ND::ERROR .= p($ND::DBH->errstr);
83 }
84
85 sub pa_xp {
86         my ($roids,$ascore,$avalue,$tscore,$tvalue) = @_;
87         my $bravery = (max(0,min(2,$tscore/$ascore)-0.6)) * (min(2,$tvalue/$avalue)-0.4);
88         return int(max($roids * 10 * $bravery,0))
89
90 }
91
92 sub unread_query {
93         return $ND::DBH->prepare_cached(q{
94                         SELECT count(*) AS unread, count(NULLIF(fp.time > $2,FALSE)) AS new
95 FROM forum_boards fb NATURAL JOIN forum_threads ft 
96         JOIN forum_posts fp USING (ftid) LEFT OUTER JOIN 
97                 (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
98 WHERE (ftv.time IS NULL OR fp.time > ftv.time) AND fbid > 0 AND
99         fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1)))
100                 });
101 }
102
103 1;