<?php
define('_IN_JOHNCMS', 1);
$headmod = 'tournament435';
$textl = 'Игра';
require_once ("../incfiles/core.php");
require_once ("../incfiles/head.php");

$q = @mysql_query("select * from `r_cupgame` where id='" . $id . "' LIMIT 1;");
$arr = @mysql_fetch_array($q);

if (empty($arr[id]))
{
    echo display_error('Игра не существует');
    require_once ('../incfiles/end.php');
    exit;
}

if (!empty($arr[rez1]) || !empty($arr[rez2]) || $arr[rez1] == '0' || $arr[rez2] == '0')
{
    echo display_error('Игра уже состоялась');
    require_once ('../incfiles/end.php');
    exit;
}

$req = @mysql_query("select * from `fman_matches` where turn='cup' AND id_match='" . $arr[id] . "';");
$res = @mysql_fetch_array($req);
$total = mysql_num_rows($req);

if ($total == 0)
{

$q1 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id1] . "' LIMIT 1;");
$arr1 = @mysql_fetch_array($q1);

$q2 = @mysql_query("select * from `fman_clubs` where id='" . $arr[id2] . "' LIMIT 1;");
$arr2 = @mysql_fetch_array($q2);



if (empty($arr1['id']) && empty($arr2['id']) && empty($arr1['name']) && empty($arr2['name']))
{
    echo display_error('Заявка не найдена');
    require_once ('../incfiles/end.php');
    exit;
}
$vaqt  = time();
$mtime = $vaqt+600;

//mysql_query("insert into `r_game` set 
//`chemp`='cup',
//`id_match`='".$id."',
//`time`='".$mtime."',

//`id_team1`='".$arr1['id']."',
//`id_team2`='".$arr2['id']."',
//`union_team1`='".$arr1['union']."',
//`union_team2`='".$arr2['union']."',
//`name_team1`='".$arr1['name']."',
//`name_team2`='".$arr2['name']."',
//`level_team1`='".$arr1['level']."',
//`level_team2`='".$arr2['level']."'
//;");

mysql_query("update `fman_matches` set
`time`='".$mtime."',
`end`='1',
`sc1`='".$arr1['sc1']."',
`sc2`='".$arr2['sc2']."',
`id1`='".$arr['id_team1']."',
`id2`='".$arr['id_team2']."'
;") or die(mysql_error());

$lastid = mysql_insert_id();
header('location: /man/match.php?id='.$lastid);

}
else
{
header('Location: /man/match.php?id='.$res[id]);
exit;
}


require_once ("../incfiles/end.php");
?>