<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');

if($user['level']<'5'){
    header('Location: main.php');
    exit;
}


if(isset($_GET['contine'])){
	$sh = mysql_fetch_assoc(mysql_query("SELECT * FROM `ship` WHERE `user`='$uid'"));
	mysql_query("UPDATE `ship` SET `hp`='$sh[hpall]' WHERE `user`='$uid'");
	
	header('Location: sea.php?sd=sea');
	exit;
	
}

switch($sd){
default:
    
    if($user['ship']=='0' or $user['ship_k']=='0'){
        
        echo "Для того чтобы совершать путешествия по морю, необходимо купить корабль и нанять команду<br><br>";
        
        if($user['ship']=='0'){
            echo "<a href='?sd=buyship'>Купить корабль</a> (30,000 <img src='img/silver.png'>)<br>";
        }
        if($user['ship_k']=='0'){
            echo "<a href='?sd=buycomand'>Нанять команду</a> (10,000 <img src='img/silver.png'>)<br>";
        }
        include ('tmp/foot.php');
        exit;
    } 
	
	$sh = mysql_fetch_assoc(mysql_query("SELECT * FROM `ship` WHERE `user`='$uid'"));
	$sk = mysql_fetch_assoc(mysql_query("SELECT * FROM `ship_k` WHERE `user`='$uid'"));

	echo "Корабль: $sh[level] ур.<br>
	Урон: $sh[uron]<br>
	Устойчивость: $sh[def]<br>
	Прочность: $sh[hpall]<br>";
	if($sh['level']<'10'){
		if($sh['level']=='1'){$cena=30000;}
		if($sh['level']=='2'){$cena=40000;}
		if($sh['level']=='3'){$cena=50000;}
		if($sh['level']=='4'){$cena=70000;}
		if($sh['level']=='5'){$cena=100000;}
		if($sh['level']=='6'){$cena=150000;}
		if($sh['level']=='7'){$cena=280000;}
		if($sh['level']=='8'){$cena=420000;}
		if($sh['level']=='9'){$cena=600000;}
		echo "[<a href='?sd=upship'>Улучшить</a> за <small>".number_format($cena)." <img src='img/silver.png' width='14'></small>]<hr>";
	}else{
		echo "<hr>";
	}
	echo "Команда: $sk[level] ур.<br>
	Удача: +$sk[silver]% <img src='img/silver.png'><br>
	Стаж: +$sk[exp]% <img src='img/exp.png'><br>";
	if($sk['level']<'10'){
		if($sk['level']=='1'){$cena=20000;}
		if($sk['level']=='2'){$cena=30000;}
		if($sk['level']=='3'){$cena=40000;}
		if($sk['level']=='4'){$cena=60000;}
		if($sk['level']=='5'){$cena=80000;}
		if($sk['level']=='6'){$cena=130000;}
		if($sk['level']=='7'){$cena=260000;}
		if($sk['level']=='8'){$cena=400000;}
		if($sk['level']=='9'){$cena=580000;}

		echo "[<a href='?sd=upcomand'>Улучшить</a> за <small>".number_format($cena)." <img src='img/silver.png' width='14'></small>]<hr><br>";
	}else{
		echo "<hr><br>";
	}
	


    echo "<ul class='list_page'><li><div class='linem'></div><a href='?sd=sea'>Выйти в море</a></li></ul><div class='linem2'></div><br><br>";
	
	$lid = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` ORDER BY `sea_medal` DESC LIMIT 1"));
	
	echo "Морская медаль: $user[sea_medal] <img src='img/sea_medal.png'><br><br>
	<small><font color=grey>В конце каждой недели проходит состязание \"<font color=aqua><i>Повелитель Морей</i></font>\", задача проста, за неделю завоевать больше всех <img src='img/sea_medal.png'> Морских Медалей!<br>Победитель получит <img src='img/almaz.png'> <font color=violet>алмазы</font> и статус \"<font color=aqua><i>Повелитель Морей</i></font>\" до результатов следующего состязания!</font><br>Сейчас лидирует: <a href='pers.php?id=$lid[id]'>$lid[login]</a></small>";
    
    
break;


case 'sea':
$q1 = mysql_query("SELECT * FROM `ship` WHERE `user`='$uid'");
$e1 = mysql_num_rows($q1);
if($e1=='0'){
	header('Location: sea.php');
	exit;
}
$q2 = mysql_query("SELECT * FROM `ship_k` WHERE `user`='$uid'");
$e2 = mysql_num_rows($q2);
if($e2=='0'){
	header('Location: sea.php');
	exit;
}

mysql_query("UPDATE `users` SET `sea`='1' WHERE `id`='$uid'");

if($_SESSION['sea']!==''){
		echo $_SESSION['sea'];
		$_SESSION['sea']="";	
	}

$q = mysql_query("SELECT * FROM `users` WHERE `sea`='1' and `id`!='$uid' LIMIT 20");
$e = mysql_num_rows($q);
if($e<'4'){
	echo "Вражеских кораблей не обнаружено<br>Для битвы в море необходимо чтобы 4 и более игроков были готовы к бою.";
	echo "<br><hr><a href='?sd=outsea'>К гавани</a>";
	include ('tmp/foot.php');
	exit;
}else{
	$w = mysql_fetch_assoc($q);
	$s = mysql_query("SELECT * FROM `ship` WHERE `user`='$w[id]' and `hp`>'0'");
	
		While($sh = mysql_fetch_assoc($s)){
			
			$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `id`='$sh[user]'"));
			
			echo "<a href='pers.php?id=$sh[user]'>$us[login]</a> - <a href='battlesea.php?id=$sh[user]'>Атаковать</a><hr>";
						
		}


	echo "<br><hr><a href='?sd=outsea'>К гавани</a>";
}

break;


case 'outsea':

mysql_query("UPDATE `users` SET `sea`='0' WHERE `id`='$uid'");

header('Location: sea.php');
exit;

break;


case 'upcomand':
$q = mysql_query("SELECT * FROM `ship_k` WHERE `user`='$uid'");
$e = mysql_num_rows($q);
if($e=='0'){
	header('Location: sea.php');
	exit;
}
$w = mysql_fetch_assoc($q);

if($w['level']>='10'){
	header('Location: sea.php');
	exit;
}

if($w['level']=='1'){$exp=8;$silver=7;$cena=20000;}
if($w['level']=='2'){$exp=11;$silver=9;$cena=30000;}
if($w['level']=='3'){$exp=14;$silver=11;$cena=40000;}
if($w['level']=='4'){$exp=17;$silver=13;$cena=60000;}
if($w['level']=='5'){$exp=20;$silver=15;$cena=80000;}
if($w['level']=='6'){$exp=24;$silver=18;$cena=130000;}
if($w['level']=='7'){$exp=30;$silver=22;$cena=260000;}
if($w['level']=='8'){$exp=38;$silver=26;$cena=400000;}
if($w['level']=='9'){$exp=50;$silver=34;$cena=580000;}

if($user['silver']<$cena){
	header('Location: sea.php');
	exit;
}

mysql_query("UPDATE `users` SET `silver`=`silver`-'$cena' WHERE `id`='$uid'");

mysql_query("UPDATE `ship_k` SET `level`=`level`+'1',`exp`='$exp',`silver`='$silver' WHERE `user`='$uid'");

header('Location: sea.php');
exit;

break;


case 'upship':
$q = mysql_query("SELECT * FROM `ship` WHERE `user`='$uid'");
$e = mysql_num_rows($q);
if($e=='0'){
	header('Location: sea.php');
	exit;
}
$w = mysql_fetch_assoc($q);

if($w['level']>='10'){
	header('Location: sea.php');
	exit;
}

if($w['level']=='1'){$uron=12;$def=11;$hp=60;$cena=30000;}
if($w['level']=='2'){$uron=18;$def=12;$hp=80;$cena=40000;}
if($w['level']=='3'){$uron=26;$def=14;$hp=100;$cena=50000;}
if($w['level']=='4'){$uron=40;$def=20;$hp=140;$cena=70000;}
if($w['level']=='5'){$uron=60;$def=26;$hp=180;$cena=100000;}
if($w['level']=='6'){$uron=85;$def=32;$hp=240;$cena=150000;}
if($w['level']=='7'){$uron=115;$def=38;$hp=300;$cena=280000;}
if($w['level']=='8'){$uron=150;$def=44;$hp=400;$cena=420000;}
if($w['level']=='9'){$uron=200;$def=50;$hp=500;$cena=600000;}

if($user['silver']<$cena){
	header('Location: sea.php');
	exit;
}

mysql_query("UPDATE `users` SET `silver`=`silver`-'$cena' WHERE `id`='$uid'");

mysql_query("UPDATE `ship` SET `level`=`level`+'1',`uron`='$uron',`def`='$def',`hp`='$hp',`hpall`='$hp' WHERE `user`='$uid'");

header('Location: sea.php');
exit;

break;



case 'buycomand':
    
    if($user['ship_k']>='1'){
        header('Location: sea.php');
        exit;
    }
    if($user['silver']<'10000'){
        header('Location: sea.php');
        exit;
    }
    
    mysql_query("UPDATE `users` SET `ship_k`='1',`silver`=`silver`-'10000' WHERE `id`='$uid'");
	
    mysql_query("INSERT INTO `ship_k` SET `user`='$uid',`level`='1',`exp`='5',`silver`='5'");
	
    header('Location: sea.php');
    exit;
    
break;
    
case 'buyship':
    
    if($user['ship']>='1'){
        header('Location: sea.php');
        exit;
    }
    if($user['silver']<'30000'){
        header('Location: sea.php');
        exit;
    }
    
    mysql_query("UPDATE `users` SET `ship`='1',`silver`=`silver`-'30000' WHERE `id`='$uid'");
	
	mysql_query("INSERT INTO `ship` SET `user`='$uid',`level`='1',`uron`='10',`def`='10',`hp`='40',`hpall`='40'");
    
    header('Location: sea.php');
    exit;
    
break;


    
}
include ('tmp/foot.php');
?>