#!/usr/bin/php
<?php
/* Lives (limited Respawns) Written by dukevin. 
Make sure to set CYCLE_INVULERABLE_TIME to 3 or higher
and LADDERLOG_WRITE_CYCLE_CREATED 1 */

$lives_per_player = 3;	//How many lives per player?
$lives_privacy = false;	//Keep private the lives message or announce it publically? "true" = private, "false" = public.
$xCoord = 100;			//X Coord of respawning.
$yCoord = 100;			//Y Coord of respawning.
$randomnessX = 20;		//Randomness of X coordinate. 0 = not random, 10 = hardily random, 90 = extremely random.
$randomnessY = 20;		//Randomness of Y coordinate. 0 = not random, 10 = hardily random, 90 = extremely random.
$color = '0x00ff88';	//Color of the messages
/*===========================================================*/
$i = 0;
$players = array();
$lives = array();
while (1)  {
    $line = rtrim(fgets(STDIN, 1024));
	if (preg_match("/^CYCLE_CREATED/", $line) )
	{
		$split = explode(" ",$line);
		$name = $split[1];
		$players[$i] = $name;
		$lives[$i] = $lives_per_player;
		$i++;
		$xCoords = mt_rand($xCoord-$randomnessX, $xCoord+$randomnessX);
		$yCoords = mt_rand($yCoord-$randomnessY, $yCoord+$randomnessY);
	}
	if($lives_privacy)
	{
		$method = "player_message";
		$body = "You have";
		$q = '"';
		$color1='';
	}
	else if(!$lives_privacy)
	{
		$method = "console_message";
		$body = "has";
		$q = '';
		$color1=$color;
	}
	if (preg_match("/^DEATH_FRAG|DEATH_SUICIDE|PLAYER_KILLED|DEATH_SHOT_FRAG|DEATH_DEATHZONE|DEATH_SHOT_SUICIDE|DEATH_TEAMKILL|DEATH_SHOT_TEAMKILL|DEATH_ZOMBIEZONE|DEATH_DEATHSHOT|DEATH_SELF_DESTRUCT/", $line) )
	{
		$split = explode(" ",$line);
		$died = $split[1];
		$key = array_search("$died", $players);
		$lives[$key]--;
		if($lives[$key] >= 0)
		{
			echo "respawn_player $died $xCoords $yCoords $xCoords $yCoords \n";
			if ($lives[$key] == 1)
				$string = "respawn";
			else 
				$string = "respawns";
			echo "$method $color1$died $q$color$body 0xffffff$lives[$key]$color $string left.$q\n";
		}
		else if($lives[$key] <= 0)
			echo "console_message $color$died has been eliminated.\n";
	}
	if (preg_match("/^ROUND_COMMENCING/", $line) )
	{
		echo "console_message All players get $color$lives_per_player 0xfffffflives.\n";
		unset($xCoords, $yCoords);
		$xCoords = $xCoord;
		$yCoords = $yCoord;
		unset($players);
		unset($lives);
		$i=0;
	}
}
?>