<?

class n3rd_mohaa_stats{

	# VARS:
	
		function n3rd_mohaa_stats($template, $maindir, $qconsole, $index_max, $version = '2.0 BETA'){
			$this->template = $template;
			$this->maindir = $maindir;
			$this->qconsole = $qconsole;
			$this->index_max = $index_max;
			$this->version = $version;
		}
		
		# Mogelijke Death Messages:
		# SELF & SPECIAL:
			var $dm = array(
				"took himself out of commision" => "suicide",
				"was burned to a crisp" => "suicide",
				"cratered" => "cratered",
				"tripped on his own grenade" => "own grenade",
				"played catch with himself" => "own grenade",
				"blew himself up" => "suicide",
				"rocketed himself" => "suicide",
				"shot himself in the" => "suicide",
				"died" => "suicide",
				"blew up" => "blew up",
				"was killed by" => "suicide",
				# NADE:
				"is picking" => "grenade",
				"tripped on" => "grenade",
				# ROCKET:
				"was blown away by" => "rocket",
				"took" => "rocket",
				# GUN:
				"was shot by" => "gun",
				"was gunned down by" => "gun",
				# SINPER:
				"was sniped by" => "sniper",
				# RIFLE:
				"was rifled by" => "rifle",
				# SMG:
				"was perforated by" => "SMG",
				# MG:
				"was machine-gunned by" => "MG",
				# BASH:
				"was clubbed by" => "bash",
				"was bashed by" => "bash",
				# SHOTGUN:
				"was pumped full of buckshot by" => "shotgun",
				"was hunted down by" => "shotgun"
			);
		
		# Locaties waar iemand mogelijk dodelijk geraakt kan worden:
			var $location = array(
				"head",
				"neck",
				"upper torso",
				"middle torso",
				"lower torso",
				"pelvis",
				"upper right arm",
				"upper left arm",
				"upper right leg",
				"upper left leg",
				"lower right arm",
				"lower left arm",
				"lower right leg",
				"lower left leg",
				"right hand",
				"left hand",
				"right foot",
				"left foot",
				"face",
				"kisser"				
			);
			
		var $strip = array(
			"'s shrapnel out of his teeth",
			"'s grenade",
			"'s rocket right",
			"'s rocket",
			"'s SMG",
			"himself out of commision",
			"his own grenade",			
		);
		
	# // VARS

	function kills(){
		$file = file($this->qconsole);
		
		foreach($file as $rule){
			if(substr($rule, 0, 5) !== 'CHAT:' && substr($rule, 0, 2) !== 'SV' && substr($rule, 0, 4) !== 'Rcon' && substr($rule, 0, 6) !== 'status'){
				foreach($this->dm as $message => $weapon){
					if(strpos($rule, $message)){
						$explode = explode($message, $rule);
						$info[1] = addslashes(trim($explode[0]));
						if(strpos($explode[1], ' in the ')){
							$expl = explode(' in the ', $explode[1]);
							$info[2] = addslashes(trim(str_replace($this->strip, '', $expl[0])));
							$info[3] = addslashes(trim($expl[1]));
						}else{
							$info[2] = addslashes(trim(str_replace($this->strip, '', $explode[1])));
							$info[3] = '';
						}
						$info[4] = addslashes(trim($weapon));
						$result[] = $info;
					}
				}
			}
		}
		return $result;
	}
	
	function count_kills(){
		$kills = $this->kills();
		$players = array();
	
		foreach($kills as $nr => $kill){
			# Killed person Check:
			if(!in_array($kill[1], $players)){
				$players[] = $kill[1];
				$pid = array_search($kill[1], $players);
				$data[$pid]['kills'] = 0;
				$data[$pid]['deaths'] = 0;
				foreach($this->dm as $dm_v){
					$data[$pid][$dm_v] = 0;
				}
				foreach($this->location as $location_v){
					$data[$pid][$location_v] = 0;
				}
			}
			# Killer Check:
			if(!empty($kill[2]) && !in_array($kill[2], $players)){
				$players[] = $kill[2];
				$pid = array_search($kill[2], $players);
				$data[$pid]['kills'] = 0;
				$data[$pid]['deaths'] = 0;
				foreach($this->dm as $dm_v){
					$data[$pid][$dm_v] = 0;
				}
				foreach($this->location as $location_v){
					$data[$pid][$location_v] = 0;
				}
			}
			# Optellen van de data:
			$data[array_search($kill[1], $players)]['deaths']++;
			if(!empty($kill[2])){ # Als de persoon door iemand anders gedood werd:
				$data[array_search($kill[2], $players)]['kills']++;
				if(!empty($kill[3])){
					$data[array_search($kill[2], $players)][trim($kill[3])]++;
					}
				$data[array_search($kill[2], $players)][$kill[4]]++;
			}else{ # Als de persoon zichzelf doode:
				$data[array_search($kill[1], $players)][$kill[4]]++;
			}
		}
		$return['players'] = $players;
		$return['data'] = $data;
		return $return;
	}
	
