<?php include('inc/header.php'); ?>
<h1>Member - Send an item</h1>
<div class="container">
<?php
if(isset($_GET['sendToAll'])) {
	if(isset($_POST['senditems'])) {
		if(is_numeric($_POST['itemid']) AND is_numeric($_POST['itemcount']) AND isset($_POST['confirmSending'])) {
			$getNumberOfActiveCharacters = @odbc_result(@odbc_exec($odbc_connect, 'SELECT COUNT([m_idPlayer]) as count FROM [' . $_CONFIG['db_databases']['chr'] . '].[dbo].[CHARACTER_TBL] WHERE [isblock] = \'F\''), 'count');
			$getCharacterArray = @odbc_exec($odbc_connect, 'SELECT [m_idPlayer] FROM [' . $_CONFIG['db_databases']['chr'] . '].[dbo].[CHARACTER_TBL] WHERE [isblock] = \'F\'');
			$successRate = 0;
			
			while($get = @odbc_fetch_array($getCharacterArray)) {
				$query = '
					INSERT INTO [' . $_CONFIG['db_databases']['chr'] . '].[dbo].[ITEM_SEND_TBL] (
						m_idPlayer, 
						serverindex, 
						Item_Name, 
						Item_count, 
						m_nAbilityOption, 
						m_bItemResist, 
						m_nResistAbilityOption, 
						idSender, 
						nPiercedSize
					) VALUES (
						\'' . $get['m_idPlayer'] . '\',
						\'01\',
						' . $_POST['itemid'] .',
						' . $_POST['itemcount'] . ',
						0,
						0,
						0,
						\'0000000\',
						0
					)';
				if(@odbc_exec($odbc_connect, $query)) {
					$successRate = $successRate + 1;
				}
			}
			if($successRate > 0) {
				echo '<div class="success">Item #' . $_POST['itemid'] . ' wurde ' . $_POST['itemcount'] . 'x erfolgreich an ' . $successRate . ' von insgesamt ' . $getNumberOfActiveCharacters . ' aktiven Charakteren gesendet  (' . round($successRate / $getNumberOfActiveCharacters * 100, 2) . '% Erfolgsrate).</div>';
			}
			else {
				echo '<div class="fail">Itemversand fand nicht statt.</div>';
			}
		}
		else {
			echo '<div class="fail">Bitte gebe eine gültige Item-ID, eine gültige Anzahl ein und bestätige deine Eingabe.</div>';
		}
	}

	echo '
	<a href="account_senditem.php" alt="">Items an einzelnen Spieler schicken</a>
	<br />
	<h2>Item an alle Spieler schicken</h2>
	<br />
	Beachte: Die Ausführung des Skripts kann relativ lange dauern. Die Seite während des Vorgangs bloß nicht aktualisieren!
	<br />
	<br />
	<form method="post">
		<input style="margin:3px 0;" type="text" name="itemid" required="required" placeholder="Item ID"/><br style="clear: left;"/>
		<input style="margin:3px 0;" type="text" name="itemcount" required="required" placeholder="Anzahl" size="4"/> x<br style="clear: left;"/>
		<input style="margin:3px 0;" type="checkbox" name="confirmSending" required="required"/><br style="clear: left;"/>
		<input style="margin:3px 0;" type="submit" name="senditems" value="Item an alle Spieler senden" />
	</form>
	';
}
else {
	if(isset($_POST['senditem_sbm'])) {
		odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['chr'] . ']');
		$charid = odbc_exec($odbc_connect, 'SELECT m_idPlayer FROM [CHARACTER_TBL] WHERE m_szName=\''.mssql_escape_string($_POST['character']).'\'');
		if(odbc_num_rows($charid) == 1) {
			odbc_exec($odbc_connect, 'INSERT INTO [ITEM_SEND_TBL](
			m_idPlayer, serverindex, Item_Name, Item_count, m_nAbilityOption, m_bItemResist, m_nResistAbilityOption, idSender, nPiercedSize
			) VALUES(
			\''.mssql_escape_string(odbc_result($charid, 'm_idPlayer')).'\',
			\'01\',
			\''.mssql_escape_string($_POST['itemname']).'\',
			\''.mssql_escape_string($_POST['count']).'\',
			\''.mssql_escape_string($_POST['upgrade']).'\',
			\''.mssql_escape_string($_POST['element']).'\',
			\''.mssql_escape_string($_POST['elementupgrade']).'\',
			\'0000000\',
			\''.mssql_escape_string($_POST['piercing']).'\'
			)');
			echo '<div class="success">The item has successfully been send!</div>';
		} else {
			echo '<div class="fail">Character does not exist!</div>';
		}
	}
	echo '
<a href="account_senditem.php?sendToAll" alt="">Items an alle Spieler schicken</a>
<br />
<h2>Item an einen Spieler schicken</h2>
<form action="" method="post">
<table>
	<tr>
		<td>Character:</td>
		<td><input type="text" name="character" /></td>
	</tr>
	<tr>
		<td>Item Name:</td>
		<td><input type="text" name="itemname" /></td>
	</tr>
	<tr>
		<td>Item Count:</td>
		<td><input type="text" name="count" /></td>
	</tr>
	<tr>
		<td>Item Upgrade:</td>
		<td>
			<select name="upgrade">
				<option value="0">0</option>
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
				<option value="6">6</option>
				<option value="7">7</option>
				<option value="8">8</option>
				<option value="9">9</option>
				<option value="10">10</option>
				<option value="11">11</option>
				<option value="12">12</option>
				<option value="13">13</option>
				<option value="14">14</option>
				<option value="15">15</option>
				<option value="16">16</option>
				<option value="17">17</option>
				<option value="18">18</option>
				<option value="19">19</option>
				<option value="20">20</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>Piercing Slots:</td>
		<td>
			<select name="piercing">
				<option value="0">0</option>
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
				<option value="6">6</option>
				<option value="7">7</option>
				<option value="8">8</option>
				<option value="9">9</option>
				<option value="10">10</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>Element Type:</td>
		<td>
			<select name="element">
				<option value="0">None</option>
				<option value="1">Fire</option>
				<option value="2">Water</option>
				<option value="3">Electric</option>
				<option value="4">Wind</option>
				<option value="5">Earth</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>Element Upgrade:</td>
		<td>
			<select name="elementupgrade">
				<option value="0">0</option>
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
				<option value="6">6</option>
				<option value="7">7</option>
				<option value="8">8</option>
				<option value="9">9</option>
				<option value="10">10</option>
				<option value="11">11</option>
				<option value="12">12</option>
				<option value="13">13</option>
				<option value="14">14</option>
				<option value="15">15</option>
				<option value="16">16</option>
				<option value="17">17</option>
				<option value="18">18</option>
				<option value="19">19</option>
				<option value="20">20</option>
			</select>
		</td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="senditem_sbm" value="Send Item" /></td>
	</tr>
</table>
</form>';
}
echo '
<br />
<h2>Häufig benötigte Items</h2>
<table cellspacing="0">
	<thead>
		<tr>
			<td>Itemname</td>
			<td>ID</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Kheldor Cloak</td>
			<td>54019</td>
		</tr>
		<tr>
			<td>Sapphire Chips</td>
			<td>26460</td>
		</tr>
		<tr>
			<td>Perins</td>
			<td>26456</td>
		</tr>
		<tr>
			<td>Chip Perin (CP)</td>
			<td>54063</td>
		</tr>
		<tr>
			<td>Event Token(CP)</td>
			<td>54174</td>
		</tr>
	</tbody>
</table>';
?>
</div>
<?php include('inc/footer.php'); ?>