#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use POSIX qw(floor);

###########################################
#
#	Partner to ex.cgi - manages high
#	scores set by users
#
#	Last-Modified: Nov. 24, 2003 - 15:53 EST
#
###########################################

print "Content-Type: text/html\n\n";

## DEBUGGING SWITCH
my $DEBUG = 0;

## Get info from query
my $reason = param('why') || "lose";		print "reason = $reason\n" if $DEBUG;
my $new_name = param('name') || "undefined";	print "new_name = $new_name\n" if $DEBUG;
my $moves_left = param('moves') || "0";		print "moves_left = $moves_left\n" if $DEBUG;
my $play_time = param('time') || "250";		print "play_time = $play_time\n" if $DEBUG;

## Make sure $new_name is only alphabet based (a-z, A-Z, 0-9)
if ($new_name eq "undefined" || $new_name eq "") {
	$new_name = "Guest" . int(rand(1000));
} else {
	$new_name =~ s/\W//g;
}
my $name_length = length($new_name);
if ($name_length > 15) {
	$new_name = substr($new_name, 0, 15);
}

## Calculate bonus time (worth points)
my $bonus_max = 600;	# no bonus after 600 seconds (10 mins)
my $bonus_left = $bonus_max - $play_time;
if ($bonus_left < 0) { $bonus_left = 0; }	print "bonus_left = $bonus_left\n" if $DEBUG;

## Calculate score
my $score = 0;
foreach my $add_move_bonus (0..$moves_left) {
	$score += (1425 + (int(rand(12))+1*int(rand(17))+1));	#426-647 bonus/move left
}
foreach my $add_time_bonus (0..$bonus_left) {
	$score += (117 + int(rand(23)));			#17-40 bonus/second left
}

## add new_score to list, then resort and pull off the lowest score
my $list = "logs/highs.log";
my $new_entry = $score . ":" . $new_name;
my @pairs;
my @highs;

# get current high scores
open(LIST, "$list");
my $str = <LIST>;
close LIST;

## Separate the score:name pairs into array elements
@pairs = split(/\^/, $str);
push @pairs, $new_entry if ($reason eq "win");	# add new score to list
my @sorted = sort{$b <=> $a} @pairs;		# sort list
pop (@sorted) if ($reason eq "win");		# drop lowest score

if ($reason eq "win") {
	## clear current highs.log
	open(TMP, "+>$list");
	close TMP;

	## write new high score list
	my $new_str = join('^', @sorted);
	$new_str =~ s/\n//;
	open(NEW, "+>>$list");
	print NEW "$new_str";
	close NEW;
}

print << "END_HEAD";

<html>
<head>
<title>High Scores</title>
</head>
<body>
<h1>Top 25 Scores:</h1>
END_HEAD

if ($reason eq "win") {
	print "<h3>Your score, if it made the top 25, is bold</h3>\n";
} else {
	print "<h3>You have been lost to the Ex-Maze... better luck next time!</h3>\n";
}

print << "END_NAV";
<hr>
<div align="center">
Where would you like to go?<br>
<a href="http://cs.marlboro.edu/~eholder/index.html">HOME</a>
 || 
<a href="http://cs.marlboro.edu/~eholder/cgi/ex/ex.cgi">EX-MAZE</a>
 || 
<a href="http://cs.marlboro.edu/~eholder/cgi/ex/ex-pod.html">DOCUMENTATION</a>
 || 
<a href="http://cs.marlboro.edu/~eholder/cgi/ex/ex.cgi_html">CODE</a>
<hr>
<table cellpadding=0 cellspacing=5 border=0>
<tr><td>Rank:</td><td>Name:</td><td>Score:</td></tr>
END_NAV

#####################
#	FORMAT:
# Rank:	Name:	Score:
# 1.	Jed	13251
# ...	...	...
# 25.	Earl	1094
#####################

my @tmp;
my $loop = 0;
while (defined $sorted[$loop]) {
	@tmp = split(/\:/, $sorted[$loop]);
	$loop++;
	if ($sorted[$loop-1] == $score) {
		print "<tr><td><B>$loop\.</b></td><td><b>$tmp[1]</b></td><td><b>$tmp[0]</b></td></tr>";
	} else {
		print "<tr><td>$loop\.</td><td>$tmp[1]</td><td>$tmp[0]</td></tr>";
	}
}

print << "END_HTML";
</table>
</div>
</body>
</html>

END_HTML
