]> ruin.nu Git - ndwebbie.git/blob - ND/Web/XMLPage.pm
minor changes
[ndwebbie.git] / ND / Web / XMLPage.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::XMLPage;
21 use strict;
22 use warnings;
23 use CGI qw/:standard/;
24 use HTML::Template;
25
26 use ND::Include;
27 use ND::Web::Page;
28 use ND::Web::Include;
29
30 use base qw/ND::Web::Page/;
31
32 sub noAccess () {
33         HTML::Template->new(filename => 'templates/NoAccess.tmpl', global_vars => 1, cache => 1);
34 };
35
36 sub process : method {
37 }
38
39 sub listTargets () : method {
40         my $self = shift;
41         my $DBH = $self->{DBH};
42         my $query = $DBH->prepare(qq{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, released_coords, coords(x,y,z),c.launched,c.wave,c.joinable
43 FROM raid_claims c
44         JOIN raid_targets t ON c.target = t.id
45         JOIN raids r ON t.raid = r.id
46         JOIN current_planet_stats p ON t.planet = p.id
47 WHERE c.uid = ? AND r.tick+c.wave > ? AND r.open AND not r.removed
48 ORDER BY r.tick+c.wave,x,y,z});
49         $query->execute($ND::UID,$self->{TICK});
50         my @targets;
51         while (my $target = $query->fetchrow_hashref){
52                 my $coords = "Target $target->{id}";
53                 $coords = $target->{coords} if $target->{released_coords};
54                 push @targets,{Coords => $coords, Launched => $target->{launched}, Raid => $target->{raid}
55                         , Target => $target->{id}, Tick => $target->{landingtick}, Wave => $target->{wave}
56                         , AJAX => $self->{AJAX}, JoinName => $target->{joinable} ? 'N' : 'J'
57                         , Joinable => $target->{joinable} ? 'FALSE' : 'TRUE'};
58         }
59         my $template = HTML::Template->new(filename => "templates/targetlist.tmpl", cache => 1);
60         $template->param(Targets => \@targets);
61         return $template->output;
62 }
63
64
65 sub render : method {
66         my $self = shift;
67         my $DBH = $self->{DBH};
68
69
70         chdir '/var/www/ndawn/code';
71
72         my $template = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
73
74         my $TICK = $self->{TICK};
75         my $ATTACKER = $self->{ATTACKER};
76
77         $self->{XML} = 0;
78         $self->{AJAX} = 1;
79
80         $self->process;
81
82         my $type = 'text/html';
83         if ($self->{HTTP_ACCEPT} =~ m{application/xhtml\+xml}){
84                 $type = 'application/xhtml+xml'
85         }
86         my $body;
87         if ($self->{XML}){
88                 $type = 'text/xml';
89                 $template = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
90                 $body = HTML::Template->new(filename => "templates/$self->{PAGE}.xml.tmpl", cache => 1);
91         }else{
92                 $body = HTML::Template->new(filename => "templates/$self->{PAGE}.tmpl", global_vars => 1, cache => 1);
93                 $body->param(PAGE => $self->{PAGE});
94         }
95
96         $body = $self->render_body($body);
97
98         unless ($self->{XML}){
99                 my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$self->{UID});
100
101                 $fleetupdate = 0 unless defined $fleetupdate;
102
103                 my ($last_forum_visit) = $DBH->selectrow_array(q{SELECT last_forum_visit FROM users WHERE uid = $1}
104                         ,undef,$self->{UID}) or $ND::ERROR .= p($DBH->errstr);
105                 my ($unread,$newposts) = $DBH->selectrow_array(unread_query(),undef,$self->{UID},$last_forum_visit)
106                         or $ND::ERROR .= p($DBH->errstr);
107                 
108                 $template->param(UnreadPosts => $unread);
109                 $template->param(NewPosts => $newposts);
110                 $template->param(Tick => $TICK);
111                 $template->param(isMember => (($TICK - $fleetupdate < 24) || $self->isScanner()) && $self->{PLANET} && $self->isMember);
112                 $template->param(isHC => $self->isHC);
113                 $template->param(isDC => $self->isDC());
114                 $template->param(isBC => $self->isBC());
115                 $template->param(isIntel => $self->isBC());
116                 $template->param(isAttacker => $ATTACKER && (!$self->isMember() || ((($TICK - $fleetupdate < 24) || $self->isScanner()) && $self->{PLANET})));
117                 if ($ATTACKER && (!$self->isMember() || ((($TICK - $fleetupdate < 24) || $self->isScanner()) && $self->{PLANET}))){
118                         $template->param(Targets => $self->listTargets);
119                 }
120                 $template->param(Coords => param('coords') ? param('coords') : '1:1:1');
121                 my ($css) = $DBH->selectrow_array(q{SELECT css FROM users WHERE uid = $1},undef,$ND::UID);
122                 $template->param(CSS => $css);
123                 $template->param(TITLE => $self->{TITLE});
124         }
125         $template->param(Error => $ND::ERROR);
126         $template->param(BODY => $body->output);
127         my $output = $template->output;
128         print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);
129         print $output;
130 };
131
132 1;