1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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::Usermgm;
26 our @ISA = qw/Exporter/;
28 our @EXPORT = qw/addPoints setHost setPNick getShips getFleet/;
31 my ($ship,$command) = @_;
33 unless (defined $ship){
34 $ND::server->command("notice $ND::nick Usage: .$command ship | % can be used as wildcard, e.g. beet%");
37 if (officer() || dc()){
38 my $f = $ND::DBH->prepare(qq{SELECT username,SUM(fs.amount) AS amount
40 JOIN (SELECT DISTINCT ON (planet) planet,fid FROM fleets
41 WHERE mission = 'Full fleet' AND name <> 'Unit'
42 ORDER BY planet,tick DESC,fid DESC) f USING (planet)
43 JOIN fleet_ships fs USING (fid)
44 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
45 GROUP BY username ORDER BY amount DESC
47 if ($command eq 'shipshome'){
48 $f = $ND::DBH->prepare(q{
49 SELECT username,SUM(amount) AS amount
51 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
52 GROUP BY username ORDER BY amount DESC
59 while (my $user = $f->fetchrow_hashref){
60 $user->{last} = '?' unless defined $user->{last};
61 $text .= "$user->{username}: $user->{amount} ";
63 $total += $user->{amount};
66 $ND::server->command("notice $ND::nick $ND::B$i$ND::B Users with $ND::B$total $ship$ND::B: $text");
68 $ND::server->command("msg $ND::target $ND::B$i$ND::B Couldn't find any user with $ND::B$ship$ND::B:");
74 my ($nick,$command) = @_;
76 unless (defined $nick){
77 $ND::server->command("notice $ND::nick Usage: $command nick | % can be used as wildcard, e.g. barr%");
80 if (officer() || dc()){
81 my $f = $ND::DBH->prepare(q{SELECT fs.ship, fs.amount, username
83 JOIN (SELECT fid,username
85 JOIN users u USING (planet)
86 WHERE mission = 'Full fleet' AND name <> 'Unit'
88 ORDER BY planet,tick DESC,fid DESC
96 while (my $ship = $f->fetchrow_hashref){
97 unless (defined $username) {
98 $username = $ship->{username};
99 $text = "$ND::B$username$ND::O has: "
101 $text .= "$ship->{ship} $ship->{amount} ";
104 $ND::server->command("notice $ND::nick $text");
106 $ND::server->command("notice $ND::nick Couldn't find any fleet for $nick");
115 if(defined $msg && $msg =~ /^(\S+)(?: (-?(\d+)?(\.\d+)?))?$/){
119 $ND::server->command("notice $ND::nick syntax: .$t nick [points] | % can be used for wildcards \%arro\% will match barrow, if the number of points isn't specified, then 1 will be assumed.");
122 if ( ($t eq "d" && dc())
123 || ($t eq "a" && bc())
124 || ($t eq "h" && officer())
125 || ($t eq "s" && scanner())){
126 $points = 1 unless $points;
127 if ($points*$points > 400){
128 $ND::server->command("msg $ND::target Values between -20 and 20 please");
131 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
133 my @row = $f->fetchrow();
135 my $type = "defense";
136 $type = "attack" if $t eq "a";
137 $type = "humor" if $t eq "h";
138 $type = "scan" if $t eq "s";
139 my ($fleets) = $ND::DBH->selectrow_array('SELECT count(*) FROM raids r JOIN raid_targets rt ON r.id = rt.raid JOIN raid_claims rc ON rt.id = rc.target WHERE not launched AND uid = ? AND tick + 24 > tick();',undef,$row[0]);
140 if ($t eq 'a' && $fleets > 0 && $points > 0){
141 $ND::server->command("msg $ND::target $row[1] has $fleets claimed waves the last 24 ticks that aren't marked as launched, so no points.");
145 $ND::DBH->do("UPDATE users SET $type = $type + ? WHERE uid = ?",undef,$points,$row[0]);
146 $ND::server->command("msg $ND::target $row[1] has been given $points $type");
147 }elsif ($f->rows == 0){
148 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
150 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
155 $ND::server->command("msg $ND::target You don't have access for that");
160 my ($msg, $command) = @_;
163 if(defined $msg && $msg =~ /^(\S+) (\S+)$/){
167 $ND::server->command("notice $ND::nick syntax: .sethost nick host | % can be used for wildcards \%arro% will match barrow");
171 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
173 my ($uid,$nick) = $f->fetchrow();
175 my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE hostmask ILIKE ? AND NOT (username ILIKE ?)",undef,$host,$nick);
176 if ((not defined $username) && $ND::DBH->do("UPDATE users SET hostmask = ? WHERE uid = ?",undef,$host,$uid) > 0){
177 $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s host to: $ND::B$host$ND::B");
178 }elsif(defined $username){
179 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B.");
181 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
183 }elsif ($f->rows == 0){
184 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
186 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
193 my ($msg, $command) = @_;
196 if(defined $msg && $msg =~ /^(\S+) ([^.\s]+)$/){
200 $ND::server->command("notice $ND::nick syntax: .$command nick pnick | % can be used for wildcards \%arro% will match barrow");
204 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
206 my ($uid,$nick) = $f->fetchrow();
208 my ($username,$p_nick) = $ND::DBH->selectrow_array("SELECT username, pnick FROM users WHERE pnick ILIKE ? AND NOT (username ILIKE ?)",undef,$pnick,$nick);
209 my $hostname = "$pnick.users.netgamers.org";
210 if ((not defined $username) && $ND::DBH->do("UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?",undef,$pnick,$hostname,$uid) > 0){
211 $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s pnick to: $ND::B$pnick$ND::B and hostname to $ND::B$hostname$ND::B");
212 }elsif(defined $username){
213 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with pnick $ND::B$p_nick$ND::B.");
215 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
217 }elsif ($f->rows == 0){
218 $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
220 $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");