X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=Scans.pm;h=eec0828561eac5445fb877bc5286bcbfec266b84;hb=fe6db7c088929577f239ee7f8ce4615d938432b6;hp=5ad4b71f8a0102a4708cfc864323b4f8b50afc73;hpb=fbc70113998582851b1830f42eca62acfcf03dd3;p=NDIRC.git diff --git a/Scans.pm b/Scans.pm index 5ad4b71..eec0828 100644 --- a/Scans.pm +++ b/Scans.pm @@ -25,20 +25,25 @@ require Exporter; our @ISA = qw/Exporter/; -our @EXPORT = qw/addScan addScanGroup sendScan/; +our @EXPORT = qw/addScan addScanGroup sendScan reqScan scanReqs/; sub addScan { - my ($id,$verbose) = @_; + my ($id,$verbose, $quiet) = @_; if (1){ - unless ($ND::DBH->selectrow_array("SELECT scan_id FROM scans WHERE scan_id = ? AND tick >= tick() - 168",undef,$id)){ + unless ($ND::DBH->selectrow_array(q{SELECT scan_id FROM scans WHERE NOT groupscan AND scan_id = ? AND tick >= tick() - 168},undef,$id)){ my @user = $ND::DBH->selectrow_array(q{SELECT uid,username, scan_points, tick() FROM users WHERE hostmask ILIKE ? },undef,$ND::address); - if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,"type") VALUES (?,tick(),COALESCE(?,'-1'))}, + if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,uid) VALUES (?,tick(),COALESCE(?,-1))}, undef,$id,$user[0]) == 1){ if (@user){ $ND::DBH->do('UPDATE users SET scan_points = scan_points + 1 WHERE uid = ? ',undef,$user[0]); $user[2] += 1; - $ND::server->command("msg $ND::target Added scan, at tick $user[3]. $user[1] points now $user[2]"); + if ($quiet){ + $ND::server->command("msg $ND::target Added scan, points now $user[2]"); + } + else{ + $ND::server->command("msg $ND::target Added scan, at tick $user[3]. $user[1] points now $user[2]"); + } }elsif ($verbose){ $ND::server->command("msg $ND::target Added scan, but unknown user, no points"); } @@ -51,10 +56,10 @@ sub addScan { sub addScanGroup { my ($id,$verbose) = @_; if (1){ - unless ($ND::DBH->selectrow_array("SELECT scan_id FROM scans WHERE type = 'group' AND scan_id = ? AND tick >= tick() - 168",undef,$id)){ + unless ($ND::DBH->selectrow_array("SELECT scan_id FROM scans WHERE groupscan AND scan_id = ? AND tick >= tick() - 168",undef,$id)){ my @user = $ND::DBH->selectrow_array(q{SELECT uid,username, scan_points, tick() FROM users WHERE hostmask ILIKE ? },undef,$ND::address); - if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,"type",scan) VALUES (?,tick(),'group',COALESCE(?,'-1'))}, + if ($ND::DBH->do(q{INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (?,tick(),true,COALESCE(?,-1))}, undef,$id,$user[0]) == 1){ if (@user){ $ND::server->command("msg $ND::target Added scan group, at tick $user[3]. Points will be added after parsing"); @@ -84,4 +89,107 @@ sub sendScan { } } +our %scanid = (p => 1, s => 2, t => 3, u => 4, n => 5, j => 7, a => 8); + +our @scantypes = ('Planet','Surface Analysis', 'Technology Analysis' + ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit'); + +sub reqScan { + my ($msg, $command) = @_; + + my ($x, $y, $z, $type, $typeid); + if (defined $msg && $msg =~ /^([pstunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/){ + $typeid = $scanid{$1}; + $type = $scantypes[$typeid-1]; + ($x,$y,$z) = ($2,$3,$4); + $msg = $5; + }else{ + $ND::server->command("notice $ND::nick syntax: $command type X:Y:Z message" + . " | type is the first character in the scan name, like p for planet scan, message is a message to scanners, message is a message to scanners, like plz or thanks like plz or thanks"); + return; + } + + if (my $user = member){ + + my $planet = $ND::DBH->selectrow_array(q{SELECT planetid($1,$2,$3,$4)} + ,undef,$x,$y,$z,$ND::tick); + + my $query = $ND::DBH->prepare(q{SELECT scan_id + FROM scans + WHERE planet = $1 AND type = $2 AND tick >= $3}); + $query->execute($planet,$type,$ND::tick); + + if (my $scan = $query->fetchrow_hashref){ + $ND::server->command("notice $ND::nick scan already exist: " + . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}"); + }else{ + my $req = $ND::DBH->prepare(q{SELECT * FROM scan_requests + WHERE uid = $1 AND planet = $2 + AND type = $3 AND NOT sent + }); + $req->execute($user->{uid},$planet,$type); + + my $id; + if(my $scan = $req->fetchrow_hashref){ + $req = $ND::DBH->prepare(q{UPDATE scan_requests + SET nick = $1, tick = tick(), time = NOW() + WHERE id = $2}); + $req->execute($ND::nick,$scan->{id}); + $id = $scan->{id}; + }else{ + $req = $ND::DBH->prepare(q{INSERT INTO scan_requests + (uid,nick,planet,type) VALUES($1,$2,$3,$4) + RETURNING (id)}); + $req->execute($user->{uid},$ND::nick,$planet,$type); + $id = $req->fetchrow; + } + + if ($id){ + $ND::server->command("msg $ND::scanchan " + . "$ND::B$id$ND::O http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z" + . " ($x:$y:$z $type) | <$ND::nick> $msg" + ); + $ND::server->command("notice $ND::nick sent request ($x:$y:$z $type)"); + }else{ + $ND::server->command("notice $ND::nick something went wrong.."); + } + + } + } +} + +our %scantypes; +{ + my $i = 1; + %scantypes = map {$_ => $i++} @scantypes; +} + +sub scanReqs { + my ($msg, $command) = @_; + + if (scanner()){ + + my $reqs = $ND::DBH->prepare(q{SELECT min(sr.id) AS id, x,y,z,type + FROM scan_requests sr + JOIN current_planet_stats p ON p.id = sr.planet + WHERE sr.time > NOW() - '30 min'::INTERVAL + AND NOT EXISTS (SELECT scan_id FROM scans + WHERE planet = sr.planet + AND type = sr.type + AND tick >= sr.tick + ) + GROUP BY x,y,z,type + ORDER BY id + }); + $reqs->execute; + my $text = ''; + while (my $req = $reqs->fetchrow_hashref){ + $text .= "$ND::B$req->{id}$ND::O http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} " + } + + $text = 'No unhandled reqests.' unless $text; + $ND::server->command("notice $ND::nick $text"); + } +} + 1;