]> ruin.nu Git - NDIRC.git/blob - Delling.pm
T2 is 70% and T3 is 50% now
[NDIRC.git] / Delling.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 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::Delling;
20
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26 extends 'NDIRC::Bot';
27
28 use POE::Session;
29 use ND::DB;
30
31 use NDIRC::DiscordContext;
32
33 my ($tick,$stattick) = (0,0);
34 my $last_announcement = 0;
35
36 after irc_public => sub {
37         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
38         my ($nick,$username,$address) = ( split /[!@]/, $who );
39         my $channel = $where->[0];
40
41         my $irc = $sender->get_heap();
42
43         #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
44         my $dbh = DB();
45         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
46         $seen->execute($address);
47
48         my $nickname = $irc->nick_name;
49         if ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
50                 local $_ = $1;
51                 my $text = $2;
52                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
53                         $msg = ".anon $_ $text";
54                         $self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh);
55                 }
56         }elsif ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
57                 #Command parsed and run successfully
58         }
59 };
60
61 after irc_msg => sub {
62         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
63         my ($nick,$username,$address) = ( split /[!@]/, $who );
64         my $irc = $sender->get_heap();
65
66         my $dbh = DB();
67         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
68         $seen->execute($address);
69
70         if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
71                 #Command parsed and run successfully
72         }else{
73                 $irc->yield(notice => $nick, "unknown command");
74         }
75 };
76
77 sub irc_join {
78         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
79         my ($nick,$username,$address) = ( split /[!@]/, $who );
80         my $irc = $sender->get_heap();
81         my $disp = $self->disp;
82
83         my $dbh = DB();
84         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
85         $seen->execute($address);
86
87         if($self->disp->has_command('voice',$channel)){
88                 my $flags = $dbh->prepare_cached(q{
89 SELECT DISTINCT flag
90 FROM users u
91         JOIN groupmembers g USING (uid)
92         JOIN channel_group_flags gf USING (gid)
93 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
94                 });
95                 $flags->execute($address, $channel);
96                 my $mode = '';
97                 my @who;
98                 while (my ($flag) = $flags->fetchrow()){
99                         $mode .= $flag;
100                         push @who, $nick;
101                 }
102                 say "$mode - @who";
103                 $irc->yield(mode => $channel, $mode, @who) if $mode;
104         }
105
106         if (lc $channel ~~ @{$self->targets->{members}}){
107                 if (time - $last_announcement < 1){
108                         $last_announcement = time;
109                         return;
110                 }
111                 my $user = $dbh->selectrow_hashref(q{
112 SELECT uid,pid,hostmask,password
113         ,(SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid) AS last_forum_visit
114 FROM users u WHERE hostmask = ?
115                 }, undef, $address);
116                 if ($user){
117                         unless ($user->{password}) {
118                                 $irc->yield(privmsg => $channel, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points");
119                         }
120                 }
121                 if (time - $last_announcement < 2){
122                         $last_announcement = time;
123                         return;
124                 }
125                 $last_announcement = time;
126         }
127 }
128
129 sub refresh {
130         my ($self,$kernel,$heap) = @_[OBJECT,KERNEL,HEAP];
131         $kernel->delay( refresh => 60 );
132         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
133
134         my $irc = $heap->{irc};
135         my $disp = $self->disp;
136         my $dbh = DB();
137         my $scans = $dbh->prepare(q{SELECT s.scan_id
138                         ,coords(x,y,z),type
139                         ,array_agg(sr.nick) AS nick
140                         ,array_agg(sr.id) AS id
141                 FROM scan_requests sr
142                         JOIN scans s USING (pid,type)
143                         JOIN current_planet_stats USING (pid)
144                 WHERE sr.time > NOW() - '30 min'::INTERVAL
145                         AND s.tick >= sr.tick AND NOT sr.sent
146                 GROUP BY scan_id,x,y,z,type
147                 });
148         my $sentscan = $dbh->prepare(q{UPDATE scan_requests
149                 SET sent = TRUE WHERE id = ANY($1)
150                 });
151         $scans->execute;
152         my @scanreqs;
153         while (my $scan = $scans->fetchrow_hashref){
154                 $self->message("($scan->{coords} $scan->{type})"
155                         ." http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}"
156                         , @{$scan->{nick}});
157                 $sentscan->execute($scan->{id});
158                 push @scanreqs, @{$scan->{id}};
159         }
160
161         if (scalar @scanreqs > 0)
162         {
163                 my $scanreqs =  join(', ', @scanreqs);
164                 $self->toTarget(scan => "Handled the following requests: $scanreqs");
165         }
166
167         my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
168         if ($tick != $row[0]){
169                 $tick = $row[0];
170         }
171         if (defined $row[1] && $stattick != $row[1]){
172                 $stattick = $row[1];
173                 $self->toTarget(spam => "New tick: $stattick");
174                 my $raids = $dbh->prepare(q{UPDATE raids SET open = true WHERE NOT open AND NOT removed AND open_tick <= $1 RETURNING id});
175                 $raids->execute($stattick);
176                 while (my $raid = $raids->fetchrow_hashref) {
177                         $self->toTarget(members => "\@everyone Raid is now open: https://nd.ruin.nu/raids/view/$raid->{id}");
178                 }
179         }
180         my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
181         my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
182         $ircreqs->execute;
183         while (my $req = $ircreqs->fetchrow_hashref){
184                 if ($req->{channel} eq 'def'){
185                         $self->toTarget(def => "> __**## $req->{username} via webbie ##**__ >> $req->{message}");
186                 }else{
187                         $self->toTarget($req->{channel} => "> <$req->{username} via webbie> $req->{message}");
188                 }
189                 $upircreq->execute($req->{id});
190         }
191         my $defmissions = $dbh->prepare(q{
192 SELECT username,call,tick,dm.fleet,p.value
193         ,100 * SUM(fs.amount * (metal + crystal + eonium) / 100) / p.value AS value
194 FROM users u
195         JOIN current_planet_stats p USING (pid)
196         JOIN launch_confirmations USING (uid)
197         JOIN fleets f USING (fid)
198         JOIN fleet_ships fs USING (fid)
199         JOIN defense_missions dm ON f.fid = dm.fleet
200         JOIN ship_stats ss USING (ship)
201 WHERE NOT dm.announced
202 GROUP BY username,call,tick,dm.fleet,p.value
203                 });
204         my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?});
205         $defmissions->execute();
206         while (my $mission = $defmissions->fetchrow_hashref){
207                 $self->toTarget(def => "<c06>$mission->{username} sent def</c> to call $mission->{call}, $mission->{value}% of value (tick $mission->{tick}) https://nd.ruin.nu/calls/edit/$mission->{call}");
208                 $updefmis->execute($mission->{fleet});
209         }
210
211 }
212
213 after sig_usr2 => sub {
214         my $self = shift;
215 };
216
217 after _start => sub {
218  ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
219 };
220
221 after discord_message_create => sub {
222         my ($self, $gw, $hash) = @_;
223
224         my $author = $hash->{author};
225         my $msg = $hash->{content};
226         my $channel_id = $hash->{channel_id};
227         my $author_name = $author->{username}.'#'.$author->{discriminator};
228         my $author_id = $author->{id};
229         return if $author->{'id'} eq $self->discord_id; # Ignore my own messages
230
231         my $channel = "D-".$channel_id;
232         if (exists $self->discord_channels->{$channel_id}) {
233                 $channel = 'dm' if ($self->discord_channels->{$channel_id}->{type} == 1);
234         } else
235         {
236                 $channel = 'dm';
237         }
238
239         say localtime(time) . " - $channel_id $channel $author_name $author_id";
240         if ($channel eq 'dm')
241         {
242                 say " - '$msg'";
243         }
244
245         my $c = NDIRC::DiscordContext->new({
246                         discord_id => $author_name,
247                         channel_id => $channel_id,
248                         author_id => $author_id,
249                         channel => $channel,
250                         disp => $self->disp,
251                         model => DB(),
252                         bot => $self,
253                         discord => $self->discord,
254                 });
255
256         return $self->handleCommand($c,$msg);
257
258 };
259
260 1;