<?php 
include_once './include/common.inc.php';
sajax_init();
sajax_export("doSub","listfso","checkserv","doView");
sajax_handle_client_request();
//$GLOBALS['sajax_debug_mode'] = 1;

//if(!$_userid)	showmessage($LANG['login_website'], $MODULE['member']['linkurl'].'login.php?forward='.urlencode($PHP_URL));
if(!$_userid) header('location:/member/login.php?forward='.urlencode($PHP_URL));
$today = date('Y-m-d',$PHP_TIME);


function doSub($id,$idfso,$tonum,$tocheck)
{

	global $db,$_userid,$_username,$MOD,$PHP_TIME;
	if(!$id) return "<span class=font06>管理员提醒：请选择服务器。</span>";
	if(!$idfso) return "<span class=font06>管理员提醒：请选择角色。</span>"; 
	if(!$tonum) return "<span class=font06>管理员提醒：请输入清洗数量。</span>";
	if(!is_numeric($tonum)) return "<span class=font06>管理员提醒：输入的数量必须是数字！</span>";
	if($tonum < 1 ) return "<span class=font06>管理员提醒：输入的数量必须大于0。</span>";
	if(!$tocheck) return "<span class=font06>管理员提醒：请选择清洗内容。</span>";
	if($tocheck<>1 and $tocheck<>2) return "<span class=font06>管理员提醒：清洗内容发生异常请联系管理员。</span>";

 	if (!check_con($id)) return "<span class=font06>管理员提醒：当前分区连接失败，请与管理员联系。</span>";
		
	$sqldb = real_con($id);
	if(is_logonline($id,$idfso)) return "<span class=font06>管理员提醒：请确保您已正常退出游戏！</span>";

	$sqldb = real_con($id);
	$sqldb->select_db(game_fso($id));
	 $fsoinfo = $sqldb->get_one("SELECT AccountID,Level,OnlineTime FROM user WHERE ID='$value'");

	  if($fsoinfo)
	  {
	  $sqldb->select_db(game_acc($id));
	 $accinfo = $sqldb->get_one("SELECT username FROM user WHERE idx='$fsoinfo[AccountID]'");
	 if(!$accinfo)
	  {
	 return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">非法请求！</TD></TR></TBODY></TABLE>';
	}else{
	  if($accinfo['username'] != "$_username") return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">非法操作，你的操作已被记录！</TD></TR></TBODY></TABLE>';
	}
	}
	
    
	$sqldb->select_db(game_fso($id));
	$fsoinfo = $sqldb->get_one("SELECT AccountID,Level,Gold,gold,items,vip FROM user WHERE ID='$idfso' AND deleted=0");
    	if(!$fsoinfo) return "<span class=font06>未查询到角色数据！</span>";
	

	$sqldb->select_db(game_acc($id));
	$accinfo = $sqldb->get_one("SELECT username FROM user WHERE idx='$fsoinfo[AccountID]'");
	if(!$accinfo) return "<span class=font06>错误的请求！</span>";


	if ($tocheck==1){
	
		 
	
		if($fsoinfo['Level'] < 1) return "<span class=font06>您的等级太底了！</span>";
		$gold = $fsoinfo['gold'];
		if($gold < 50000000) return "<span class=font06>您好！你的金币不足5千万，不能给你转换阵营</span>";
		$newgold = $fsoinfo['Gold'] - 50000000;
		$toGold=$fsoinfo['gold'];
		if($fsoinfo['gold'] < 50000000)  return '<TABLE class=mw_tabA cellSpacing=0 cellPadding=0 width="90%" align=center border=0><TBODY><TR><TD height="30" colspan="2">很遗憾！</TD></TR><TD height=30 style="text-align:center;color:#900;font-weight:bold">转朝失败！</TD></TR><TR><TD height=30 align="center">原因：金币！转朝费：'.$toGold.'金币。 </TD></TR><TR><TD class=pat10 align="center"><input type="button" class="hsbtn" value="确认" onclick="doView();CloseLoad();"></TD></TR></TBODY></TABLE>';
		$newpk=$gold-50000000;
		$sqldb->select_db(game_fso($id));
		$sqldb->query("update  user set Gold='$newGold',gold='$newpk',faction='0' WHERE ID='$idfso'");
		return '<TABLE class=mw_tabA cellSpacing=0 cellPadding=0 width="90%" align=center border=0><TBODY><TR><TD height="30" colspan="2">祝贺你：</TD></TR><TD height=30 style="text-align:center;color:#900;font-weight:bold">转周成功！</TD></TR><TR><TD height=30 align="center">本次互转花费：'.$toGold.'金币，还有：'.$newpk.'金币。 </TD></TR><TR><TD class=pat10 align="center"><input type="button" class="hsbtn" value="确认" onclick="doView();CloseLoad();"></TD></TR></TBODY></TABLE>';
		
	}
	
	if ($tocheck==2){
	
		 
	
		if($fsoinfo['Level'] < 1) return "<span class=font06>您的等级太底了！</span>";
		$gold = $fsoinfo['gold'];
		if($gold < 50000000) return "<span class=font06>您好！你的金币不足5千万，不能给你转换阵营</span>";
		$newgold = $fsoinfo['Gold'] - 50000000;
		$toGold=$fsoinfo['gold'];
		if($fsoinfo['gold'] < 50000000)  return '<TABLE class=mw_tabA cellSpacing=0 cellPadding=0 width="90%" align=center border=0><TBODY><TR><TD height="30" colspan="2">很遗憾！</TD></TR><TD height=30 style="text-align:center;color:#900;font-weight:bold">转朝失败！</TD></TR><TR><TD height=30 align="center">原因：金币！转朝费：'.$toGold.'金币。 </TD></TR><TR><TD class=pat10 align="center"><input type="button" class="hsbtn" value="确认" onclick="doView();CloseLoad();"></TD></TR></TBODY></TABLE>';
		$newpk=$gold-50000000;
		$sqldb->select_db(game_fso($id));
		$sqldb->query("update  user set Gold='$newGold',gold='$newpk',faction='1' WHERE ID='$idfso'");
		return '<TABLE class=mw_tabA cellSpacing=0 cellPadding=0 width="90%" align=center border=0><TBODY><TR><TD height="30" colspan="2">祝贺你：</TD></TR><TD height=30 style="text-align:center;color:#900;font-weight:bold">转商成功！</TD></TR><TR><TD height=30 align="center">本次互转花费：'.$toGold.'金币，还有：'.$newpk.'金币。 </TD></TR><TR><TD class=pat10 align="center"><input type="button" class="hsbtn" value="确认" onclick="doView();CloseLoad();"></TD></TR></TBODY></TABLE>';
		
	}
       
}


