#!/usr/bin/perl -w

use CGI;

$query = CGI::new();

print $query->header();
print $query->start_html( -title => 'Mornington Crescent',
						  -text  => "white",
						  -bgcolor => "blue",
						  -link  => "cyan",
						  -alink => "cyan",
						  -vlink => "lightcyan" );

open PRETEXT, "pretext";
while (<PRETEXT>) {
	print;
}
close PRETEXT;

my (@matt, @stu);
my $i = 0;
open MATT, "matt";
while (<MATT>) {
	my ($short, $long) = ( /(\S*)\t*\"(.*)\"\n/ );
#	print "Short = $short<br>";
#	print "Long  = $long<br><br>";

	next unless defined $short;
	
	$matt[$i]{"short"} = $short;
	$matt[$i]{"long"} = $long;
	$matt[$i++]{"available"} = "true"; 
}

$matt[$i]{"short"} = "morningtoncrescent";
$matt[$i]{"long"} = "Mornington Crescent";
$matt[$i]{"available"} = "false";
	

$i=0;
open STU, "stu";
while (<STU>) {
    my ($short, $long) = ( /(\S*)\t*\"(.*)\"\n/ );
    #print "Short = $short<br>";
    #print "Long  = $long<br><br>";
    
	next unless defined $short;
        
	$stu[$i]{"short"} = $short;
    $stu[$i]{"long"} = $long;
    $stu[$i++]{"available"} = "true";
}

$stu[$i]{"short"} = "morningtoncrescent";
$stu[$i]{"long"} = "Mornington Crescent";
$stu[$i]{"available"} = "false";

$i = 0;
my @options;

open OPTIONS, "options";
while (<OPTIONS>) {
	next unless (/\S/);
	chop $_;
	$options[$i++] = $_;
}
close OPTIONS;

$i=0;
my @moves;
open MOVES, "moves";
while (<MOVES>) {	
	next unless (/\S/);
	chop $_;
	$moves[$i++] = $_;
}
close MOVES;
        
$i=0;
my @preopt;
open PREOPT, "pre-opt";
while (<PREOPT>) {
    next unless (/\S/);
    chop $_;
    $preopt[$i++] = $_;
}
close PREOPT;

$i=0;
my @postopt;
open POSTOPT, "post-opt";
while (<POSTOPT>) {
    next unless (/\S/);
    chop $_;
    $postopt[$i++] = $_;
}
close POSTOPT;

$i=0;
my @quibble;
open QUIBBLE, "quibble";
while (<QUIBBLE>) {
    next unless (/\S/);
    chop $_;
    $quibble[$i++] = $_;
}
close QUIBBLE;

$i=0;
my @pos;
open POS, "pos";
while (<POS>) {
    next unless (/\S/);
    chop $_;
    $pos[$i++] = $_;
}
close POS;

$i=0;
my @winning;
open WINNING, "winning";
while (<WINNING>) {
    next unless (/\S/);
    chop $_;
    $winning[$i++] = $_;
}
close WINNING;



if ($path = $query->param("path") ) {

    #print "Path is $path<br>";
    
	if ($path =~ /,/ ) {
		#We're not at MC yet
		($this_station, $path) = ( $path =~ /(\S*?),(\S*)/ );
		$label = "Next Station";

	} else {

		# Show End Game
		$label = "Restart";
		$this_station = $path;
		$path="";
	}	 
	
	($short) = ( $this_station =~ /_(.*)$/ ); 

    if ($this_station =~ /^stu/) {
		$person = "Stu";
		$opponent = "Matt";
		
		
		for (my $i = 0; $i < scalar(@stu); $i++) {
			if ( $stu[$i]{"short"} eq $short ) {
				$long = $stu[$i]{"long"};
			}
		}


	} else {
		$person = "Matt";
		$opponent = "Stu";
		
		for (my $i = 0; $i < scalar(@matt); $i++) {
			if ( $matt[$i]{"short"} eq $short ) {
				$long = $matt[$i]{"long"};
			}
		}


    }	

	print "<img src=/mc/" . $this_station . ".jpg><br>";
	
	# Here's where we construct the commentry
	$commentry = $options[int( rand( scalar(@options) ) )];

	$commentry = $preopt[int( rand( scalar(@preopt) ) )] . " $commentry";	
	if ( int( rand 2 ) ) {
		$commentry .= " $postopt[int( rand( scalar(@postopt) ) )]";
	}

	if ($this_station =~  /morningtoncrescent/i ) {
        $commentry = $winning[int( rand( scalar(@winning) ) )];
    }


	$quibble = $quibble[int( rand( scalar(@quibble) ) )];	

	$commentry =~ s/<quibble>/$quibble/g;
	$commentry =~ s/<player>/$person/g;
	$commentry =~ s/<opponent>/$opponent/g;
	$commentry =~ s/<station>/$long/g;

	while ($commentry =~ /<pos>/) {
		$commentry =~ s/<pos>/$pos[int( rand( scalar(@pos) ) )]/;
	}

	while ($commentry =~ /<manouvre>/) {
		$commentry =~ s/<manouvre>/$moves[int( rand( scalar(@moves) ) )]/;
	}
   

    print "$commentry <br>\n";
	

} else {

    # There's no set path, so we need to create one
    $path = "stu_pimlico,matt_westminster,stu_morningtoncrescent";
    
	# Decide the winner first
	if (int(rand 2) + 1 == 1) {
		$path = "stu_morningtoncrescent";
	} else {
		my $rn = int( rand( scalar(@stoo) - 1 ) ); 
		$path = "stu_" . $stu[$rn]{"short"} . ",matt_morningtoncrescent";
		$stu[$rn]{"available"} = "false";
	}
    
    for (my $i = 0; $i < int(rand 4) + 1 ; $i++) {
    	
    	my $rn;
    	do {
    		$rn = int( rand( scalar( @matt ) -1 ) );
    	} until ($matt[$rn]{"available"} eq "true");
    
    	$path = "matt_" . $matt[$rn]{"short"} . "," . $path;
		$matt[$rn]{"available"} = "false";

		do {
			$rn = int( rand( scalar( @matt ) -1 ) );
		} until ($stu[$rn]{"available"} eq "true");

        $path = "stu_" . $stu[$rn]{"short"} . "," . $path;
        $stu[$rn]{"available"} = "false";
                
	}
    
    $label = "Begin";
	
	open BLURB, "blurb";
	while (<BLURB>) {
		print;
	}
	close BLURB;

	#print "Blurb about MC goes HERE!<br>";
 	#print "New path is @path\n";
}
        

$query->delete('path');


print $query->startform();
print $query->hidden( -name  => "path",
					  -value => $path );


#$query->param( -name => "path", -value => @path);

					  
print $query->submit( -label => $label );

print $query->end_html();

open POSTTEXT, "posttext";
while (<POSTTEXT>) {
	print;
}