1 #**************************************************************************
2 # Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu> *
4 # This program is free software; you can redistribute it and/or modify *
5 # it under the terms of the GNU General Public License as published by *
6 # the Free Software Foundation; either version 2 of the License, or *
7 # (at your option) any later version. *
9 # This program is distributed in the hope that it will be useful, *
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 # GNU General Public License for more details. *
14 # You should have received a copy of the GNU General Public License *
15 # along with this program; if not, write to the *
16 # Free Software Foundation, Inc., *
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 #**************************************************************************/
25 use NDIRC::Dispatcher;
28 help => q(syntax: .getscans X:Y:Z),
30 }, class extends NDIRC::Command {
31 method execute($c,$args) {
32 my ($x,$y,$z) = $args =~ /(\d+)\D+(\d+)\D+(\d+)/ or die 'ARGS';
33 $c->reply("https://nd.ruin.nu/stats/find/$x:$y:$z");
37 my %scanid = (p => 1, l => 2, d => 3, u => 4, n => 5, j => 7, a => 8);
39 my @scantypes = ('Planet','Landing', 'Development'
40 ,'Unit', 'News', 'Incoming', 'Jumpgate', 'Advanced Unit');
43 help => q(syntax: gs 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, like plz or thanks like plz or thanks),
46 }, class extends NDIRC::Command {
47 method execute($c,$args) {
48 my ($typeid, $x, $y, $z, $msg) = $args =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+) (\S.*)/
50 $typeid = $scanid{$typeid};
51 my $type = $scantypes[$typeid-1];
53 my ($planet,$dists,$tick) = $c->model->selectrow_array(q{
54 SELECT pid,distorters,tick
55 FROM current_planet_stats LEFT JOIN current_development_scans USING (pid)
56 WHERE x = $1 AND y = $2 AND z = $3
59 $c->reply("There is no planet with coords $x:$y:$z, try again after the tick.");
63 my $query = $c->model->prepare(q{
64 SELECT scan_id FROM scans
65 WHERE pid = $1 AND type = $2 AND tick >= tick()
67 $query->execute($planet,$type);
69 if (my $scan = $query->fetchrow_hashref){
70 $c->reply("scan already exist: "
71 . "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
73 my $req = $c->model->prepare(q{
74 SELECT * FROM scan_requests
76 AND pid = $2 AND type = $3 AND NOT sent
78 $req->execute($c->uid,$planet,$type);
81 if(my $scan = $req->fetchrow_hashref){
82 $req = $c->model->prepare(q{
83 UPDATE scan_requests SET nick = $1, tick = tick(), time = NOW()
86 $req->execute($c->nick,$scan->{id});
89 $req = $c->model->prepare(q{
90 INSERT INTO scan_requests (uid,nick,pid,type)
91 VALUES($1,$2,$3,$4) RETURNING (id)
93 $req->execute($c->uid,$c->nick,$planet,$type);
99 $dists = "$dists dists PT$tick"
101 $dists = "DISTS UNKNOWN, ADD DEVSCAN"
103 $c->message(privmsg => $c->disp->targets->{scan}
104 ,"<b>$id</b> http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z"
105 . " ($x:$y:$z $type - $dists) | <".$c->nick."> $msg"
107 $c->reply("sent request ($x:$y:$z $type)");
109 $c->reply("something went wrong..");
118 %scantypes = map {$_ => $i++} @scantypes;
121 command scanreqs => {
122 help => q(syntax: .scanreqs [-pdunja] | Lists scan requests that haven't been handled. The argument can be used to omit types you don't have, like .scanreqs -ja to list all requests except jumpgates and advanced unit scans.),
123 acl => q(irc_scanreqs),
124 }, class extends NDIRC::Command {
125 method execute($c,$msg) {
127 if ($msg =~ /^-([pdunja]+)$/){
129 push @notype, $scantypes[$scanid{$_}-1];
135 my $reqs = $c->model->prepare(q{
136 SELECT min(sr.id) AS id, x,y,z,type
137 FROM scan_requests sr
138 JOIN current_planet_stats p USING (pid)
139 WHERE sr.time > NOW() - '30 min'::INTERVAL
140 AND NOT EXISTS (SELECT scan_id FROM scans
149 $reqs->execute(\@notype);
151 while (my $req = $reqs->fetchrow_hashref){
152 $text .= "<b>$req->{id}</b> http://game.planetarion.com/waves.pl?id=$scantypes{$req->{type}}&x=$req->{x}&y=$req->{y}&z=$req->{z} "
155 $c->reply($text || 'No unhandled requests.');
160 help => q(syntax: scan type X:Y:Z [tick] | type is the first character in the scan name, like p for planet scan. Specify a tick if you want scans older than the specified tick.),
163 }, class extends NDIRC::Command {
164 method execute($c,$msg) {
165 my ($t, $x, $y, $z, $tick)
166 = $msg =~ /^([pdunja]) (\d+)\D+(\d+)\D+(\d+)(?: (\d+))?/ or die 'ARGS';
167 my $type =$scantypes[$scanid{$t}-1];
169 my $scan = $c->model->selectrow_hashref(q{
170 SELECT id, scan_id, tick FROM scans
171 WHERE type = $1 AND pid = planetid($2,$3,$4,0) AND COALESCE(tick < $5,TRUE)
172 ORDER BY tick DESC LIMIT 1
173 },undef,$type,$x,$y,$z,$tick);
174 unless ($scan->{id}){
175 $c->reply("No $type scan for $x:$y:$z. Planet might not exist.");
181 my $fid = $c->model->selectrow_array(q{
182 SELECT fid FROM fleet_scans WHERE id = $1
183 },undef,$scan->{id});
184 my $ships = $c->model->prepare(q{
185 SELECT ship,amount FROM fleet_ships WHERE fid = $1 ORDER BY num
187 $ships->execute($fid);
188 while (my $ship = $ships->fetchrow_hashref){
189 $text .= " $ship->{ship} $ship->{amount} |"
193 $text = "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}";
196 $c->reply("<b>$type</b> scan of $x:$y:$z "
197 . "($scan->{scan_id} pt: <b>$scan->{tick}</b>): $text");
202 help => q(syntax: .anon nick message),
204 acl => q(irc_anonscan),
205 }, class extends NDIRC::Command {
206 method execute($c,$msg) {
207 my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
209 $c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
210 .$c->channel.")</c>");
211 $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
216 help => q(syntax: .addscan list of scans),
217 }, class extends NDIRC::Command {
218 method execute($c,$msg) {
221 my $oldscan = $dbh->prepare(q{
222 SELECT scan_id FROM scans
223 WHERE groupscan = $1 AND scan_id = LOWER($2) AND tick >= tick() - 168
225 my $addscan = $dbh->prepare(q{
226 INSERT INTO scans (scan_id,tick,groupscan,uid) VALUES (LOWER($1),tick(),$2,COALESCE($3,-1))
228 my $user = $dbh->selectrow_hashref(q{SELECT uid,username, scan_points, tick()
229 FROM users WHERE uid = ? },undef,$c->uid);
234 while ($msg =~ m{/.+?scan(_id|_grp)?=(\w+)}g){
236 my $group = ($1 ~~ /_grp/ || 0);
237 unless($dbh->selectrow_array($oldscan,undef,$group,$id)){
238 $addscan->execute($id,$group,$user->{uid});
246 if ($user->{uid} && ($scans || $groupscans)){
247 $dbh->do(q{UPDATE users SET scan_points = scan_points + $2 WHERE uid = $1}
248 ,undef,$user->{uid},$scans);
249 my $points = $user->{scan_points} + $scans;
250 $c->reply("Added $scans scans and $groupscans groupscans, $points in total.");