<?php

define('_IN_JOHNCMS', 1);
$textl = 'Barcha o`yinlar';
$rootpath = '../../';
require_once("../../incfiles/core.php");
require_once("../../incfiles/head.php");

if (!$user_id)
    {
       $error = $lng['access_guest_forbidden'];
       echo functions::display_error($error, '<a href="index.php">' . $lng['back'] . '</a>');
       require_once ('../../incfiles/end.php');
       exit();
       
    }
 // Avto ishtirokchi qilish
$res = mysql_fetch_row(mysql_query("select * from `knb_users` where `user_id`='".$user_id."'"));
                if ($res==0)
                    {
                        mysql_query("insert into `knb_users` set
                            `user_id` = '". $user_id ."',
                            `name` = '".$login."'");
                    }
                    
$gameid = abs(intval($_GET['id']));
if (!empty($gameid))
    {
       
        $finish_game = mysql_result(mysql_query("select `opponent_user_id` from `knb_games` where `id`='".$gameid."'"),0);
            
            if($finish_game!=0){
                echo '<div class="rmenu">Bu o`yin o`ynalgan! </div>';
                
                }else {
                     mysql_query("update `knb_games` set 
                        `opponent_user_id`='".$user_id."',
                        `opponent_user_name` = '".$login."',
                        `opponent_choice` = '".mysql_real_escape_string(functions::checkin($_POST['opponent_choice']))."' where `id`='".$gameid."'
                    ");
                    
                    
                    $result = mysql_fetch_assoc(mysql_query("select * from `knb_games` where `id`='". $gameid ."'"));
                    $new_balans = 2* $result['stavka'];
                    mysql_query("update `users` set `balans`=balans-".$result['stavka']." where `id`='".$user_id."'");
                           
                    $ishod = '';
                    if (($_POST['opponent_choice']=='Tosh') && ($result['choice'])=='Tosh') $ishod='draw';
                    if (($_POST['opponent_choice']=='Tosh') && ($result['choice'])=='Qaychi')$ishod='win';
                    if (($_POST['opponent_choice']=='Tosh') && ($result['choice'])=='Qog`oz') $ishod='loss';
                    
                    if (($_POST['opponent_choice']=='Qaychi') && ($result['choice'])=='Tosh') $ishod='loss';
                    if (($_POST['opponent_choice']=='Qaychi') && ($result['choice'])=='Qaychi') $ishod='draw';
                    if (($_POST['opponent_choice']=='Qaychi') && ($result['choice'])=='Qog`oz') $ishod='win';
                    
                    if (($_POST['opponent_choice']=='Qog`oz') && ($result['choice'])=='Tosh') $ishod='win';
                    if (($_POST['opponent_choice']=='Qog`oz') && ($result['choice'])=='Qaychi') $ishod='loss';
                    if (($_POST['opponent_choice']=='Qog`oz') && ($result['choice'])=='Qog`oz') $ishod='draw';
                    
                   
                    
                    if ($ishod=='win') 
                        {
                            echo '<div class="gmenu">Siz yutdingiz.Balansingizga qo`shilgan tanga: <strong>'.$result['stavka'].'</strong><br />';

                            mysql_query("update `knb_users` set 
                                `games` = games + 1,
                                `win`= win + 1
                                 where `user_id`='".$user_id."'
                            ");
                            mysql_query("update `users` SET `balans` = balans + ".$new_balans." where `id` = '".$user_id."'");
                            mysql_query("update `knb_users` set 
                                `games` = games + 1,
                                `loss` = loss + 1 where `user_id`='".$result['create_user_id']."'
                            ");
                        }
                    
                    if ($ishod=='loss') 
                        {
                            echo '<div class="rmenu">Siz yutqazdingiz! Balnsingizdan ayrilgan tanga: <strong>'.$result['stavka'].'</strong><br />';

                            mysql_query("update `knb_users` set 
                                `games` = games + 1,
                                `loss` = loss + 1
                                 where `user_id`='".$user_id."'
                            ");
                            
                            mysql_query("update `knb_users` set 
                                `games` = games + 1,
                                `win` = win + 1
                                 where `user_id`='".$result['create_user_id']."'
                            ");
							mysql_query("update `users` SET `balans` = balans + ".$new_balans." where `id` = '".$result['create_user_id']."'");
                            
                        }
                    if ($ishod=='draw') 
                        {
                            echo '<div class="bmenu">Durrang! Tikilgan stavkangiz qaytarildi.<br />';
                            mysql_query("update `knb_users` set 
                                `games` = games + 1,
                                `draw`= draw + 1
                                 where `user_id`='".$user_id."'
                            ");
							mysql_query("update `users` SET `balans` = balans + ".$result['stavka']." where `id`='".$user_id."'");
							mysql_query("update `users` SET `balans` = balans + ".$result['stavka']." where `id`='".$result['create_user_id']."'");
                            
                            mysql_query("update `knb_users` set 
                                `games` = games + 1,
                                `draw` = draw + 1
                                 where `user_id`='".$result['create_user_id']."'
                            ");
                        }
                        
                    echo "Sizni tanlovingiz: <strong>".htmlspecialchars(trim($_POST['opponent_choice']))."</strong><br />";
                    echo "Raqibingiz tanlovi: <strong>".$result['choice']."</strong><br />";
                    echo '</div>';
                    
                    echo '<a href="index.php">Qaytish</a>';
                    require_once ('../../incfiles/end.php');
                    exit();
                }
    }
$userinfo = mysql_fetch_assoc(mysql_query("select * from `knb_users` where `user_id`='".$user_id."'"));
$userball = mysql_fetch_assoc(mysql_query("select * from `users` where `id`='".$user_id."'"));
?>
<div class="phdr" align="center">Tosh, qaychi, qog'oz</div>
<?
echo'<table class="table"  width="100%" border="0" cellspacing="1" cellpadding="4">

    <tr class="list1"><td>Sizning balans</td><td align="center"> <b>'.$userball['balans'].'</b></td></tr>
    <tr class="list2"><td>Jami o`yinlar</td><td align="center"> <b>'.$userinfo['games'].'</b></td></tr>
    <tr class="list1"><td>Yutuq</td><td align="center"> <b>'.$userinfo['win'].'</b></td></tr>
    <tr class="list2"><td>Durrang</td><td align="center"> <b>'.$userinfo['draw'].'</b></td></tr>
    <tr class="list1"><td>Mag`lubiyat</td><td align="center"> <b>'.$userinfo['loss'].'</b></td></tr>
	</table>
    <div class="gmenu" align="center"><a href="rating.php" style="font-weight:bold;">O`yinchilar reytingi</a></div>';
	?>
<table class="table"  width="100%" border="0" cellspacing="1" cellpadding="4">
    
    <tr height="200%" class="un_title">
        
            <td width="2%" align="center">№</td>
            
            <td width="20%" align="center">O`yin boshlagan</td>
            <td width="5%" align="center">Stavka</td>
            
            <td>Sizning tanlov</td>
        
   
    </tr>
<?php
$i=1;
$sql= mysql_query("select * from `knb_games` where `opponent_user_id`='0'");
while ($res = mysql_fetch_assoc($sql))
    {
        echo $i % 2 ? '<tr class="list2">' : '<tr class="list1">';
            echo '<td align="center">'.$i.'</td>';
            echo '<td width="20%" align="center"><a href="history.php?id='.$res['create_user_id'].'">'.$res['create_user_name'].'</a></td>';
            echo '<td width="5%" align="center">'.$res['stavka'].'</td>';
            
                if ($res['create_user_id']!=$user_id)
                  {
                    echo '<td>';
                        ?>
                        <form action="index.php?id=<?php echo $res['id']; ?>" method="POST">
                            <select name="opponent_choice">
                                <option>Tosh</option>
                                <option>Qaychi</option>
                                <option>Qog`oz</option>
                            </select>
                            
                            <input type="submit" name="play" value="O'ynash" class="write" />
                        </form>
                        <?php
                    echo '</td>';
                    } else
                      {
                        ?> <td> <strong><?php echo $res['choice'];   ?> </strong></td> <?php
                      }
        
        echo '</tr>';
        $i++;
    } 
?>
</table><p>
<div ><a href="create_game.php"><input type="button" value="O`yin boshlash" class="write"/></a></div></p>
<?php
require_once ('../../incfiles/end.php');