function doView($id,$value)
{
  global $db,$_username,$MOD;
  if(!$id) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">游戏分区读取错误！</TD></TR></TBODY></TABLE>';
	if(!$value) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">请选择角色！</TD></TR></TBODY></TABLE>';
	  
	if (!check_con($id)) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">当前分区连接失败，请与管理员联系</TD></TR></TBODY></TABLE>';
	
	if (is_logonline($id,$value)) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">请确保您已正常退出游戏！</TD></TR></TBODY></TABLE>';
		
	$sqldb = real_con($id);
	$sqldb->select_db(game_fso($id));
  $fsoinfo = $sqldb->get_one("SELECT AccountID,Level,gold FROM user WHERE ID='$value'");

  if($fsoinfo)
  {
  $sqldb->select_db(game_acc($id));
  $accinfo = $sqldb->get_one("SELECT username FROM user WHERE idx='$fsoinfo[AccountID]'");
  if(!$accinfo)
  {
    return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">非法请求！</TD></TR></TBODY></TABLE>';
	}else{
	  if($accinfo['username'] != "$_username") return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">非法操作，你的操作已被记录！</TD></TR></TBODY></TABLE>';
	}

  $Level = $fsoinfo['Level'];
  $gold = $fsoinfo['gold'];

  
	if($gold < 50000000) return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">您的金币不足5千万</TD></TR></TBODY></TABLE>';
	
	
	if($Level && $gold)
	{
  return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH style="font-weight:bold;font-size:14px">角色等级： </TH>
  <TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><em style="font-weight:bold;color:#f00">'.$Level.'</em> 级</TD></TR>
  <TR><TH style="font-weight:bold;font-size:14px">携带金币： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><em style="font-weight:bold;color:#f00">'.$gold.'</em> 金币</TD></TR>
  <TR><TH style="font-weight:bold;font-size:14px">选择阵营： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><select id="tocheck"><option value=1>=周朝=</option><option value=2>=商朝=</option><option value=3></option><option value=4></option></select></TD></TR> 
  <TR><TH style="font-weight:bold;font-size:14px">支付金币： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><INPUT class=inputA type="text" id="tonum" value="'.$gold.'" style="font-weight:bold;font-size:14px;color:#F00">（输入所需金币数值）</TD></TR>
  <TR><TH></TH><TD colSpan=2 ><input id="subok" type="submit" onClick="doSub();" class="hsbtn" value="确定"></TD></TR></TBODY></TABLE>';
  }else{
  return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">角色信息读取失败!</TD></TR></TBODY></TABLE>';
}
}else{
  return '<TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH></TH><TD style="color:#f00;font-size:14px;font-weight:bold;padding:20px 0px;text-align:left;">未读取到角色信息!</TD></TR></TBODY></TABLE>';
}
}


