]> ruin.nu Git - NDIRC.git/blob - Usermgm.pm
Converted laston, split half the command into lastseen
[NDIRC.git] / Usermgm.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::Usermgm;
20 use strict;
21 use warnings;
22 use ND::DB;
23 use NDIRC::Access;
24 require Exporter;
25
26 our @ISA = qw/Exporter/;
27
28 our @EXPORT = qw/addPoints setHost setPNick getShips getFleet/;
29
30 sub getShips {
31         my ($ship,$command) = @_;
32
33         unless (defined $ship){
34                 $ND::server->command("notice $ND::nick Usage: .$command ship | % can be used as wildcard, e.g. beet%");
35                 return;
36         }
37         if (officer() || dc()){
38                 my $f = $ND::DBH->prepare(qq{SELECT username,SUM(fs.amount) AS amount
39                         FROM users u 
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
46                         });
47                 if ($command eq 'shipshome'){
48                         $f = $ND::DBH->prepare(q{
49 SELECT username,SUM(amount) AS amount
50 FROM available_ships
51 WHERE ship ILIKE ? AND uid IN (SELECT uid FROM groupmembers WHERE gid = 2)
52 GROUP BY username ORDER BY amount DESC
53                         });
54                 }
55                 $f->execute($ship);
56                 my $text;
57                 my $i = 0;
58                 my $total = 0;
59                 while (my $user = $f->fetchrow_hashref){
60                         $user->{last} = '?' unless defined $user->{last};
61                         $text .= "$user->{username}: $user->{amount} ";
62                         $i++;
63                         $total += $user->{amount};
64                 }
65                 if ($text){
66                         $ND::server->command("notice $ND::nick $ND::B$i$ND::B Users with $ND::B$total $ship$ND::B: $text");
67                 }else{
68                         $ND::server->command("msg $ND::target $ND::B$i$ND::B Couldn't find any user with $ND::B$ship$ND::B:");
69                 }
70         }
71 }
72
73 sub getFleet {
74         my ($nick,$command) = @_;
75
76         unless (defined $nick){
77                 $ND::server->command("notice $ND::nick Usage: $command nick | % can be used as wildcard, e.g. barr%");
78                 return;
79         }
80         if (officer() || dc()){
81                 my $f = $ND::DBH->prepare(q{SELECT fs.ship, fs.amount, username
82                         FROM fleet_ships fs
83                                 JOIN (SELECT fid,username
84                                         FROM fleets f
85                                                 JOIN users u USING (planet)
86                                         WHERE mission = 'Full fleet' AND name <> 'Unit'
87                                                 AND username ILIKE $1
88                                         ORDER BY planet,tick DESC,fid DESC
89                                         LIMIT 1
90                                 ) f  USING (fid)
91                         ORDER BY num
92                 });
93                 $f->execute($nick);
94                 my $text;
95                 my $username;
96                 while (my $ship = $f->fetchrow_hashref){
97                         unless (defined $username) {
98                                 $username = $ship->{username};
99                                 $text = "$ND::B$username$ND::O has: "
100                         }
101                         $text .= "$ship->{ship} $ship->{amount} ";
102                 }
103                 if ($text){
104                         $ND::server->command("notice $ND::nick $text");
105                 }else{
106                         $ND::server->command("notice $ND::nick Couldn't find any fleet for $nick");
107                 }
108         }
109 }
110
111 sub addPoints {
112         my ($msg, $t) = @_;
113
114         my ($nick, $points);
115         if(defined $msg && $msg =~ /^(\S+)(?: (-?(\d+)?(\.\d+)?))?$/){
116                 $nick = $1;
117                 $points = $2;
118         }else{
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.");
120                 return;
121         }
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");
129                         return;
130                 }
131                 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
132                 $f->execute($nick);
133                 my @row = $f->fetchrow();
134                 if ($f->rows == 1){
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.");
142                                 return;
143                         }
144                         $type .= "_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");
149                 }else{
150                         $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
151                 }
152                 $f->finish;
153
154         }else{
155                 $ND::server->command("msg $ND::target You don't have access for that");
156         }
157 }
158
159 sub setHost {
160         my ($msg, $command) = @_;
161
162         my ($nick, $host);
163         if(defined $msg && $msg =~ /^(\S+) (\S+)$/){
164                 $nick = $1;
165                 $host = $2;
166         }else{
167                 $ND::server->command("notice $ND::nick syntax: .sethost nick host | % can be used for wildcards \%arro% will match barrow");
168                 return;
169         }
170         if (hc()){
171                 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
172                 $f->execute($nick);
173                 my ($uid,$nick) = $f->fetchrow();
174                 if ($f->rows == 1){
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.");
180                         }else{
181                                 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
182                         }
183                 }elsif ($f->rows == 0){
184                         $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
185                 }else{
186                         $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
187                 }
188                 $f->finish;
189         }
190 }
191
192 sub setPNick {
193         my ($msg, $command) = @_;
194
195         my ($nick, $pnick);
196         if(defined $msg && $msg =~ /^(\S+) ([^.\s]+)$/){
197                 $nick = $1;
198                 $pnick = $2;
199         }else{
200                 $ND::server->command("notice $ND::nick syntax: .$command nick pnick | % can be used for wildcards \%arro% will match barrow");
201                 return;
202         }
203         if (hc()){
204                 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
205                 $f->execute($nick);
206                 my ($uid,$nick) = $f->fetchrow();
207                 if ($f->rows == 1){
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.");
214                         }else{
215                                 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
216                         }
217                 }elsif ($f->rows == 0){
218                         $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
219                 }else{
220                         $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
221                 }
222                 $f->finish;
223         }
224 }
225
226 1;