<?php 

define('H', $_SERVER['DOCUMENT_ROOT'].'/');
include_once H.'sys/inc/start.php';
include_once H.'sys/inc/compress.php';
include_once H.'sys/inc/sess.php';
include_once H.'sys/inc/settings.php';
include_once H.'sys/inc/db_connect.php';
include_once H.'sys/inc/ipua.php';
include_once H.'sys/inc/fnc.php';
include_once H.'sys/inc/user.php';
require 'config.php';

if (isset($user) && isset($_GET['stavka']) && isset($_GET['sid'])) 
{
  $coins = (int) $_GET['stavka'];
  $bilet = get_num();
  
  if (!isset($config['stavka'][$coins])) {
    $err = 'Такой ставки не существует..';
  }
  
  if (!isset($config['stavka'][$coins])) {
    $err = 'Такой ставки не существует!!';
  } elseif ($user[$config['coins_table']] < $config['stavka'][$coins]) {
    $err = 'У тебя не хватает средств!!!';
  }
  
  if ($_GET['sid'] != $_SESSION['sid']) {
    $err = 'Попробуй еще раз!!';
  }
  
  if (mysql_result(mysql_query("SELECT COUNT(*) FROM `kamikaze` WHERE `id_user` = '$user[id]' AND `status` = '0'"), 0) != 0) {
    $err = 'Ты еще эту игру не закончил!';
  }
  
  if (!isset($err)) {
    mysql_query("UPDATE `kamikaze` SET `status` = '2' WHERE `id_user` = '$user[id]' AND `status` = '1' LIMIT 1");
    mysql_query("UPDATE `user` SET `" . $config['coins_table'] . "` = `" . $config['coins_table'] . "` - '" . $config['stavka'][$coins] . "' WHERE `id` = '$user[id]' LIMIT 1");
    mysql_query("INSERT INTO `kamikaze`(`time`, `id_user`, `numbers`, `key`, `md5`, `step`, `table`, `stavka`, `over`) VALUES ('$time', '$user[id]', '$bilet[numbers]', '" . $bilet['key'] . "', '" . $bilet['md5'] . "', '0', '" . $config['coins_table'] . "', '" . $config['stavka'][$coins] . "', 'game')");
    header ('Location: ?game');
    exit;
  }
}

if (isset($user) && mysql_result(mysql_query("SELECT COUNT(*) FROM `kamikaze` WHERE `id_user` = '$user[id]' AND `status` != '2'"), 0) != 0) {
  $game = mysql_fetch_assoc(mysql_query("SELECT * FROM `kamikaze` WHERE `id_user` = '$user[id]' AND `status` != '2' LIMIT 1"));
  $numbers = explode('|', $game['numbers']);
} else {
  $game = array('step' => -1, 'stavka' => '0', 'over' => '0');
}

if (isset($user) && isset($_GET['over']) && isset($game['id'])) {
  if ($game['status'] == 0 && $game['over'] == 'game' && $game['step'] > 0) {
    $coins = ($game['stavka'] * $config['step'][$game['step']-1]);
    mysql_query("UPDATE `kamikaze` SET `status` = '2', `over` = 'win', `win` = '$coins' WHERE `id_user` = '$user[id]' AND `status` = '0' AND `over` = 'game' LIMIT 1");
    mysql_query("UPDATE `user` SET `" . $config['coins_table'] . "` = `" . $config['coins_table'] . "` + '" . $coins . "' WHERE `id` = '$user[id]' LIMIT 1");
    header ('Location: ?game');
    exit;
  }
}

if (isset($user) && $game['over'] == 'game' && isset($_GET['y']) && isset($_GET['x'])) 
{
  if ($_GET['y'] != $game['step']) {
    $err = 'Вот не надо тут подменять ссылки!';
  } 
  
  if (!isset($err)) 
  {
    if ($_GET['x'] != $numbers[$game['step']]) {
      mysql_query("UPDATE `kamikaze` SET `step` = `step` + '1' WHERE `id` = '$game[id]' LIMIT 1");
    } else {
      mysql_query("UPDATE `kamikaze` SET `over` = 'loss', `status` = '1' WHERE `id` = '$game[id]' LIMIT 1");
    }
    
    header ('Location: ?game');
    exit;
  }
}

if (isset($err)) {
  $voice = $err;
} elseif ($game['over'] == 'loss') {
  $voice = $_voice['loss'][mt_rand(0, count($_voice['loss'])-1)];
} elseif ($game['step'] >= 0 && $game['step'] <= 5) {
  $voice = $_voice['game'][0][mt_rand(0, count($_voice['game'][0])-1)];
} elseif ($game['step'] >= 6 && $game['step'] <= 8) {
  $voice = $_voice['game'][1][mt_rand(0, count($_voice['game'][1])-1)];
} elseif ($game['step'] == 9) {
  $voice = $_voice['game'][2][mt_rand(0, count($_voice['game'][2])-1)];
} elseif ($game['step'] == 10) {
  $voice = $_voice['game'][3][mt_rand(0, count($_voice['game'][3])-1)];
} elseif (!isset($user)) {
  $voice = 'Тебе нужно <a href="/aut.php">авторизоваться</a>!';
} else {
  $voice = 'Делай ставку, и полетели!!!';
}

