]> ruin.nu Git - NDIRC.git/blob - Def.pm
e9647d6259bf1712911b1dad7251e314dd17e617
[NDIRC.git] / Def.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
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.                                   *
8 #                                                                         *
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.                          *
13 #                                                                         *
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 #**************************************************************************/
19 package NDIRC::Def;
20 use strict;
21 use warnings;
22 use ND::DB;
23 use ND::Include;
24 use NDIRC::Access;
25 use NDIRC::Misc;
26 require Exporter;
27
28 our @ISA = qw/Exporter/;
29
30 our @EXPORT = qw/showCall setType takeCall covCall ignoreCall defcall anon setDefPrio/;
31
32 sub showCall {
33         my ($msg,$command) = @_;
34         my ($id);
35         if (defined $msg && $msg =~ /^(\d+)$/){
36                 $id = $1;
37         }else{
38                 $ND::server->command("notice $ND::nick Usage: $command callid");
39                 return;
40         }
41         if (dc()){
42                 my $f = $ND::DBH->prepare(<<SQL
43                 SELECT i.id,coords(p.x,p.y,p.z), p.planet_status,p.nick, p.alliance, p.race,i.eta,i.amount,i.fleet,i.shiptype,p.relationship,c.landing_tick - (SELECT value::integer FROM misc WHERE id = 'TICK')
44                 FROM incomings i
45                         JOIN calls c ON i.call = c.id
46                                 JOIN current_planet_stats p ON i.sender = p.id
47                                 WHERE i.call = ? 
48                                 ORDER BY p.x,p.y,p.z;
49 SQL
50 );
51                 $f->execute($id);
52                 while (my @row = $f->fetchrow()){
53                         @row = map (valuecolor(0),@row);
54                         $ND::server->command("notice $ND::nick (CALL $id) $row[0]: $row[1], $row[3] ($row[2]), $row[4] ($row[10]), $row[5], ETA: $row[11](/$row[6]), Amount: $row[7],  $row[8], Type: $row[9]");
55                 }
56         }
57 }
58
59 sub setType {
60         my ($msg,$command) = @_;
61         my ($type,$id,$x,$y,$z) = @_;
62         if ($command eq 'settypeall'){
63                 if (defined $msg && $msg =~ /^(\d+) (.*)$/){
64                         $type = $2;
65                         $id = $1
66                 }else{
67                         $ND::server->command("notice $ND::nick Usage: $command callId type");
68                         return;
69                 }
70         }else{
71                 if (defined $msg && $msg =~ /^(\d+) (\d+):(\d+):(\d+) (.*)$/){
72                         $type = $5;
73                         $id = $1;
74                         $x = $2;
75                         $y = $3;
76                         $z = $4;
77                 }elsif (defined $msg && $msg =~ /^(\d+) (.*)$/){
78                         $id = $1;
79                         $type = $2;
80                 }else{
81                         $ND::server->command("notice $ND::nick Usage: $command incId type | or: $command callId X:Y:Z type");
82                         return;
83                 }
84         }
85         if (my $user = dc()){
86                 my $fleet;
87                 my $query = qq{
88                         SELECT i.id,call,shiptype, coords(x,y,z),c.landing_tick - tick() FROM incomings i 
89                                 JOIN current_planet_stats p ON i.sender = p.id
90                                 JOIN calls c ON i.call = c.id
91                         };
92                 if ($command eq 'settypeall'){
93                         $fleet = $ND::DBH->prepare(qq{
94                                         $query
95                                         WHERE i.call = ?
96                                 });
97                         $fleet->execute($id);
98                 }elsif (defined $x){
99                         $fleet = $ND::DBH->prepare(qq{
100                                         $query
101                                         WHERE i.call = ? AND p.id = planetid(?,?,?,tick()) 
102                                 });
103                         $fleet->execute($id,$x,$y,$z);
104                 }else{
105                         $fleet = $ND::DBH->prepare(qq{
106                                         $query
107                                         WHERE i.id = ?
108                                 });
109                         $fleet->execute($id);
110                 }       
111                 while (my ($id,$call,$oldtype,$coords,$tick) = $fleet->fetchrow()){
112                         if($ND::DBH->do(q{UPDATE incomings SET shiptype = ? WHERE id = ?},undef,$type,$id) == 1){
113                                 def_log $user->{uid}, $call , "Set fleet: [B] $id [/B] to: [B] $type [/B]";
114                                 $ND::server->command("msg $ND::target Set fleet from $coords on call $call to $type (previously $oldtype)");
115                                 if ($tick < 0 && not (defined $x && $x eq 'call')){
116                                         $ND::server->command("msg $ND::target This call is old, did you use the call id, instead of inc id by accident? You can use .settypeall callid to set the type on all incs in a call.");
117                                 }
118                         }
119                 }
120         }
121 }
122 sub takeCall {
123         my ($msg,$command) = @_;
124         my ($id);
125         if (defined $msg && $msg =~ /^(\d+)$/){
126                 $id = $1;
127         }else{
128                 $ND::server->command("notice $ND::nick Usage: $command callid");
129                 return;
130         }
131         if (my $user = dc()){
132                 if ($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?) WHERE id = ?}
133                                 ,undef,$ND::address,$id) == 1){
134                         $ND::server->command("msg $ND::target Updated the DC for call $id");
135                         def_log $user->{uid}, $id , "Used: [B] $command [/B]";
136                 }
137         }
138 }
139
140 sub covCall {
141         my ($msg,$command) = @_;
142         my ($id);
143         if (defined $msg && $msg =~ /^(\d+)$/){
144                 $id = $1;
145         }else{
146                 $ND::server->command("notice $ND::nick Usage: $command callid");
147                 return;
148         }
149         if (my $user = dc()){
150                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = TRUE, open = FALSE WHERE id = ?}
151                                 ,undef,$ND::address,$id) == 1){
152                         $ND::server->command("msg $ND::target Marked call $id as covered");
153                         def_log $user->{uid}, $id , "Used: [B] $command [/B]";
154                 }
155         }
156 }
157
158 sub ignoreCall {
159         my ($msg,$command) = @_;
160         my ($id);
161         if (defined $msg && $msg =~ /^(\d+)$/){
162                 $id = $1;
163         }else{
164                 $ND::server->command("notice $ND::nick Usage: $command callid");
165                 return;
166         }
167         if (my $user = dc()){
168                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = FALSE, open = FALSE WHERE id = ?}
169                                 ,undef,$ND::address,$id) == 1){
170                         $ND::server->command("msg $ND::target Marked call $id as ignored");
171                         def_log $user->{uid}, $id , "Used: [B] $command [/B]";
172                 }
173         }
174 }
175
176 sub defcall {
177         my ($msg,$command) = @_;
178         my ($mess,$nick,$callnr) = @_;
179         if (defined $msg && $msg =~ /^(\d+)?(.*)$/){
180                 $callnr = $1;
181                 $mess = $2;
182         }else{
183                 $ND::server->command("notice $ND::nick Usage: $command [callid] message | if a call id is given, then shiptypes and eta will be fetched from the database and added to the message");
184                 return;
185         }
186         if (dc()){
187                 my $call = "";
188                 if ($callnr){
189                         my $st = $ND::DBH->prepare(q{
190         SELECT c.landing_tick - (SELECT value::integer FROM misc WHERE id = 'TICK'), concat(i.shiptype||'/') AS shiptype
191         FROM calls c 
192                 JOIN incomings i ON i.call = c.id
193                 LEFT OUTER JOIN users dc ON dc.uid = c.dc
194                 JOIN users u ON u.uid = c.member
195         WHERE not covered AND c.id = ?
196         GROUP BY c.id,c.landing_tick
197         ORDER BY c.landing_tick;
198                         });
199                         if (my @row = $ND::DBH->selectrow_array($st,undef,$callnr)){
200                                 chop($row[1]);
201                                 $call = "(Anti $row[1] ETA: $row[0])"
202                         }
203                 }
204                 $ND::server->command("notice $ND::memchan DEFENSE REQUIRED!! WAKE UP!!");
205                 $ND::server->command("msg $ND::memchan DEFENSE REQUIRED $mess $call MSG $ND::nick TO RESPOND");
206         }
207 }
208
209 sub anon {
210         my ($msg,$command) = @_;
211         my ($target,$mess);
212         if (defined $msg && $msg =~ /^(\S+) (.+)$/){
213                 $target = $1;
214                 $mess = $2;
215         }else{
216                 $ND::server->command("notice $ND::nick Usage: $command nick message");
217                 return;
218         }
219         if (dc()){
220                 $ND::server->command("msg $target ".chr(2).$mess);
221                 $ND::server->command("msg $ND::target ".chr(3)."3$1 << $2");
222         }
223 }
224
225
226 sub setDefPrio {
227         my ($msg,$command) = @_;
228         my ($min,$max);
229         if (defined $msg && $msg =~ /^(\d+)\D(\d+)$/){
230                 $min = $1;
231                 $max = $2;
232         }else{
233                 $ND::server->command("notice $ND::nick Usage: $command Min Max");
234                 return;
235         }
236         if (hc()){
237                 $ND::DBH->begin_work;
238                 my $update = $ND::DBH->prepare('UPDATE misc SET value = ? :: int WHERE id = ?');
239                 $update->execute($min,'DEFMIN');
240                 $update->execute($max,'DEFMAX');
241                 if ($ND::DBH->commit){
242                         $ND::server->command("msg $ND::target min def prio set to $ND::B$min$ND::B and max set to $ND::B$max");
243                 }else{
244                         $ND::server->command("msg $ND::target something went wrong");
245                 }
246         }
247 }
248
249 1;