2 #**************************************************************************
3 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 #**************************************************************************/
26 use DBD::Pg qw(:pg_types);
30 for my $file ("/home/whale/db.pl")
32 unless (my $return = do $file){
33 warn "couldn't parse $file: $@" if $@;
34 warn "couldn't do $file: $!" unless defined $return;
35 warn "couldn't run $file" unless $return;
39 $dbh->trace("3","/tmp/scanstest");
40 $dbh->do("SET CLIENT_ENCODING TO 'LATIN1';");
42 my $scangroups = $dbh->prepare(q{SELECT scan_id,tick,scan FROM scans WHERE "type" = 'group' AND scan ~ '^-?[0-9]+$'});
43 my $oldscan = $dbh->prepare(q{SELECT scan_id FROM scans WHERE scan_id = ? AND tick >= tick() - 168});
44 my $addScan = $dbh->prepare(q{INSERT INTO scans (scan_id,tick,"type") VALUES (?,?,COALESCE(?,'-1'))});
45 my $donegroup = $dbh->prepare(q{UPDATE scans SET "type" = 'donegroup' WHERE scan_id = ? AND "type" = 'group' AND tick = ?});
49 while (my @group = $scangroups->fetchrow){
50 my $file = get("http://game.planetarion.com/showscan.pl?scan_grp=$group[0]");
53 while ($file =~ m/showscan.pl\?scan_id=(\d+)/g){
54 unless ($dbh->selectrow_array($oldscan,undef,$1)){
55 $addScan->execute($1,$group[1],$group[2]);
59 my $f = $dbh->prepare('UPDATE users SET scan_points = scan_points + ? WHERE uid = ? ');
60 $f->execute($points,$group[2]);
61 $donegroup->execute($group[0],$group[1]);
64 my $emptyscans = $dbh->prepare('SELECT scan_id,tick,"type"::integer,tick() FROM scans WHERE planet is NULL AND type ~ \'^-?[0-9]+$\'');
65 my $update = $dbh->prepare('UPDATE scans SET tick = ?, "type" = ?, scan = ? , planet = ? WHERE scan_id = ? AND tick = ?');
66 $addScan = $dbh->prepare('INSERT INTO scans (tick,scan_id,"type",scan,planet) VALUES($1,$2,$3,$4,$5)') or die $dbh->errstr;
67 my $findplanet = $dbh->prepare('SELECT planetid(?,?,?,?)');
69 my $oldcoords = $dbh->prepare(q{SELECT x,y,z FROM planet_stats
70 WHERE id = ? AND tick = ?});
71 my $delscan = $dbh->prepare('DELETE FROM scans WHERE scan_id = ? AND tick = ?');
72 unless ($emptyscans->execute){
73 my $cleanup = $dbh->prepare('UPDATE scans SET "type" = \'-1\' WHERE planet is NULL');
77 while (my @scan = $emptyscans->fetchrow){
78 my $file = get("http://game.planetarion.com/showscan.pl?scan_id=$scan[0]");
79 if ($file =~ /((?:\w| )*) (?:Scan|Probe) on (\d+):(\d+):(\d+) in tick (\d+)/){
85 my ($planet) = $dbh->selectrow_array($findplanet,undef,$x,$y,$z,$tick);
87 if ($scan[1] + 48 < $scan[3]){
88 $delscan->execute($scan[0],$scan[1]);
93 if ($file =~ /(Note: [^<]*)/){
95 <table class="closedPlanet">
99 if ($type eq 'Planet'){
100 $file =~ s/(\d),(\d)/$1$2/g;
101 if($file =~ m/Metal\D+(\d+)\D+(\d+).+?Crystal\D+(\d+)\D+(\d+).+?Eonium\D+(\d+)\D+(\d+)/s){
103 <table cellpadding="2">
104 <tr><th></th><th>Metal</th><th>Crystal</th><th>Eonium</th></tr>
105 <tr><td>Asteroids</td><td>$1</td><td>$3</td><td>$5</td></tr>
106 <tr><td>Resources</td><td>$2</td><td>$4</td><td>$6</td></tr>
111 my $f = $dbh->prepare("UPDATE covop_targets SET metal = ?, crystal = ?, eonium = ? WHERE planet = ?");
112 if ($f->execute($2,$4,$6,$planet) < 1){
113 $f = $dbh->prepare("INSERT INTO covop_targets (planet,metal, crystal, eonium) VALUES(?,?,?,?)");
114 $f->execute($planet,$2,$4,$6);
116 }elsif ($type eq 'Jumpgate'){
128 my $f = $dbh->prepare("SELECT add_intel(?,?,?,?,?,?,?,?,?,?,-1)");
130 while ($file =~ m/(\d+):(\d+):(\d+)\D+"left"\>(Attack|Defend|Return)<\/td><td>([^<]*)<\/td><td>(\d+)\D+(\d+)/g){
132 $row = "even" if ($i % 2 == 0);
134 $f->execute($tick,$6,$x,$y,$z,$1,$2,$3,$7,$4);# or $server->command("echo " . DBI->errstr);
137 <tr class="$row"><td><a href="check?coords=$1:$2:$3">$1:$2:$3</a></td><td class="$4">$4</td><td>$5</td><td>$6</td><td>$7</td></tr>};
140 $scantext .= "</table>\n";
141 }elsif ($type eq 'News'){
142 $scantext .= "<table>\n";
145 my $f = $dbh->prepare("SELECT add_intel(?,?,?,?,?,?,?,?,?,?,-1)");
146 while( $file =~ m{top">((?:\w| )+)\D+(\d+)</td><td class="left" valign="top">(.+?)</td></tr>}g){
148 $row = "even" if ($i % 2 == 0);
152 my $text = $cgi->escapeHTML($3);
154 my ($x,$y,$z) = $dbh->selectrow_array($oldcoords,undef,$planet,$t);
156 if($news eq 'Launch' && $text =~ m/(?:[^<]*) fleet has been launched, heading for (\d+):(\d+):(\d+), on a mission to (Attack|Defend). Arrival tick: (\d+)/g){
160 $mission = 'AllyDef' if $eta == 6 && $x != $1;
161 $f->execute($t,$eta,$1,$2,$3,$x,$y,$z,-1,$mission) or print $dbh->errstr;
162 $class = qq{ class="$mission"};
163 }elsif($news eq 'Incoming' && $text =~ m/We have detected an open jumpgate from (?:[^<]*), located at (\d+):(\d+):(\d+). The fleet will approach our system in tick (\d+) and appears to have roughly (\d+) ships/g){
166 $mission = 'Defend' if $eta <= 6;
167 $mission = 'AllyDef' if $eta == 6 && $x != $1;
168 $f->execute($t,$eta,$x,$y,$z,$1,$2,$3,$5,$mission) or print $dbh->errstr;
169 $class = qq{ class="$mission"};
171 $text =~ s{(\d+):(\d+):(\d+)}{<a href="check?coords=$1:$2:$3">$1:$2:$3</a>}g;
172 $scantext .= "<tr class =\"$row\"><td$class>$news</td><td>$t</td><td>$text</td></tr>\n";
174 $scantext .= "</table>\n";
175 } elsif($type eq 'Unit' || $type eq 'Advanced Unit' || $type eq 'Surface Analysis' || $type eq 'Technology Analysis'){
176 $scantext .= "<table>\n";
181 my $f = $dbh->prepare(qq{SELECT "type","class" FROM ship_stats WHERE name = ?});
184 while($file =~ m{((?:[a-zA-Z]| )+)</t[dh]><td(?: class="right")?>(\d+)}sg){
187 $row = "even" if ($i % 2 == 0);
188 $scantext .= "<tr class=\"$row\"><td>$1</td><td>$2</td></tr>\n";
190 $sec = $2 if ($1 eq 'Security Centre');
191 $dist = $2 if ($1 eq 'Wave Distorter');
193 if (my $ship = $f->fetchrow_hashref){
194 $total{$ship->{class}} += $2;
195 $visible{$ship->{class}} += $2 unless $ship->{type} eq 'Cloak';
198 if ($type =~ 'Unit'){
199 my $scan .= q{<table>
200 <tr><th>Class</th><th>Total</th><th>Visible</th></tr>};
202 for my $type (qw/Fighter Corvette Frigate Destroyer Cruiser Battleship/){
203 next unless $total{$type};
206 $row = "even" if ($i % 2 == 0);
207 $visible{$type} = 0 unless $visible{$type};
208 $scan .= "<tr class=\"$row\"><td>$type</td><td>".$total{$type}."</td><td>".$visible{$type}."</td></tr>\n";
210 $scan .= "</table>\n";
211 $addScan->execute($tick-1,$scan[0],'Ship Classes',$scan,$planet);
213 $scantext .= "<tr class=\"odd\"><td>No</td><td>Ships</td></tr>\n" unless $i;
214 $scantext .= "</table>\n";
216 if($type eq 'Surface Analysis'){
217 my $f = $dbh->prepare("UPDATE covop_targets SET structures = ?, sec_centres = ?, dists = ? WHERE planet = ?");
218 if ($f->execute($total,$sec,$dist,$planet) < 1){
219 $f = $dbh->prepare("INSERT INTO covop_targets (planet,structures, sec_centres, dists) VALUES(?,?,?,?)");
220 $f->execute($planet,$total,$sec,$dist);
223 } elsif($type eq 'Military'){
224 $scantext .= "<table>\n";
226 my @totals = (0,0,0,0);
228 my $f = $dbh->prepare(qq{SELECT "type","class" FROM ship_stats WHERE name = ?});
229 while($file =~ m/big left">((?:[a-zA-Z]| )+)<\/t[dh]>.*?center>(\d+).*?center>(\d+).*?center>(\d+).*?center>(\d+)/sg){
230 next if ($2+$3+$4+$5 == 0);
231 my @ships = ($2,$3,$4,$5);
233 my ($type,$class) = $dbh->selectrow_array($f,undef,$1);
235 $row = "even" if ($i % 2 == 0);
236 $scantext .= "<tr class=\"$row\"><td>$1</td><td>$2</td><td>$3</td><td>$4</td><td>$5</td></tr>\n";
238 unless ($type eq "Cloak"){
244 foreach my $i (0,1,2,3){
245 if ($ships[$i] > 0 && $eta[$i] < 9 && ($class =~ /Frigate|Destroyer/)){
247 }elsif ($ships[$i] > 0 && $eta[$i] < 10 && ($class =~ /Cruiser|Battleship/)){
252 $scantext .= "<tr class=\"total\"><td>Total uncloaked</td><td>$totals[0]</td><td>$totals[1]</td><td>$totals[2]</td><td>$totals[3]</td></tr>\n";
253 $scantext .= "<tr><td>Initial eta</td><td>$eta[0]</td><td>$eta[1]</td><td>$eta[2]</td><td>$eta[3]</td></tr>\n";
254 $scantext .= "</table>\n";
256 unless ($scantext || $type eq 'Incoming'){
257 print "Something wrong with scan $scan[0] type $type at tick $tick";
259 $update->execute($tick,$type,$scantext,$planet,$scan[0],$scan[1]) or warn DBI->errstr;
261 my $f = $dbh->prepare('UPDATE users SET scan_points = scan_points - 1 WHERE uid = ? ');
262 $f->execute($scan[2]);
263 $delscan->execute($scan[0],$scan[1]);