File:  [Local Repository] / chik / chik.cgi
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Nov 1 04:06:49 2000 UTC (23 years, 6 months ago) by boris
Branches: MAIN
CVS tags: HEAD
Wrote a working version.

#!/usr/bin/perl

=head1 NAME

chik.cgi - a robot for competitions with separate starts

=head1 DESCRIPTION

The robot is a Web CGI interface. It must authentificate
the team. If the regnumber and password are corrrect, it sends
a message to the team's email and demonstrates the questions on
the web. Also, it updates the field "StartTime" and sends message
to the secretary "Team NN started at I<Date>"

=head1 USES

MySQL table Chik with the fields:
   CREATE TABLE Chik (RegNum Int NOT NULL, 
                      Name TINYTEXT, 
                      Password TINYTEXT,
                      Email TINYTEXT, 
                      Started ENUM('Y','N') DEFAULT 'N', 
                      StartDate TIMESTAMP(14), Unique(RegNum));

It expects to find the file F<../../cfg/chik/questions.cfg>, with 
the questions, <F<../../cfg/chik/h.cfg> with the headers and
F<../../cfg/chik/f.cfg> with the footers.


=head1 AUTHOR

Boris Veytsman

=head1 DATE

$Date: 2000/11/01 04:06:49 $

=head1 VERSION

$Revision: 1.2 $

=cut

use strict;
use CGI qw/:standard/;
use DBI;

my $secretary="elir\@immisrael.com";
my $SENDMAIL = "/usr/sbin/sendmail";
my $questions = "../../cfg/chik/q.txt";
my $header = "../../cfg/chik/h.html";
my $footer = "../../cfg/f.html";

my $query=new CGI;

print $query->header;
print $query->start_html(-title=>'Robot turnira CHIK',-bgcolor=>'#fff0e0');
print Include_virtual("../dimrub/db/reklama.html");
print $query->h1({'-align'=>'center'},'Робот турнира ЧИК');

open(HEADER,$header);
print <HEADER>;
close HEADER;

print "<p> Сейчас на куличках ", `date`," </p>\n";

if ($query->param('Look')) {
    print_list($query);
    $query->delete('Look');
} elsif ($query->param('Start')) {
    $query->delete('Start');
    print_questions($query);
}

print_query($query);

open(FOOTER,$footer);
print <FOOTER>;
close FOOTER;


print $query->end_html;


exit 0;


sub Include_virtual {
    my ($fn, $output) = (@_, '');

    open F , $fn
	or return; #die "Can't open the file $fn: $!\n";
	
    while (<F>) {
	if (/<!--#include/o) {
	    s/<!--#include virtual="\/(.*)" -->/&Include_virtual($1)/e;
	}
	if (/<!--#exec/o) {
	    s/<!--#exec.*cmd\s*=\s*"([^"]*)".*-->/`$1`/e;
	}
	$output .= $_;
    }
    return $output;
}



sub print_query {
    my	$query = shift;
    print $query->start_form;
    print $query->h2("Начать игру");
    print "<p>\n";
    print "Рег. номер: ", 
    $query->textfield('regnum'), " ",
    "Пароль: ",$query->password_field('password'), "</p>";
    print "<p>", $query->submit("Start","Start"), " ";
    print $query->defaults('Reset'),"</p>\n";
    print $query->h2("Посмотреть, кто играет");
    print "<p>", $query->submit("Look","Look"), "</p>";
    print $query->end_form;
}



sub print_list {
    my $query = shift;

    my $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")
	or do {
	    print h1("Временные проблемы") . "Робот временно не
			работает. Заходите попозже.";
	    return 0;
	};
    my $sth=$dbh->prepare("SELECT RegNum, Name, StartDate FROM
                           Chik WHERE Started='Y' Order by StartDate");
    $sth->execute;
    print h3("Начали игру:");
    print "<table>\n";
    print "<tr><th>Номер</th><th>Команда</th><th>Начало игры</th></tr>\n";
    while (my @line=$sth->fetchrow) {
	my $date=pop @line;
	$date=convert_date($date);
	push @line,$date;
	print_line(@line);
    }
    print "</table>\n";
    $sth=$dbh->prepare("SELECT RegNum, Name FROM
                              Chik WHERE Started='N' Order by RegNum");
    $sth->execute;
    print h3("Не начали игру:");
    print "<table>\n";
    print "<tr><th>Номер</th><th>Команда</th></tr>\n";
    while (my @line=$sth->fetchrow) {
	print_line(@line);
    }
    print "</table>\n";
    
    $dbh->disconnect;
    return 0;
    
}


sub print_line {
    print "<tr><td>",join("</td><td>",@_),"</td></tr>\n";
}
    
     
sub print_questions {
    my $query = shift;
    my $regnum=$query->param('regnum');
    my $password=$query->param('password');
    my $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")
	or do {
	    print h1("Временные проблемы") . "Робот временно не
			работает. Заходите попозже.";
	    return 1;
	};
    $regnum=$dbh->quote($regnum);
    $password=$dbh->quote($password);
    my $sth=$dbh->prepare("SELECT Name, Email, Started, StartDate FROM
                           Chik WHERE RegNum=$regnum 
                           AND Password=$password");
    $sth->execute;

    my @result=$sth->fetchrow;
    if (!scalar @result) {
	print "<p><strong>Пароль неверен. Попробуйте еще раз.</p>\n";
	return 1;
    }
    my ($name,$email,$started,$startdate)=@result;
    $startdate=convert_date($startdate);
    if ($started eq 'Y') {
	print "<p>Ваша команда уже начала играть в $startdate</p>\n";
    } else {
	$sth=$dbh->prepare("UPDATE  Chik SET Started='Y'
                       WHERE RegNum=$regnum");
	$sth->execute;
	open(F, "| $SENDMAIL -t -n" );
	print F <<"EOT";
To: $secretary
From: boris\@plmsc.psu.edu
Subject: Chik: komanda nachala igrat
MIME-Version: 1.0
Content-type: text/plain; charset="koi8-r"

EOT
        print F "Команда $name, регистрационный номер $regnum ",
    "начала играть в $startdate\n";
	print F "\n\n--\nРобот\n";
	close F;
	open(F, "| $SENDMAIL -t -n" );
	print F <<"EOT";
To: $email
From: boris\@plmsc.psu.edu
Subject: Voprosy CHIK
MIME-Version: 1.0
Content-type: text/plain; charset="koi8-r"

-------------------------------------------------------------
EOT

        open(QUEST,$questions);
	
	print F <QUEST>;
	print F <<"EOT";

-------------------------------------------------------------
EOT
	print F "\n\n--\nРобот\n";
	close F;
	close (QUEST);
    }
    open(QUEST,$questions);
    print "<hr>\n<pre>\n";
    print <QUEST>;
    print "</pre>\n<hr>\n";
    return 0;
}

sub convert_date {
    my $date=shift;
    my $year=substr($date,0,4);
    my $month=substr($date,4,2);
    my $day=substr($date,6,2);
    my $hour=substr($date,8,2);
    my $min=substr($date,10,2);
    my $sec=substr($date,12,2);
    return "$year-$month-$day $hour:$min:$sec";
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>