]> ruin.nu Git - NDIRC.git/blob - Eos.pm
Need to send a real value to execute when the role isn't available
[NDIRC.git] / Eos.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::Eos;
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 my ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
32 my $last_announcement = 0;
33
34 after irc_public => sub {
35         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
36         my ($nick,$username,$address) = ( split /[!@]/, $who );
37         my $channel = $where->[0];
38
39         my $irc = $sender->get_heap();
40
41         if ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
42                 my $_ = $1;
43                 my $text = $2;
44                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
45                         $msg = ".anon $_ $text";
46                 }
47
48         }
49         if ($self->parseCommand($msg,$irc,$nick,$address,$channel,DB())){
50                 #Command parsed and run successfully
51         }
52 };
53
54
55 after irc_msg => sub {
56         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
57         my ($nick,$username,$address) = ( split /[!@]/, $who );
58         my $irc = $sender->get_heap();
59
60         if ($self->parseCommand($msg,$irc,$nick,$address,'pm',DB())){
61                 #Command parsed and run successfully
62         }else{
63                 my $disp = $self->disp;
64                 $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 $nick >> $msg");
65                 $self->parseCommand("~report_incs $msg",$irc,$nick,' BATCH ',$disp->targets->{def},DB());
66         }
67 };
68
69 sub irc_join {
70         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
71         my ($nick,$username,$address) = ( split /[!@]/, $who );
72         my $irc = $sender->get_heap();
73         my $disp = $self->disp;
74
75         my $dbh = DB();
76
77         if (lc $channel ~~ lc $disp->targets->{members}){
78                 if (time - $last_announcement < 1){
79                         $last_announcement = time;
80                         return;
81                 }
82                 my $user = $dbh->selectrow_hashref(q{
83 SELECT uid,pid,hostmask,password
84         ,(SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid) AS last_forum_visit
85 FROM users u WHERE hostmask = ?
86                 }, undef, $address);
87                 if ($user){
88                         if ($user->{password} eq ''){
89                                 $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");
90                         }
91                         if ($tick > 36 && not defined $user->{pid}){
92                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords.");
93                         }
94
95                         if (not defined $user->{last_forum_visit}){
96                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: Go read the forum! https://nd.ruin.nu/forum");
97                         }else {
98                                 my $unread = $dbh->selectrow_hashref(q{SELECT * FROM unread_posts($1)},undef,$user->{uid});
99                                 if ($unread && $unread->{new}){
100                                         $irc->yield(notice => $nick, "$unread->{new} posts since your last forum visit ($unread->{unread} unread posts in total) https://nd.ruin.nu/forum/allUnread");
101                                 }
102                         }
103                 }
104                 if (time - $last_announcement < 2){
105                         $last_announcement = time;
106                         return;
107                 }
108                 $last_announcement = time;
109         }
110 }
111
112 sub refresh {
113         my ($self,$kernel,$heap) = @_[OBJECT,KERNEL,HEAP];
114         $kernel->delay( refresh => 60 );
115         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
116
117         my $irc = $heap->{irc};
118         my $disp = $self->disp;
119         my $dbh = DB();
120
121         my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
122         if ($tick != $row[0]){
123                 $tick = $row[0];
124                 $irc->yield(privmsg => $disp->targets->{def}, "New tick: $tick");
125         }
126         if (defined $row[1] && $stattick != $row[1]){
127                 $stattick = $row[1];
128                 $irc->yield(privmsg => $disp->targets->{members}, "New tick: $stattick");
129         }
130         my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
131         my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
132         $ircreqs->execute;
133         while (my $req = $ircreqs->fetchrow_hashref){
134                 if ($req->{channel} eq 'def'){
135                         $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}");
136                         $self->parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$dbh);
137                 }elsif(exists $disp->targets->{$req->{channel}}){
138                         $irc->yield(privmsg => $disp->targets->{$req->{channel}}, "<$req->{username} via webbie> $req->{message}");
139                 }
140                 $upircreq->execute($req->{id});
141         }
142         my $defmissions = $dbh->prepare(q{
143 SELECT username,call,tick,dm.fleet,p.value
144         ,100 * SUM(fs.amount * (metal + crystal + eonium) / 100) / p.value AS value
145 FROM users u
146         JOIN current_planet_stats p USING (pid)
147         JOIN launch_confirmations USING (uid)
148         JOIN fleets f USING (fid)
149         JOIN fleet_ships fs USING (fid)
150         JOIN defense_missions dm ON f.fid = dm.fleet
151         JOIN ship_stats ss ON fs.ship = ss.name
152 WHERE NOT dm.announced
153 GROUP BY username,call,tick,dm.fleet,p.value
154                 });
155         my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?});
156         $defmissions->execute();
157         while (my $mission = $defmissions->fetchrow_hashref){
158                 $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}");
159                 $updefmis->execute($mission->{fleet});
160         }
161 }
162
163 1;