From 6fe6edd0f7b5e12d88ec0549252104e5a287888c Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 16 Jan 2007 12:59:31 +0000 Subject: [PATCH] split out some functionality --- ND/Web/Graph.pm | 69 +++++++++++++++++++++++++++++++++++++++++ ND/Web/Pages/Forum.pm | 1 + ND/Web/Pages/Graph.pm | 71 ++++++++++++------------------------------- startup.pl | 1 + 4 files changed, 91 insertions(+), 51 deletions(-) create mode 100644 ND/Web/Graph.pm diff --git a/ND/Web/Graph.pm b/ND/Web/Graph.pm new file mode 100644 index 0000000..b32bde7 --- /dev/null +++ b/ND/Web/Graph.pm @@ -0,0 +1,69 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ + +package ND::Web::Graph; +use strict; +use warnings; +use ND::Include; +use GD::Graph::lines; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/graphFromQuery/; + +sub graphFromQuery { + my ($x,$y,$settings,$query,) = @_; + + my %graph_settings = ( + line_width => 1, + y_number_format => sub { prettyValue abs $_[0]}, + legend_placement => 'BL', + #zero_axis => 1, + box_axis => 0, + boxclr => 'black', + axislabelclr => 'black', + ); + + my $fields = $query->{NUM_OF_FIELDS}; + my @fields; + for (my $i = 0; $i < $fields; $i++){ + push @fields,[]; + } + while (my @result = $query->fetchrow){ + for (my $i = 0; $i < $fields; $i++){ + push @{$fields[$i]},$result[$i]; + } + + } + $graph_settings{x_label_skip} = int(($#{$fields[0]}) / 10); + + my $graph = GD::Graph::lines->new($x,$y); + $graph->set_legend(@{$query->{NAME}}[1..$fields]) or die $graph->error; + + for my $key (keys %{$settings}){ + $graph_settings{$key} = $settings->{$key}; + } + + $graph->set(%graph_settings); + my $gd = $graph->plot(\@fields) or die $graph->error; + return $gd->png; +} + +1; diff --git a/ND/Web/Pages/Forum.pm b/ND/Web/Pages/Forum.pm index d5b9163..a54d23e 100644 --- a/ND/Web/Pages/Forum.pm +++ b/ND/Web/Pages/Forum.pm @@ -202,6 +202,7 @@ sub render { my @categories; while (my $category = $categories->fetchrow_hashref){ $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr); + #TODO: really need to do this outside, so you don't need moderate access if ($category->{id} == $board->{fcid}){ $BODY->param(Category => $category->{category}); } diff --git a/ND/Web/Pages/Graph.pm b/ND/Web/Pages/Graph.pm index fc0ba8e..7b20efe 100644 --- a/ND/Web/Pages/Graph.pm +++ b/ND/Web/Pages/Graph.pm @@ -19,10 +19,10 @@ package ND::Web::Pages::Graph; use strict; -use warnings FATAL => 'all'; +use warnings; use CGI qw/:standard/; use ND::Include; -use GD::Graph::lines; +use ND::Web::Graph; $ND::PAGES{graph} = {parse => \&parse, process => \&process, render=> \&render}; @@ -42,7 +42,7 @@ sub render { my ($x,$y,$z); if ($uri =~ m{^/\w+/(stats|ranks)/(.*)}){ $type = $1; - if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))(?: |:(\d+))?$}){ + if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?$}){ $x = $1; $y = $2; $z = $3; @@ -52,22 +52,10 @@ sub render { die "Not a proper type" unless defined $type; my %graph_settings = ( - line_width => 1, y_number_format => sub { prettyValue abs $_[0]}, - legend_placement => 'BL', - #x_label => 'tick', - y_label => '', - x_label_skip => 50, - x_tick_offset => 13, - zero_axis => 1, - box_axis => 0, - boxclr => 'black', - axislabelclr => 'black', title => $type, y1_label => 'size', y2_label => 'rest', - - ); my $findGraph; @@ -75,49 +63,30 @@ sub render { $findGraph = $DBH->prepare(q{SELECT graph FROM planet_graphs WHERE planet = planetid($1,$2,$3,$4) AND tick = $4 AND type = $5}); $findGraph->execute($x,$y,$z,$ND::TICK,$type) or die $DBH->errstr; } - my $img; if (defined $findGraph and my $graph = $findGraph->fetchrow_hashref){ $img = $graph->{graph}; - }elsif(defined $z){ - my $planets = $DBH->prepare(q{SELECT id, tick,size,coords(x,y,z),score,size,value,xp,scorerank,sizerank,valuerank,xprank from planets natural join planet_stats where id = planetid($1,$2,$3,$4) ORDER BY tick ASC}); - $planets->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr; - - my @score; - my @size; - my @value; - my @xp; - my @ticks; - - while (my $tick = $planets->fetchrow_hashref){ - push @ticks,$tick->{tick}; + }elsif(defined $x){ + my $query; + if (defined $z){ if ($type eq 'stats'){ - push @score,$tick->{score}; - push @size,$tick->{size}; - push @value,$tick->{value}; - push @xp,$tick->{xp}*60; + $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}); }elsif($type eq 'ranks'){ - push @score,-$tick->{scorerank}; - push @size,-$tick->{sizerank}; - push @value,-$tick->{valuerank}; - push @xp,-$tick->{xprank}; + $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}); } - - } - my $graph = GD::Graph::lines->new(500,300); - if ($type eq 'stats'){ - $graph->set_legend(qw{score size value xp*60}) or die $graph->error; - $graph_settings{two_axes} = 1; - $graph_settings{use_axis} = [2,1,2,2]; - }elsif($type eq 'ranks'){ - $graph->set_legend(qw{score size value xp}) or die $graph->error; - $graph_settings{y_max_value} = 0; - $graph_settings{two_axes} = 1; - $graph_settings{use_axis} = [2,1,2,2]; + $query->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr; + }else{ + if ($type eq 'stats'){ + $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}); + }elsif($type eq 'ranks'){ + $query = $DBH->prepare(q{SELECT tick,scorerank,sizerank,valuerank,xprank FROM galaxies WHERE x = $1 AND y = $2 ORDER BY tick ASC}); + } + $query->execute($x,$y) or die $DBH->errstr; } - $graph->set(%graph_settings); - my $gd = $graph->plot([\@ticks,\@score,\@size,\@value,\@xp]) or die $graph->error; - $img = $gd->png; + + $graph_settings{two_axes} = 1; + $graph_settings{use_axis} = [2,1,2,2]; + $img = graphFromQuery 500,300,\%graph_settings,$query; } die 'no image' unless defined $img; diff --git a/startup.pl b/startup.pl index 7741e31..88a406a 100644 --- a/startup.pl +++ b/startup.pl @@ -19,6 +19,7 @@ use ND::Include; use ND::Web::AuthHandler; use ND::Web::Include; use ND::Web::Forum; +use ND::Web::Graph; use ND::Web::Pages::Main; use ND::Web::Pages::AddIntel; -- 2.39.2