]> ruin.nu Git - ndwebbie.git/blob - scripts/scans.pl
fixed group scans, also change quote style to enable sql hilight
[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 use CGI;
25 use DBI;
26 use DBD::Pg qw(:pg_types);
27 use LWP::Simple;
28 use lib qw{/var/www/ndawn/};
29 use ND::DB;
30
31 our $dbh = ND::DB::DB();
32
33 #$dbh->trace("1","/tmp/scanstest");
34
35 #my $test = $dbh->prepare(q{INSERT INTO scans (tick,scan_id) VALUES(1,3) RETURNING id});
36 #print ;
37 $dbh->do(q{SET CLIENT_ENCODING TO 'LATIN1';});
38
39 my $scangroups = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans WHERE groupscan AND NOT parsed});
40 my $oldscan = $dbh->prepare(q{SELECT scan_id FROM scans WHERE scan_id = ? AND tick >= tick() - 168});
41 my $addScan = $dbh->prepare(q{INSERT INTO scans (scan_id,tick,uid) VALUES (?,?,COALESCE(?,'-1'))});
42 my $parsedscan = $dbh->prepare(q{UPDATE scans SET tick = ?, type = ?, planet = ?, parsed = TRUE WHERE id = ?});
43 my $addpoints = $dbh->prepare(q{UPDATE users SET scan_points = scan_points + ? WHERE uid = ? });
44 my $delscan = $dbh->prepare(q{DELETE FROM scans WHERE id = ?});
45
46 $scangroups->execute or die $dbh->errstr;
47
48 while (my $group = $scangroups->fetchrow_hashref){
49         $dbh->begin_work;
50         my $file = get("http://game.planetarion.com/showscan.pl?scan_grp=$group->{scan_id}");
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->{tick},$group->{uid});
56                         ++$points;
57                 }
58         }
59         $addpoints->execute($points,$group->{uid});
60         $parsedscan->execute($group->{tick},'GROUP',undef,$group->{id});
61         $dbh->commit;
62 }
63
64 my $newscans = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans WHERE NOT groupscan AND NOT parsed});
65 my $findplanet = $dbh->prepare(q{SELECT planetid(?,?,?,?)});
66 my $findcoords = $dbh->prepare(q{SELECT * FROM planetcoords(?,?)});
67 my $addfleet = $dbh->prepare(q{INSERT INTO fleets (name,mission,sender,target,tick,eta,back,amount,ingal,uid) VALUES(?,?,?,?,?,?,?,?,?,-1) RETURNING id});
68 my $fleetscan = $dbh->prepare(q{INSERT INTO fleet_scans (id,scan) VALUES(?,?)});
69 my $addships = $dbh->prepare(q{INSERT INTO fleet_ships (id,ship,amount) VALUES(?,?,?)});
70 my $addpdata = $dbh->prepare(q{INSERT INTO planet_data (planet,tick,rid,amount,uid) VALUES(?,?,(SELECT id FROM planet_data_types WHERE category = ? AND name = ?), ?,-1) RETURNING id});
71 my $pdatascan = $dbh->prepare(q{INSERT INTO data_scans (id,scan) VALUES(?,?)});
72
73 $newscans->execute or die $dbh->errstr;
74 while (my $scan = $newscans->fetchrow_hashref){
75         my $file = get("http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
76         next unless defined $file;
77         if ($file =~ /((?:\w| )*) (?:Scan|Probe) on (\d+):(\d+):(\d+) in tick (\d+)/){
78                 $dbh->begin_work;
79                 eval {
80                 my $type = $1;
81                 my $x = $2;
82                 my $y = $3;
83                 my $z = $4;
84                 my $tick = $5;
85
86                 if($dbh->selectrow_array(q{SELECT * FROM scans WHERE scan_id = ? AND tick = ? AND id <> ?},undef,$scan->{scan_id},$tick,$scan->{id})){
87                         $dbh->rollback;
88                         $delscan->execute($scan->{id});
89                         $addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0;
90                         die "Duplicate scan: $scan->{id} http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}\n";
91                 }
92                 my ($planet) = $dbh->selectrow_array($findplanet,undef,$x,$y,$z,$tick);
93                 unless ($planet){
94                         $dbh->rollback;
95                         next;
96                 }
97                 my $scantext = "";
98                 if ($file =~ /(Note: [^<]*)/){
99                         #TODO: something about planet being closed?
100                 }
101                 if ($type eq 'Planet'){
102                         $file =~ s/(\d),(\d)/$1$2/g;
103                         while($file =~ m/(Metal|Crystal|Eonium)\D+(\d+)\D+(\d+)/g){
104                                 my $id = $dbh->selectrow_array($addpdata,undef,$planet,$tick
105                                         ,'roid',$1, $2) or die $dbh->errstr;
106                                 $pdatascan->execute($id,$scan->{id});
107                                 $id = $dbh->selectrow_array($addpdata,undef,$planet,$tick
108                                         ,'resource',$1, $3) or die $dbh->errstr;
109                                 $pdatascan->execute($id,$scan->{id});
110                         }
111                 }elsif ($type eq 'Jumpgate'){
112                         while ($file =~ m/(\d+):(\d+):(\d+)\D+"left"\>(Attack|Defend|Return)<\/td><td>([^<]*)<\/td><td>(\d+)\D+(\d+)/g){
113                                 
114                                 my ($sender) = $dbh->selectrow_array($findplanet,undef,$1,$2,$3,$tick) or die $dbh->errstr;
115                                 my $id = addfleet($5,$4,"",$sender,$planet,$tick+$6,$6
116                                         ,undef,$7, $x == $1 && $y == $2);
117                                 $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
118                         }
119                 }elsif ($type eq 'News'){
120                         while( $file =~ m{top">((?:\w| )+)\D+(\d+)</td><td class="left" valign="top">(.+?)</td></tr>}g){
121                                 my $news = $1;
122                                 my $t = $2;
123                                 my $text = $3;
124                                 my ($x,$y,$z) = $dbh->selectrow_array($findcoords,undef,$planet,$t);
125                                 die "No coords for: $planet tick $t" unless defined $x;
126                                 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){
127                                         my $eta = $6 - $t;
128                                         my $mission = $5;
129                                         my $back = $6 + $eta;
130                                         $mission = 'AllyDef' if $eta == 6 && $x != $2;
131                                         my ($target) = $dbh->selectrow_array($findplanet,undef
132                                                 ,$2,$3,$4,$t) or die $dbh->errstr;
133                                         die "No target: $2:$3:$4" unless defined $target;
134                                         my $id = addfleet($1,$mission,"",$planet,$target,$6
135                                                 ,$eta,$back,undef, ($x == $2 && $y == $3));
136                                         $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
137                                 }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){
138                                         my $eta = $5 - $t;
139                                         my $mission = '';
140                                         my $back = $5 + $eta;
141                                         $mission = 'Defend' if $eta <= 6;
142                                         $mission = 'AllyDef' if $eta == 6 && $x != $2;
143                                         my ($target) = $dbh->selectrow_array($findplanet,undef
144                                                 ,$2,$3,$4,$t) or die $dbh->errstr;
145                                         die "No target: $2:$3:$4" unless defined $target;
146                                         my $id = addfleet($1,$mission,"",$planet,$target,$5
147                                                 ,$eta,$back,$6, ($x == $2 && $y == $3));
148                                         $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
149                                 }
150                         }
151                 } elsif($type eq 'Surface Analysis' || $type eq 'Technology Analysis'){
152                         my $cat = ($type eq 'Surface Analysis' ? 'struc' : 'tech');
153                         while($file =~ m{((?:[a-zA-Z]| )+)</t[dh]><td(?: class="right")?>(\d+)}sg){
154                                 my $id = $dbh->selectrow_array($addpdata,undef,$planet,$tick
155                                         ,$cat,$1, $2) or die $dbh->errstr;
156                                 $pdatascan->execute($id,$scan->{id});
157                         }
158
159                 } elsif($type eq 'Unit' || $type eq 'Advanced Unit'){
160                         my $id = addfleet($type,'Full fleet',$file,$planet,undef,$tick,undef,undef,undef);
161                         $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
162                 } elsif($type eq 'Incoming'){
163                         while($file =~ m{class="left">Fleet: (.*?)</td><td class="left">Mission: (\w+)</td></tr>(.*?)Total Ships: (\d+)}sg){
164                                 my $id = addfleet($1,$2,$3,$planet,undef,$tick,undef,undef,$4);
165                                 $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
166                         }
167                 } else {
168                         print "Something wrong with scan $scan->{id} type $type at tick $tick http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
169                 }
170                 $parsedscan->execute($tick,$type,$planet,$scan->{id}) or die $dbh->errstr;
171                 $dbh->commit;
172                 #$dbh->rollback;
173                 };
174                 if ($@) {
175                         warn $@;
176                         $dbh->rollback;
177                 }
178         }else{
179                 warn "Nothing useful in scan: $scan->{id} http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}\n";
180                 $delscan->execute($scan->{id});
181                 $addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0;
182         }
183 }
184
185 sub addfleet {
186         my ($name,$mission,$ships,$sender,$target,$tick,$eta,$back,$amount,$ingal) = @_;
187
188         $ingal = 0 unless $ingal;
189         $back = $tick + 4 if $ingal && $eta <= 4;
190
191         if ($mission eq 'Return'){
192                 ($sender,$target) = ($target,$sender);
193                 $back = $tick + $eta if $eta;
194         }
195
196         my @ships;
197         my $total = undef;
198         while($ships =~ m{((?:[a-zA-Z]| )+)</td><td>(\d+)}sg){
199                 $total = 0 unless defined $total;
200                 $total += $2;
201                 push @ships, [$1,$2];
202         }
203         $amount = $total unless defined $amount;
204         my $id = $dbh->selectrow_array($addfleet,undef,$name,$mission,$sender
205                 ,$target,$tick, $eta, $back, $amount,$ingal) or die $dbh->errstr;
206         for my $s (@ships){
207                 unshift @{$s},$id;
208                 $addships->execute(@{$s}) or die $dbh->errstr;
209         }
210         return $id;
211 };