<?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));
if (!$MOD['enablecname']) showmessage("对不起，管理员未开启角色改名！");

function doSub($id,$idfso,$newname)
{
	global $db,$_username,$MOD;
	if(!$id) return "<span class=font06>请选择服务器</span>";
	if(!$idfso) return "<span class=font06>请选择角色</span>";
	if(!$newname) return "<span class=font06>请输入新角色名</span>";
	if(is_badword($newname)) return "<span class=font06>新角色名不合法，请不要尝注入！</span>";
	if(is_badgamename($newname)) return "<span class=font06>新角色名不能包含敏感词汇</span>";
	if(strlen($newname) < 4) return "<span class=font06>角色名太短</span>";
	if(strlen($newname) > 20) return "<span class=font06>角色名太长</span>";
	
 	if (!check_con($id)) return "<span class=font06>当前分区连接失败，请与管理员联系</span>";
		
	$sqldb = real_con($id);
	$sqldb->select_db(game_fso($id));
  if (is_logonline($id,$idfso)) return "<span class=font06>请确保您已正常退出游戏！</span>";
  $sqldb->select_db(game_fso($id));
  $fsoinfo = $sqldb->get_one("SELECT AccountID,Gold FROM user WHERE ID='$idfso' AND deleted = 0");
  if(!$fsoinfo) return "<span class=font06>错误的请求！</span>";
	if($fsoinfo['Gold'] < $MOD['cnamemoney']) "<span class=font06>角色携带游戏币不足！</span>";
	$gold = $fsoinfo['Gold'] - $MOD['cnamemoney'];
	
	$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($accinfo['username'] != "$_username") return "<span class=font06>非法操作，你的操作已被记录！</span>";

	$sqldb->select_db(game_fso($id));
  $check = $sqldb->get_one("SELECT ID FROM user WHERE Name='$newname'");
	if($check)
	{
  return "<span class=font06>该昵称已被其它玩家使用，请更换！</span>";
  }else{
  $sqldb->query("UPDATE user SET UniqueName='$newname',Name='$newname',Gold='$gold' WHERE ID='$idfso'");
  if($sqldb->affected_rows())
  {
    return "true";
  }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_online($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,Metier,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>';
	}
	
	if($fsoinfo['Gold'] < $MOD['cnamemoney']) 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>';
	
	$gold = $fsoinfo['Gold'];
	$metier = getmetier($fsoinfo['Metier']);
	if($gold && $metier)
	{
  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">'.$metier.'</TD></TR><TR><TH style="font-weight:bold;font-size:14px">携带游戏币： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold">'.$gold.'</TD></TR><TR><TH style="font-weight:bold;font-size:14px">新角色名： </TH><TD colSpan=2><INPUT class=inputA type="text" id="newname" value="" 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 = 0");
    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(this.value)"><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 = 0");
    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'];
	
    $query = "SELECT truename,telephone,qq,area FROM ".TABLE_MEMBER_INFO." WHERE userid=$_userid";
	@extract($db->get_one($query));	
	if(!$area) showmessage("请选激活游戏分区再进行此操作！","/game/actgame.php");

	$query = "SELECT * FROM ".TABLE_GAMESERVER." WHERE id IN ($area) ORDER BY id";
	$result=$db->query($query);
	while($r=$db->fetch_array($result))
	{
		$gamelists[]=$r;
	}
		
	include template($mod,'cname');
?>