<?php
#Class Sims
#Автор FeST [ICQ 3452223]

Class Sims{
	
	var $id;
	
	function item($id_i,$id,$skill)
	{	
		global $sims_user;
		global $diz;
		$work_id = mysql_result(mysql_query("SELECT `work_time` FROM `sims_user` WHERE `id_u`='".$id."'"),0);
		if($work_id>0)
		{	
		
		$msg = '<div class="'.$diz['post_1'].'">Вы сейчас не можете зайти домой, так как вы сейчас на работе</div>';
		echo $msg;
		Sims::navigation();
		Sims::sims_life();
		}
		else
		{
		$result = mysql_result(mysql_query("SELECT COUNT(*) FROM `sims_i` WHERE `id_u`='".$id."' AND `id`='".$id_i."'"),0);
			switch($sims_user['place'])
			{
			case 'kitchen':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/112.png" /> <b style="font-size:1.3em;">Кухня</b>';
			break;
			case 'toilet':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/toilet_paper.png" /> <b style="font-size:1.3em;">Санузел</b>';
			break;
			case 'room':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/sp.png" /> <b style="font-size:1.3em;">Спальня</b>';
			break;
			case 'guest':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/20.png" /> <b style="font-size:1.3em;">Гостинная</b>';
			break;
			
			}
			$msg .= '<b style="font-size:1.3em;float:right;">'.date("H:i",time()).'</b></div>';
		if($result!=0)
		{	
			$msg .= '<div class="'.$diz['post_1'].'"><img src="i/reload.png" /> <a style="font-weight:bold;color:#99CC00;text-decoration:underline" href="item.php?id='.$id_i.'&amp;skill='.$skill.'&amp;rand='.mt_rand().'"> Обновить</a><br/><table>';
			$item = mysql_query("SELECT * FROM `sims_i` WHERE `id`='".$id_i."'");
			$arr = mysql_fetch_array($item);
			if($sims_user['last_id']!=$arr['id'] || $sims_user['last_type']!=$skill)
			{
				if($skill!='n')$arr['time']=$arr['time']*3;
				mysql_query("UPDATE `sims_user` SET `last_type`='".$skill."',`last_update`='".(time()+$arr['time'])."',`last_id`='".$arr['id']."' WHERE `id_u`='".$sims_user['id_u']."'");
				header("Location:item.php?id=".$id_i."&skill=".$skill);
				exit;
			}
			
			
			
			switch($skill)
			{	
			
			
				case'n':
				if($sims_user['last_update']<time())
				{	
				$add = '';
					$ex = explode(':',$arr['plus']);
						if($ex[1]!=0)
						{
						if(($sims_user['culinar']+$ex[1])>100)$add .="`culinar`='100',";
						else $add .="`culinar`=`culinar`+".$ex[1].",";
						}
						if($ex[3]!=0)
						{
						if(($sims_user['comfort']+$ex[3])>100)$add .="`comfort`='100',";
						else $add .="`comfort`=`comfort`+".$ex[3].",";
						}
						if($ex[5]!=0)
						{
						if(($sims_user['nujda']+$ex[5])>100)$add .="`nujda`='100',";
						else $add .="`nujda`=`nujda`+".$ex[5].",";
						}
						if($ex[7]!=0)
						{
						if(($sims_user['bodrost']+$ex[7])>100)$add .="`bodrost`='100',";
						else $add .="`bodrost`=`bodrost`+".$ex[7].",";
						}
						if($ex[9]!=0)
						{
						if(($sims_user['dosug']+$ex[9])>100)$add .="`dosug`='100',";
						else $add .="`dosug`=`dosug`+".$ex[9].",";
						}
						if($ex[11]!=0)
						{
						if(($sims_user['obshenie']+$ex[11])>100)$add .="`obshenie`='100',";
						else $add .="`obshenie`=`obshenie`+".$ex[11].",";
						}
						if($ex[13]!=0)
						{
						if(($sims_user['gigiena']+$ex[13])>100)$add .="`gigiena`='100',";
						else $add .="`gigiena`=`gigiena`+".$ex[13].",";
						}
						if($ex[15]!=0)
						{
						if(($sims_user['yut']+$ex[15])>100)$add .="`yut`='100',";
						else $add .="`yut`=`yut`+".$ex[15].",";
						}
				mysql_query("UPDATE `sims_i` SET `iznos`=`iznos`+0.2 WHERE `id`='".$arr['id']."'");		
				mysql_query("UPDATE `sims_user` SET ".$add."`last_update`='0',`last_id`='0' WHERE `id_u`='".$sims_user['id_u']."'");
				header("Location:house.php");
				exit;
				}
				
				$msg .= "<tr class='".$diz['post_1']."'>
				<td colspan='2'>
				<img src='i/b_".$arr['id_i'].".png' />
				</td>
				<td><b>".$arr['name']."</b> (Осталось ".sims_timer($sims_user['last_update']-time()).")<br/><div style='font-size:11px'>
				<img src='i/comfort.png' /> ".sims_translit(mysql_result(mysql_query("SELECT `type` FROM `sims_item` WHERE `id`='".$arr['id_i']."'"),0))." (<a style='font-weight:bold;color:#99CC00;text-decoration:underline' href='house.php?end'>Закончить</a>)
				</div>
				</td>
				</tr>";
		
			break;
			
			case'k':
			case't':
			case'o':
			case'c':
			case'l':
			case'te':
			case'y':
			$skills = sims_get_skill($skill);
			
			$res = mysql_result(mysql_query("SELECT COUNT(*) FROM `sims_i` WHERE `id`='".$id_i."' AND `exp` LIKE '%$skills%'"),0);
			
				if($res!=0)
				{	
						if($sims_user['last_update']<time())
				{	
				$rand = rand(5,20);
				mysql_query("UPDATE `sims_i` SET `iznos`=`iznos`+0.2 WHERE `id`='".$arr['id']."'");		
				mysql_query("UPDATE `sims_user` SET `".$skills."`=`".$skills."`+".$rand.",`last_update`='0',`last_id`='0' WHERE `id_u`='".$sims_user['id_u']."'");
				header("Location:house.php");
				exit;
				}
				
					$msg .= "<tr class='".$diz['post_1']."'>
				<td colspan='2'>
				<img src='i/b_".$arr['id_i'].".png' />
				</td>
				<td><b>".$arr['name']."</b> (Осталось ".sims_timer($sims_user['last_update']-time()).")<br/><div style='font-size:11px'>
				<img src='i/skill.png' />Изучение <b>".sims_translit2($skills)."</b> (<a style='font-weight:bold;color:#99CC00;text-decoration:underline' href='house.php?end'>Закончить</a>)
				</div>
				</td>
				</tr>";
				}
					break;
					
			}
			$msg .= '</table></div>';
			
			echo $msg;
			Sims::navigation();
			Sims::sims_life();
			Sims::sims_level();
		}
	}
	}
	function house($id)
	{
			
		$city_id = mysql_result(mysql_query("SELECT `city_id` FROM `sims_user` WHERE `id_u`='".$id."'"),0);
		$house_id = mysql_result(mysql_query("SELECT `house_id` FROM `sims_user` WHERE `id_u`='".$id."'"),0);
		$work_id = mysql_result(mysql_query("SELECT `work_time` FROM `sims_user` WHERE `id_u`='".$id."'"),0);
		if($work_id>0)
		{	
		global $diz;
		$msg = '<div class="'.$diz['post_1'].'">Вы сейчас не можете зайти домой, так как вы сейчас на работе</div>';
		echo $msg;
		Sims::navigation();
		Sims::sims_life();
		}
		else
		{
		if($city_id == 0)
		{
			
			$all_house = mysql_result(mysql_query("SELECT COUNT(*) FROM `sims_city`"),0);
			
			if($all_house == 0)
			{
				$error = 'Извините, но городов еще нет =( <br /> Обратитесь к администрации сайта';
				echo $error;
				return true;
			}
			else
			{
			global $diz;
			
			$msg = '<div class="'.$diz['post_1'].'">Выберите город, в котором вы хотите жить :</div>';
			
			$city = mysql_query("SELECT * FROM `sims_city`");
			while($arr = mysql_fetch_array($city))
			$msg .= "<div class='".$diz['post_1']."'><img src='i/".$arr['image']."' /><br/><a href='city_in.php?id=".$arr['id']."'><b>".$arr['name']."</b></a></div>";
			
			echo $msg;
			return true;
			}
			
		}
		else
		{
			global $diz;
			
			$place_id = mysql_result(mysql_query("SELECT `place` FROM `sims_user` WHERE `id_u`='".$id."'"),0);

			$result = mysql_result(mysql_query("SELECT `place` FROM `sims_user` WHERE `id_u`='".$id."'"),0);
			switch($result)
			{
			case 'kitchen':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/112.png" /> <b style="font-size:1.3em;">Кухня</b>';
			break;
			case 'toilet':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/toilet_paper.png" /> <b style="font-size:1.3em;">Санузел</b>';
			break;
			case 'room':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/sp.png" /> <b style="font-size:1.3em;">Спальня</b>';
			break;
			case 'guest':
			$msg = '<div class="'.$diz['post_1'].'"><img src="i/20.png" /> <b style="font-size:1.3em;">Гостинная</b>';
			break;
			
			}
			$msg .= '<b style="font-size:1.3em;float:right;">'.date("H:i",time()).'</b></div>';
			$f = mysql_query("SELECT * FROM `sims_i` WHERE `id_u`='".$id."' AND `place`='".$result."'");
			$count = mysql_result($f,0);
			
			if($count)
			{
			$f = mysql_query("SELECT * FROM `sims_i` WHERE `id_u`='".$id."' AND `place`='".$result."'");

			$msg .= '<div class="'.$diz['post_1'].'"><table>';
			while($arr = mysql_fetch_array($f))
			{
				$add = '';
				$explode = explode(':',$arr['plus']);
				if($explode[1]!=0)$add .='<img src="i/comfort.png" />Питание '.$explode[1];
				if($explode[3]!=0)$add .='<img src="i/comfort.png" />Комфорт '.$explode[3];
				if($explode[5]!=0)$add .='<img src="i/comfort.png" />Нужда '.$explode[5];
				if($explode[7]!=0)$add .='<img src="i/comfort.png" />Бодрость '.$explode[7];
				if($explode[9]!=0)$add .='<img src="i/comfort.png" />Досуг '.$explode[9];
				if($explode[11]!=0)$add .='<img src="i/comfort.png" />Общение '.$explode[11];
				if($explode[13]!=0)$add .='<img src="i/comfort.png" />Гигиена '.$explode[13];
				if($explode[15]!=0)$add .='<img src="i/comfort.png" />Уют '.$explode[15];
				
				$add1 = '';
				$explode = explode(':',$arr['exp']);
				$counts = count($explode);
				$k=0;
				$t=0;
				$o=0;
				$c=0;
				$l=0;
				$te=0;
				$u=0;
				if($counts!=0)
				{
					for($i=0;$i<($counts+1);$i++)
					{
					if($explode[$i]=='culinaria')$k=1;
					if($explode[$i]=='tehnika')$t=1;
					if($explode[$i]=='obayanie')$o=1;
					if($explode[$i]=='culture')$c=1;
					if($explode[$i]=='logika')$l=1;
					if($explode[$i]=='tvorchesto')$te=1;
					if($explode[$i]=='uborka')$u=1;
					}
				
				}
				if($k!=0)$add1 .='<img src="i/skill.png" /> <a href="item.php?id='.$arr['id'].'&amp;skill=k">Кулинария</a> ';
				if($t!=0)$add1 .='<img src="i/skill.png" /> <a href="item.php?id='.$arr['id'].'&amp;skill=t">Техника</a> ';
				if($o!=0)$add1 .='<img src="i/skill.png" /> <a href="item.php?id='.$arr['id'].'&amp;skill=o">Обаяние</a> ';
				if($c!=0)$add1 .='<img src="i/skill.png" /> <a href="item.php?id='.$arr['id'].'&amp;skill=c">Культура тела</a> ';
				if($l!=0)$add1 .='<img src="i/skill.png" /> <a href="item.php?id='.$arr['id'].'&amp;skill=l">Логика</a> ';
				if($te!=0)$add1 .='<img src="i/skill.png" /> <a href="item.php?id='.$arr['id'].'&amp;skill=te">Творчество</a> ';
				if($u!=0)$add1 .='<img src="i/skill.png" /> <a href="item.php?id='.$arr['id'].'&amp;skill=y">Уборка</a> ';
				
				
				
				
				$msg .= "<tr class='".$diz['post_1']."'>
				<td colspan='2'>
				<img src='i/b_".$arr['id_i'].".png' />
				</td>
				<td><a href='item.php?id=".$arr['id']."&amp;skill=n'><b>".$arr['name']."</b></a><br/><div style='font-size:11px'>
				<img src='i/repair.png' /><a href='repair.php?id=".$arr['id']."'>Износ : ".$arr['iznos']."%</a> ".$add." ".($add1!='' ? "<span style='font-weight:bold;color:#99CC00;'>Изучать</span>".$add1 : "")."
				</div>
				</td>
				</tr>";
			
			}
			$msg .= '
			<tr class="'.$diz['post_1'].'">
				<td colspan="2">
				<img src="i/b_buy.png" />
				</td>
				<td><a href="buy_room.php?room='.$result.'"><b>Купить место</b></a><br/>
				400$
				</td>
				</tr>
			</table></div>';
			}
			else
			$msg .='У вас нет ничего в этой комнате';
			echo $msg;
		
		Sims::navigation();
		Sims::sims_life();
		}
		}
	
	}

	
	
	
	function navigation()
	{
	global $diz;
	global $sims_user;
	$msg ='<div class="'.$diz['post_1'].'" style="font-size:11px">
	<table width="100%">
	<tr>
	<td align="center">
	<a style="display:block" href="house.php?place=guest"><b><u>Гостиная</u></b> ['.$sims_user['room_1'].']</a>
	</td>
	<td>|</td>
	<td align="center">
	<a style="display:block" href="house.php?place=toilet"><b><u>Санузел</u></b> ['.$sims_user['room_2'].']</a>
	</td>
	<td>|</td>
	<td align="center">
	<a style="display:block" href="house.php?place=kitchen"><b><u>Кухня</u></b> ['.$sims_user['room_3'].']</a>
	</td>
	<td>|</td>
	<td align="center">
	<a style="display:block" href="house.php?place=room"><b><u>Спальня</u></b> ['.$sims_user['room_4'].']</a>
	</td>
	
	</tr>
	</table>
	
	</div>';
	
	
	
	
	
	echo $msg;
	
	}
	function sims_life()
	{
		global $sims_user;
		global $diz;
		
		echo "<div class='".$diz['post_2']."' style='font-size:11px'>
		<img src='i/comfort.png' /> Питание ".sims_color($sims_user['culinar'])."
		<img src='i/comfort.png' /> Комфорт ".sims_color($sims_user['comfort'])."
		<img src='i/comfort.png' /> Нужда ".sims_color($sims_user['nujda'])."
		<img src='i/comfort.png' /> Бодрость ".sims_color($sims_user['bodrost'])."
		<img src='i/comfort.png' /> Досуг ".sims_color($sims_user['dosug'])."
		<img src='i/comfort.png' /> Общение ".sims_color($sims_user['obshenie'])."
		<img src='i/comfort.png' /> Гигиена ".sims_color($sims_user['gigiena'])."
		<img src='i/comfort.png' /> Уют ".sims_color($sims_user['yut'])."
		<img src='i/comfort.png' /> Доллары ".$sims_user['money']."
		<img src='i/comfort.png' /> Кредиты ".$sims_user['credits']."
		</div>";
	
	
	}
	function sims_level()
	{
		global $sims_user;
		global $diz;
		
		echo "
		<div class='".$diz['post_2']."' style='font-size:11px'>
		<img src='i/skill.png' /> Кулинария ".$sims_user['culinaria']." [".$sims_user['culinaria_level']."]
		<img src='i/skill.png' /> Техника ".$sims_user['tehnika']." [".$sims_user['tehnika_level']."]
		<img src='i/skill.png' /> Обаяние ".$sims_user['obayanie']." [".$sims_user['obayanie_level']."]
		<img src='i/skill.png' /> Культура тела ".$sims_user['culture']." [".$sims_user['culture_level']."]
		<img src='i/skill.png' /> Логика ".$sims_user['logika']." [".$sims_user['logika_level']."]
		<img src='i/skill.png' /> Творчество ".$sims_user['tvorchesto']." [".$sims_user['tvorchesto_level']."]
		<img src='i/skill.png' /> Уборка ".$sims_user['uborka']." [".$sims_user['uborka_level']."]
		</div>";
	
	
	}
	
	function life()
	{
		
		global $sims_user;
		
		if(time()>$sims_user['last'])
		{
		
			$res = (time()-$sims_user['last'])/1800;
			
			if($res<1)
			return false;
			else
			{
				if($res > 70)
				$res = 70;
			
				$update = (($sims_user['culinar']-$res)<0 ? 0 : $sims_user['culinar']-$res);
				$update2 = (($sims_user['comfort']-$res)<0 ? 0 : $sims_user['comfort']-$res);
				$update3 = (($sims_user['nujda']-$res)<0 ? 0 : $sims_user['nujda']-$res);
				$update4 = (($sims_user['bodrost']-$res)<0 ? 0 : $sims_user['bodrost']-$res);
				$update5 = (($sims_user['dosug']-$res)<0 ? 0 : $sims_user['dosug']-$res);
				$update6 = (($sims_user['obshenie']-$res)<0 ? 0 : $sims_user['obshenie']-$res);
				$update7 = (($sims_user['gigiena']-$res)<0 ? 0 : $sims_user['gigiena']-$res);
				$update8 = (($sims_user['yut']-$res)<0 ? 0 : $sims_user['yut']-$res);
			
				mysql_query("UPDATE `sims_user` SET `yut`='".$update8."',`gigiena`='".$update7."',`obshenie`='".$update6."',`dosug`='".$update5."',`bodrost`='".$update4."',`nujda`='".$update3."',`last`='".time()."',`culinar`='".$update."',`comfort`='".$update2."' WHERE `id_u`='".$sims_user['id_u']."'");
	
	return true;
			}
	

		}
	}

	function protect()
	{
	global $sims_user;
	
		
		if($sims_user['culinar']<=0 || $sims_user['comfort']<=0 || $sims_user['nujda']<=0 || $sims_user['bodrost']<=0 || $sims_user['dosug']<=0 || $sims_user['obshenie']<=0 || $sims_user['gigiena']<=0 || $sims_user['yut']<=0)
		return false;
		else
		return true;
	
	
	}
}
?>
