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