#!/usr/bin/perl -W

$ploticus_chron = 'ploticus png -prefab chron data=- x=1 y=2 xgrid=color=0.8 ygrid=color=0.8 datefmt=yyyymmdd mode=line gapmissing=yes yrange=0 -font Arial -diagfile /dev/null';
$bar_width = 600;

################################################################################

use Time::Local;

%capacity_day = ();
%players_day = ();
%capacity_hour = ();
%players_hour = ();
%capacity_10m = ();
%players_10m = ();

%map = ();
%game = ();
%map7 = ();
%game7 = ();

$grand_total = 0;
$grand_total7 = 0;
$now = time;

while ($line = <>) {
	chomp $line;
	($datetime, $game, $mission, $map, $maxplayers, $players) = split /\t/, $line;
	next if $map eq '';
	$datetime =~ m|(....)(..)(..)/(..)(..)| or die 'match failed';
	$year = $1;
	$month = $2;
	$day = $3;
	$date = "$year$month$day";
	$hour = $4;
	$min = $5;
	$time = timegm(0, $min, $hour, $day, $month - 1, $year);
#	$gmm = "$game: $mission: $map";
	$gmm = $map;
	$gm = "$game: $mission";
	$players = 0 if ($players < 0);
	$maxplayers = 0 if ($maxplayers < 0);
	$grand_total += $players;

	# all time stats
	if (exists $players_day{$date}) {
		$players_day{$date} += $players;
		$capacity_day{$date} += $maxplayers;
	} else {
		$players_day{$date} = $players;
		$capacity_day{$date} = $maxplayers;
	}

	if ($players != 0) {
		if (exists $map{$gmm}) {
			$map{$gmm} += $players;
		} else {
			$map{$gmm} = $players;
		}
		if (exists $game{$gm}) {
			$game{$gm} += $players;
		} else {
			$game{$gm} = $players;
		}
	}

	# last 7 days
	next unless ($now - 7 * 24 * 60 * 60 < $time);

	$grand_total7 += $players;

	if (exists $players_hour{"$date.$hour"}) {
		$players_hour{"$date.$hour"} += $players;
		$capacity_hour{"$date.$hour"} += $maxplayers;
	} else {
		$players_hour{"$date.$hour"} = $players;
		$capacity_hour{"$date.$hour"} = $maxplayers;
	}

	if ($players != 0) {
		if (exists $map7{$gmm}) {
			$map7{$gmm} += $players;
		} else {
			$map7{$gmm} = $players;
		}
		if (exists $game7{$gm}) {
			$game7{$gm} += $players;
		} else {
			$game7{$gm} = $players;
		}
	}

	# last 24 hours
	next unless ($now - 24 * 60 * 60 < $time);
	
	if (exists $players_10m{"$date.$hour:$min"}) {
		$players_10m{"$date.$hour:$min"} += $players;
		$capacity_10m{"$date.$hour:$min"} += $maxplayers;
	} else {
		$players_10m{"$date.$hour:$min"} = $players;
		$capacity_10m{"$date.$hour:$min"} = $maxplayers;
	}

}

################################################################################

$now = gmtime;

open INDEX, '>', '../index.html';

