<?php
 //error_reporting(0);
session_start();
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
if ($msectime - (float)$_SESSION['lasttime'] < 2000) {
	echo json_encode(array('info'=>0,msg=>'2秒发送一次'));die;
	       // echo json_encode(array('info'=>0,'msg'=>'无此授权卡'));die;
} else {
	$_SESSION['lasttime'] = $msectime;
}
//date_default_timezone_set('PRC');
header("Content-type: text/html; charset=utf8");
//var_dump($_POST);die;
	include 'config.php';
	$type = $_POST['type'];
	$checknum=$_POST['pswd'];
   // $quid = trim(poststr('qu'));
    $quid=$_POST['qu'];
    $qu = $quarr[$quid];
  //  $uid = trim(poststr('uid'));
    $uid=$_POST['username'];
    $gid = $qu['gid'];
    $zoneid = $qu['zoneid'];
    $manageurl = $qu['manageurl'];
    $dbip = $qu['host'];
    $dbname = $qu['dbname'];
    $dbuser = $qu['user'];
    $dbpwd = $qu['pwd'];
    $quname = $qu['name'];

$uid=$_POST['username'];

if(!$uid){
	echo json_encode(array('info'=>0,msg=>'uid不能为空'));die;
}
$pswd=$_POST['pswd'];

if(!$pswd){
	echo json_encode(array('info'=>0,msg=>'后台密码不能为空'));die;
}

$db2 = mysqli_connect('127.0.0.1','root','wch123.com','cdks',3306) or die("数据库连接错误1");


$db2->query('set names utf8');
//var_dump($mysql);die;
//$ss = mysqli_fetch_assoc($mysql->query("SELECT FROM cdk WHERE uid = '$rid' limit 1"));
$ss = mysqli_fetch_assoc($db2->query("SELECT * FROM cdks.cdk WHERE uid = '{$uid}' "));
//var_dump($ss);die;
if($ss['status'] != 1 && $type != 'pay'){
    echo json_encode(array('info'=>0,msg=>'角色未授权'));die;
}
if($ss['pass'] != $checknum && $type != 'pay'){
    echo json_encode(array('info'=>0,msg=>'后台密码错误'));die;
}