	function player_pages($input){
		$players = $input['players'];
		$data = $input['data'];
		
		foreach($players as $pid => $pnick){
			# output plaatsen in:
			$output = $this->maindir . '/players/' . $pid . '.html';
			$file = fopen($output , 'w');
			arsort($data[$pid]);
			
			fwrite($file, $this->t_top('../', htmlspecialchars(stripslashes($pnick))));
			
			fwrite($file, "<h2>General information:</h2>\n");
			fwrite($file, "<table width='100%'>\n");
			fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Ratio:</td><td colspan='2'>" . $this->ratio($data[$pid]['kills'], $data[$pid]['deaths']) . "</td></tr>\n");
			fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Total kills:</td><td colspan='2'>" . $data[$pid]['kills'] . "</td></tr>\n");
			fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Total deaths:</td><td colspan='2'>" . $data[$pid]['deaths'] . "</td></tr>\n");
			fwrite($file, "</table>\n");
			fwrite($file, "<h2>Aim information:</h2>\n");
			fwrite($file, "<table width='100%'>\n");
			foreach($data[$pid] as $k => $v){
				if(in_array($k, $this->location)){
				fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>" . $k . "</td><td>" . $v . "</td><td>" . $this->percent($v, $data[$pid]['kills']) . "%</td></tr>\n");
				}
			}
			fwrite($file, "</table>\n");
			fwrite($file, "<h2>Weapon information:</h2>\n");
			fwrite($file, "<table width='100%'>\n");
			foreach($data[$pid] as $k => $v){
				if(in_array($k, $this->dm)){
				fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>" . $k . "</td><td colspan='2'>" . $v . "</td></tr>\n");
				}
			}
			fwrite($file, "</table>\n");
			
			fwrite($file, $this->t_bottom('../', htmlspecialchars(stripslashes($pnick))));
			fclose($file);
		}
	}
	
	function ratio($kills, $deaths){
		if($deaths == 0){
			return $kills;
		}else{
			return round(($kills/$deaths) ,2);
		}
	}
	
	function percent($part, $total){
		if($total == 0){
			return 0;
		}else{
			$percent = (100/$total);
			return round(($percent*$part) ,2);
		}
	}
	
	function t_top($basedir, $title){
		$file = file('templates/' . $this->template . '/top.html');
		$file = str_replace('%basedir%', $basedir, $file);
		$file = str_replace('%date%', date('d/m/Y'), $file);
		$file = str_replace('%version%', $this->version, $file);
		$file = str_replace('%title%', $title, $file);
		$file = str_replace('%time%', date('H:i:s'), $file);
		$file = str_replace('%phpversion%', phpversion(), $file);
		return implode('', $file);
	}
	
	function t_bottom($basedir, $title){
		$file = file('templates/' . $this->template . '/bottom.html');
		$file = str_replace('%basedir%', $basedir, $file);
		$file = str_replace('%date%', date('d/m/Y'), $file);
		$file = str_replace('%version%', $this->version , $file);
		$file = str_replace('%title%', $title, $file);
		$file = str_replace('%time%', date('H:i:s'), $file);
		$file = str_replace('%phpversion%', phpversion(), $file);
		return implode('', $file);
	}
	
