1 #**************************************************************************
2 # Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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;
31 use NDIRC::DiscordContext;
33 my ($tick,$stattick) = (0,0);
34 my $last_announcement = 0;
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];
41 my $irc = $sender->get_heap();
43 #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
45 my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
46 $seen->execute($address);
48 my $nickname = $irc->nick_name;
49 if ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
52 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
53 $msg = ".anon $_ $text";
54 $self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh);
56 }elsif ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
57 #Command parsed and run successfully
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();
67 my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
68 $seen->execute($address);
70 if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
71 #Command parsed and run successfully
73 $irc->yield(notice => $nick, "unknown command");
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;
84 my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
85 $seen->execute($address);
87 if($self->disp->has_command('voice',$channel)){
88 my $flags = $dbh->prepare_cached(q{
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');
95 $flags->execute($address, $channel);
98 while (my ($flag) = $flags->fetchrow()){
103 $irc->yield(mode => $channel, $mode, @who) if $mode;
106 if (lc $channel ~~ lc $disp->targets->{members}){
107 if (time - $last_announcement < 1){
108 $last_announcement = time;
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 = ?
117 unless ($user->{password}) {
118 $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");
120 if ($tick > 12 && not defined $user->{pid}){
121 $irc->yield(privmsg => $disp->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords.");
124 if (not defined $user->{last_forum_visit}){
125 $irc->yield(privmsg => $disp->targets->{members}, "$nick: Go read the forum! https://nd.ruin.nu/forum");
127 my $unread = $dbh->selectrow_hashref(q{SELECT * FROM unread_posts($1)},undef,$user->{uid});
128 if ($unread && $unread->{new}){
129 $irc->yield(notice => $nick, "$unread->{new} posts since your last forum visit ($unread->{unread} unread posts in total) https://nd.ruin.nu/forum/allUnread");
133 if (time - $last_announcement < 2){
134 $last_announcement = time;
137 $last_announcement = time;
142 my ($self,$kernel,$heap) = @_[OBJECT,KERNEL,HEAP];
143 $kernel->delay( refresh => 60 );
144 print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
146 my $irc = $heap->{irc};
147 my $disp = $self->disp;
149 my $scans = $dbh->prepare(q{SELECT s.scan_id
151 ,array_agg(sr.nick) AS nick
152 ,array_agg(sr.id) AS id
153 FROM scan_requests sr
154 JOIN scans s USING (pid,type)
155 JOIN current_planet_stats USING (pid)
156 WHERE sr.time > NOW() - '30 min'::INTERVAL
157 AND s.tick >= sr.tick AND NOT sr.sent
158 GROUP BY scan_id,x,y,z,type
160 my $sentscan = $dbh->prepare(q{UPDATE scan_requests
161 SET sent = TRUE WHERE id = ANY($1)
164 while (my $scan = $scans->fetchrow_hashref){
165 $heap->{irc}->yield(notice => $scan->{nick}, "($scan->{coords} $scan->{type})"
166 ." http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
167 $sentscan->execute($scan->{id});
170 my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
171 if ($tick != $row[0]){
173 $irc->yield(privmsg => $disp->targets->{def}, "New tick: $tick");
175 if (defined $row[1] && $stattick != $row[1]){
177 $irc->yield(privmsg => $disp->targets->{members}, "New tick: $stattick");
179 my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
180 my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
182 while (my $req = $ircreqs->fetchrow_hashref){
183 if ($req->{channel} eq 'def'){
184 $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}");
185 $self->parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$dbh);
186 }elsif(exists $disp->targets->{$req->{channel}}){
187 $irc->yield(privmsg => $disp->targets->{$req->{channel}}, "<$req->{username} via webbie> $req->{message}");
189 $upircreq->execute($req->{id});
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
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
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 $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}");
208 $updefmis->execute($mission->{fleet});
213 after sig_usr2 => sub {
217 after _start => sub {
218 ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
221 after discord_message_create => sub {
225 my $author = $hash->{author};
226 my $msg = $hash->{content};
227 my $channel_id = $hash->{channel_id};
228 my $author_name = $author->{username}.'#'.$author->{discriminator};
229 my $author_id = $author->{id};
230 return if $author->{'id'} eq $self->discord_id; # Ignore my own messages
233 if (exists $self->discord_channels->{$channel_id}) {
234 $channel = "#".$self->discord_channels->{$channel_id};
237 say localtime(time) . " - $channel_id $channel $author_name $author_id";
239 my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
242 return 0 unless $self->disp->has_command($command,$channel);
244 say localtime(time) . " - $msg";
246 my $c = NDIRC::DiscordContext->new({
247 discord_id => $author_name,
248 channel_id => $channel_id,
252 discord => $self->discord,
255 return $self->disp->run_command($c,$command,$args);