use DBI;
use DBD::Pg qw(:pg_types);
use LWP::Simple;
-use lib qw{/var/www/ndawn/lib/};
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
use ND::DB;
our $dbh = ND::DB::DB();
my $addfleet = $dbh->prepare(q{INSERT INTO fleets (name,mission,sender,target,tick,eta,back,amount,ingal,uid) VALUES(?,?,?,?,?,?,?,?,?,-1) RETURNING id});
my $fleetscan = $dbh->prepare(q{INSERT INTO fleet_scans (id,scan) VALUES(?,?)});
my $addships = $dbh->prepare(q{INSERT INTO fleet_ships (id,ship,amount) VALUES(?,?,?)});
-my $addpdata = $dbh->prepare(q{INSERT INTO planet_data (id,tick,scan,rid,amount) VALUES(?,?,?,(SELECT id FROM planet_data_types WHERE category = ? AND name = ?), ?)});
+my $addplanetscan = $dbh->prepare(q{INSERT INTO planet_scans
+ (id,tick,planet,metal_roids,metal,crystal_roids,crystal,eonium_roids,eonium
+ ,agents,guards,light,medium,heavy,hidden)
+ VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
+my $adddevscan = $dbh->prepare(q{INSERT INTO development_scans
+ (id,tick,planet,light_fac,medium_fac,heavy_fac,amps,distorters
+ ,metal_ref,crystal_ref,eonium_ref,reslabs,fincents,seccents
+ ,travel,infra,hulls,waves,extraction,covert,mining,total)
+ VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+ });
$dbh->begin_work or die 'No transaction';
$newscans->execute or die $dbh->errstr;
$dbh->pg_savepoint('scans') or die "No savepoint";
-my %production = (None => 0, Low => 35, Medium => 65, High => 100);
while (my $scan = $newscans->fetchrow_hashref){
+ $dbh->pg_release('scans') or die "Couldn't save";
+ $dbh->pg_savepoint('scans') or die "Couldn't save";
my $file = get("http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
next unless defined $file;
if ($file =~ /((?:\w| )*) (?:Scan|Probe) on (\d+):(\d+):(\d+) in tick (\d+)/){
$dbh->pg_rollback_to('scans') or die "rollback didn't work";
$delscan->execute($scan->{id});
$addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0;
- die "Duplicate scan: $scan->{id} http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}\n";
+ warn "Duplicate scan: $scan->{id} http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}\n";
+ next;
}
my ($planet) = $dbh->selectrow_array($findplanet,undef,$x,$y,$z,$tick);
unless ($planet){
$dbh->pg_rollback_to('scans') or die "rollback didn't work";
+ if ( $x == 0 && $y == 0 && $z == 0 ){
+ $delscan->execute($scan->{id});
+ $addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0;
+ }
next;
}
my $scantext = "";
#TODO: something about planet being closed?
}
if ($type eq 'Planet'){
+ my @values = ($scan->{id},$tick,$planet);
$file =~ s/(\d),(\d)/$1$2/g;
+
while($file =~ m/"center">(Metal|Crystal|Eonium)\D+(\d+)\D+([\d,]+)/g){
- my ($roids,$res) = ($2,$3);
- $roids =~ s/,//g;
- $addpdata->execute($planet,$tick,$scan->{id}
- ,'roid',$1, $roids) or die $dbh->errstr;
- $res =~ s/,//g;
- $addpdata->execute($planet,$tick,$scan->{id}
- ,'resource',$1, $res) or die $dbh->errstr;
+ push @values,$2,$3;
+ }
+ if($file =~ m{Security\ Guards .+? "center">(\d+)</td>
+ .+? "center">(\d+)</td>}sx){
+ push @values,$1,$2;
}
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>}){
- $addpdata->execute($planet,$tick,$scan->{id}
- ,'planet','Light Usage', $production{$1}) or die $dbh->errstr;
- $addpdata->execute($planet,$tick,$scan->{id}
- ,'planet','Medium Usage', $production{$2}) or die $dbh->errstr;
- $addpdata->execute($planet,$tick,$scan->{id}
- ,'planet','Heavy Usage', $production{$3}) or die $dbh->errstr;
+ push @values,$1,$2,$3;
}
if($file =~ m{<span class="superhighlight">([\d,]+)</span>}){
- my $res = $1;
- $res =~ s/,//g;
- $addpdata->execute($planet,$tick,$scan->{id}
- ,'planet','Production', $res) or die $dbh->errstr;
+ push @values,$1;
}
+ $addplanetscan->execute(@values);
}elsif ($type eq 'Jumpgate'){
#print "$file\n";
while ($file =~ m{(\d+):(\d+):(\d+)\D+(Attack|Defend|Return)</td><td class="left">([^<]*)\D+(\d+)\D+(\d+)}g){
$fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
}
}
- } elsif($type eq 'Surface Analysis' || $type eq 'Technology Analysis'){
- my $cat = ($type eq 'Surface Analysis' ? 'struc' : 'tech');
+ } elsif($type eq 'Development'){
+ my @values = ($scan->{id},$tick,$planet);
+ my $total = 0;
while($file =~ m{((?:[a-zA-Z]| )+)</t[dh]><td(?: class="right")?>(\d+)}sg){
- $addpdata->execute($planet,$tick,$scan->{id}
- ,$cat,$1, $2) or die $dbh->errstr;
+ push @values,$2;
+ $total += $2 if $#values <= 13;
}
-
+ push @values,$total;
+ $adddevscan->execute(@values);
} elsif($type eq 'Unit' || $type eq 'Advanced Unit'){
my $id = addfleet($type,'Full fleet',$file,$planet,undef,$tick,undef,undef,undef);
$fleetscan->execute($id,$scan->{id}) or die $dbh->errstr;
print "Something wrong with scan $scan->{id} type $type at tick $tick http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
}
$parsedscan->execute($tick,$type,$planet,$scan->{id}) or die $dbh->errstr;
- $dbh->pg_release('scans') or die "Couldn't save";
- $dbh->pg_savepoint('scans') or die "Couldn't save";
#$dbh->rollback;
};
if ($@) {