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