]> ruin.nu Git - NDIRC.git/blob - Def.pm
use float to not get integer overflow
[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(?,?,?,0) 
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                                 log_message $user->{uid}, "DC set fleet: $id to: $type";
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 (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                 }
136         }
137 }
138
139 sub covCall {
140         my ($msg,$command) = @_;
141         my ($id);
142         if (defined $msg && $msg =~ /^(\d+)$/){
143                 $id = $1;
144         }else{
145                 $ND::server->command("notice $ND::nick Usage: $command callid");
146                 return;
147         }
148         if (dc()){
149                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = TRUE, open = FALSE WHERE id = ?}
150                                 ,undef,$ND::address,$id) == 1){
151                         $ND::server->command("msg $ND::target Marked call $id as covered");
152                 }
153         }
154 }
155
156 sub ignoreCall {
157         my ($msg,$command) = @_;
158         my ($id);
159         if (defined $msg && $msg =~ /^(\d+)$/){
160                 $id = $1;
161         }else{
162                 $ND::server->command("notice $ND::nick Usage: $command callid");
163                 return;
164         }
165         if (dc()){
166                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = FALSE, open = FALSE WHERE id = ?}
167                                 ,undef,$ND::address,$id) == 1){
168                         $ND::server->command("msg $ND::target Marked call $id as ignored");
169                 }
170         }
171 }
172
173 sub defcall {
174         my ($msg,$command) = @_;
175         my ($mess,$nick,$callnr) = @_;
176         if (defined $msg && $msg =~ /^(\d+)?(.*)$/){
177                 $callnr = $1;
178                 $mess = $2;
179         }else{
180                 $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");
181                 return;
182         }
183         if (dc()){
184                 my $call = "";
185                 if ($callnr){
186                         my $st = $ND::DBH->prepare(q{
187         SELECT c.landing_tick - (SELECT value::integer FROM misc WHERE id = 'TICK'), concat(i.shiptype||'/') AS shiptype
188         FROM calls c 
189                 JOIN incomings i ON i.call = c.id
190                 LEFT OUTER JOIN users dc ON dc.uid = c.dc
191                 JOIN users u ON u.uid = c.member
192         WHERE not covered AND c.id = ?
193         GROUP BY c.id,c.landing_tick
194         ORDER BY c.landing_tick;
195                         });
196                         if (my @row = $ND::DBH->selectrow_array($st,undef,$callnr)){
197                                 chop($row[1]);
198                                 $call = "(Anti $row[1] ETA: $row[0])"
199                         }
200                 }
201                 $ND::server->command("notice $ND::memchan DEFENSE REQUIRED!! WAKE UP!!");
202                 $ND::server->command("msg $ND::memchan DEFENSE REQUIRED $mess $call MSG $ND::nick TO RESPOND");
203         }
204 }
205
206 sub anon {
207         my ($msg,$command) = @_;
208         my ($target,$mess);
209         if (defined $msg && $msg =~ /^(\S+) (.+)$/){
210                 $target = $1;
211                 $mess = $2;
212         }else{
213                 $ND::server->command("notice $ND::nick Usage: $command nick message");
214                 return;
215         }
216         if (dc()){
217                 $ND::server->command("msg $target ".chr(2).$mess);
218                 $ND::server->command("msg $ND::target ".chr(3)."3$1 << $2");
219         }
220 }
221
222
223 sub setDefPrio {
224         my ($msg,$command) = @_;
225         my ($min,$max);
226         if (defined $msg && $msg =~ /^(\d+)\D(\d+)$/){
227                 $min = $1;
228                 $max = $2;
229         }else{
230                 $ND::server->command("notice $ND::nick Usage: $command Min Max");
231                 return;
232         }
233         if (hc()){
234                 $ND::DBH->begin_work;
235                 my $update = $ND::DBH->prepare('UPDATE misc SET value = ? :: int WHERE id = ?');
236                 $update->execute($min,'DEFMIN');
237                 $update->execute($max,'DEFMAX');
238                 if ($ND::DBH->commit){
239                         $ND::server->command("msg $ND::target min def prio set to $ND::B$min$ND::B and max set to $ND::B$max");
240                 }else{
241                         $ND::server->command("msg $ND::target something went wrong");
242                 }
243         }
244 }
245
246 1;