]> ruin.nu Git - ndwebbie.git/blob - scripts/scans.pl
proper resources in cov op table
[ndwebbie.git] / scripts / scans.pl
1 #!/usr/bin/perl
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
22 use strict;
23 use warnings;
24 use CGI;
25 use DBI;
26 use DBD::Pg qw(:pg_types);
27 use LWP::Simple;
28
29 our $dbh;
30 for my $file ("/home/whale/db.pl")
31 {
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;
36         }
37 }
38
39 $dbh->trace("3","/tmp/scanstest");
40 $dbh->do("SET CLIENT_ENCODING TO 'LATIN1';");
41
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 = ?});
46
47 $scangroups->execute;
48
49 while (my @group = $scangroups->fetchrow){
50         my $file = get("http://game.planetarion.com/showscan.pl?scan_grp=$group[0]");
51
52         my $points = 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]);
56                         ++$points;
57                 }
58         }
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]);
62 }
63
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(?,?,?,?)');
68 my $delscan = $dbh->prepare('DELETE FROM scans WHERE scan_id = ? AND tick = ?');
69 unless ($emptyscans->execute){
70         my $cleanup = $dbh->prepare('UPDATE scans SET "type" = \'-1\' WHERE planet is NULL');
71         $cleanup->execute;
72         $emptyscans->execute;
73 }
74 while (my @scan = $emptyscans->fetchrow){
75         my $file = get("http://game.planetarion.com/showscan.pl?scan_id=$scan[0]");
76         if ($file =~ /((?:\w| )*) (?:Scan|Probe) on (\d+):(\d+):(\d+) in tick (\d+)/){
77                 my $type = $1;
78                 my $x = $2;
79                 my $y = $3;
80                 my $z = $4;
81                 my $tick = $5;
82                 my ($planet) = $dbh->selectrow_array($findplanet,undef,$x,$y,$z,$tick);
83                 unless ($planet){
84                         if ($scan[1] + 48 < $scan[3]){
85                                 $delscan->execute($scan[0],$scan[1]);
86                         }
87                         next;
88                 }
89                 my $scantext = "";
90                 if ($file =~ /(Note: [^<]*)/){
91                         $scantext .= qq{
92                         <table class="closedPlanet">
93                         <tr><td>$1</td></tr>
94                         </table>};
95                 }
96                 if ($type eq 'Planet'){
97                         $file =~ s/(\d),(\d)/$1$2/g;
98                         if($file =~ m/Metal\D+(\d+)\D+(\d+).+?Crystal\D+(\d+)\D+(\d+).+?Eonium\D+(\d+)\D+(\d+)/s){
99                                 $scantext .= <<HTML
100         <table cellpadding="2">
101         <tr><th></th><th>Metal</th><th>Crystal</th><th>Eonium</th></tr>
102         <tr><td>Asteroids</td><td>$1</td><td>$3</td><td>$5</td></tr>
103         <tr><td>Resources</td><td>$2</td><td>$4</td><td>$6</td></tr>
104         </table>
105 HTML
106                                 }
107                         ;
108                         my $f = $dbh->prepare("UPDATE covop_targets SET metal = ?, crystal = ?, eonium = ? WHERE planet = ?");
109                         if ($f->execute($2,$4,$6,$planet) < 1){
110                                 $f = $dbh->prepare("INSERT INTO covop_targets (planet,metal, crystal, eonium) VALUES(?,?,?,?)");
111                                 $f->execute($planet,$2,$4,$6);
112                         }
113                 }elsif ($type eq 'Jumpgate'){
114                         $scantext .= <<HTML
115         <table>
116         <tr>
117         <th>Coords</th>
118         <th>Mission</th>
119         <th>Fleet</th>
120         <th>Eta</th>
121         <th>Amount</th>
122         </tr>
123 HTML
124                         ;
125                         my $f = $dbh->prepare("SELECT add_intel(?,?,?,?,?,?,?,?,?,?,-1)");
126                         my $i = 1;
127                         while ($file =~ m/(\d+):(\d+):(\d+)\D+"left"\>(Attack|Defend|Return)<\/td><td>([^<]*)<\/td><td>(\d+)\D+(\d+)/g){
128                                 my $row = "odd";
129                                 $row = "even" if ($i % 2 == 0);
130                                 if ($4 ne 'Return'){
131                                         $f->execute($tick,$6,$x,$y,$z,$1,$2,$3,$7,$4);# or $server->command("echo " . DBI->errstr);
132                                 }
133                                 $scantext .= qq{
134         <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>};
135                                 $i++;
136                         }
137                         $scantext .= "</table>\n";
138                 }elsif ($type eq 'News'){
139                         $scantext .= "<table>\n";
140                         my $i = 1;
141                         my $cgi = new CGI;
142                         my $f = $dbh->prepare("SELECT add_intel(?,?,?,?,?,?,?,?,?,?,-1)");
143                         while( $file =~ m{top">((?:\w| )+)\D+(\d+)</td><td class="left" valign="top">(.+?)</td></tr>}g){
144                                 my $row = "odd";
145                                 $row = "even" if ($i % 2 == 0);
146                                 $i++;
147                                 my $news = $1;
148                                 my $t = $2;
149                                 my $text = $cgi->escapeHTML($3);
150                                 my $class = '';
151
152                                 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){
153
154                                         my $eta = $5 - $t;
155                                         my $mission = $4;
156                                         $mission = 'AllyDef' if $eta == 6 && $x != $1;
157                                         $f->execute($t,$eta,$1,$2,$3,$x,$y,$z,-1,$mission) or print $dbh->errstr;
158                                         $class = qq{ class="$mission"};
159                                 }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){
160                                         my $eta = $4 - $t;
161                                         my $mission = '';
162                                         $mission = 'Defend' if $eta <= 6;
163                                         $mission = 'AllyDef' if $eta == 6 && $x != $1;
164                                         $f->execute($t,$eta,$x,$y,$z,$1,$2,$3,$5,$mission) or print $dbh->errstr;
165                                         $class = qq{ class="$mission"};
166                                 }
167                                 $text =~ s{(\d+):(\d+):(\d+)}{<a href="check?coords=$1:$2:$3">$1:$2:$3</a>}g;
168                                 $scantext .= "<tr class =\"$row\"><td$class>$news</td><td>$t</td><td>$text</td></tr>\n";
169                         }
170                         $scantext .= "</table>\n";
171                 } elsif($type eq 'Unit' || $type eq 'Advanced Unit' || $type eq 'Surface Analysis' || $type eq 'Technology Analysis'){
172                         $scantext .= "<table>\n";
173                         my $i = 0;
174                         my $total = 0;
175                         my $sec = 0;
176                         my $dist = 0;
177                         my $f = $dbh->prepare(qq{SELECT "type","class" FROM ship_stats WHERE name = ?});
178                         my %visible;
179                         my %total;
180                         while($file =~ m{((?:[a-zA-Z]| )+)</t[dh]><td(?: class="right")?>(\d+)}sg){
181                                 $i++;
182                                 my $row = "odd";
183                                 $row = "even" if ($i % 2 == 0);
184                                 $scantext .= "<tr class=\"$row\"><td>$1</td><td>$2</td></tr>\n";
185                                 $total += $2;
186                                 $sec = $2 if ($1 eq 'Security Centre');
187                                 $dist = $2 if ($1 eq 'Wave Distorter');
188                                 $f->execute($1);
189                                 if (my $ship = $f->fetchrow_hashref){
190                                         $total{$ship->{class}} += $2;
191                                         $visible{$ship->{class}} += $2 unless $ship->{type} eq 'Cloak';
192                                 }
193                         }
194                         if ($type =~ 'Unit'){
195                                 my $scan .= q{<table>
196                                         <tr><th>Class</th><th>Total</th><th>Visible</th></tr>};
197                                 my $i = 0;
198                                 for my $type (qw/Fighter Corvette Frigate Destroyer Cruiser Battleship/){
199                                         next unless $total{$type};
200                                         $i++;
201                                         my $row = "odd";
202                                         $row = "even" if ($i % 2 == 0);
203                                         $visible{$type} = 0 unless $visible{$type};
204                                         $scan .= "<tr class=\"$row\"><td>$type</td><td>".$total{$type}."</td><td>".$visible{$type}."</td></tr>\n";
205                                 }
206                                 $scan .= "</table>\n";
207                                 $addScan->execute($tick-1,$scan[0],'Ship Classes',$scan,$planet);
208                         }
209                         $scantext .= "<tr class=\"odd\"><td>No</td><td>Ships</td></tr>\n" unless $i;
210                         $scantext .= "</table>\n";
211
212                         if($type eq 'Surface Analysis'){
213                                 my $f = $dbh->prepare("UPDATE covop_targets SET structures = ?, sec_centres = ?, dists = ? WHERE planet = ?");
214                                 if ($f->execute($total,$sec,$dist,$planet) < 1){
215                                         $f = $dbh->prepare("INSERT INTO covop_targets (planet,structures, sec_centres, dists) VALUES(?,?,?,?)");
216                                         $f->execute($planet,$total,$sec,$dist);
217                                 }
218                         }
219                 } elsif($type eq 'Military'){
220                         $scantext .= "<table>\n";
221                         my $i = 1;
222                         my @totals = (0,0,0,0);
223                         my @eta = (8,8,8,8);
224                         my $f = $dbh->prepare(qq{SELECT "type","class" FROM ship_stats WHERE name = ?});
225                         while($file =~ m/big left">((?:[a-zA-Z]| )+)<\/t[dh]>.*?center>(\d+).*?center>(\d+).*?center>(\d+).*?center>(\d+)/sg){
226                                 next if ($2+$3+$4+$5 == 0);
227                                 my @ships = ($2,$3,$4,$5);
228                                 my $row = "odd";
229                                 my ($type,$class) = $dbh->selectrow_array($f,undef,$1);
230                                 #print "$1 $type\n";
231                                 $row = "even" if ($i % 2 == 0);
232                                 $scantext .= "<tr class=\"$row\"><td>$1</td><td>$2</td><td>$3</td><td>$4</td><td>$5</td></tr>\n";
233                                 $i++;
234                                 unless ($type eq "Cloak"){
235                                         $totals[0] += $2;
236                                         $totals[1] += $3;
237                                         $totals[2] += $4;
238                                         $totals[3] += $5;
239                                 }
240                                 foreach my $i (0,1,2,3){
241                                         if ($ships[$i] > 0 && $eta[$i] < 9 && ($class =~ /Frigate|Destroyer/)){
242                                                 $eta[$i] = 9;
243                                         }elsif ($ships[$i] > 0 && $eta[$i] < 10 && ($class =~ /Cruiser|Battleship/)){
244                                                 $eta[$i] = 10;
245                                         }
246                                 }
247                         }
248                         $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";
249                         $scantext .= "<tr><td>Initial eta</td><td>$eta[0]</td><td>$eta[1]</td><td>$eta[2]</td><td>$eta[3]</td></tr>\n";
250                         $scantext .= "</table>\n";
251                 }
252                 unless ($scantext || $type eq 'Incoming'){
253                         print "Something wrong with scan $scan[0] type $type at tick $tick";
254                 }
255                 $update->execute($tick,$type,$scantext,$planet,$scan[0],$scan[1]) or warn DBI->errstr;
256         }else{
257                 my $f = $dbh->prepare('UPDATE users SET scan_points = scan_points - 1 WHERE uid = ? ');
258                 $f->execute($scan[2]);
259                 $delscan->execute($scan[0],$scan[1]);
260         }
261 }
262 $dbh->disconnect;