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