]> ruin.nu Git - NDIRC.git/blob - Usermgm.pm
Converted .getships and .shipshome
[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 getFleet/;
29
30 sub getFleet {
31         my ($nick,$command) = @_;
32
33         unless (defined $nick){
34                 $ND::server->command("notice $ND::nick Usage: $command nick | % can be used as wildcard, e.g. barr%");
35                 return;
36         }
37         if (officer() || dc()){
38                 my $f = $ND::DBH->prepare(q{SELECT fs.ship, fs.amount, username
39                         FROM fleet_ships fs
40                                 JOIN (SELECT fid,username
41                                         FROM fleets f
42                                                 JOIN users u USING (planet)
43                                         WHERE mission = 'Full fleet' AND name <> 'Unit'
44                                                 AND username ILIKE $1
45                                         ORDER BY planet,tick DESC,fid DESC
46                                         LIMIT 1
47                                 ) f  USING (fid)
48                         ORDER BY num
49                 });
50                 $f->execute($nick);
51                 my $text;
52                 my $username;
53                 while (my $ship = $f->fetchrow_hashref){
54                         unless (defined $username) {
55                                 $username = $ship->{username};
56                                 $text = "$ND::B$username$ND::O has: "
57                         }
58                         $text .= "$ship->{ship} $ship->{amount} ";
59                 }
60                 if ($text){
61                         $ND::server->command("notice $ND::nick $text");
62                 }else{
63                         $ND::server->command("notice $ND::nick Couldn't find any fleet for $nick");
64                 }
65         }
66 }
67
68 sub addPoints {
69         my ($msg, $t) = @_;
70
71         my ($nick, $points);
72         if(defined $msg && $msg =~ /^(\S+)(?: (-?(\d+)?(\.\d+)?))?$/){
73                 $nick = $1;
74                 $points = $2;
75         }else{
76                 $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.");
77                 return;
78         }
79         if (   ($t eq "d" && dc())
80                 || ($t eq "a" && bc())
81                 || ($t eq "h" && officer())
82                 || ($t eq "s" && scanner())){
83                 $points = 1 unless $points;
84                 if ($points*$points > 400){
85                         $ND::server->command("msg $ND::target Values between -20 and 20 please");
86                         return;
87                 }
88                 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
89                 $f->execute($nick);
90                 my @row = $f->fetchrow();
91                 if ($f->rows == 1){
92                         my $type = "defense";
93                         $type = "attack" if $t eq "a";
94                         $type = "humor" if $t eq "h";
95                         $type = "scan" if $t eq "s";
96                         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]);
97                         if ($t eq 'a' && $fleets > 0 && $points > 0){
98                                 $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.");
99                                 return;
100                         }
101                         $type .= "_points";
102                         $ND::DBH->do("UPDATE users SET $type = $type + ? WHERE uid = ?",undef,$points,$row[0]);
103                         $ND::server->command("msg $ND::target $row[1] has been given $points $type");
104                 }elsif ($f->rows == 0){
105                         $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
106                 }else{
107                         $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
108                 }
109                 $f->finish;
110
111         }else{
112                 $ND::server->command("msg $ND::target You don't have access for that");
113         }
114 }
115
116 sub setHost {
117         my ($msg, $command) = @_;
118
119         my ($nick, $host);
120         if(defined $msg && $msg =~ /^(\S+) (\S+)$/){
121                 $nick = $1;
122                 $host = $2;
123         }else{
124                 $ND::server->command("notice $ND::nick syntax: .sethost nick host | % can be used for wildcards \%arro% will match barrow");
125                 return;
126         }
127         if (hc()){
128                 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
129                 $f->execute($nick);
130                 my ($uid,$nick) = $f->fetchrow();
131                 if ($f->rows == 1){
132                         my ($username,$hostname) = $ND::DBH->selectrow_array("SELECT username, hostmask FROM users WHERE hostmask ILIKE ? AND NOT (username ILIKE ?)",undef,$host,$nick);
133                         if ((not defined $username) && $ND::DBH->do("UPDATE users SET hostmask = ? WHERE uid = ?",undef,$host,$uid) > 0){
134                                 $ND::server->command("msg $ND::target Updated $ND::B$nick${ND::B}'s host to: $ND::B$host$ND::B");
135                         }elsif(defined $username){
136                                 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with host: $ND::B$hostname$ND::B.");
137                         }else{
138                                 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
139                         }
140                 }elsif ($f->rows == 0){
141                         $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
142                 }else{
143                         $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
144                 }
145                 $f->finish;
146         }
147 }
148
149 sub setPNick {
150         my ($msg, $command) = @_;
151
152         my ($nick, $pnick);
153         if(defined $msg && $msg =~ /^(\S+) ([^.\s]+)$/){
154                 $nick = $1;
155                 $pnick = $2;
156         }else{
157                 $ND::server->command("notice $ND::nick syntax: .$command nick pnick | % can be used for wildcards \%arro% will match barrow");
158                 return;
159         }
160         if (hc()){
161                 my $f = $ND::DBH->prepare("SELECT uid,username FROM users WHERE username ILIKE ?");
162                 $f->execute($nick);
163                 my ($uid,$nick) = $f->fetchrow();
164                 if ($f->rows == 1){
165                         my ($username,$p_nick) = $ND::DBH->selectrow_array("SELECT username, pnick FROM users WHERE pnick ILIKE ? AND NOT (username ILIKE ?)",undef,$pnick,$nick);
166                         my $hostname = "$pnick.users.netgamers.org";
167                         if ((not defined $username) && $ND::DBH->do("UPDATE users SET pnick = ?, hostmask = ? WHERE uid = ?",undef,$pnick,$hostname,$uid) > 0){
168                                 $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");
169                         }elsif(defined $username){
170                                 $ND::server->command("msg $ND::target $ND::B$username$ND::B already exists with pnick $ND::B$p_nick$ND::B.");
171                         }else{
172                                 $ND::server->command("msg $ND::target Couldn't update $ND::B$username${ND::B}'s host");
173                         }
174                 }elsif ($f->rows == 0){
175                         $ND::server->command("msg $ND::target No hit, maybe spelling mistake, or add % as wildcard");
176                 }else{
177                         $ND::server->command("msg $ND::target More than 1 user matched, please refine the search");
178                 }
179                 $f->finish;
180         }
181 }
182
183 1;