<?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);
if (!$MOD['enablezs']) showmessage("对不起，管理员未开启转生加点功能！");


function doSub($id,$idfso)
{
  
	global $db,$_userid,$_username,$MOD,$PHP_TIME;
	if(!$id) return "<span class=font06>管理员提醒：请选择服务器。</span>";
	if(!$idfso) return "<span class=font06>管理员提醒：请选择角色。</span>"; 
	if (!check_con($id)) return "<span class=font06>管理员提醒：当前分区连接失败，请与管理员联系。</span>";
	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,MetierLevel,Gold,Con,Str,`Int`,Dex,Hit,AP,SP,zs FROM user WHERE ID='$idfso' AND deleted=0");
    	if(!$fsoinfo) return "<span class=font06>未查询到角色数据！</span>";
	
	if($fsoinfo['Level'] < $MOD['zslevel']) return "<span class=font06>您的等级未达到转生等级！</span>";
	if($fsoinfo['zs'] < 0) return "<span class=font06>转生数据异常，请联系管理员！</span>";
	if($fsoinfo['zs']+1 > $MOD[zsnum]) return "<span class=font06>角色达到最大转生次数！</span>";
	if($fsoinfo['Gold'] < $MOD[zsgold]) return "<span class=font06>角色携带金钱不足以完成本次转生！</span>";
	
		$newGold=$fsoinfo['Gold']-$MOD[zsgold];
		$newap=($fsoinfo['zs']+1)*$MOD[zsap]+70;
		$newskills="40000070915000000000DF40000000007050000000008F400000000081500000730060500000A90001500000C00000";
		if ($fsoinfo['zs']+1 >= $MOD[zsspstar])
		{
		$newsp=($fsoinfo['zs']-$MOD[zsspstar]+2)*$MOD[zssp];
		}else{
		$newsp=0;
		}
		$newzs=$fsoinfo['zs']+1;

	$sqldb->select_db(game_fso($id));
	$sqldb->query("update  user set Level='1',Gold='$newGold',Con='1',Str='1',`Int`='1',Dex='1',Hit='1',AP='$newap',SP='$newsp',zs='$newzs',Skills='$newskills' WHERE ID='$idfso'");
	
		
	if($sqldb->affected_rows())
	{  
//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">第'.$newzs.'次转生成功！</TD></TR><TR><TD height=30 align="center">获得AP：'.$newap.'点，SP：'.$newsp'点，重要技能全5级。 </TD></TR><TR><TD class=pat10 align="center"><input type="button" class="hsbtn" value="确认" onclick="doView();CloseLoad();"></TD></TR></TBODY></TABLE>';
	    return "<span class=font06>第'$newzs'次转生成功!</span>";
	}else{
	 return "<span class=font06>转生失败，请与管理员联系！</span>";
	}
		
}


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));
	
	//判断zs字段是否存在
	//try {
	//$fsozs=$sqldb->get_one("SELECT count(zs) FROM user");
	//} catch (Exception $e) { 
	//return $e;
	//}
	
  $fsoinfo = $sqldb->get_one("SELECT AccountID,Level,Con,Str,`Int`,Dex,Hit,AP,SP,zs 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'];
  $nowzsnum = $fsoinfo['zs'];
  $zslevel=$MOD[zslevel];
  $allzsnum=$MOD[zsnum];
  $Con= $fsoinfo['Con'];
  $Str= $fsoinfo['Str'];
  $Int= $fsoinfo['Int'];
  $Dex= $fsoinfo['Dex'];
  $Hit= $fsoinfo['Hit'];
  $AP= $fsoinfo['AP'];
  $SP= $fsoinfo['SP'];


   if($Level)
	{
  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"><SPAN style="font-weight:bold;color:#f00">'.$Level.'</SPAN> 级。(最低需要：'.$zslevel.'级)</TD></TR>
  <TR><TH style="font-weight:bold;font-size:14px">当前转数： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><SPAN style="font-weight:bold;color:#f00">'.$nowzsnum.'</SPAN> 转。(当前开放： '.$allzsnum.' 转)</TD></TR>
  <TR><TH style="font-weight:bold;font-size:14px">角色⑤围： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold">体质(<SPAN style="font-weight:bold;color:#f00">'.$Con.'</SPAN>) / 力量(<SPAN style="font-weight:bold;color:#f00">'.$Str.'</SPAN>) / 智力(<SPAN style="font-weight:bold;color:#f00">'.$Int.'</SPAN>) / 敏捷(<SPAN style="font-weight:bold;color:#f00">'.$Dex.'</SPAN>) / 精神(<SPAN style="font-weight:bold;color:#f00">'.$Hit.'</SPAN>)</TD></TR> 
  <TR><TH style="font-weight:bold;font-size:14px">可用属性： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold">AP:[<SPAN style="font-weight:bold;color:#f00">'.$AP.'</SPAN>]  SP:[<SPAN style="font-weight:bold;color:#f00">'.$SP.'</SPAN>]</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>";

  $query= "select enablezs from  ".TABLE_GAMESERVER."  where id='".$id."' and enablezs='1'";
  $p = $db->get_one($query);

  if(!$p['enablezs']){
	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,'mzs');
?>