	function awards_page($input){
		$players = $input['players'];
		$data = $input['data'];
		$dm_order = array();
		
		$output = $this->maindir . '/awards.html';
		$file = fopen($output , 'w');
		
		fwrite($file, $this->t_top('', 'Awards'));
		
		fwrite($file, "<h2>General:</h2>\n");
		fwrite($file, "<table width='100%'>\n");
		foreach($players as $pid => $pnick){
			$most_kills[$pid] = $data[$pid]['kills'];
			$most_deaths[$pid] = $data[$pid]['deaths'];
			$highest_ratio[$pid] = $this->ratio($data[$pid]['kills'], $data[$pid]['deaths']);
		}
		arsort($highest_ratio);
		fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Highest ratio:</td><td><a href='players/" . key($highest_ratio) . ".html'>" . htmlspecialchars(stripslashes($players[key($highest_ratio)])) . "</a></td><td>" . current($highest_ratio) . "</td></tr>\n");
		arsort($most_kills);
		fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Most kills:</td><td><a href='players/" . key($most_kills) . ".html'>" . htmlspecialchars(stripslashes($players[key($most_kills)])) . "</a></td><td>" . current($most_kills) . "</td></tr>\n");
		arsort($most_deaths);
		fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Most deaths:</td><td><a href='players/" . key($most_deaths) . ".html'>" . htmlspecialchars(stripslashes($players[key($most_deaths)])) . "</a></td><td>" . current($most_deaths) . "</td></tr>\n");
		fwrite($file, "</table>\n");
		
		fwrite($file, "<h2>Aim awards:</h2>\n");
		fwrite($file, "<table width='100%'>\n");
		foreach($this->location as $l_v){
			foreach($players as $pid => $pnick){
				$l_order[$l_v][$pid] = $this->percent($data[$pid][$l_v], $data[$pid]['kills']);
			}
			arsort($l_order[$l_v]);
			fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Highest amount " . $l_v . " shots:</td><td><a href='players/" . key($l_order[$l_v]) . ".html'>" . htmlspecialchars(stripslashes($players[key($l_order[$l_v])])) . "</a></td><td>" . current($l_order[$l_v]) . "%</td><td>" . $data[key($l_order[$l_v])][$l_v] . " Kills</td></tr>\n");
		}
		fwrite($file, "</table>\n");
		
		fwrite($file, "<h2>Weapon awards:</h2>\n");
		fwrite($file, "<table width='100%'>\n");
		foreach($this->dm as $dm_v){
			if(!array_key_exists($dm_v, $dm_order)){
				foreach($players as $pid => $pnick){
					$dm_order[$dm_v][$pid] = $this->percent($data[$pid][$dm_v], $data[$pid]['kills']);
				}
				arsort($dm_order[$dm_v]);
				fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td>Highest amount " . $dm_v . " kills:</td><td><a href='players/" . key($dm_order[$dm_v]) . ".html'>" . htmlspecialchars(stripslashes($players[key($dm_order[$dm_v])])) . "</a></td><td>" . current($dm_order[$dm_v]) . "%</td><td>" . $data[key($dm_order[$dm_v])][$dm_v] . " Kills</td></tr>\n");
			}
		}
		fwrite($file, "</table>\n");
		
		fwrite($file, $this->t_bottom('', 'Awards'));
		fclose($file);
	}
	
	function style(){
		$style = implode('', file('templates/' . $this->template . '/style.css'));
		$output = $this->maindir . '/style.css';
		$file = fopen($output , 'w');
		fwrite($file, $style);
		fclose($file);
	}
	
	function ratio_page($input){
		$players = $input['players'];
		$data = $input['data'];
		
		foreach($players as $pid => $pnick){
			$ratio[$pid] = $this->ratio($data[$pid]['kills'], $data[$pid]['deaths']);
		}
		
		$output = $this->maindir . '/ratio.html';
		$file = fopen($output , 'w');
		fwrite($file, $this->t_top('', 'Ratio'));
		fwrite($file, "<h2>Ratio ranking:</h2>\n");
		fwrite($file, "<table width='100%'>\n");
		fwrite($file, "<tr class='tr-out'><td>Player:</td><td>Kills:</td><td>Deaths:</td><td>Ratio:</td></tr>\n");
		arsort($ratio);
		foreach($ratio as $pid => $value){
			fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td><a href='players/" . $pid . ".html'>" . htmlspecialchars(stripslashes($players[$pid])) . "</a></td><td>" . $data[$pid]['kills'] . "</td><td>" . $data[$pid]['deaths'] . "</td><td>" . $value . "</td></tr>\n");
		}
		fwrite($file, "</table>\n");
		fwrite($file, $this->t_bottom('', 'Ratio'));
		fclose($file);
	}
	
