<?php
ob_start();
$c_host = 'localhost';
$c_login = 'civa';
$c_pass = 'gsd12a';
$c_db = 'civa';
$link = mysql_connect($c_host, $c_login, $c_pass)
  or
die ("Не возможно присоединитса к Базе Данных. Сервер возможно перезагружаетса. Обновите страницу через пару секунд");

mysql_select_db ($c_db)
  or die ("Could not select database");
  mysql_query("SET NAMES 'windows-1251'", $link);
  mysql_set_charset("utf8");

header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: private");
header("Cache-Control: max-stale=0");
header("Content-type: text/html; charset=utf8");

  error_reporting(0);
$time = $_SERVER['REQUEST_TIME'];
$date = date('H:i:s',$time);
$dt=date("Y-m-d H:i:s");
$self=$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];


function send_mess($l_to,$what,$a,$b)
{
$whom=mysql_fetch_array(mysql_query("SELECT * FROM users WHERE login='$l_to'"));
	if($whom['login'] != "")
	{
$to_whom_id=$whom[id];
$when=date('G:i:s');
$login_send= $a;
$id_send=$b;
  $as=$time+5;
mysql_query("UPDATE users SET msg_send='$as' WHERE id='$id'");
mysql_query("
INSERT INTO `msg` (
`id` ,
`by_whom` ,
`by_whom_id` ,
`to_whom` ,
`to_whom_id` ,
`when` ,
`what` ,
`read`,
`input`,
`output`
)
VALUES (
NULL , '$login_send', '$id_send', '$l_to', '$to_whom_id', '$when', '$what', '0','1','1'
)
");
	}
}



$sql_cas = mysql_query("SELECT * FROM `castles` WHERE `owner_clan`!='0'");
while($res = mysql_fetch_assoc($sql_cas))
	{
	$c = mysql_fetch_array(mysql_query("SELECT * FROM `clan` WHERE `id`='$res[owner_clan]'"));
	if ($res[type]=='1') $type = 'grain';
	if ($res[type]=='2') $type = 'lumber';
	if ($res[type]=='3') $type = 'stone';
	if ($res[type]=='4') $type = 'money';
	if ($res[type]=='5') $type = 'iron';

	$all_members = mysql_result(mysql_query("SELECT count(id) FROM `users` WHERE `clan`='$res[owner_clan]'"),0);
	$all_res = $res['count'];
	$res_get = intval($res['count']/$all_members);


	mysql_query("UPDATE `users` SET `".$type."`=".$type."+'$res_get' WHERE `clan`='$res[owner_clan]'");
	$usql=mysql_query("SELECT * FROM users WHERE defeat='0' AND `clan`='$res[owner_clan]'");
	while($u=mysql_fetch_assoc($usql))
	{
	echo $u[country].' получил '.$res_get.' '.$type;
	$text='Ваша доля от дохода замка: <b>'.$res_get.'</b> ед. ресурса';
	send_mess($u['login'],$text,'Система','');
	}







	}









?>