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