switch($_POST['type']){

                        case 'charge':
						
						                            $mailid = trim(poststr('rechargeId'));

                            $mailnum = trim(poststr('number'));
                            if ($mailnum == '' || $mailnum < 0 || $mailnum > 1000000) {
                              //  exit('发送数量错误');
								echo json_encode(array('info'=>0,msg=>'数量最大1000000'));die;
                            }
						
							$roleId = $_POST["username"];

    $connnect = new Mongo("mongodb://127.0.0.1:27017");

    $db = $connnect->selectDB($dbname)->selectCollection("char");

    $cursor = $db->findOne(array('name' => $roleId));

	$roleId11=$cursor['_id']->{'$id'};


								
    $collection = $connnect->selectDB($dbname)->selectCollection("mail");

    $document = array(

    "flag" => 1,
    "receiverUuid" => $roleId11,
    "senderName" => "充值系统",
    "head" => 0,
    "items" => [
        [
            (int)$mailid,
            (int)$mailnum
        ]
    ],
    "title" => "充值系统",
    "type" => 1,
    "time" => time(),
    "content" => "123"
);

	    $result = $collection->insert($document);

    		echo json_encode(array('info'=>1,'msg'=>'充值成功，邮箱领取！'));
						break;
						
						
						
                        case 'mail':
                            $mailid = trim(poststr('mailid'));

                            $mailnum = trim(poststr('mailnumyj'));
                            if ($mailnum == '' || $mailnum < 0 || $mailnum > 9) {
                               // exit('发送数量错误');
							   echo json_encode(array('info'=>0,msg=>'数量最大9'));die;
                            }

							$roleId = $_POST["username"];

    $connnect = new Mongo("mongodb://127.0.0.1:27017");

    $db = $connnect->selectDB($dbname)->selectCollection("char");

    $cursor = $db->findOne(array('name' => $roleId));

	$roleId11=$cursor['_id']->{'$id'};


								
    $collection = $connnect->selectDB($dbname)->selectCollection("mail");

    $document = array(

    "flag" => 1,
    "receiverUuid" => $roleId11,
    "senderName" => "系统邮件",
    "head" => 0,
    "items" => [
        [
            (int)$mailid,
            (int)$mailnum
        ]
    ],
    "title" => "系统邮件",
    "type" => 1,
    "time" => time(),
    "content" => "123"
);

	    $result = $collection->insert($document);

    		echo json_encode(array('info'=>1,'msg'=>'发送成功，邮箱领取！'));

	
							 break;

                        
 
case 'clearbag':
$roleId = $_POST["username"];

//链接mongodb
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');

$filter  = [ 'name' => ''.$roleId.'' ]; //查询条件 user_id大于0
$query   = new MongoDB\Driver\Query($filter); //查询请求
$list    = $manager->executeQuery('goal.Player', $query); // 执行查询 location数据库下的box集合

foreach ($list as $document) {
    
$roleId11 = $document->_id;//能打印下吗
}
//print_r($roleId11);





$bulk = new MongoDB\Driver\BulkWrite();

$bulk->delete(array(_id => $roleId11));

//print_r($bulk);


    $result = $manager->executeBulkWrite('goal.PlayerItem', $bulk);
    
    if($result->getDeletedCount()){
    		echo json_encode(array('info'=>1,'msg'=>'已清除，请重新登录！'));
    	}else{
    		echo json_encode(array('info'=>0,'msg'=>'清除失败，没有数据！'));
    	}





	
		
                        break;
 
case 'clearresource':
$roleId = $_POST["username"];

//链接mongodb
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');

$filter  = [ 'name' => ''.$roleId.'' ]; //查询条件 user_id大于0
$query   = new MongoDB\Driver\Query($filter); //查询请求
$list    = $manager->executeQuery('goal.Player', $query); // 执行查询 location数据库下的box集合

foreach ($list as $document) {
    
$roleId11 = $document->_id;//能打印下吗
}
//print_r($roleId11);





$bulk = new MongoDB\Driver\BulkWrite();

$bulk->delete(array(_id => $roleId11));

//print_r($bulk);


    $result = $manager->executeBulkWrite('goal.PlayerEquipment', $bulk);
    
    if($result->getDeletedCount()){
    		echo json_encode(array('info'=>1,'msg'=>'已清除，请重新登录！'));
    	}else{
    		echo json_encode(array('info'=>0,'msg'=>'清除失败，没有数据！'));
    	}





	
		
                        break;

case 'clearherb':
$roleId = $_POST["username"];

//链接mongodb
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');

$filter  = [ 'name' => ''.$roleId.'' ]; //查询条件 user_id大于0
$query   = new MongoDB\Driver\Query($filter); //查询请求
$list    = $manager->executeQuery('goal.Player', $query); // 执行查询 location数据库下的box集合

foreach ($list as $document) {
    
$roleId11 = $document->_id;//能打印下吗
}
//print_r($roleId11);





$bulk = new MongoDB\Driver\BulkWrite();

$bulk->delete(array(_id => $roleId11));

//print_r($bulk);


    $result = $manager->executeBulkWrite('goal.PlayerCoachItem', $bulk);
    
    if($result->getDeletedCount()){
    		echo json_encode(array('info'=>1,'msg'=>'已清除，请重新登录！'));
    	}else{
    		echo json_encode(array('info'=>0,'msg'=>'清除失败，没有数据！'));
    	}





	
		
                        break;

case 'clearstone':
$roleId = $_POST["username"];

//链接mongodb
$manager = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');

$filter  = [ 'name' => ''.$roleId.'' ]; //查询条件 user_id大于0
$query   = new MongoDB\Driver\Query($filter); //查询请求
$list    = $manager->executeQuery('goal.Player', $query); // 执行查询 location数据库下的box集合

foreach ($list as $document) {
    
$roleId11 = $document->_id;//能打印下吗
}
//print_r($roleId11);





$bulk = new MongoDB\Driver\BulkWrite();

$bulk->delete(array(_id => $roleId11));

//print_r($bulk);


    $result = $manager->executeBulkWrite('goal.PlayerEquipPiece', $bulk);
    
    if($result->getDeletedCount()){
    		echo json_encode(array('info'=>1,'msg'=>'已清除，请重新登录！'));
    	}else{
    		echo json_encode(array('info'=>0,'msg'=>'清除失败，没有数据！'));
    	}





	
		
                        break;
                        
case 'pay':{
	$cdk = $_POST['cdk'];
	$pswd = $_POST['pswd'];

    $xxs = mysqli_fetch_assoc($db2->query("SELECT * FROM cdk WHERE uid = '$uid' limit 1"));

    if($xxs['id'] !=''){
        echo json_encode(array('info'=>0,'msg'=>'角色已授权,无需再次授权'));die;
    }
    $xxx = mysqli_fetch_assoc($db2->query("SELECT * FROM cdk WHERE cdk = '$cdk' limit 1"));
    if($xxx['cdk'] ==''){
        echo json_encode(array('info'=>0,'msg'=>'无此授权卡'));die;
    }
    if($xxx['status'] != 0){
        echo json_encode(array('info'=>0,'msg'=>'此授权卡已被使用'));die;
    }
    if($db2->query("UPDATE cdk SET status = 1 , qid = '$quid' ,uid = '$uid', pass = '$pswd' WHERE cdk = '$cdk';")){
        echo json_encode(array('info'=>1,'msg'=>'角色授权成功'));die;
    }else{
        echo json_encode(array('info'=>0,'msg'=>'授权失败.请联系管理员'));die;
    }

}
break;


} 

?>