]> ruin.nu Git - ndwebbie.git/blob - index.pl
intel link on check page, and red background for untaken calls
[ndwebbie.git] / index.pl
1 #!/usr/bin/perl -w -T
2 #**************************************************************************
3 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
4 #                                                                         *
5 #   This program is free software; you can redistribute it and/or modify  *
6 #   it under the terms of the GNU General Public License as published by  *
7 #   the Free Software Foundation; either version 2 of the License, or     *
8 #   (at your option) any later version.                                   *
9 #                                                                         *
10 #   This program is distributed in the hope that it will be useful,       *
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 #   GNU General Public License for more details.                          *
14 #                                                                         *
15 #   You should have received a copy of the GNU General Public License     *
16 #   along with this program; if not, write to the                         *
17 #   Free Software Foundation, Inc.,                                       *
18 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19 #**************************************************************************/
20
21 package ND;
22 use CGI qw/:standard/;
23 use HTML::Template;
24 use DBI;
25 use DBD::Pg qw(:pg_types);
26 #use Apache2::Request;
27 use strict;
28
29 local $ND::DBH;
30 local $ND::USER;
31 local $ND::UID;
32 local $ND::PLANET;
33 local $ND::TEMPLATE;
34 local $ND::BODY;
35 local $ND::TICK;
36
37 our $DBH = undef;
38 our $USER = $ENV{'REMOTE_USER'};
39 my $error;# = $ND::r->param('page');
40
41 if ($ENV{'DOCUMENT_ROOT'} =~ m{((\w|/)+)}){
42         chdir $1;
43 }
44
45 our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1);
46
47 for my $file ("db.pl","include.pl"){
48         unless (my $return = do $file){
49                 print "couldn't parse $file: $@" if $@;
50                 print "couldn't do $file: $!"    unless defined $return;
51                 print "couldn't run $file"       unless $return;
52         }
53 }
54
55 our ($UID,$PLANET) = $DBH->selectrow_array('SELECT uid,planet FROM users WHERE username = ?'
56         ,undef,$ENV{'REMOTE_USER'});
57
58 our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef);
59
60
61 my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?');
62 $query->execute($UID);
63
64 our $ATTACKER = 0;
65 undef our %GROUPS;
66 while (my ($name,$attack,$gid) = $query->fetchrow()){
67         $GROUPS{$name} = $gid;
68         $ATTACKER = 1 if $attack;
69 }
70
71
72 our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)');
73
74 my $page = 'main';
75 if (param('page') =~ /^(main|check|motd|points|covop|top100|launchConfirmation|addintel|defrequest|raids|editRaid|calls|intel|users|alliances|memberIntel|resources|planetNaps)$/){
76         $page = $1;
77 }
78
79 our $XML = 0;
80 $XML = 1 if param('xml') and $page =~ /^(raids)$/;
81
82 our $AJAX = 1;
83
84 my $type = 'text/html';
85 if ($XML){
86         $type = 'text/xml';
87         $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1);
88         $ND::BODY = HTML::Template->new(filename => "templates/${page}.xml.tmpl", cache => 1);
89 }else{
90         $ND::BODY = HTML::Template->new(filename => "templates/${page}.tmpl", global_vars => 1, cache => 1);
91         $ND::BODY->param(PAGE => $page);
92 }
93
94
95 unless (my $return = do "${page}.pl"){
96         $error .= "<p><b>couldn't parse $page: $@</b></p>" if $@;
97         $error .= "<p><b>couldn't do $page: $!</b></p>"    unless defined $return;
98         $error .= "<p><b>couldn't run $page</b></p>"       unless $return;
99 }
100
101 unless ($XML){
102         my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID);
103
104
105         $TEMPLATE->param(Tick => $TICK);
106         $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember());
107         $TEMPLATE->param(isHC => isHC());
108         $TEMPLATE->param(isDC => isDC());
109         $TEMPLATE->param(isBC => isBC());
110         $TEMPLATE->param(isIntel => isBC());
111         $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET)));
112         if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){
113                 $ND::TEMPLATE->param(Targets => listTargets());
114         }
115         $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1');
116         $TEMPLATE->param(Error => $error);
117
118 }
119 $ND::TEMPLATE->param(BODY => $ND::BODY->output);
120 my $output = $TEMPLATE->output;
121 print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output);
122 print $output;
123
124
125 $DBH->disconnect;
126 $DBH = undef;
127 $UID = undef;
128 $USER = undef;
129 $PLANET = undef;
130 $TEMPLATE = undef;
131 $TICK = undef;
132 undef %GROUPS;
133 $ND::BODY = undef;
134
135 1;