#!/usr/bin/perl ## ### allstars.pl -- prettyprint the allstars table for MoxQuizz ## ### Author: Moxon (AKA Sascha Lüdecke) ## ### Copyright (C) 2000 Moxon AKA Sascha Lüdecke ## ## 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., 675 Mass Ave, Cambridge, MA 02139, USA. ## ### # $Id: allstars.pl,v 1.12 2002/02/22 10:20:34 chat Exp $ my $allstarsfile = 'rankallstars.data'; my $format = "TXT"; my $monthly = 0; my $currentmonthonly = 0; my $monthlybase = 'allstars'; while ($_ = shift @ARGV) { # options first /--file/ && do { $allstarsfile = shift @ARGV; print STDERR "Reading from $allstarsfile\n"; next }; /--html/ && do { $format = "HTML"; next }; /--xml/ && do { $format = "XML"; next }; /--text/ && do { $format = "TXT"; next }; /--monthly-prefix/ && do { $prefix = shift @ARGV; $monthlybase = $prefix . "." . $monthlybase; next }; /--monthly/ && do { $monthly = 1; next }; /--current-month-only/ && do { $currentmonthonly = 1; next }; # commands last /--list/ && do { list_entries(); exit 0 }; /--table/ && do { calc_table(); exit 0 }; /--help/ && do { print_help(); exit 0 }; print STDERR "Unknown parameter: \"$_\"\n"; } print STDERR "No command found, use --help to get help.\n"; exit 1; ########################################################################### # # list all entries # ########################################################################### ## SAMPLE ## ## # this file records all won games. Format is: ## # time, duration, num of questions, winscore, nick, hostmask ## 964263720 371 : 41 15 -- MindMaster *!whistle@*.blowup.net ## sub list_entries () { if (open FILE, "$allstarsfile") { while () { chomp; # skip comments if ($_ !~ /^\#.*/) { $_ =~ /(\d+) (\d+) : (\d+) (\d+) -- ([^ ]+) ([^ ]+)/; print localtime($1) . " $1 $2\t$3\t$4 $5 $6\n"; } } close FILE; } else { die "Could not open $allstarsfile."; } } ########################################################################### # # calculate table(s in case of monthly tables) # ########################################################################### sub calc_table () { local %games, %score, %durations; local $lastmonth = ""; $| = 1; # read the file if (open FILE, "$allstarsfile") { while () { chomp; if ($_ !~ /^\#.*/ && $_ !~ /^\s*$/ ) { $_ =~ /(\d+) (\d+) : (\d+) (\d+) -- ([^ ]+) ([^ ]+)/; ($when, $duration, $sumscore, $score, $name, $host) = ($1, $2, $3, $4, $5, $6); if ($lastmonth eq "") { $lastmonth = getYearMonth($when); } if ($monthly && getYearMonth($when) ne $lastmonth) { if (!$currentmonthonly) { print_table(); } else { print STDERR "Skipping $lastmonth.\n" } $lastmonth = getYearMonth($when); undef %games; undef %durations; undef %score; } $games{$name} += 1; $durations{$name} += $duration; $score{$name} += 10 * $sumscore / ( log10($score) * $duration ); } } close FILE; print_table(); } else { die "Could not open $allstarsfile."; } } ########################################################################### # # print a table -- assumes %games, %score and %duration to be filled # ########################################################################### sub print_table () { if ($monthly) { print STDERR "Writing month: $monthlybase.$lastmonth\n"; open (STDOUT, ">$monthlybase.$lastmonth"); } # set page length so show everything on a single page $= = 1_000_000; $- = 0; # set format $^ = $format . "HEAD"; $~ = $format . "BODY"; # show the table $pos = 1; foreach $n (sort {$score{$b} <=> $score{$a}} keys %games) { write; $pos += 1; $games{"Summary"} += $games{$n}; $durations{"Summary"} += $durations{$n}; $score{"Summary"} += $score{$n}; } # show some summaries $~ = $format . "SUMS"; $pos = ""; $n = "Summary"; write; # print footer $~ = $format . "FOOT"; write; if ($monthly) { close (STDOUT); } } ########################################################################### # # print a help text # ########################################################################### sub print_help () { print <<"HELP"; $0 -- prettyprint the allstars table from MoxQuizz Usage: $0 [parameter] command Commands: --list Just print all entries with human understandable dates. --table Prettyprint the table. --help Print this text. Paramter: --file Use instead of \'$allstarsfile\' for input. --monthly Calculate allstars per month and write to files like $monthlybase.2001-01 --monthly-prefix Prepend to above filenames Output formats in case of --table: --html Generated HTML output --xml Generate XML output --text Generate Text output (this is the default) Example: allstars.pl --file rankallstars --monthly --monthly-prefix en --xml --table Generates XML allstars table from file 'rankallstars' on a monthly base into files named alike en.$monthlybase.2001-01 HELP } ########################################################################### # # help routines like log10 and getMonth # ########################################################################### sub log10 ($) { my $n = shift; return log($n)/log(10); } sub getYearMonth($) { my $secs = shift; ($_, $_, $_, $_, $month, $year, $_, $_, $_) = localtime $secs; return sprintf("%d-%02d", 1900 + $year, $month + 1); } ########################################################################### # # output formats # # To get a new format, give it a tag (e.g. XML) and write formats named # HEAD, BODY, SUMS, FOOT # ########################################################################### ########################################################################### # TXT ########################################################################### format TXTHEAD = Allstars table -------------- Rank | Name | Games | Score | Avg score | Avg duration -----+------------------+-------+---------+-----------+------------- . format TXTBODY = @>>> | @<<<<<<<<<<<<<<< | @#### | @##.### | @##.### | @##.### $pos, $n, $games{$n}, $score{$n}, $score{$n}/$games{$n}, $durations{$n}/$games{$n} . format TXTSUMS = -----+------------------+-------+---------+-----------+------------- @>>> | @<<<<<<<<<<<<<<< | @#### | @##.### | @##.### | @##.### $pos, $n, $games{$n}, $score{$n}, $score{$n}/$games{$n}, $durations{$n}/$games{$n} . format TXTFOOT = . ########################################################################### # HTML ########################################################################### format HTMLHEAD =
. format HTMLBODY = $pos, $n, $games{$n}, $score{$n}, $score{$n}/$games{$n}, $durations{$n}/$games{$n} . format HTMLSUMS = $pos, $n, $games{$n}, $score{$n}, $score{$n}/$games{$n}, $durations{$n}/$games{$n} . format HTMLFOOT =
Rank  Name Games   Score  Avg score   Avg duration 
@>>> @<<<<<<<<<<<<<<<<<<<< @###   @##.###   @##.### @##.###
@>>> @<<<<<<<<<<<<<<<<<<<< @###   @##.###   @##.### @##.###
This table was created on @<<<<<<<<<<<<<<<<<<<<<<< .
"" . localtime(time) . ########################################################################### # XML ########################################################################### format XMLHEAD = . format XMLBODY = @>>>> $pos @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $n @##### $games{$n} @###.### $score{$n} @###.### $score{$n}/$games{$n} @###.### $durations{$n}/$games{$n} . format XMLSUMS = @>>>> $pos @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $n @##### $games{$n} @###.#### $score{$n} @###.#### $score{$n}/$games{$n} @###.#### $durations{$n}/$games{$n} . format XMLFOOT = @>>>>>>>>>>>>>>>>>>>>>>>>>>> "" . localtime(time) .