<?php
include('cfg.php');
$cost_save=20;
$cost_load=5;
echo '<link rel="stylesheet" type="text/css" href="style.css"/>
<body>
<div class="head">'.$date.'-['.$user['country'].'] </div>';
switch ($_GET['act']) {
  case "":

echo '<div class="money"><u>Выберите действие:</u></div>
<a class="razd" href="save_load.php?act=save">Сохранить (<b>'.$cost_save.'</b> золота)</a>
<a class="razd" href="save_load.php?act=load">Загрузить (<b>'.$cost_load.'</b> золота)</a>';
    break;
  case "save":
	if($user['gold'] >= $cost_save)
    {
$workers=$user['workers'];
$sc=$user['scientists'];
$solders=$user['solders'];
$horses=$user['horses'];
$archers=$user['archers'];
$gold=$user['gold']-$cost_save;
//считаем всех рабов
$sql=mysql_query("SELECT * FROM res_get WHERE id_user='$id'");
while($a=mysql_fetch_array($sql))
{
	$workers+=$a['workers'];
}
$sql=mysql_query("SELECT * FROM teach WHERE id_user='$id'");
while($a=mysql_fetch_array($sql))
{
	$workers+=$a['amount'];
}
//считаем учёных
$sql=mysql_query("SELECT * FROM teach WHERE id_user='$id'");
while($a=mysql_fetch_array($sql))
{
	$sc+=$a['sc'];
}
$sql=mysql_query("SELECT * FROM sc WHERE id_user='$id'");
while($a=mysql_fetch_array($sql))
{
	$sc+=$a['sc'];
}
//возвращаем армии
$sql=mysql_query("SELECT * FROM wars WHERE id_attacker='$id'");
while($a=mysql_fetch_array($sql))
{
	$solders+=$a['solders'];
	$horses+=$a['horses'];
	$archers+=$a['archers'];
}
$sql=mysql_query("SELECT * FROM ubuildings WHERE owner_id='$id'");
while($a=mysql_fetch_array($sql))
{
	$solders+=$a['solders'];
	$archers+=$a['archers'];
}

$buildings='';
$sql=mysql_query("SELECT * FROM buildings");
while($b=mysql_fetch_array($sql))
{
	$check=mysql_query("SELECT * FROM ubuildings WHERE owner_id='$id' AND sname='$b[sname]'");
	if(mysql_num_rows($check) == 1)
	{
		$bld=mysql_fetch_array($check);
		$buildings.='|'.$bld[name].','.$bld[lvl].'';
	}
	else
	{
		$buildings.='|none,0';
	}
}
$buildings.='|stop,';
$pass=rand(100,999);
mysql_query("INSERT INTO save SET
login='$user[login]',
country='$user[country]',
pass='$pass',
workers='$workers',
scientists='$sc',
solders='$solders',
horses='$horses',
archers='$archers',
solders_a='$user[solders_a]',
solders_s='$user[solders_s]',
horses_a='$user[horses_a]',
horses_s='$user[horses_s]',
archers_a='$user[archers_a]',
archers_s='$user[archers_s]',
money='$user[money]',
iron='$user[iron]',
lumber='$user[lumber]',
grain='$user[grain]',
stone='$user[stone]',
land='$user[land]',
total_land='$user[total_land]',
woods='$user[woods]',
mountains='$user[mountains]',
freeplace='$user[freeplace]',
spy='$user[spy]',
sab='$user[sab]',
vor='$user[vor]',
verb='$user[verb]',
west_look='$user[west_look]',
east_look='$user[east_look]',
unrest='$user[unrest]',
nalog='$user[nalog]',
sc_lumber='$user[sc_lumber]',
sc_stone='$user[sc_stone]',
sc_iron='$user[sc_iron]',
sc_grain='$user[sc_grain]',
sc_wood='$user[sc_wood]',
sc_lvl='$user[sc_lvl]',
sc_workers='$user[sc_workers]',
sc_army='$user[sc_army]',
sc_arive='$user[sc_arive]',
sc_savem='$user[sc_savem]',
sc_savew='$user[sc_savew]',
grab='$user[grab]',
weapon='$user[weapon]',
armor='$user[armor]',
guns='$user[guns]',
guns_lvl='$user[guns_lvl]',
guns_type='$user[guns_type]',
buy_calc='$user[buy_calc]',
lite='$user[lite]',
new_guy='$user[new_guy]',
gold='$gold',
last_gold='$user[last_gold]',
sovet='$user[sovet]',
buildings='$buildings',
time_reg='$user[time_reg]'
");
echo '<div class="money">Страна успешно сохранена';
mysql_query("UPDATE users SET gold=gold-'$cost_save' WHERE id='$id'");
	}
	else
	{
		echo '<div class="money">У вас не достаточно золота(нужно <b>'.$cost_save.'</b>)';
	}
echo '</div><a class="razd" href="save_load.php">Назад</a>';
    break;
  case "load":
	if($user['gold'] >= $cost_load)
    {
$last_save=mysql_query("SELECT * FROM save WHERE login='$user[login]' ORDER BY id DESC LIMIT 1");
if(mysql_num_rows($last_save) == 1)
{
	$s=mysql_fetch_array($last_save);
mysql_query("UPDATE users SET
workers='$s[workers]',
scientists='$s[scientists]',
solders='$s[solders]',
horses='$s[horses]',
archers='$s[archers]',
solders_a='$s[solders_a]',
solders_s='$s[solders_s]',
horses_a='$s[horses_a]',
horses_s='$s[horses_s]',
archers_a='$s[archers_a]',
archers_s='$s[archers_s]',
money='$s[money]',
iron='$s[iron]',
lumber='$s[lumber]',
grain='$s[grain]',
stone='$s[stone]',
land='$s[land]',
total_land='$s[total_land]',
woods='$s[woods]',
mountains='$s[mountains]',
freeplace='$s[freeplace]',
spy='$s[spy]',
sab='$s[sab]',
vor='$s[vor]',
verb='$s[verb]',
west_look='$s[west_look]',
east_look='$s[east_look]',
unrest='$s[unrest]',
nalog='$s[nalog]',
sc_lumber='$s[sc_lumber]',
sc_stone='$s[sc_stone]',
sc_iron='$s[sc_iron]',
sc_grain='$s[sc_grain]',
sc_wood='$s[sc_wood]',
sc_lvl='$s[sc_lvl]',
sc_workers='$s[sc_workers]',
sc_army='$s[sc_army]',
sc_arive='$s[sc_arive]',
sc_savem='$s[sc_savem]',
sc_savew='$s[sc_savew]',
grab='$s[grab]',
weapon='$s[weapon]',
armor='$s[armor]',
guns='$s[guns]',
guns_lvl='$s[guns_lvl]',
guns_type='$s[guns_type]',
buy_calc='$s[buy_calc]',
lite='$s[lite]',
new_guy='$s[new_guy]',
last_gold='$s[last_gold]',
sovet='$s[sovet]',
time_reg='$s[time_reg]'
WHERE id='$id'
");
mysql_query("DELETE FROM ubuildings WHERE owner_id='$id'");
$b_info=explode("|",$s['buildings']);
$sql=mysql_query("SELECT * FROM buildings ");
$k=1;
while($b=mysql_fetch_array($sql))
{
	$bi=explode(",",$b_info[$k]);
	if($bi[0] != "none")
	{
		mysql_query("INSERT INTO ubuildings SET
		name='$bi[0]',
		sname='$b[sname]',
		lvl='$bi[1]',
		time_end='1',
		owner_id='$id',
		workers='0',
		end='1',
		time_start='2',
		land='100'
		");
	}
	$k++;
}
mysql_query("UPDATE users SET gold=gold-'$cost_load' WHERE id='$id'");
echo '<div class="money">Вы успешно загрузили последнее сохранение</div>';
}
else
{
	echo '<div class="money">У вас нету сохранений</b>)';
}
	}
	else
	{
		echo '<div class="money">У вас не достаточно золота(нужно <b>'.$cost_load.'</b>)';
	}
echo '</div><a class="razd" href="save_load.php">Назад</a>';
    break;

}
echo '<div class="money">Меню</div>
<a class="razd" href="game.php">'.$iback.' Назад</a>
<a class="razd" href="exit.php">'.$iexit.' Выход</a>
<div class="footer"><center><a href="http://wapworld.pp.ua">© WapWorld Games</a><br/><br />
<script type="text/javascript" src="http://mobtop.ru/c/73888.js"></script><noscript><a href="http://mobtop.ru/in/73888"><img src="http://mobtop.ru/73888.gif" alt="MobTop.Ru - Рейтинг и статистика мобильных сайтов"/></a></noscript>
</div>
</body></html>

';
?>