]> ruin.nu Git - NDIRC.git/blob - Def.pm
9297e3c5b846f8b18e1caa7396c80aafeb96d8a8
[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 ND::IRC::Def;
20 use strict;
21 use warnings;
22 use ND::DB;
23 use ND::Include;
24 use ND::IRC::Access;
25 use ND::IRC::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 ($id) = @_;
34         DB();
35         if (dc()){
36                 my $f = $ND::DBH->prepare(<<SQL
37                 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')
38                 FROM incomings i
39                         JOIN calls c ON i.call = c.id
40                                 JOIN current_planet_stats p ON i.sender = p.id
41                                 WHERE i.call = ? 
42                                 ORDER BY p.x,p.y,p.z;
43 SQL
44 );
45                 $f->execute($id);
46                 while (my @row = $f->fetchrow()){
47                         @row = map (valuecolor(0),@row);
48                         $ND::server->command("msg $ND::target (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]");
49                 }
50         }
51 }
52
53 sub setType {
54         my ($type,$id,$x,$y,$z) = @_;
55         DB();
56         if (my $user = dc()){
57                 my $fleet;
58                 my $query = qq{
59                         SELECT i.id,call,shiptype, coords(x,y,z),c.landing_tick - tick() FROM incomings i 
60                                 JOIN current_planet_stats p ON i.sender = p.id
61                                 JOIN calls c ON i.call = c.id
62                         };
63                 if (defined $x && $x eq 'call'){
64                         $fleet = $ND::DBH->prepare(qq{
65                                         $query
66                                         WHERE i.call = ?
67                                 });
68                         $fleet->execute($id);
69                 }elsif (defined $x){
70                         $fleet = $ND::DBH->prepare(qq{
71                                         $query
72                                         WHERE i.call = ? AND p.id = planetid(?,?,?,0) 
73                                 });
74                         $fleet->execute($id,$x,$y,$z);
75                 }else{
76                         $fleet = $ND::DBH->prepare(qq{
77                                         $query
78                                         WHERE i.id = ?
79                                 });
80                         $fleet->execute($id);
81                 }       
82                 while (my ($id,$call,$oldtype,$coords,$tick) = $fleet->fetchrow()){
83                         if($ND::DBH->do(q{UPDATE incomings SET shiptype = ? WHERE id = ?},undef,$type,$id) == 1){
84                                 log_message $user->{uid}, "DC set fleet: $id to: $type";
85                                 $ND::server->command("msg $ND::target Set fleet from $coords on call $call to $type (previously $oldtype)");
86                                 if ($tick < 0 && not (defined $x && $x eq 'call')){
87                                         $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.");
88                                 }
89                         }
90                 }
91         }
92 }
93 sub takeCall {
94         my ($id) = @_;
95         DB();
96         if (dc()){
97                 if ($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?) WHERE id = ?}
98                                 ,undef,$ND::address,$id) == 1){
99                         $ND::server->command("msg $ND::target Updated the DC for call $id");
100                 }
101         }
102 }
103
104 sub covCall {
105         my ($id) = @_;
106         DB();
107         if (dc()){
108                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = TRUE, open = FALSE WHERE id = ?}
109                                 ,undef,$ND::address,$id) == 1){
110                         $ND::server->command("msg $ND::target Marked call $id as covered");
111                 }
112         }
113 }
114
115 sub ignoreCall {
116         my ($id) = @_;
117         DB();
118         if (dc()){
119                 if($ND::DBH->do(q{UPDATE calls SET dc = (SELECT uid FROM users WHERE hostmask ILIKE ?), covered = FALSE, open = FALSE WHERE id = ?}
120                                 ,undef,$ND::address,$id) == 1){
121                         $ND::server->command("msg $ND::target Marked call $id as ignored");
122                 }
123         }
124 }
125
126 sub defcall {
127         my ($msg,$nick,$callnr) = @_;
128         DB();
129         if (dc()){
130                 my $call = "";
131                 if ($callnr){
132                         my $st = $ND::DBH->prepare(q{
133         SELECT c.landing_tick - (SELECT value::integer FROM misc WHERE id = 'TICK'), concat(i.shiptype||'/') AS shiptype, p.x
134         FROM calls c 
135                 JOIN incomings i ON i.call = c.id
136                 LEFT OUTER JOIN users dc ON dc.uid = c.dc
137                 JOIN users u ON u.uid = c.member
138                 JOIN current_planet_stats p ON u.planet = p.id
139         WHERE not covered AND c.id = ?
140         GROUP BY c.id,c.landing_tick,p.x
141         ORDER BY c.landing_tick;
142                         });
143                         if (my @row = $ND::DBH->selectrow_array($st,undef,$callnr)){
144                                 chop($row[1]);
145                                 $call = "(Anti $row[1] ETA: $row[0] Cluster: $row[2])"
146                         }
147                 }
148                 $ND::server->command("notice $ND::memchan DEFENSE REQUIRED!! WAKE UP!!");
149                 $ND::server->command("msg $ND::memchan DEFENSE REQUIRED $msg $call MSG $nick TO RESPOND");
150         }
151 }
152
153 sub anon {
154         my ($target,$msg) = @_;
155         if (dc()){
156                 $ND::server->command("msg $target ".chr(2).$msg);
157                 $ND::server->command("msg $ND::target ".chr(3)."3$1 << $2");
158         }
159 }
160
161
162 sub setDefPrio {
163         my ($min,$max) = @_;
164         DB();
165         if (hc()){
166                 $ND::DBH->begin_work;
167                 my $update = $ND::DBH->prepare('UPDATE misc SET value = ? :: int WHERE id = ?');
168                 $update->execute($min,'DEFMIN');
169                 $update->execute($max,'DEFMAX');
170                 if ($ND::DBH->commit){
171                         $ND::server->command("msg $ND::target min def prio set to $min and max set to $max");
172                 }else{
173                         $ND::server->command("msg $ND::target something went wrong");
174                 }
175         }
176 }
177
178 1;