]> ruin.nu Git - NDIRC.git/blob - Def.pm
New fleet infrastructure and possible to list ships at home
[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 use CGI qw/:standard/;
27 require Exporter;
28
29 our @ISA = qw/Exporter/;
30
31 our @EXPORT = qw/showCall setType takeCall covCall ignoreCall
32         defcall anon setDefPrio setCalc getCalc/;
33
34 sub showCall {
35         my ($msg,$command) = @_;
36         my ($id);
37         if (defined $msg && $msg =~ /^(\d+)$/){
38                 $id = $1;
39         }else{
40                 $ND::server->command("notice $ND::nick Usage: $command callid");
41                 return;
42         }
43         if (dc()){
44                 my $f = $ND::DBH->prepare(<<SQL
45                 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')
46                 FROM incomings i
47                         JOIN calls c ON i.call = c.id
48                                 JOIN current_planet_stats p ON i.sender = p.id
49                                 WHERE i.call = ? 
50                                 ORDER BY p.x,p.y,p.z;
51 SQL
52 );
53                 $f->execute($id);
54                 while (my @row = $f->fetchrow()){
55                         @row = map (valuecolor(0),@row);
56                         $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]");
57                 }
58         }
59 }
60
61 sub setType {
62         my ($msg,$command) = @_;
63         my ($type,$id,$x,$y,$z) = @_;
64         if ($command eq 'settypeall'){
65                 if (defined $msg && $msg =~ /^(\d+) (.*)$/){
66                         $type = $2;
67                         $id = $1
68                 }else{
69                         $ND::server->command("notice $ND::nick Usage: $command callId type");
70                         return;
71                 }
72         }else{
73                 if (defined $msg && $msg =~ /^(\d+) (\d+):(\d+):(\d+) (.*)$/){
74                         $type = $5;
75                         $id = $1;
76                         $x = $2;
77                         $y = $3;
78                         $z = $4;
79                 }elsif (defined $msg && $msg =~ /^(\d+) (.*)$/){
80                         $id = $1;
81                         $type = $2;
82                 }else{
83                         $ND::server->command("notice $ND::nick Usage: $command incId type | or: $command callId X:Y:Z type");
84                         return;
85                 }
86         }
87         if (my $user = dc()){
88                 my $fleet;
89                 my $query = qq{
90                         SELECT i.id,call,shiptype, coords(x,y,z),c.landing_tick - tick() FROM incomings i 
91                                 JOIN current_planet_stats p ON i.sender = p.id
92                                 JOIN calls c ON i.call = c.id
93                         };
94                 if ($command eq 'settypeall'){
95                         $fleet = $ND::DBH->prepare(qq{
96                                         $query
97                                         WHERE i.call = ?
98                                 });
99                         $fleet->execute($id);
100                 }elsif (defined $x){
101                         $fleet = $ND::DBH->prepare(qq{
102                                         $query
103                                         WHERE i.call = ? AND p.id = planetid(?,?,?,tick()) 
104                                 });
105                         $fleet->execute($id,$x,$y,$z);
106                 }else{
107                         $fleet = $ND::DBH->prepare(qq{
108                                         $query
109                                         WHERE i.id = ?
110                                 });
111                         $fleet->execute($id);
112                 }       
113                 $type = escapeHTML($type);
114                 while (my ($id,$call,$oldtype,$coords,$tick) = $fleet->fetchrow()){
115                         if($ND::DBH->do(q{UPDATE incomings SET shiptype = ? WHERE id = ?},undef,$type,$id) == 1){
116                                 def_log $user->{uid}, $call , "Set fleet: [B] $id [/B] to: [B] $type [/B]";
117                                 $ND::server->command("msg $ND::target Set fleet from $coords on call $call to $type (previously $oldtype)");
118                                 if ($tick < 0 && not (defined $x && $x eq 'call')){
119                                         $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.");
120                                 }
121                         }
122                 }
123         }
124 }
125 sub takeCall {
126         my ($msg,$command) = @_;
127         my ($id);
128         if (defined $msg && $msg =~ /^(\d+)$/){
129                 $id = $1;
130         }else{
131                 $ND::server->command("notice $ND::nick Usage: $command callid");
132                 return;
133         }
134         if (my $user = dc()){
135                 if ($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?) WHERE id = ?}
136                                 ,undef,$ND::address,$id) == 1){
137                         $ND::server->command("msg $ND::target Updated the DC for call $id");
138                         def_log $user->{uid}, $id , "Used: [B] $command [/B]";
139                 }
140         }
141 }
142
143 sub covCall {
144         my ($msg,$command) = @_;
145         my ($id);
146         if (defined $msg && $msg =~ /^(\d+)$/){
147                 $id = $1;
148         }else{
149                 $ND::server->command("notice $ND::nick Usage: $command callid");
150                 return;
151         }
152         if (my $user = dc()){
153                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = TRUE, open = FALSE WHERE id = ?}
154                                 ,undef,$ND::address,$id) == 1){
155                         $ND::server->command("msg $ND::target Marked call $id as covered");
156                         def_log $user->{uid}, $id , "Used: [B] $command [/B]";
157                 }
158         }
159 }
160
161 sub ignoreCall {
162         my ($msg,$command) = @_;
163         my ($id);
164         if (defined $msg && $msg =~ /^(\d+)$/){
165                 $id = $1;
166         }else{
167                 $ND::server->command("notice $ND::nick Usage: $command callid");
168                 return;
169         }
170         if (my $user = dc()){
171                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = FALSE, open = FALSE WHERE id = ?}
172                                 ,undef,$ND::address,$id) == 1){
173                         $ND::server->command("msg $ND::target Marked call $id as ignored");
174                         def_log $user->{uid}, $id , "Used: [B] $command [/B]";
175                 }
176         }
177 }
178
179 sub defcall {
180         my ($msg,$command) = @_;
181         my ($mess,$nick,$callnr) = @_;
182         if (defined $msg && $msg =~ /^(\d+)?(.*)$/){
183                 $callnr = $1;
184                 $mess = $2;
185         }else{
186                 $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");
187                 return;
188         }
189         if (dc()){
190                 my $call = "";
191                 if ($callnr){
192                         my $st = $ND::DBH->prepare(q{
193         SELECT c.landing_tick - (SELECT value::integer FROM misc WHERE id = 'TICK'), concat(i.shiptype||'/') AS shiptype
194         FROM calls c 
195                 JOIN incomings i ON i.call = c.id
196                 LEFT OUTER JOIN users dc ON dc.uid = c.dc
197                 JOIN users u ON u.uid = c.member
198         WHERE not covered AND c.id = ?
199         GROUP BY c.id,c.landing_tick
200         ORDER BY c.landing_tick;
201                         });
202                         if (my @row = $ND::DBH->selectrow_array($st,undef,$callnr)){
203                                 chop($row[1]);
204                                 $call = "(Anti $row[1] ETA: $row[0])"
205                         }
206                 }
207                 $ND::server->command("notice $ND::memchan DEFENSE REQUIRED!! WAKE UP!!");
208                 $ND::server->command("msg $ND::memchan DEFENSE REQUIRED $mess $call MSG $ND::nick TO RESPOND");
209         }
210 }
211
212 sub anon {
213         my ($msg,$command) = @_;
214         my ($target,$mess);
215         if (defined $msg && $msg =~ /^(\S+) (.+)$/){
216                 $target = $1;
217                 $mess = $2;
218         }else{
219                 $ND::server->command("notice $ND::nick Usage: $command nick message");
220                 return;
221         }
222         if (dc()){
223                 $ND::server->command("msg $target ".chr(2).$mess);
224                 $ND::server->command("msg $ND::target ".chr(3)."3$1 << $2");
225         }
226 }
227
228 sub setCalc {
229         my ($msg,$command) = @_;
230         my ($id, $calc);
231         if (defined $msg && $msg =~ /^(\d+) (.+)$/){
232                 $id = $1;
233                 $calc = escapeHTML($2);
234         }else{
235                 $ND::server->command("notice $ND::nick Usage: $command callid calc");
236                 return;
237         }
238         if (my $user = dc()){
239                 if ($ND::DBH->do(q{UPDATE calls SET calc = $2 WHERE id = $1}
240                                 ,undef,$id, $calc) == 1){
241                         $ND::server->command("notice $ND::nick Updated calc call $ND::B$id$ND::O");
242                         def_log $user->{uid}, $id , 'Updated calc to: [URL]'
243                                 .$calc.'[/URL]';
244                 }
245         }
246 }
247
248 sub getCalc {
249         my ($msg,$command) = @_;
250         my ($id);
251         if (defined $msg && $msg =~ /^(\d+)$/){
252                 $id = $1;
253         }else{
254                 $ND::server->command("notice $ND::nick Usage: $command callid");
255                 return;
256         }
257         if (my $user = dc()){
258                 my $calc = $ND::DBH->selectrow_array(q{SELECT calc
259                         FROM calls WHERE id = $1}
260                         ,undef,$id);
261                 $ND::server->command("notice $ND::nick calc for call $ND::B$id$ND::O: $calc");
262         }
263 }
264
265 sub setDefPrio {
266         my ($msg,$command) = @_;
267         my ($min,$max);
268         if (defined $msg && $msg =~ /^(\d+)\D(\d+)$/){
269                 $min = $1;
270                 $max = $2;
271         }else{
272                 $ND::server->command("notice $ND::nick Usage: $command Min Max");
273                 return;
274         }
275         if (hc()){
276                 $ND::DBH->begin_work;
277                 my $update = $ND::DBH->prepare('UPDATE misc SET value = ? :: int WHERE id = ?');
278                 $update->execute($min,'DEFMIN');
279                 $update->execute($max,'DEFMAX');
280                 if ($ND::DBH->commit){
281                         $ND::server->command("msg $ND::target min def prio set to $ND::B$min$ND::B and max set to $ND::B$max");
282                 }else{
283                         $ND::server->command("msg $ND::target something went wrong");
284                 }
285         }
286 }
287
288 1;