]> ruin.nu Git - ndwebbie.git/blob - scripts/scans.pl
New structure for planet, surface and tech scans
[ndwebbie.git] / scripts / scans.pl
1 #!/usr/bin/perl
2 #**************************************************************************
3 #   Copyright (C) 2006,2007 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 no warnings 'exiting';
25 use CGI;
26 use DBI;
27 use DBD::Pg qw(:pg_types);
28 use LWP::Simple;
29 use lib qw{/var/www/ndawn/lib/};
30 use ND::DB;
31
32 our $dbh = ND::DB::DB();
33
34 #$dbh->trace("1","/tmp/scanstest");
35
36 #my $test = $dbh->prepare(q{INSERT INTO scans (tick,scan_id) VALUES(1,3) RETURNING id});
37 #print ;
38 $dbh->do(q{SET CLIENT_ENCODING TO 'LATIN1';});
39
40 my $scangroups = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans
41         WHERE groupscan AND NOT parsed FOR UPDATE
42 });
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,uid) VALUES (?,?,?)});
45 my $parsedscan = $dbh->prepare(q{UPDATE scans SET tick = ?, type = ?, planet = ?, parsed = TRUE WHERE id = ?});
46 my $addpoints = $dbh->prepare(q{UPDATE users SET scan_points = scan_points + ? WHERE uid = ? });
47 my $delscan = $dbh->prepare(q{DELETE FROM scans WHERE id = ?});
48
49 $scangroups->execute or die $dbh->errstr;
50
51 while (my $group = $scangroups->fetchrow_hashref){
52         $dbh->begin_work;
53         my $file = get("http://game.planetarion.com/showscan.pl?scan_grp=$group->{scan_id}");
54
55         my $points = 0;
56         while ($file =~ m/showscan.pl\?scan_id=(\d+)/g){
57                 unless ($dbh->selectrow_array($oldscan,undef,$1)){
58                         $addScan->execute($1,$group->{tick},$group->{uid});
59                         ++$points;
60                 }
61         }
62         $addpoints->execute($points,$group->{uid});
63         $parsedscan->execute($group->{tick},'GROUP',undef,$group->{id});
64         $dbh->commit;
65 }
66
67 my $newscans = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans
68         WHERE NOT groupscan AND NOT parsed FOR UPDATE
69 });
70 my $findplanet = $dbh->prepare(q{SELECT planetid(?,?,?,?)});
71 my $findoldplanet = $dbh->prepare(q{SELECT id FROM planet_stats WHERE x = $1 AND y = $2 AND z = $3 AND tick <= $4 ORDER BY tick DESC LIMIT 1});
72 my $findcoords = $dbh->prepare(q{SELECT * FROM planetcoords(?,?)});
73 my $addfleet = $dbh->prepare(q{INSERT INTO fleets (name,mission,sender,target,tick,eta,back,amount,ingal,uid) VALUES(?,?,?,?,?,?,?,?,?,-1) RETURNING id});
74 my $fleetscan = $dbh->prepare(q{INSERT INTO fleet_scans (id,scan) VALUES(?,?)});
75 my $addships = $dbh->prepare(q{INSERT INTO fleet_ships (id,ship,amount) VALUES(?,?,?)});
76 my $addplanetscan = $dbh->prepare(q{INSERT INTO planet_scans
77         (id,tick,planet,metal_roids,metal,crystal_roids,crystal,eonium_roids,eonium
78                 ,agents,guards,light,medium,heavy,hidden)
79         VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
80 my $addstrucscan = $dbh->prepare(q{INSERT INTO structure_scans
81         (id,tick,planet,light_fac,medium_fac,heavy_fac,amps,distorters
82                 ,metal_ref,crystal_ref,eonium_ref,reslabs,fincents,seccents,total)
83         VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
84
85 my $addtechscan = $dbh->prepare(q{INSERT INTO tech_scans
86         (id,tick,planet,travel,infra,hulls,waves,extraction,covert,mining)
87         VALUES(?,?,?,?,?,?,?,?,?,?)});
88
89
90 $dbh->begin_work or die 'No transaction';
91 $newscans->execute or die $dbh->errstr;
92 $dbh->pg_savepoint('scans') or die "No savepoint";
93
94 while (my $scan = $newscans->fetchrow_hashref){
95         $dbh->pg_release('scans') or die "Couldn't save";
96         $dbh->pg_savepoint('scans') or die "Couldn't save";
97         my $file = get("http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
98         next unless defined $file;
99         if ($file =~ /((?:\w| )*) (?:Scan|Probe) on (\d+):(\d+):(\d+) in tick (\d+)/){
100                 eval {
101                 my $type = $1;
102                 my $x = $2;
103                 my $y = $3;
104                 my $z = $4;
105                 my $tick = $5;
106
107                 if($dbh->selectrow_array(q{SELECT * FROM scans WHERE scan_id = ? AND tick = ? AND id <> ?},undef,$scan->{scan_id},$tick,$scan->{id})){
108                         $dbh->pg_rollback_to('scans') or die "rollback didn't work";
109                         $delscan->execute($scan->{id});
110                         $addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0;
111                         warn "Duplicate scan: $scan->{id} http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}\n";
112                         next;
113                 }
114                 my ($planet) = $dbh->selectrow_array($findplanet,undef,$x,$y,$z,$tick);
115                 unless ($planet){
116                         $dbh->pg_rollback_to('scans') or die "rollback didn't work";
117                         if ( $x == 0 && $y == 0 && $z == 0 ){
118                                 $delscan->execute($scan->{id});
119                                 $addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0;
120                         }
121                         next;
122                 }
123                 my $scantext = "";
124                 if ($file =~ /(Note: [^<]*)/){
125                         #TODO: something about planet being closed?
126                 }
127                 if ($type eq 'Planet'){
128                         my @values = ($scan->{id},$tick,$planet);
129                         $file =~ s/(\d),(\d)/$1$2/g;
130
131                         while($file =~ m/"center">(Metal|Crystal|Eonium)\D+(\d+)\D+([\d,]+)/g){
132                                 push @values,$2,$3;
133                         }
134                         if($file =~ m{Security\ Guards .+? "center">(\d+)</td>
135                                         .+? "center">(\d+)</td>}sx){
136                                 push @values,$1,$2;
137                         }
138                         if($file =~ m{<td class="center">([A-Z][a-z]+)</td><td class="center">([A-Z][a-z]+)</td><td class="center">([A-Z][a-z]+)</td>}){
139                                 push @values,$1,$2,$3;
140                         }
141                         if($file =~ m{<span class="superhighlight">([\d,]+)</span>}){
142                                 push @values,$1;
143                         }
144                         $addplanetscan->execute(@values);
145                 }elsif ($type eq 'Jumpgate'){
146                 #print "$file\n";
147                         while ($file =~ m{(\d+):(\d+):(\d+)\D+(Attack|Defend|Return)</td><td class="left">([^<]*)\D+(\d+)\D+(\d+)}g){
148                                 
149                                 my ($sender) = $dbh->selectrow_array($findplanet,undef,$1,$2,$3,$tick) or die $dbh->errstr;
150                                 ($sender) = $dbh->selectrow_array($findoldplanet,undef,$1,$2,$3,$tick) if ((not defined $sender) && $4 eq 'Return');
151                                 my $id = addfleet($5,$4,undef,$sender,$planet,$tick+$6,$6
152                                         ,undef,$7, $x == $1 && $y == $2);
153                                 $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
154                         }
155                 }elsif ($type eq 'News'){
156                         while( $file =~ m{top">((?:\w| )+)\D+(\d+)</td><td class="left" valign="top">(.+?)</td></tr>}g){
157                                 my $news = $1;
158                                 my $t = $2;
159                                 my $text = $3;
160                                 my ($x,$y,$z) = $dbh->selectrow_array($findcoords,undef,$planet,$t);
161                                 die "No coords for: $planet tick $t" unless defined $x;
162                                 if($news eq 'Launch' && $text =~ m/The (.*?) fleet has been launched, heading for (\d+):(\d+):(\d+), on a mission to (Attack|Defend). Arrival tick: (\d+)/g){
163                                         my $eta = $6 - $t;
164                                         my $mission = $5;
165                                         my $back = $6 + $eta;
166                                         $mission = 'AllyDef' if $eta == 6 && $x != $2;
167                                         my ($target) = $dbh->selectrow_array($findplanet,undef
168                                                 ,$2,$3,$4,$t) or die $dbh->errstr;
169                                         die "No target: $2:$3:$4" unless defined $target;
170                                         my $id = addfleet($1,$mission,undef,$planet,$target,$6
171                                                 ,$eta,$back,undef, ($x == $2 && $y == $3));
172                                         $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
173                                 }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){
174                                         my $eta = $5 - $t;
175                                         my $mission = '';
176                                         my $back = $5 + $eta;
177                                         $mission = 'Defend' if $eta <= 6;
178                                         $mission = 'AllyDef' if $eta == 6 && $x != $2;
179                                         my ($target) = $dbh->selectrow_array($findplanet,undef
180                                                 ,$2,$3,$4,$t) or die $dbh->errstr;
181                                         die "No target: $2:$3:$4" unless defined $target;
182                                         my $id = addfleet($1,$mission,undef,$target,$planet,$5
183                                                 ,$eta,$back,$6, ($x == $2 && $y == $3));
184                                         $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
185                                 }
186                         }
187                 } elsif($type eq 'Surface Analysis'){
188                         my @values = ($scan->{id},$tick,$planet);
189                         my $total = 0;
190                         while($file =~ m{((?:[a-zA-Z]| )+)</t[dh]><td(?: class="right")?>(\d+)}sg){
191                                 push @values,$2;
192                                 $total += $2;
193                         }
194                         push @values,$total;
195                         $addstrucscan->execute(@values);
196                 } elsif($type eq 'Technology Analysis'){
197                         my @values = ($scan->{id},$tick,$planet);
198                         while($file =~ m{((?:[a-zA-Z]| )+)</t[dh]><td(?: class="right")?>(\d+)}sg){
199                                 push @values,$2;
200                         }
201                         $addtechscan->execute(@values);
202                 } elsif($type eq 'Unit' || $type eq 'Advanced Unit'){
203                         my $id = addfleet($type,'Full fleet',$file,$planet,undef,$tick,undef,undef,undef);
204                         $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
205                 } elsif($type eq 'Incoming'){
206                         while($file =~ m{class="left">Fleet: (.*?)</td><td class="left">Mission: (\w+)</td></tr>(.*?)Total Ships: (\d+)}sg){
207                                 my $id = addfleet($1,$2,$3,$planet,undef,$tick,undef,undef,$4);
208                                 $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
209                         }
210                 } else {
211                         print "Something wrong with scan $scan->{id} type $type at tick $tick http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
212                 }
213                 $parsedscan->execute($tick,$type,$planet,$scan->{id}) or die $dbh->errstr;
214                 #$dbh->rollback;
215                 };
216                 if ($@) {
217                         warn $@;
218                         $dbh->pg_rollback_to('scans') or die "rollback didn't work";
219                 }
220         }else{
221                 warn "Nothing useful in scan: $scan->{id} http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}\n";
222                 $delscan->execute($scan->{id});
223                 $addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0;
224         }
225 }
226 #$dbh->rollback;
227 $dbh->commit;
228
229 sub addfleet {
230         my ($name,$mission,$ships,$sender,$target,$tick,$eta,$back,$amount,$ingal) = @_;
231
232         die "no sender" unless defined $sender;
233
234         $ingal = 0 unless $ingal;
235         $back = $tick + 4 if $ingal && $eta <= 4;
236
237         if ($mission eq 'Return'){
238                 ($sender,$target) = ($target,$sender);
239                 $back = $tick + $eta if $eta;
240         }
241
242         my @ships;
243         my $total = 0;
244         while(defined $ships && $ships =~ m{((?:[a-zA-Z]| )+)</td><td(?: class="right")?>(\d+)}sg){
245                 $total += $2;
246                 push @ships, [$1,$2];
247         }
248         $amount = $total if (!defined $amount) && defined $ships;
249         my $id = $dbh->selectrow_array($addfleet,undef,$name,$mission,$sender
250                 ,$target,$tick, $eta, $back, $amount,$ingal) or die $dbh->errstr;
251         for my $s (@ships){
252                 unshift @{$s},$id;
253                 $addships->execute(@{$s}) or die $dbh->errstr;
254         }
255         return $id;
256 };