]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/AllianceRankings.pm
29bb2df612117c44e9a230674892cb2c12eed3d5
[ndwebbie.git] / NDWeb / Pages / AllianceRankings.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 NDWeb::Pages::AllianceRankings;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
24 use NDWeb::Include;
25
26 use base qw/NDWeb::XMLPage/;
27
28 $NDWeb::Page::PAGES{alliancerankings} = __PACKAGE__;
29
30 sub parse {
31         #TODO: Need to fix some links first
32         #if ($uri =~ m{^/[^/]+/(\w+)}){
33         #       param('order',$1);
34         #}
35 }
36
37 sub render_body {
38         my $self = shift;
39         my ($BODY) = @_;
40         $self->{TITLE} = 'Top Alliances';
41         my $DBH = $self->{DBH};
42
43         return $self->noAccess unless $self->isMember;
44
45         my $error = '';
46
47         $BODY->param(isHC => $self->isHC);
48
49         my $offset = 0;
50         if (defined param('offset') && param('offset') =~ /^(\d+)$/){
51                 $offset = $1;
52         }
53         $BODY->param(Offset => $offset);
54         $BODY->param(PrevOffset => $offset - 100);
55         $BODY->param(NextOffset => $offset + 100);
56
57         my $order = 'scorerank';
58         if (defined param('order') && param('order') =~ /^(scorerank|sizerank|members|avgsize|avgscore)$/){
59                 $order = $1;
60         }
61         $BODY->param(Order => $order);
62         $order .= ' DESC' unless $order =~ /rank$/;
63
64
65         #my $extra_columns = '';
66         #if ($self->isHC){
67         #       $extra_columns = ",alliance_status,hit_us, alliance,relationship,nick";
68         #}
69         my $query = $DBH->prepare(qq{SELECT a.name,
70                 size, size_gain, size_gain_day,
71                 score,score_gain,score_gain_day,
72                 avgsize,avgsize_gain,avgsize_gain_day,
73                 avgscore,avgscore_gain,avgscore_gain_day,
74                 sizerank,sizerank_gain,sizerank_gain_day,
75                 scorerank,scorerank_gain,scorerank_gain_day,
76                 members,members_gain,members_gain_day
77         FROM 
78                 ( SELECT id, members,members_gain,members_gain_day, size, score, (size/members) AS avgsize, (score/members) AS avgscore, sizerank, scorerank, size_gain, score_gain, (size_gain/members) AS avgsize_gain, (score_gain/members) AS avgscore_gain, sizerank_gain, scorerank_gain, size_gain_day, score_gain_day, (size_gain_day/members) AS avgsize_gain_day, (score_gain_day/members) AS avgscore_gain_day, sizerank_gain_day, scorerank_gain_day
79                         FROM alliance_stats WHERE tick = (( SELECT max(tick) AS max FROM alliance_stats))) ast
80                 NATURAL JOIN alliances a
81                 ORDER BY $order LIMIT 100 OFFSET ?});
82         $query->execute($offset) or $error .= p($DBH->errstr);
83         my @alliances;
84         while (my $alliance = $query->fetchrow_hashref){
85                 for my $type (qw/members size score avgsize avgscore/){
86                         #$alliance->{$type} = prettyValue($alliance->{$type});
87                         next unless defined $alliance->{"${type}_gain_day"};
88                         $alliance->{"${type}img"} = 'stay';
89                         $alliance->{"${type}img"} = 'up' if $alliance->{"${type}_gain_day"} > 0;
90                         $alliance->{"${type}img"} = 'down' if $alliance->{"${type}_gain_day"} < 0;
91                         if( $type eq 'size' || $type eq 'score'){
92                                 $alliance->{"${type}rankimg"} = 'stay';
93                                 $alliance->{"${type}rankimg"} = 'up' if $alliance->{"${type}rank_gain_day"} < 0;
94                                 $alliance->{"${type}rankimg"} = 'down' if $alliance->{"${type}rank_gain_day"} > 0;
95                         }
96                         for my $type ($type,"${type}_gain","${type}_gain_day"){
97                                 $alliance->{$type} =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g; #Add comma for ever 3 digits, i.e. 1000 => 1,000
98                         }
99                 }
100                 push @alliances,$alliance;
101         }
102         $BODY->param(Alliances => \@alliances);
103         $BODY->param(Error => $error);
104         return $BODY;
105 }
106
107 1;