<?php
if($__TOKEN = "hardcodeshitbykernstudios"){
	echo '<h2 class="bg-h2-1 bot">Top 100 Character</h2>';
	$conn = sqlsrv_connect($__CONFIG['SQLHost'], array("Database"=>$__CONFIG['SQLDB'], "UID"=>$__CONFIG['SQLUID'], "PWD"=>$__CONFIG['SQLPWD']));
	if(!$conn){
		echo print_r(sqlsrv_errors(), true);
	}else{
		echo '<center><table width="900px">';
		echo '<tr><th>#</th><th>Username</th><th>Level</th><th><a href="?module=topChar&order=nEXP">Exp</a></th><th><a href="?module=topChar&order=nPlayMin">Play Time</a></th><th>Class</th><th>Guild</th></tr>';
		$i = 1;
		$order = 'ORDER BY nExp DESC';
		if(isset($_GET['order'])){
			switch($_GET['order']){
				case 'nEXP':
					$order = 'ORDER BY nExp DESC';
					break;
				case 'nPlayMin';
					$order = 'ORDER BY nPlayMin DESC';
					break;
			}
		}
		$getChar = sqlsrv_query($conn, "SELECT TOP 100 tCharacter.nCharNo, sID, nLevel, nExp, nPlayMin, nClass FROM w00_Character..tCharacter, w00_Character..tCharacterShape WHERE tCharacter.nCharNo = tCharacterShape.nCharNo AND bDeleted = 0 AND nAdminLevel = 0  ".$order.";", array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
		while($fetchChar = sqlsrv_fetch_array($getChar)){
			$selectGuild = sqlsrv_query($conn, "SELECT sName FROM w00_Character..tGuild WHERE nNo = (SELECT nNo FROM w00_Character..tGuildMember WHERE nCharNo = ?);", array($fetchChar['nCharNo']), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
			$guild = 'None';
			if(sqlsrv_num_rows($selectGuild) == 1){
				$fetchGuild = sqlsrv_fetch_array($selectGuild);
				$guild = $fetchGuild['sName'];
			}
			echo '<tr><th>'.$i.'</th><th>'.$fetchChar['sID'].'</th><th>'.$fetchChar['nLevel'].'</th><th>'.$fetchChar['nExp'].'</th><th>'.minutesToTime($fetchChar['nPlayMin']).'</th><th>'.getClassToString($fetchChar['nClass']).'</th><th>'.utf8_encode($guild).'</th></tr>';
			$i++;
		}
		echo '</table></center>';
	}
}
?>