<?php 

session_start();
header("Content-type: text/html; charset=windows-1251");
define("PATH", $_SERVER['DOCUMENT_ROOT']);
include(PATH."../core/func.php");
include(PATH."../core/db.php");
include(PATH."../core/set.php");
include(PATH."../core/define.php");
include(PATH."../core/defineset.php");
db_connect();

	$res = db_query("SELECT `id`, `login`, `login2`, `summa`, `item`, `item2`, `dat`, `dat2`, `md5` FROM `sapper_games_active`");
	while($row = db_assoc($res)){
		if($row["dat2"] < time()-310)
		{
			$bank = round($row["summa"],2) * 1.9;
				//echo json_encode(array("result"=>1));
				db_query("INSERT INTO `sapper_games_end` (`id`, `summa`, `item`, `item2`, `login`, `login2`, `winner`, `dat`, `dat2`, `md5`) VALUES ('".$row["id"]."', '".$row["summa"]."', '".db_string($row["item"])."', '".db_string($newitem)."', '".db_string($row["login"])."', '".db_string($row["login2"])."', '".db_string($row["login"])."', '".db_string($row["dat"])."', '".date("Y-m-d H:i:s")."', '".db_string($row["md5"])."')");
				db_query("DELETE FROM `sapper_games_active` WHERE `id` = ".$row["id"]);
				db_query("UPDATE `knb_users` SET `balance` = `balance` + ".round($bank,2)." WHERE `login` = '".db_string($row["login"])."'");	
				$REFres = db_query("SELECT `ref` FROM `knb_users` WHERE `login`  = '".db_string($row["login"])."'");
				$REFrow = db_assoc($REFres);			
				if($REFrow['ref'] != NULL){
					db_query("UPDATE `knb_users` SET `balance` = `balance` + ".round($bank * procReferer,2)."  WHERE `login`  = '".db_string($REFrow['ref'])."'");
				}
				
				$REFres2 = db_query("SELECT `ref2` FROM `knb_users` WHERE `login`  = '".db_string($row["login"])."'");
				$REFrow = db_assoc($REFres2);			
				if($REFrow['ref2'] != NULL){
					db_query("UPDATE `knb_users` SET `balance` = `balance` + ".round($bank * procReferer2,2)."  WHERE `login`  = '".db_string($REFrow['ref2'])."'");
				}	
		}
	}
	//echo json_encode(array("type_gamer"=>'bbbbb',"type_creator"=>'dddd'));
db_close();
?>

<h1 style="text-align: left;">
<img  src="style/img/game.png" align="center">100 последних Ваших игр</h1>
<script>
$(function(){
    $( "#dialogs" ).dialog({
      autoOpen: false,
      
    });
	$( ".dialog_row" ).click(function(e) {
	  e.preventDefault();
	  
	  //var ids = $(this).attr("login_id");
	  var login = $(this).attr("login");
	  var md5 = $(this).attr("md5");
	  var primary_key = $(this).attr("primary_key");
	  
   	  var bet = $(this).attr("bet");
  
	  var datas = $(this).attr("datas");
	  var aaa = $(this).attr("idd");
	  var sss = 'Game: '+aaa+', Created: '+login+' in '+datas+', Bet: '+bet+'. Primary key: '+primary_key;
	  $("#id").html(aaa);
	  //$("#id_login").html(ids);
	  $("#login").html(login);
	  $("#md5_login").html(md5);
	  $("#primary_key").html(primary_key);
	  $("#sss").html(sss);
	  $("#datas").html(datas);
      $("#dialogs").dialog( "open");
	  $("#dialogs").dialog({ width: 530 });
	  
    });
});
</script>
<?php
db_connect();
	$res = db_query('SELECT * FROM `sapper_games_end` WHERE `login` = "'.db_string($_SESSION["login"]).'" OR `login2` = "'.db_string($_SESSION["login"]).'" ORDER BY `dat2` DESC LIMIT 100');
db_close();

echo '
<table class="table1">
  <tr class="title">
    <td>Сумма</td>
    <td>Игрок 1</td>
    <td>Предмет 1</td>
    <td>Игрок 2</td>
    <td>Предмет 2</td>
	<td>Исход</td>
    <td>Время</td>
	<td>md5</td>
  </tr>
 ';
 
 
while($row = db_assoc($res)){ 
echo '
 <tr>
	<td>'.round($row['summa'] ,2).' руб.</td>
    <td><a target="_blank" href="/?p=wprofile&amp;login='.htmlspecialchars($row['login']).'">'.htmlspecialchars($row['login']).'</a></td>
    <td>'.$row["item"].'</td>
    <td><a target="_blank" href="/?p=wprofile&amp;login='.htmlspecialchars($row['login2']).'">'.htmlspecialchars($row['login2']).'</a></td>
    <td>'.$row["item2"].'</td>
	<td>'.($_SESSION["login"] == $row["winner"]?"<font color=\"#0F680B\">Победа</font>":"<font color=\"#f00\">Поражение</font>").'</td>
    <td>'.$row['dat2'].'</td>
	<td><center><a href="#" class="dialog_row" idd="'.$row[id].'" login="'.$row[login].'" login_id="'.$rows[id].'" datas="'.$row[dat2].'" md5="'.$row[md5].'" primary_key="'.md5($row[md5]).'" bet="'.$row[item].'" ids="'.$row[id].'" ><span class="protect_md5" title="Контроль невмешательства MD5"></span></a></center></li>
  </tr>';
 }
echo '</table>';
?>
<div class="" id="dialogs" title="Контроль невмешательства MD5">
  <p>Игра "КНБ", ID <b><span id="id_login"></span></b> (создатель: <b><span id="login"></span></b>).<br>
  MD5 Hash: <b><span id="md5_login"></span></b><br>
  MD5 Text: <br>
  <textarea cols="50" rows="4" id="sss"></textarea></p>
  
  
</div>