function listfso($id)
{
  global $db,$_userid,$_username;
  if(!$id) return "游戏分区读取错误";
  
  if (!check_con($id)) return "<span class=font06>当前分区连接失败，请与管理员联系</span>";
		
  $sqldb = real_con($id);
		
  $info = $sqldb->get_one("SELECT idx FROM user WHERE username='$_username'");
  if($info)
  {
  $sqldb->select_db(game_fso($id));

  $users = array();
  $rs = $sqldb->query("SELECT ID,Name FROM user WHERE AccountID='".$info['idx']."' AND deleted != 1");
    if(!$rs)
    {
      return "该分区没有角色！";
    }else{
    while($r = $sqldb->fetch_array($rs))
    {
     $users[] = $r;
    }
    foreach($users as $value)
    {
      $bstr .= '<option value='.$value[ID].'>'.$value[Name].'</option>';
    }
      return '<select id="idfso" onchange="doView()"><option>-- 请选择角色 --</option>'.$bstr.'</select>';
    }  
  
  }else{
  return "您没有激活该分区";
  }
}

function checkserv($id)
{
 global $db,$_userid,$_username;
  if(!$id) return "<span class=font06>请选择分区</span>";
  if (!check_con($id)) return "<span class=font06>当前分区连接失败，请与管理员联系</span>";

  $sqldb = real_con($id);
  $info = $sqldb->get_one("SELECT idx FROM user WHERE username='$_username'");
  if($info)
  {
  $sqldb->select_db(game_fso($id));
  $infou = $sqldb->get_one("SELECT ID FROM user WHERE AccountID='".$info['idx']."' AND deleted != 1");
    if(!$infou)
    {
      return "<span class=font06>该分区没有角色！</span>";
    }else{
      return "true";
    }  
  
  }else{
  return "<span class=font06>您没有激活该分区</span>";
  }
  return "true";
}


	$head['title'] = '阵营转换';
	$head['keywords'] = $MOD['seo_keywords'];
	$head['description'] = $MOD['seo_description'];
	
    $infos = $db->get_one("SELECT area FROM ".TABLE_MEMBER_INFO." WHERE userid='$_userid'");

    if(!$infos['area']) showmessage("请选激活游戏分区再进行此操作！","/game/actgame.php");
  
    $gamelists = array();
    $rs = $db->query("SELECT * FROM ".TABLE_GAMESERVER." WHERE id IN ($infos[area]) ORDER BY id");
    while($r = $db->fetch_array($rs))
    {
    $gamelists[] = $r;
    }
	
	include template($mod,'hyzh');
?> 