#!/usr/bin/php
<?php
/*Map rotation script by dukevin that rotates maps but never picks the same map until each map has gone once. Edit settings below:*/

//Add as many maps as you want in the line below, separated by a comma:
$maplist=array("mymap-1.aamap.xml","mymap-2.aamap.xml","mymap-3.aamap.xml","mymap-4.aamap.xml"); 

//Display a message when a certain map is chosen. To disable all messages, add double backslashes // in front of $messages below:
$messages=array("Welcome to map 1", "Map 2 is made by name", "Map 3 tactics: hide in the corner", "In map 4, try to do this");

//Play a map this many times before switching to the next one:
$rounds = 1;

//Display how many rounds before map change? set $alert to "true" if yes, "false" if no. Alerts not displayed if $rounds = 1
$alert = true;

/*======================================================================*/
$played = array();
$last = -1;
$played = array_fill(0, count($maplist), 0);
while(true){
$repeating = false;
$line = rtrim(fgets(STDIN, 1024));
if (preg_match("/^ROUND_COMMENCING/", $line))
{
	for($x=0; $x<(sizeof($maplist)); $x++)
	{
		if(($played[$x] < $rounds && $played[$x] != 0))
		{
			$played[$x]++;
			echo "MAP_FILE ". ($maplist[$x]) ." \n";
			echo "CONSOLE_MESSAGE ". ($messages[$x]) ." \n";
			if ($rounds > 1 && $alert) echo "CONSOLE_MESSAGE 0xaaaaaaEach map plays for $rounds rounds. ". ($rounds-$played[$x]) ." rounds left.\n";
			$repeating = true;
			break;
		}
	}
	if(!$repeating)
	{	
		if(min($played) == $rounds) //all the maps have been played
		{
			echo "console_message Recycling maps...\n";
			$played = array_fill(0, count($maplist), 0);
		}	
		do $num = mt_rand(0,(sizeof($maplist)-1)); while(($played[$num]>=$rounds)||$last==$num);
		$last = $num;
		echo "MAP_FILE ". ($maplist[$num]) ." \n";
		echo "CONSOLE_MESSAGE ". ($messages[$num]) ." \n";
		$played[$num]++;
		if ($rounds > 1 && $alert) echo "CONSOLE_MESSAGE 0xaaaaaa Each map plays for $rounds rounds. ". ($rounds-$played[$num]) ." rounds left.\n";
	}
}
}
?>