]> ruin.nu Git - NDIRC.git/commitdiff
Add the nosmoke command
authorMichael Andreen <harv@ruin.nu>
Sat, 29 Aug 2009 11:49:58 +0000 (13:49 +0200)
committerMichael Andreen <harv@ruin.nu>
Sat, 29 Aug 2009 11:49:58 +0000 (13:49 +0200)
Commands/Misc.pm [new file with mode: 0644]
database/nosmoke.sql [new file with mode: 0644]

diff --git a/Commands/Misc.pm b/Commands/Misc.pm
new file mode 100644 (file)
index 0000000..a576f74
--- /dev/null
@@ -0,0 +1,60 @@
+#**************************************************************************
+#   Copyright (C) 2008 by Michael Andreen <harvATruinDOTnu>               *
+#                                                                         *
+#   This program is free software; you can redistribute it and/or modify  *
+#   it under the terms of the GNU General Public License as published by  *
+#   the Free Software Foundation; either version 2 of the License, or     *
+#   (at your option) any later version.                                   *
+#                                                                         *
+#   This program is distributed in the hope that it will be useful,       *
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+#   GNU General Public License for more details.                          *
+#                                                                         *
+#   You should have received a copy of the GNU General Public License     *
+#   along with this program; if not, write to the                         *
+#   Free Software Foundation, Inc.,                                       *
+#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+#**************************************************************************/
+
+package NDIRC::Commands::Misc;
+
+use strict;
+use warnings;
+
+use Moose;
+use MooseX::MethodAttributes;
+
+sub nosmoke
+       : Help(.nosmoke nick | shows how long the person has been smoke free)
+{
+       my ($self, $c, $msg) = @_;
+       my ($nick) = $msg =~ /^(\w+)?/;
+       my $dbh = $c->model;
+
+       my $time = $dbh->selectrow_hashref(q{
+SELECT date_part('day',time) AS days, date_part('hour',time) AS hours, date_part('minute',time) AS mins
+FROM (SELECT now() - time AS time FROM last_smokes WHERE nick = $1) l
+               },undef,$nick);
+       if ($time){
+               my $weeks = int($time->{days} / 7);
+               if ($weeks){
+                       $weeks = "$weeks weeks";
+               }else{
+                       $weeks = "";
+               }
+               my $days = $time->{days} % 7;
+               $c->reply("$nick has been smoke free for: $weeks $days days $time->{hours} hours and $time->{mins} minutes");
+       }else{
+               my $nonsmokers = $dbh->prepare(q{SELECT nick FROM last_smokes ORDER BY nick});
+               $nonsmokers->execute;
+               my @nonsmokers;
+               while (my $nonsmoker = $nonsmokers->fetchrow_hashref){
+                       push @nonsmokers,$nonsmoker->{nick};
+               }
+               $c->reply("I keep track of the following ppl: @nonsmokers");
+       }
+}
+
+1;
+
diff --git a/database/nosmoke.sql b/database/nosmoke.sql
new file mode 100644 (file)
index 0000000..42c3cc0
--- /dev/null
@@ -0,0 +1,6 @@
+CREATE TABLE last_smokes (
+       nick CITEXT PRIMARY KEY,
+       time TIMESTAMPTZ NOT NULL
+);
+
+INSERT INTO last_smokes (nick,time) (SELECT SUBSTR(id,6),value::timestamptz from misc WHERE id like 'SMOKE%');