print INDEX <<END;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Tribes 2 Europe Statistics</title>
<style type='text/css'>
body { background-color: white; margin: 3em; font-family: sans-serif; }
p { text-align: center; }
address { text-align: right; }
h1, h2 { background-color: #ccffcc; padding: 0.5em; }
p.bar0, p.bar1 { text-align: left; margin-top: 0; margin-bottom: 0; font-size: 75%; }
p.bar1 { background-color: #eee; }
</style>
</head>

<body>
<h1>Tribes 2 Europe Statistics</h1>
<p>This page updated every hour, last update $now UTC. All times in UTC.
Data collected every 10 minutes by <a href='http://www.qstat.org/'>QStat</a>,
processed by a <a href='scripts/'>script</a>, and graphs plotted
with <a href='http://ploticus.sourceforge.net/'>ploticus</a>.</p>
<address>- <a href='mailto:t2stats\@strcprstskrzkrk.co.uk'>James Bursa</a></address>

END

################################################################################

open PLOTA1, "|$ploticus_chron xinc='14 days' stubfmt='Mmmdd' -o ../images/players_full.png";
open PLOTA2, "|$ploticus_chron xinc='14 days' stubfmt='Mmmdd' -o ../images/capacity_full.png";
open PLOTA3, "|$ploticus_chron xinc='14 days' stubfmt='Mmmdd' -o ../images/usage_full.png";

foreach $date (sort keys %players_day) {
	$meanplayer = $players_day{$date} / (6.0 * 24.0);
	$meancapacity = $capacity_day{$date} / (6.0 * 24.0);
	$usage = $meanplayer / $meancapacity * 100.0;
	print PLOTA1 "$date\t$meanplayer\n";
	print PLOTA2 "$date\t$meancapacity\n";
	print PLOTA3 "$date\t$usage\n";
}

close PLOTA1; close PLOTA2; close PLOTA3;

open PLOTB1, "|$ploticus_chron unittype=datetime xinc='1 day' stubfmt='Www' -o ../images/players_week.png";
open PLOTB2, "|$ploticus_chron unittype=datetime xinc='1 day' stubfmt='Www' -o ../images/capacity_week.png";
open PLOTB3, "|$ploticus_chron unittype=datetime xinc='1 day' stubfmt='Www' -o ../images/usage_week.png";

foreach $date (sort keys %players_hour) {
	$meanplayer = $players_hour{$date} / 6.0;
	$meancapacity = $capacity_hour{$date} / 6.0;
	$usage = $meanplayer / $meancapacity * 100.0;
	print PLOTB1 "$date:00\t$meanplayer\n";
	print PLOTB2 "$date:00\t$meancapacity\n";
	print PLOTB3 "$date:00\t$usage\n";
}

close PLOTB1; close PLOTB2; close PLOTB3;

open PLOTC1, "|$ploticus_chron unittype=datetime xinc='2 hours' stubfmt='hh' -o ../images/players_day.png";
open PLOTC2, "|$ploticus_chron unittype=datetime xinc='2 hours' stubfmt='hh' -o ../images/capacity_day.png";
open PLOTC3, "|$ploticus_chron unittype=datetime xinc='2 hours' stubfmt='hh' -o ../images/usage_day.png";

foreach $date (sort keys %players_10m) {
	$meanplayer = $players_10m{$date};
	$meancapacity = $capacity_10m{$date};
	$usage = $meanplayer / $meancapacity * 100.0;
	print PLOTC1 "$date\t$meanplayer\n";
	print PLOTC2 "$date\t$meancapacity\n";
	print PLOTC3 "$date\t$usage\n";
}

close PLOTC1; close PLOTC2; close PLOTC3;

print INDEX <<END;
<h2>Average players online</h2>
<p>All available data, averaged over each day:<br />
<img src='images/players_full.png' alt='Graph' /></p>
<p>Last 7 days, averaged over each hour:<br />
<img src='images/players_week.png' alt='Graph' /></p>
<p>Last 24 hours, sampled every 10 minutes:<br />
<img src='images/players_day.png' alt='Graph' /></p>

<h2>Average server places available</h2>
<p><img src='images/capacity_full.png' alt='Graph' /></p>
<p><img src='images/capacity_week.png' alt='Graph' /></p>
<p><img src='images/capacity_day.png' alt='Graph' /></p>

<h2>Percentage server places used</h2>
<p><img src='images/usage_full.png' alt='Graph' /></p>
<p><img src='images/usage_week.png' alt='Graph' /></p>
<p><img src='images/usage_day.png' alt='Graph' /></p>

END

################################################################################

$game_types = keys %game;

print INDEX <<END;
<h2>Game type popularity</h2>
<p><img src='images/bar.png' width='10' height='10' alt='First value' />&nbsp;all available data,
<img src='images/bar7.png' width='10' height='10' alt='second value' />&nbsp;last 7 days.
$game_types game types.</p>
END

$i = 0;
foreach $gm (sort { $game{$b} <=> $game{$a} } keys %game) {
	$players = $game{$gm};
	$players7 = exists $game7{$gm} ? $game7{$gm} : 0;
	$propn = $players / $grand_total;
	$propn7 = $players7 / $grand_total7;
	$max = $propn if $i == 0;
	$width = int($propn / $max * $bar_width);
	$width7 = int($propn7 / $max * $bar_width);
	$pct = sprintf "%.3g", $propn * 100;
	$pct7 = sprintf "%.3g", $propn7 * 100;
	$class = $i % 2;
	print INDEX <<END;
	<p class='bar$class'>$gm<br />
	<img src='images/bar.png' width='$width' height='10' alt='' /> $pct%<br />
	<img src='images/bar7.png' width='$width7' height='10' alt='' /> $pct7%</p>
END
	$i++;
}

################################################################################

$maps = keys %map;

print INDEX <<END;
<h2>Map popularity</h2>
<p><img src='images/bar.png' width='10' height='10' alt='First value' />&nbsp;all available data,
<img src='images/bar7.png' width='10' height='10' alt='second value' />&nbsp;last 7 days.
$maps maps.
Follow the link to search for a map on <a href='http://www.tribes2maps.com/'>Tribes2Maps</a>.</p>
END

$i = 0;
foreach $gmm (sort { $map{$b} <=> $map{$a} } keys %map) {
	$players = $map{$gmm};
	$players7 = exists $map7{$gmm} ? $map7{$gmm} : 0;
	$propn = $players / $grand_total;
	$propn7 = $players7 / $grand_total7;
	$max = $propn if $i == 0;
	$width = int($propn / $max * $bar_width);
	$width7 = int($propn7 / $max * $bar_width);
	$pct = sprintf "%.3g", $propn * 100;
	$pct7 = sprintf "%.3g", $propn7 * 100;
	$class = $i % 2;
	$search = $gmm;
	$search =~ s/([^\w()'*~!.-])/sprintf '%%%02x', ord $1/eg;
	$search =~ s/^Euro2_//;
	print INDEX <<END;
	<p class='bar$class'><a href="http://www.tribes2maps.com/cgi-bin/map_search.cgi?names_only=1;search=$search">$gmm</a><br />
	<img src='images/bar.png' width='$width' height='10' alt='' /> $pct%<br />
	<img src='images/bar7.png' width='$width7' height='10' alt='' /> $pct7%</p>
END
	$i++;
}

################################################################################

print INDEX <<END;
</body>
</html>
END

close INDEX;

################################################################################