	function kills_page($input){
		$players = $input['players'];
		$data = $input['data'];
		
		foreach($players as $pid => $pnick){
			$kill[$pid] = $data[$pid]['kills'];
		}
		
		$output = $this->maindir . '/kills.html';
		$file = fopen($output , 'w');
		fwrite($file, $this->t_top('', 'Kills'));
		fwrite($file, "<h2>Kill ranking:</h2>\n");
		fwrite($file, "<table width='100%'>\n");
		fwrite($file, "<tr class='tr-out'><td>Player:</td><td>Kills:</td><td>Deaths:</td><td>Ratio:</td></tr>\n");
		arsort($kill);
		foreach($kill as $pid => $value){
			fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td><a href='players/" . $pid . ".html'>" . htmlspecialchars(stripslashes($players[$pid])) . "</a></td><td>" . $data[$pid]['kills'] . "</td><td>" . $data[$pid]['deaths'] . "</td><td>" . $this->ratio($data[$pid]['kills'], $data[$pid]['deaths']) . "</td></tr>\n");
		}
		fwrite($file, "</table>\n");
		fwrite($file, $this->t_bottom('', 'Kills'));
		fclose($file);
	}
	
	function index_page($input){
		$players = $input['players'];
		$data = $input['data'];
		
		$output = $this->maindir . '/index.html';
		$file = fopen($output , 'w');
		fwrite($file, $this->t_top('', 'N3RD\'s MoH:AA stats'));
		foreach($players as $pid => $pnick){
			$kill[$pid] = $data[$pid]['kills'];
			$ratio[$pid] = $this->ratio($data[$pid]['kills'], $data[$pid]['deaths']);
		}
		
		arsort($ratio);
		fwrite($file, "<h2>Ratio top " . $this->index_max . ":</h2>\n");
		fwrite($file, "<table width='100%'>\n");
		fwrite($file, "<tr class='tr-out'><td>Player:</td><td>Kills:</td><td>Deaths:</td><td>Ratio:</td></tr>\n");
		for($i=0;$i<$this->index_max;$i++){
			if(isset($ratio[key($ratio)])){
				fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td><a href='players/" . key($ratio) . ".html'>" . htmlspecialchars(stripslashes($players[key($ratio)])) . "</a></td><td>" . $data[key($ratio)]['kills'] . "</td><td>" . $data[key($ratio)]['deaths'] . "</td><td>" . $this->ratio($data[key($ratio)]['kills'], $data[key($ratio)]['deaths']) . "</td></tr>\n");
			}
			next($ratio);
		}
		fwrite($file, "</table>\n");

		arsort($kill);
		fwrite($file, "<h2>Kill top " . $this->index_max . ":</h2>\n");
		fwrite($file, "<table width='100%'>\n");
		fwrite($file, "<tr class='tr-out'><td>Player:</td><td>Kills:</td><td>Deaths:</td><td>Ratio:</td></tr>\n");
		for($i=0;$i<$this->index_max;$i++){
			if(isset($kill[key($kill)])){
				fwrite($file, "<tr class='tr-out' onmouseover=\"this.className='tr-over'\" onmouseout=\"this.className='tr-out'\"><td><a href='players/" . key($kill) . ".html'>" . htmlspecialchars(stripslashes($players[key($kill)])) . "</a></td><td>" . $data[key($kill)]['kills'] . "</td><td>" . $data[key($kill)]['deaths'] . "</td><td>" . $this->ratio($data[key($kill)]['kills'], $data[key($kill)]['deaths']) . "</td></tr>\n");
			}
			next($kill);
		}
		fwrite($file, "</table>\n");
		fwrite($file, $this->t_bottom('', 'N3RD\'s MoH:AA stats'));
		fclose($file);
	}
	
	function temp_dir(){
		if(!is_dir($this->maindir)){
			mkdir($this->maindir, 0777);
		}
		if(!is_dir($this->maindir . '/players')){
			mkdir($this->maindir . '/players', 0777);
		}
	}
	
}


?>