]> ruin.nu Git - NDIRC.git/blob - Delling.pm
Bugfix, bring back to shortcut to .anon
[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 AI::MegaHAL;
32
33 has hal => (
34         is => 'ro',
35         isa => 'Object',
36         lazy_build => 1,
37 );
38
39 my ($tick,$stattick) = (0,0);
40 my $last_announcement = 0;
41
42 after irc_public => sub {
43         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
44         my ($nick,$username,$address) = ( split /[!@]/, $who );
45         my $channel = $where->[0];
46
47         my $irc = $sender->get_heap();
48
49         #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
50         my $dbh = DB();
51         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
52         $seen->execute($address);
53
54         my $nickname = $irc->nick_name;
55         if ($msg =~ /^$nickname: (.*)/i){
56                 my $text = $self->hal->do_reply($1);
57                 $irc->yield(privmsg => $channel, "$nick: $text");
58         }elsif ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
59                 my $_ = $1;
60                 my $text = $2;
61                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
62                         $msg = ".anon $_ $text";
63                         $self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh);
64                 }
65         }elsif ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
66                 #Command parsed and run successfully
67         }elsif($self->disp->has_command('teachbot',$channel)){
68                 $msg =~ s/^\S+:\s*//;
69                 $self->hal->learn($msg);
70         }
71 };
72
73 after irc_msg => sub {
74         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
75         my ($nick,$username,$address) = ( split /[!@]/, $who );
76         my $irc = $sender->get_heap();
77
78         my $dbh = DB();
79         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
80         $seen->execute($address);
81
82         if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
83                 #Command parsed and run successfully
84         }else{
85                 $irc->yield(notice => $nick, "unknown command");
86         }
87 };
88
89 sub irc_join {
90         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
91         my ($nick,$username,$address) = ( split /[!@]/, $who );
92         my $irc = $sender->get_heap();
93         my $disp = $self->disp;
94
95         my $dbh = DB();
96         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
97         $seen->execute($address);
98
99         if($self->disp->has_command('voice',$channel)){
100                 my $flags = $dbh->prepare_cached(q{
101 SELECT DISTINCT flag
102 FROM users u
103         JOIN groupmembers g USING (uid)
104         JOIN channel_group_flags gf USING (gid)
105 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
106                 });
107                 $flags->execute($address, $channel);
108                 my $mode = '';
109                 my @who;
110                 while (my ($flag) = $flags->fetchrow()){
111                         $mode .= $flag;
112                         push @who, $nick;
113                 }
114                 say "$mode - @who";
115                 $irc->yield(mode => $channel, $mode, @who) if $mode;
116         }
117
118         if (lc $channel ~~ lc $disp->targets->{members}){
119                 if (time - $last_announcement < 1){
120                         $last_announcement = time;
121                         return;
122                 }
123                 my $user = $dbh->selectrow_hashref(q{
124 SELECT uid,pid,hostmask,password
125         ,(SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid) AS last_forum_visit
126 FROM users u WHERE hostmask = ?
127                 }, undef, $address);
128                 if ($user){
129                         if ($user->{password} eq ''){
130                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points");
131                         }
132                         if ($tick > 12 && not defined $user->{pid}){
133                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords.");
134                         }
135
136                         if (not defined $user->{last_forum_visit}){
137                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: Go read the forum! https://nd.ruin.nu/forum");
138                         }else {
139                                 my $unread = $dbh->selectrow_hashref(q{SELECT * FROM unread_posts($1)},undef,$user->{uid});
140                                 if ($unread && $unread->{new}){
141                                         $irc->yield(notice => $nick, "$unread->{new} posts since your last forum visit ($unread->{unread} unread posts in total) https://nd.ruin.nu/forum/allUnread");
142                                 }
143                         }
144                 }
145                 if (time - $last_announcement < 2){
146                         $last_announcement = time;
147                         return;
148                 }
149                 $last_announcement = time;
150         }
151 }
152
153 sub refresh {
154         my ($self,$kernel,$heap) = @_[OBJECT,KERNEL,HEAP];
155         $kernel->delay( refresh => 60 );
156         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
157
158         my $irc = $heap->{irc};
159         my $disp = $self->disp;
160         my $dbh = DB();
161         my $scans = $dbh->prepare(q{SELECT s.scan_id
162                         ,coords(x,y,z),type
163                         ,array_agg(sr.nick) AS nick
164                         ,array_agg(sr.id) AS id
165                 FROM scan_requests sr
166                         JOIN scans s USING (pid,type)
167                         JOIN current_planet_stats USING (pid)
168                 WHERE sr.time > NOW() - '30 min'::INTERVAL
169                         AND s.tick >= sr.tick AND NOT sr.sent
170                 GROUP BY scan_id,x,y,z,type
171                 });
172         my $sentscan = $dbh->prepare(q{UPDATE scan_requests
173                 SET sent = TRUE WHERE id = ANY($1)
174                 });
175         $scans->execute;
176         while (my $scan = $scans->fetchrow_hashref){
177                 $heap->{irc}->yield(notice => $scan->{nick}, "($scan->{coords} $scan->{type})"
178                         ." http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
179                 $sentscan->execute($scan->{id});
180         }
181
182         my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
183         if ($tick != $row[0]){
184                 $tick = $row[0];
185                 $irc->yield(privmsg => $disp->targets->{def}, "New tick: $tick");
186         }
187         if (defined $row[1] && $stattick != $row[1]){
188                 $stattick = $row[1];
189                 $irc->yield(privmsg => $disp->targets->{members}, "New tick: $stattick");
190         }
191         my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
192         my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
193         $ircreqs->execute;
194         while (my $req = $ircreqs->fetchrow_hashref){
195                 if ($req->{channel} eq 'def'){
196                         $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}");
197                         $self->parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$dbh);
198                 }elsif(exists $disp->targets->{$req->{channel}}){
199                         $irc->yield(privmsg => $disp->targets->{$req->{channel}}, "<$req->{username} via webbie> $req->{message}");
200                 }
201                 $upircreq->execute($req->{id});
202         }
203         my $defmissions = $dbh->prepare(q{
204 SELECT username,call,tick,dm.fleet,p.value
205         ,100 * SUM(fs.amount * (metal + crystal + eonium) / 100) / p.value AS value
206 FROM users u
207         JOIN current_planet_stats p USING (pid)
208         JOIN launch_confirmations USING (uid)
209         JOIN fleets f USING (fid)
210         JOIN fleet_ships fs USING (fid)
211         JOIN defense_missions dm ON f.fid = dm.fleet
212         JOIN ship_stats ss USING (ship)
213 WHERE NOT dm.announced
214 GROUP BY username,call,tick,dm.fleet,p.value
215                 });
216         my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?});
217         $defmissions->execute();
218         while (my $mission = $defmissions->fetchrow_hashref){
219                 $irc->yield(privmsg => $disp->targets->{def}, chr(3)."06 $mission->{username} sent def to call $mission->{call}, $mission->{value}% of value (tick $mission->{tick}) https://nd.ruin.nu/calls/edit/$mission->{call}");
220                 $updefmis->execute($mission->{fleet});
221         }
222
223 }
224
225 sub _build_hal {
226         my $hal = AI::MegaHAL->new('Path' => '.', 'Banner' => 0, 'Prompt' => 0, 'Wrap' => 0, 'AutoSave' => 1);
227         return $hal;
228 }
229
230 after sig_usr2 => sub {
231         my $self = shift;
232         say 'Saving brain!';
233         $self->hal->_cleanup;
234 };
235
236 after _start => sub {
237  ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
238 };
239
240 1;