X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=scripts%2Fscans.pl;h=e9795f7eba5e87e49b131481289fdc9671494fa4;hb=5b6c3c7d0003d636c1c3da6d25734e4aad5736a5;hp=dc5b575681b790e32fae97c417c2d86e595d9eb2;hpb=bb6ade01b9d1665e3dbb03a8fb9e16152be1a338;p=ndwebbie.git diff --git a/scripts/scans.pl b/scripts/scans.pl index dc5b575..e9795f7 100755 --- a/scripts/scans.pl +++ b/scripts/scans.pl @@ -21,11 +21,12 @@ use strict; use warnings; +no warnings 'exiting'; use CGI; use DBI; use DBD::Pg qw(:pg_types); use LWP::Simple; -use lib qw{/var/www/ndawn/}; +use lib qw{/var/www/ndawn/lib/}; use ND::DB; our $dbh = ND::DB::DB(); @@ -36,7 +37,9 @@ our $dbh = ND::DB::DB(); #print ; $dbh->do(q{SET CLIENT_ENCODING TO 'LATIN1';}); -my $scangroups = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans WHERE groupscan AND NOT parsed}); +my $scangroups = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans + WHERE groupscan AND NOT parsed FOR UPDATE +}); my $oldscan = $dbh->prepare(q{SELECT scan_id FROM scans WHERE scan_id = ? AND tick >= tick() - 168}); my $addScan = $dbh->prepare(q{INSERT INTO scans (scan_id,tick,uid) VALUES (?,?,?)}); my $parsedscan = $dbh->prepare(q{UPDATE scans SET tick = ?, type = ?, planet = ?, parsed = TRUE WHERE id = ?}); @@ -61,20 +64,28 @@ while (my $group = $scangroups->fetchrow_hashref){ $dbh->commit; } -my $newscans = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans WHERE NOT groupscan AND NOT parsed}); +my $newscans = $dbh->prepare(q{SELECT id,scan_id,tick,uid FROM scans + WHERE NOT groupscan AND NOT parsed FOR UPDATE +}); my $findplanet = $dbh->prepare(q{SELECT planetid(?,?,?,?)}); +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}); my $findcoords = $dbh->prepare(q{SELECT * FROM planetcoords(?,?)}); 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 = ?), ?)}); +$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->begin_work; eval { my $type = $1; my $x = $2; @@ -83,14 +94,14 @@ while (my $scan = $newscans->fetchrow_hashref){ my $tick = $5; if($dbh->selectrow_array(q{SELECT * FROM scans WHERE scan_id = ? AND tick = ? AND id <> ?},undef,$scan->{scan_id},$tick,$scan->{id})){ - $dbh->rollback; + $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"; } my ($planet) = $dbh->selectrow_array($findplanet,undef,$x,$y,$z,$tick); unless ($planet){ - $dbh->rollback; + $dbh->pg_rollback_to('scans') or die "rollback didn't work"; next; } my $scantext = ""; @@ -99,16 +110,35 @@ while (my $scan = $newscans->fetchrow_hashref){ } if ($type eq 'Planet'){ $file =~ s/(\d),(\d)/$1$2/g; - while($file =~ m/(Metal|Crystal|Eonium)\D+(\d+)\D+(\d+)/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; + } + if($file =~ m{([A-Z][a-z]+)([A-Z][a-z]+)([A-Z][a-z]+)}){ $addpdata->execute($planet,$tick,$scan->{id} - ,'roid',$1, $2) or die $dbh->errstr; + ,'planet','Light Usage', $production{$1}) or die $dbh->errstr; $addpdata->execute($planet,$tick,$scan->{id} - ,'resource',$1, $3) or die $dbh->errstr; + ,'planet','Medium Usage', $production{$2}) or die $dbh->errstr; + $addpdata->execute($planet,$tick,$scan->{id} + ,'planet','Heavy Usage', $production{$3}) or die $dbh->errstr; + } + if($file =~ m{([\d,]+)}){ + my $res = $1; + $res =~ s/,//g; + $addpdata->execute($planet,$tick,$scan->{id} + ,'planet','Production', $res) or die $dbh->errstr; } }elsif ($type eq 'Jumpgate'){ - while ($file =~ m/(\d+):(\d+):(\d+)\D+"left"\>(Attack|Defend|Return)<\/td>([^<]*)<\/td>(\d+)\D+(\d+)/g){ + #print "$file\n"; + while ($file =~ m{(\d+):(\d+):(\d+)\D+(Attack|Defend|Return)([^<]*)\D+(\d+)\D+(\d+)}g){ my ($sender) = $dbh->selectrow_array($findplanet,undef,$1,$2,$3,$tick) or die $dbh->errstr; + ($sender) = $dbh->selectrow_array($findoldplanet,undef,$1,$2,$3,$tick) if ((not defined $sender) && $4 eq 'Return'); my $id = addfleet($5,$4,undef,$sender,$planet,$tick+$6,$6 ,undef,$7, $x == $1 && $y == $2); $fleetscan->execute($id,$scan->{id}) or die $dbh->errstr; @@ -164,12 +194,11 @@ while (my $scan = $newscans->fetchrow_hashref){ 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->commit; #$dbh->rollback; }; if ($@) { warn $@; - $dbh->rollback; + $dbh->pg_rollback_to('scans') or die "rollback didn't work"; } }else{ warn "Nothing useful in scan: $scan->{id} http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}\n"; @@ -177,10 +206,14 @@ while (my $scan = $newscans->fetchrow_hashref){ $addpoints->execute(-1,$scan->{uid}) if $scan->{uid} > 0; } } +#$dbh->rollback; +$dbh->commit; sub addfleet { my ($name,$mission,$ships,$sender,$target,$tick,$eta,$back,$amount,$ingal) = @_; + die "no sender" unless defined $sender; + $ingal = 0 unless $ingal; $back = $tick + 4 if $ingal && $eta <= 4; @@ -191,7 +224,7 @@ sub addfleet { my @ships; my $total = 0; - while(defined $ships && $ships =~ m{((?:[a-zA-Z]| )+)(\d+)}sg){ + while(defined $ships && $ships =~ m{((?:[a-zA-Z]| )+)(\d+)}sg){ $total += $2; push @ships, [$1,$2]; }