]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Graph.pm
Object Oriented Perl
[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;
23 use CGI qw/:standard/;
24 use ND::Include;
25 use ND::Web::Graph;
26
27 our @ISA = qw/ND::Web::Image/;
28
29 $ND::Web::Page::PAGES{graph} = 'ND::Web::Pages::Graph';
30
31 sub render_body {
32         my $self = shift;
33         my $DBH = $self->{DBH};
34
35         my $type;
36         my ($x,$y,$z);
37         if ($self->{URI} =~ m{^/\w+/(stats|ranks)/(.*)}){
38                 $type = $1;
39                 if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?$}){
40                         $x = $1;
41                         $y = $2;
42                         $z = $3;
43                 }
44         }
45
46         die "Not a proper type" unless defined $type;
47
48         my %graph_settings = (
49                 y_number_format => sub { prettyValue abs $_[0]},
50                 title => $type,
51                 y1_label => 'size',
52                 y2_label => 'rest',
53         );
54
55         my $findGraph;
56         if (defined $z){
57                 $findGraph = $DBH->prepare(q{SELECT graph FROM planet_graphs WHERE planet = planetid($1,$2,$3,$4) AND tick = $4 AND type = $5});
58                 $findGraph->execute($x,$y,$z,$ND::TICK,$type) or die $DBH->errstr;
59         }
60         my $img;
61         if (defined $findGraph and my $graph = $findGraph->fetchrow_hashref){
62                 $img = $graph->{graph};
63         }elsif(defined $x){
64                 my $query;
65                 if (defined $z){
66                         if ($type eq 'stats'){
67                                 $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});
68                         }elsif($type eq 'ranks'){
69                                 $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});
70                         }
71                         $query->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr;
72                 }else{
73                         if ($type eq 'stats'){
74                                 $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});
75                         }elsif($type eq 'ranks'){
76                                 $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});
77                         }
78                         $query->execute($x,$y) or die $DBH->errstr;
79                 }
80                 
81                 $graph_settings{two_axes} = 1;
82                 $graph_settings{use_axis} = [2,1,2,2];
83                 $graph_settings{y_max_value} = 0 if $type eq 'ranks';
84                 $img = graphFromQuery 500,300,\%graph_settings,$query;
85         }
86
87         die 'no image' unless defined $img;
88
89         return $img;
90 };
91
92 1;