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