<?php
session_start( );
include( "config.php" );
include( "getfunction.php" );
if ( $_POST[go] == "buy_shop" )
{
		$shop_id = addslashes( trim( $_POST['shop_id'] ) );
		$shop_shuliang =addslashes( trim( $_POST['shuliang'] ) );
		//echo $shop_id;
		//echo $shop_shuliang;
		//exit();
		$serverandrole = addslashes( trim( $_POST['serverandrole'] ) );
		$roleChk = addslashes( trim( $_POST['roleChk'] ) );
		$tem = explode( ":", $serverandrole );
		$sid = $tem[0];
		
		$player_id = $serverandrole;
		
		if ( $roleChk != 1 )
		{
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('请先勾选！');location.href='shop.php'</script>";
				exit( );
		}
		if ( $sid == "" || $player_id == "" )
		{
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('你还没有角色呢！');location.href='shop.php'</script>";
				exit( );
		}
		
		$sql="SELECT attributes FROM shuxing WHERE sxid =(SELECT shop_fjattrstr FROM shop_list WHERE id=".$shop_id.")" ;
		
		$shopshuxing = mysql_fetch_array( mysql_query( $sql) );
		if ( !$shopshuxing )
		{
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('该商品属性错误！');location.href='shop.php'</script>";
				exit( );
		}
		$sql="SELECT * FROM shop_list where id=".$shop_id;
		$shoprow = mysql_fetch_array( mysql_query( $sql) );
		if ( !$shoprow )
		{
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('没有这个商品！');location.href='shop.php'</script>";
				exit( );
		}
		if($shoprow[shop_fjattrstr]>=800 && $shoprow[shop_fjattrstr]<999)
		{
		$shop_shuliang=1;
		}
		$sql="SELECT * FROM account where name='".$_SESSION['accountName']."' and ".$shoprow[shop_prick_type].">=".$shoprow[shop_prick]*$shop_shuliang;
		//echo $sql;
		//exit();
		$accountrow = mysql_fetch_array( mysql_query( $sql) );
		if ( !$accountrow )
		{
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('你的钱不够啊！');location.href='shop.php'</script>";
				exit( );
		}
		
		//$conn = mysql_connect( $serverrow[mysqlip], $serverrow[mysqlname], $serverrow[mysqlpass] );
		if ( !$conn )
		{
				include( "config.php" );
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('数据库连接！');location.href='shop.php'</script>";
				exit( );
		}
		if ( !mysql_select_db( "aionx_gs", $conn ) )
		{
				include( "config.php" );
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('数据库选择！');location.href='shop.php'</script>";
				exit( );
		}
		mysql_query( "SET @@character_set_connection=utf8,@@character_set_results=utf8,@@character_set_client=utf8" );
		$sql="SELECT * FROM players WHERE account_name ='".$_SESSION['accountName']."' and id='".$player_id."'";
		$playerinfo = mysql_fetch_array( mysql_query( $sql) );
		if ( !$playerinfo )
		{
				include( "config.php" );
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('没这个角色！或者这个角色不是你的！');location.href='shop.php'</script>";
				exit( );
		}
		include( "config.php" );
		$sl = intval($shop_shuliang);
		if($sl < 1)
		{
			exit("草了你妈啊");
		}
		if ( mysql_query( "update account set ".$shoprow[shop_prick_type]."=".$shoprow[shop_prick_type]."-".$shoprow[shop_prick]*$sl." where name='".$_SESSION['accountName']."'" ) )
		{
				mysql_select_db( "aionx_gs", $conn );
				mysql_query( "SET @@character_set_connection=utf8,@@character_set_results=utf8,@@character_set_client=utf8" );
				$niubiid=md5(md5($_SESSION['accountName'].time()).$shoprow[shop_code]);
				
				if($shopshuxing[attributes] != "0")
				{
				$fujiashuxing='[{"attributes":"{\\\"fjattrstr\\\":\\\"'.$shopshuxing[attributes];
				$attachmentaa ='\\\"}",\\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":'.$shop_shuliang.',\"goodsId\":\"'.$shoprow[shop_code].'\",\"itemLevel\":0,\"notice\":false,\"rareLevel\":4,\"slotNum\":0}]';
				$attachment_items=$fujiashuxing.$attachmentaa;
				//echo $attachment_items;
				//exit();
				}else
				{
				//if start 700-720  721-740  741-760 761-780 781-800
				//如果条件都不存在则无动作
				if($shoprow[shop_fjattrstr]>=700 && $shoprow[shop_fjattrstr]<720)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":0,\"notice\":false,\"rareLevel\":1,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=721 && $shoprow[shop_fjattrstr]<740)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":0,\"notice\":false,\"rareLevel\":2,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=741 && $shoprow[shop_fjattrstr]<750)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":90,\"notice\":false,\"rareLevel\":3,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=751 && $shoprow[shop_fjattrstr]<760)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":120,\"notice\":false,\"rareLevel\":3,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=761 && $shoprow[shop_fjattrstr]<780)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":0,\"notice\":false,\"rareLevel\":4,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=781 && $shoprow[shop_fjattrstr]<790)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":0,\"notice\":false,\"rareLevel\":5,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=791 && $shoprow[shop_fjattrstr]<792)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":1,\"notice\":false,\"rareLevel\":5,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=793 && $shoprow[shop_fjattrstr]<794)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":1,\"notice\":false,\"rareLevel\":4,\"slotNum\":0}]";
				}
				if($shoprow[shop_fjattrstr]>=795 && $shoprow[shop_fjattrstr]<796)
				{
				$attachment_items = ".$shoprow[shop_code].";
				}
				if($shoprow[shop_fjattrstr]>=999 && $shoprow[shop_fjattrstr]<1000)
				{
				$attachment_items = "[{\"bind\":1,\"expireTime\":0,\"expired\":false,\"goodsCount\":".$shop_shuliang.",\"goodsId\":\"".$shoprow[shop_code]."\",\"itemLevel\":0,\"notice\":false,\"rareLevel\":4,\"slotNum\":0}]";
				}
				//if end
				}
				$date=time().'750';
				
				if($shoprow[shop_fjattrstr]>=800 && $shoprow[shop_fjattrstr]<999)
				{
				$idsql="SELECT * FROM dntg2.server_info";
				$dsidxc = mysql_fetch_array( mysql_query( $idsql) );
				$dishaid=uniqid($dsidxc[id],false);
				$skill=$shopshuxing[attributes];
				$sql="INSERT INTO `dntg2`.`role_shenjiang` (`id`, `user_role_id`, `shenjiang_id`, `jiangxing_id`, `jiangxing_fail_count`, `cur_jiangxing_id`, `jiangxing_state`, `jiangxing_time`, `skill_level`, `jiangxing_rank`, `jiangxing_rank_fail_count`, `jiangxing_rank_lucky`, `skill_time`, `create_time`, `skills`, `wuhun`, `log_update_time`) VALUES ('".$dishaid."', '".$playerinfo[id]."', '".$shoprow[shop_code]."', '', '0', '', '0', now(), '1', '5', '0', '0', now(), now(), '".$skill."', NULL, now())";
				}
				else
				{
				$sql="INSERT INTO `aionx_gs`.`inventory` (`itemUniqueId`, `itemId`, `itemCount`, `itemOwner`, `isEquiped`, `isSoulBound`, `slot`, `itemLocation`, `expireTime`, `level`, `upgrade_exp`, `epic_count_exp`, `mask`, `create_time`) VALUES ('".$niubiid."', '".$attachment_items."', '".$shop_shuliang."', '".$playerinfo[id]."', '0', '0', '0', '127', NULL, '1', '0', '0', '4696', now());";
				}
				
				if ( mysql_query( $sql) )
				{
						include( "config.php" );
						echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('购买装备成功！');location.href='shop.php?id=1'</script>";
						exit( );
				}
				include( "config.php" );
				echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('购买装备失败！但已扣费，请联系管理员。');location.href='shop.php?id=1'</script>";
				exit( );
		}
		include( "config.php" );
		echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><script>alert('购买装备失败！');location.href='shop.php?id=1'</script>";
		exit( );
}

?>
