<?php 
include_once './include/common.inc.php';
if(!$_userid) header('location:/member/login.php?forward='.urlencode($PHP_URL));
sajax_init();
sajax_export("doSub","listfso","checkserv","doView");
sajax_handle_client_request();
//$GLOBALS['sajax_debug_mode'] = 1;

if (!$MOD['enablefly']) showmessage("对不起，管理员未开启角色飞升功能！");

function doSub($id,$idfso)
{
	global $db,$_username,$_userid,$_money,$MOD;
	if(!$id) return "<span class=font06>请选择服务器</span>";
	if(!$idfso) 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,Metier,Level,Gold,Credit,MetierLevel,flys,lastfly FROM user WHERE ID='$idfso' AND deleted = 0");
  if(!$fsoinfo) return "<span class=font06>错误的请求！</span>";
	
	if($fsoinfo['Level'] < '150') 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;">对不起，你的等级未达到150级！</TD></TR></TBODY></TABLE>';
	if($fsoinfo['MetierLevel'] < '3') 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;">对不起，您还未进行第3次转职！</TD></TR></TBODY></TABLE>';
	if($fsoinfo['Gold'] < '180000000') 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;">对不起，你背包的游戏币不足1.8亿！</TD></TR></TBODY></TABLE>';
	if($fsoinfo['Credit'] < '180') 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;">对不起，你的声望不足180点！</TD></TR></TBODY></TABLE>';
	if(time() - $fsoinfo['lastfly'] < 360*1.5*2.4) 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;">对不起，每1天才能进行一次飞升！<br>'.$flytime.'后才可以进行飞升！</TD></TR></TBODY></TABLE>';

	if($fsoinfo['Metier'] == "1"){
    $Con = '15';//体
    $Str = '25';//力
    $Int = '8';//智
    $Dex = '12';//敏
    $Hit = '15';//精
    
   }elseif($fsoinfo['Metier'] == "2"){
    $Con = '12';//体
    $Str = '15';//力
    $Int = '5';//智
    $Dex = '23';//敏
    $Hit = '20';//精
   }elseif($fsoinfo['Metier'] == "3"){
    $Con = '7';//体
    $Str = '3';//力
    $Int = '32';//智
    $Dex = '12';//敏
    $Hit = '21';//精
   }elseif($fsoinfo['Metier'] == "4"){
    $Con = '8';//体
    $Str = '3';//力
    $Int = '30';//智
    $Dex = '12';//敏
    $Hit = '22';//精
   }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>';
   }
   
    if($fsoinfo['MetierLevel'] == "3"){
	$MetierLevel='4';
	}else{
	$MetierLevel=$fsoinfo['MetierLevel'];
	}
	
    $newap = ($fsoinfo['flys'] + 1)*80+195;
    $newgold = $fsoinfo['Gold'] - 180000000;
    $newcredit = $fsoinfo['Credit'] - 180;
    $lastfly = time();
    $sqldb->query("UPDATE user SET Level='40',Gold='$newgold',Credit='$newcredit',AP='$newap',MetierLevel='$MetierLevel',`Con`='$Con',`Str`='$Str',`Int`='$Int',`Dex`='$Dex',`Hit`='$Hit',flys=flys+1,lastfly='$lastfly' WHERE ID='$idfso'");
    if($sqldb->affected_rows())
    {
      return "true";
    }else{
      return "<span class=font06>操作失败，请与管理员连系！</span>";
    }
}


function doView($id,$value)
{
  global $db,$_username,$_money;$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,Metier,Level,Gold,Credit,MetierLevel,flys,lastfly FROM user WHERE ID='$value' AND deleted = 0");
  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>';
	}
	*/
	
	$metier = getmetier($fsoinfo['Metier']);
	
	if($fsoinfo['Level'] < '105') 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;">对不起，你的等级未达到150级！</TD></TR></TBODY></TABLE>';
	if($fsoinfo['MetierLevel'] < '3') 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;">对不起，您还未进行第3次转职！</TD></TR></TBODY></TABLE>';
	if($fsoinfo['Gold'] < '200000000') 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;">对不起，你背包的游戏币不足3.8亿！</TD></TR></TBODY></TABLE>';
	if($fsoinfo['Credit'] < '180') 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;">对不起，你的声望不足180点！</TD></TR></TBODY></TABLE>';
	if($fsoinfo['flys'] > '0') 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;">对不起，当前只能飞升1次！</TD></TR></TBODY></TABLE>';
	$flytime = date("m月d日 H时i分",$fsoinfo['lastfly']+360*1.5*2.4);
	if(time() - $fsoinfo['lastfly'] < 360*1.5*2.4) 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;">对不起，每15天才能进行一次飞升！<br>'.$flytime.'后才可进行飞升！</TD></TR></TBODY></TABLE>';

	$strflys = $fsoinfo['flys'] ? $fsoinfo['flys']." 次飞升" : "还未飞升";
  $strlastfly = $fsoinfo['lastfly'] ? date("m月d日 H时i分",$fsoinfo['lastfly']) : "还未飞升";
	if($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">'.$fsoinfo[Level].'级'.$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">'.$fsoinfo[Gold].'</TD></TR><TR><TH style="font-weight:bold;font-size:14px">角色声望： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold">'.$fsoinfo[Credit].'</TD></TR><TR><TH style="font-weight:bold;font-size:14px">上次飞升： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><font color=blue>'.$strlastfly.'</font></TD></TR><TR><TH style="font-weight:bold;font-size:14px">飞升状态： </TH><TD colSpan=2 style="color:#555;font-size:14px;font-weight:bold"><font color=blue>'.$strflys.'</font></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'];
	 
  if(!$_area) showmessage("你没有激活游戏分区，请与管理员联系！");
 
  $rs = CACHE_READ("gamelist_".$_area.".php");
  if(!$rs['servername']) showmessage("游戏分区数据异常，请与管理员联系！");
		
	include template($mod,'fly');
?>