#!/usr/bin/perl

# Display a line indicating the number of games with active and 
# inactive players.
# Bruno Wolff III
# Last revised October 31, 2012

use Pg;

print "content-type: text/html; charset=UTF-8\n\n";

# First try to connect
$conn = Pg::connectdb('dbname=area');
if ($conn->status != PGRES_CONNECTION_OK) {
  print "unknown with active players and unknown with only recently inactive players.\n";
  exit;
}

# Don't need to serialize since only one query is done here.

$result = $conn->exec("select count(1) from (select distinct on (gameid) touched from crate where frq > 0 order by gameid desc, touched desc) as current where touched >= localtimestamp + '12 year ago' group by touched >= localtimestamp + '4 year ago' order by touched >= localtimestamp + '4 year ago' desc");
if ($result->resultStatus != PGRES_TUPLES_OK) {
  print "unknown games with active players and unknown games with only recently inactive players.\n";
  exit;
}
if ($result->ntuples < 2) {
  print "unknown games with active players and unknown games with only recently inactive players.\n";
  exit;
}
@row = $result->fetchrow;
$active = $row[0];
@row = $result->fetchrow;
$inactive = $row[0];
print "$active games with active players and $inactive games with only recently inactive players.\n";
