#! /usr/bin/perl # Copyright (C) 2005 Nick Urbanik # 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. use warnings; use strict; use DBI; use Tk; use constant DBNAME => "games"; use constant DBHOST => "nicku.org"; use constant USER => "linus"; use constant PW => "123"; use constant DBNAME => "games"; use constant DBHOST => "nicku.org"; use Sys::Hostname; our $dbhost = hostname eq DBHOST ? "localhost" : DBHOST; our $dbh = DBI->connect( "dbi:Pg:dbname=@{[DBNAME]};host=$dbhost", USER, PW, {AutoCommit => 0} ) or die "Unable to connect to $dbhost: $!"; my $username = shift || getpwuid $>; my $statement = " SELECT groupname FROM users WHERE username = '$username' "; my $sth = $dbh->prepare( $statement ); $sth->execute or die "Unable to execute $statement"; my @groups; while ( my ( $group ) = $sth->fetchrow_array ) { push @groups, $group; } sub today() { my ( $mday, $mon, $year ) = ( localtime )[ 3, 4, 5 ]; return sprintf "%4d-%02d-%02d", $year + 1900, $mon + 1, $mday; } sub get_timeleft($\@) { my ( $dbh, $groups ) = @_; my $text = "Time Left for Games:"; foreach my $group ( @$groups ) { $statement = " SELECT date, limit_seconds, group_playing_time FROM times NATURAL JOIN limits WHERE groupname = '$group' "; my $sth = $dbh->prepare( $statement ); $sth->execute or die "Unable to execute $statement"; while ( my ( $date, $limit, $playtime ) = $sth->fetchrow_array ) { my $timeleft = $limit; $timeleft = $limit - $playtime if $date eq today; $timeleft = 0 if $timeleft < 0; my $mins = $timeleft / 60; my $sec = $timeleft % 60; $timeleft = sprintf "%02d:%02d", $mins, $sec; $text .= "\n$group: $timeleft"; } } print $text . "\n"; return $text; } sub cb_func($) { my $button = shift; $$button->configure( -text => get_timeleft $dbh, @groups ); } my $main = MainWindow->new; my $button; $button = $main->Button( -text => get_timeleft( $dbh, @groups ), -command => [ \&cb_func, \$button ] ); $button->pack; MainLoop;