$session = mt_rand(11111111, 99999999);
$_SESSION['sid'] = $session;
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<head>    
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Камикадзе</title>

<!--[if lt IE 9]>
  <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="shortcut icon" href="images/favicon.ico" />
<link rel="stylesheet" href="css/main.css" type="text/css" />
<meta name="description" content="Увлекательная игра Камикадзе" />
<meta name="keywords" content="Камикадзе, игра, мини-игры, Kamikaze, лотереи" />
<style>
<? if ($game['over'] == 'game') { ?>
@keyframes move {
  0% {
    background-position: -<?= ($game['step']-1) * 59?>px 0px;
  }
}
<? } ?>

<? if ($game['over'] == 'game' && $game['step'] == count($config['step'])) { ?>
@keyframes bomb {
  from {
    background-position: -4px 70px;
  }
  to {
    background-size: 0px, auto;
    background-position: 30px 180px;
  }
}

.samolet-bomb {
  background-image: url(images/bang3.png), url(images/bomb.png);
  background-size: auto, 0px;
  animation: bomb 1s 1 linear;
  -webkit-animation: bomb 1s 1 linear;
}
<? } ?>

.body {
  background-position: -<?= $game['step'] * 60?>px 0px;
  animation: move 1s 0 linear;
  -webkit-animation: move 1s 0 linear;
}
</style>
</head>
<body>
<div class="body">
  <div class="foot">
    <a href="<?= $config['url']?>">Игры</a> / <a href="?">Камикадзе</a>
  </div>
  
  <? if (isset($_GET['game'])) { ?>
  <div class="samolet<?= ($game['over'] == 'loss' ? ' samolet-die' : '')?>">
    <div class="samolet-bomb">
      <div class="trava">
        <div class="voice">
          <?= $voice?>
        </div>
        
        <div class="display">
          <table style="width: 100%" cellpadding="0" cellspacing="0">
            <tr>
              <td class="cash"><?= (isset($user) && $user[$config['coins_table']] >= 1 ? $user[$config['coins_table']] : '<a href="' . $config['billing_url'] . '">Пополнить</a>')?></td>
              <td class="win"><?= ($game['over'] == 'game' && $game['step'] > 0 ? ($game['stavka'] * $config['step'][$game['step']-1]) : '0')?></td>
            </tr>
          </table>
        </div>

        <table class="table_kamikaze" cellpadding="0">
        <? 
        arsort($config['step']);
        
        foreach($config['step'] AS $key => $value) { ?>
          <tr>
          <? for($x = 1; $x <= 5; $x++) { ?>
            <td class="bomb <?= ($game['step'] == $key && $game['over'] != 'loss' ? 'bomb-game' : '')?> <?= (isset($numbers) && $game['step'] > $key && $numbers[$key] == $x ? 'bomb-win' : '')?> <?= (isset($numbers) && $game['step'] == $key && $numbers[$key] == $x && $game['over'] == 'loss'  ? 'bomb-win back-red' : '')?> <?= (isset($numbers) && $game['step'] < $key && $numbers[$key] == $x && $game['over'] == 'loss' ? 'bomb-loss' : '')?>"><a <?= ($game['over'] == 'game' && $game['step'] == $key ? 'href="?game&amp;x=' . $x . '&amp;y=' . $key . '"' : '')?>>&zwnj;</a></td>
          <? } ?>
            <td class="info"><a <?= (($game['step'] - 1) == $key && $game['over'] != 'loss' ? 'href="?game&amp;over=' . ($key + 1) . '" class="text-red"' : '')?>>Стоп x<?= $value?></a></td>
          </tr>
        <? } ?>
        </table>
        
        <div class="display">
          <table cellpadding="0">
            <tr>
            <? for($y = 0; $y < 6; $y++) { ?>
              <td class="stavka"><a href="?game&amp;stavka=<?= $y?>&amp;sid=<?= $session?>"><?= $config['stavka'][$y]?></a></td>
            <? } ?>
            </tr>
          </table>
        </div>
      </div>
    </div>
  </div>
  <? } else { ?>
  <div class="desc">
    - Игра &laquo;Камикадзе&raquo; на <?= $config['coins_name'][1]?> cайта!<br />
    - Поле из 10 рядов по 5 клеток. В каждом ряду по 1 мине. <br />
    - Пройти как можно выше, двигаясь снизу вверх.  <br />
    - Остановиться можно на любом ходе и забрать выигрыш кнопкой СТОП. <br />
  </div>
  
  <div class="button">
    <a href="?game"><img src="images/button-play.png" /></a>
  </div>
  
  <div class="desc">
    &laquo;Последние 5 выигрышей&raquo;<br />
    <?
    $q = mysql_query("SELECT * FROM `kamikaze` WHERE `over` = 'win' ORDER BY id DESC LIMIT 5");
    
    while($post = mysql_fetch_assoc($q)) {
      $ank = get_user($post['id_user']);
      
      echo '<a href="/info.php?id=' . $ank['id'] . '">' . $ank['nick'] . '</a> (' . vremja($post['time']) . ') выиграл ' . des2num($post['win'], $config['coins_name']) . '<br />';
    }
    ?>
  </div>
  <? } ?>

<div class="foot">
  <a href="<?= $config['url']?>">Игры</a> / <a href="?">Камикадзе</a>
</div>
</div>
</body>
</html>