]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Graph.pm
split out some functionality
[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 $ND::PAGES{graph} = {parse => \&parse, process => \&process, render=> \&render};
28
29 sub parse {
30         my ($uri) = @_;
31         $ND::USETEMPLATE = 0;
32 }
33
34 sub process {
35
36 }
37
38 sub render {
39         my ($DBH,$uri) = @_;
40
41         my $type;
42         my ($x,$y,$z);
43         if ($uri =~ m{^/\w+/(stats|ranks)/(.*)}){
44                 $type = $1;
45                 if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?$}){
46                         $x = $1;
47                         $y = $2;
48                         $z = $3;
49                 }
50         }
51
52         die "Not a proper type" unless defined $type;
53
54         my %graph_settings = (
55                 y_number_format => sub { prettyValue abs $_[0]},
56                 title => $type,
57                 y1_label => 'size',
58                 y2_label => 'rest',
59         );
60
61         my $findGraph;
62         if (defined $z){
63                 $findGraph = $DBH->prepare(q{SELECT graph FROM planet_graphs WHERE planet = planetid($1,$2,$3,$4) AND tick = $4 AND type = $5});
64                 $findGraph->execute($x,$y,$z,$ND::TICK,$type) or die $DBH->errstr;
65         }
66         my $img;
67         if (defined $findGraph and my $graph = $findGraph->fetchrow_hashref){
68                 $img = $graph->{graph};
69         }elsif(defined $x){
70                 my $query;
71                 if (defined $z){
72                         if ($type eq 'stats'){
73                                 $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});
74                         }elsif($type eq 'ranks'){
75                                 $query = $DBH->prepare(q{SELECT tick,scorerank,sizerank,valuerank,xprank FROM planets natural join planet_stats WHERE id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
76                         }
77                         $query->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr;
78                 }else{
79                         if ($type eq 'stats'){
80                                 $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});
81                         }elsif($type eq 'ranks'){
82                                 $query = $DBH->prepare(q{SELECT tick,scorerank,sizerank,valuerank,xprank FROM galaxies WHERE x = $1 AND y = $2  ORDER BY tick ASC});
83                         }
84                         $query->execute($x,$y) or die $DBH->errstr;
85                 }
86                 
87                 $graph_settings{two_axes} = 1;
88                 $graph_settings{use_axis} = [2,1,2,2];
89                 $img = graphFromQuery 500,300,\%graph_settings,$query;
90         }
91
92         die 'no image' unless defined $img;
93
94         print header(-type=> 'image/png', -Content_Length => length $img);
95         binmode STDOUT;
96         print $img;
97 }
98
99 1;