]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Graph.pm
changed so score is red and size is green as in other graphs
[ndwebbie.git] / ND / Web / Pages / Graph.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::Web::Pages::Graph;
21 use strict;
22 use warnings FATAL => 'all';
23 use ND::Include;
24 use ND::Web::Graph;
25 use DBI qw(:sql_types);
26 use DBD::Pg qw(:pg_types);
27
28
29 use base qw/ND::Web::Image/;
30
31 $ND::Web::Page::PAGES{graph} = 'ND::Web::Pages::Graph';
32
33 sub render_body {
34         my $self = shift;
35         my $DBH = $self->{DBH};
36
37         my %graph_settings = (
38                 y_number_format => sub { prettyValue abs $_[0]},
39                 y1_label => 'size',
40                 y2_label => 'rest',
41         );
42
43         my %req;
44         my $type;
45         if ($self->{URI} =~ m{^/\w+/(stats|ranks)/(.*)}){
46                 $type = $1;
47                 if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?$}){
48                         $req{x} = $1;
49                         $req{y} = $2;
50                         $req{z} = $3;
51                         if (defined $3){
52                                 ($req{id}) = $DBH->selectrow_array(q{SELECT planetid($1,$2,$3,$4)},undef,$1,$2,$3,$self->{TICK});
53                         }else{
54                                 $type = "gal$type";
55                                 $req{id} = 100*$2+$2;
56                         }
57                 }
58         }elsif($self->{URI} =~ m{^/\w+/(alliance(?:avg)?)/(\d+)}){
59                 $type = $1;
60                 $req{id} = $2;
61         }elsif($self->{URI} =~ m{^/\w+/planetvsnd/(\d+)}){
62                 $type = 'planetvsnd';
63                 $req{id} = $1;
64         }
65
66         die 'no real type' unless $type;
67
68         my $findGraph = $DBH->prepare(q{SELECT EXTRACT(EPOCH FROM last_modified) AS last_modified FROM graphs WHERE id = $1 AND tick = $2 AND type = $3});
69         $findGraph->execute($req{id},$self->{TICK},$type) or die $DBH->errstr;
70         if (my $graph = $findGraph->fetchrow_hashref){
71                 $self->{R}->set_last_modified($graph->{last_modified});
72                 if ((my $rc = $self->{R}->meets_conditions) != Apache2::Const::OK){
73                         $self->{R}->status($rc);
74                         return;
75                 }
76                 my $findGraph = $DBH->prepare(q{SELECT img FROM graphs WHERE id = $1 AND tick = $2 AND type = $3});
77                 $findGraph->execute($req{id},$self->{TICK},$type) or die $DBH->errstr;
78                 $graph = $findGraph->fetchrow_hashref;
79                 return $graph->{img}
80         }
81
82         my $img;
83         my $delGraph = $DBH->prepare(q{DELETE FROM graphs WHERE id = $1 AND type = $2});
84         my $addGraph = $DBH->prepare(q{INSERT INTO graphs (type,id,tick,img) VALUES($1,$2,$3,$4)});
85         if ($type eq 'stats' || $type eq 'ranks'){
86                 my $type = $1;
87                 my $findGraph;
88                 my ($x,$y,$z) = ($req{x},$req{y},$req{z});
89                 my $query;
90                 if ($type eq 'stats'){
91                                 $query = $DBH->prepare(q{SELECT tick,score,size,value,xp*60 AS "xp*60" FROM planets natural join planet_stats WHERE id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
92                         $graph_settings{y_min_value} = 0;
93                 }elsif($type eq 'ranks'){
94                         $query = $DBH->prepare(q{SELECT tick,-scorerank AS score,-sizerank AS size,-valuerank AS value,-xprank AS xp FROM planets natural join planet_stats WHERE id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
95                         $graph_settings{y_max_value} = 0;
96                 }
97                 $query->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr;
98                 $graph_settings{title} = $type;
99                 $graph_settings{two_axes} = 1;
100                 $graph_settings{use_axis} = [2,1,2,2];
101                 $img = graphFromQuery 500,300,\%graph_settings,$query;
102         }elsif ($type eq 'galstats' || $type eq 'galranks'){
103                 my $query;
104                 my ($x,$y) = ($req{x},$req{y});
105                 if ($type eq 'galstats'){
106                         $query = $DBH->prepare(q{SELECT tick,score,size,value,xp*60 AS "xp*60" FROM galaxies WHERE x = $1 AND y = $2 ORDER BY tick ASC});
107                         $graph_settings{y_min_value} = 0;
108                 }elsif($type eq 'galranks'){
109                         $query = $DBH->prepare(q{SELECT tick,-scorerank AS score,-sizerank AS size,-valuerank AS value,-xprank AS xp FROM galaxies WHERE x = $1 AND y = $2  ORDER BY tick ASC});
110                         $graph_settings{y_max_value} = 0;
111                 }
112                 $query->execute($x,$y) or die $DBH->errstr;
113                 $graph_settings{title} = $type;
114                 $graph_settings{two_axes} = 1;
115                 $graph_settings{use_axis} = [2,1,2,2];
116                 $img = graphFromQuery 500,300,\%graph_settings,$query;
117         }elsif ($type eq 'alliance' || $type eq 'allianceavg'){
118
119
120                 $graph_settings{title} = 'Alliance vs known members';
121                 $graph_settings{two_axes} = 1;
122                 $graph_settings{use_axis} = [2,1,1,2];
123                 $graph_settings{y2_label} = 'score';
124
125                 my $query;
126                 if ($type eq 'alliance'){
127                         $query = $DBH->prepare(q{SELECT a.tick,a.score,a.size,memsize, memscore FROM (SELECT tick,SUM(size) AS memsize,SUM(score) AS memscore FROM planets p JOIN planet_stats ps USING (id) WHERE p.alliance_id = $1 GROUP BY tick) p JOIN alliance_stats a ON a.tick = p.tick
128 WHERE a.id = $1 ORDER BY tick});
129                 }else{
130                         $graph_settings{title} = 'Average alliance vs known members';
131                         $query = $DBH->prepare(q{SELECT a.tick,a.score/members AS score,a.size/members AS size,memsize, memscore FROM (SELECT tick,AVG(size) AS memsize,AVG(score) AS memscore FROM planets p JOIN planet_stats ps USING (id) WHERE p.alliance_id = $1 GROUP BY tick) p JOIN alliance_stats a ON a.tick = p.tick
132 WHERE a.id = $1 ORDER BY tick});
133                 }
134                 $query->execute($2) or die $DBH->errstr;
135
136                 $img = graphFromQuery 500,300,\%graph_settings,$query;
137         }elsif ($type eq 'planetvsnd'){
138                 $graph_settings{title} = 'You vs ND AVG';
139                 $graph_settings{two_axes} = 1;
140                 $graph_settings{use_axis} = [2,1,1,2];
141                 $graph_settings{y2_label} = 'score';
142
143                 my $query = $DBH->prepare(q{SELECT a.tick,a.score/members AS NDscore,a.size/members as NDsize,memsize, memscore FROM (SELECT tick,size AS memsize,score AS memscore FROM planets p JOIN planet_stats ps USING (id) WHERE p.id = $1) p JOIN alliance_stats a ON a.tick = p.tick
144                         WHERE a.id = 1 ORDER BY tick});
145                 $query->execute($req{id}) or die $DBH->errstr;
146
147                 $img = graphFromQuery 500,300,\%graph_settings,$query;
148         }
149
150         die 'no image' unless defined $img;
151
152         $delGraph->execute($req{id},$type) or die $DBH->errstr;
153         $addGraph->bind_param('$1',$type,{TYPE => DBI::SQL_VARCHAR }) or die $DBH->errstr;
154         $addGraph->bind_param('$2',$req{id},{TYPE => DBI::SQL_INTEGER }) or die $DBH->errstr;
155         $addGraph->bind_param('$3',$self->{TICK},{TYPE => DBI::SQL_INTEGER }) or die $DBH->errstr;
156         $addGraph->bind_param('$4',$img,{TYPE => DBI::SQL_VARBINARY }) or die $DBH->errstr;
157         $addGraph->execute or die $DBH->errstr;
158         $self->{R}->set_last_modified(time);
159
160         return $img;
161 };
162
163 1;