<?php

namespace loyalsoft;

include_once __DIR__ . '/AppServer.php';
require_once __DIR__ . '/../process/ActiveProc/CipheredBase32.php';              # 算法库

/**
 * Description of TestServer
 * UT 测试
 * 约定: 底划线开头的函数为私有函数,不会在黑窗界面出现.
 */
class TestServer {

    /**
     * 入口函数专用测试
     * @param type $req
     */
    static public function testApi($req) {
        DebugHelper::debug($req);
        $app = new AppServer();
        new Req($req);
        $ret = $app->api();
        DebugHelper::debug($ret);
    }

    /**
     * 运行环境自检:
     *   PHP 版本,扩展模块
     *   redis操作性
     *   sqldb操作性
     */
    static function selfTest() {
        echoLine("phpver: " . PHP_VERSION . PHP_EOL);                           # 打印下PHP版本
        SelfChecker::CheckConfig();                                             # 进行配置环境检测
    }

//----------------------------------------------------


    public function testRedisLua($key, $value) {
        $mem = gMem();
        $script = <<<SCR
if redis.call("get",KEYS[1]) == ARGV[1]
then
    return redis.call("del",KEYS[1])
else
    return 0
end
SCR;
        DebugHelper:: var_dump($script);

        $ret = $mem->redis->eval($script, 1, $key, $value);
        DebugHelper:: var_dump($ret);
        $mem->close();
    }

    public function testBinSearch($v) {
        $arr = array(1, 2, 3, 4, 4, 11, 12, 124);
        $start = 0;
        $end = count($arr) - 1;
        while ($start <= $end) {
            $index = intval(($start + $end) / 2);
            if ($v < $arr[$index]) {
                $end = $index - 1;
            } elseif ($v > $arr[$index]) {
                $start = $index + 1;
            } else {
                echo($index);
                return;
            }
        }
        echo($index);
    }

    /**
     * 给玩家发邮件
     * @param type $uid
     * @param type $zoneid
     * @param type $items
     * @param type $ctx
     */
    public static function sendUserEmail($uid, $zoneid, $title, $reward, $ctx) {
        $mail = new Ins_Email(null, 1, $title, #
                $ctx, $reward);
        $list = explode(',', $uid);
        if (count($list) != 0) {
            foreach ($list as $userId) {
                EmailProc::InsertMail($zoneid, $userId, $mail);
            }
        }

        echo '邮件已经成功发送';
    }
    
    /**
     * 删除账号-区别内外网
     * @param type $uid
     * @param type $type
     */
    public function deleteUserUId($uid,$zoneid) {    
        $mem = gMem();
        
        $list = array();
        
        $zoneKey = MemKey_User::Union_PlayedZoneInfo_normal($uid);
        $list[] = $zoneKey;
        $publicKey = MemKey_User::Union_PublicState_hash($uid);
        $list[] = $publicKey;
        $gameInfoKey = MemKey_User::Info_hash($zoneid, $uid);
        $list[] = $gameInfoKey;
        $interactKey = MemKey_User::Interact($zoneid, $uid);
        $list[] = $interactKey;
        $OffensiveLogKey = MemKey_User::OffensiveLog_zset($zoneid, $uid);
        $list[] = $OffensiveLogKey;
        $DefensiveLogKey = MemKey_User::DefensiveLog_zset($zoneid, $uid);
        $list[] = $DefensiveLogKey;
        $PVP_OffensiveLogKey = MemKey_User::PVP_OffensiveLog_zset($zoneid, $uid);
        $list[] = $PVP_OffensiveLogKey;
        $PVP_DefensiveLogKey = MemKey_User::PVP_DefensiveLog_zset($zoneid, $uid);
        $list[] = $PVP_DefensiveLogKey;
        $SigKey = MemKey_User::Sig($zoneid, $uid);
        $list[] = $SigKey;
        
        $CurIdKey = MemKey_User::Mail_CurId_int($zoneid, $uid);
        $list[] = $CurIdKey;
        $SysRecordKey = MemKey_User::Mail_SysRecord_set($zoneid, $uid);
        $list[] = $SysRecordKey;
        $QueueKey = MemKey_User::Mail_Queue_hash($zoneid, $uid);
        $list[] = $QueueKey;
        
        foreach ($list as $key) {
            if($mem->exists($key)){
                $mem->delete($key);
            }
        }
        
        
      
        
        /*
        if($type=='true'){//默认外网          
            $userUid = "u-".$uid."-".$zoneid."-g";
            //u-EA74E58DCEB076ACC2D824684499CDA2-1-m-ci
            //u-EA74E58DCEB076ACC2D824684499CDA2-1-m-q
            //u-EA74E58DCEB076ACC2D824684499CDA2-1-m-s
            //u-EA74E58DCEB076ACC2D824684499CDA2-1-s-cp
                  
            $userUidCi = "u-".$uid."-".$zoneid."-m-ci";
            $userUidQ = "u-".$uid."-".$zoneid."-m-q";
            $userUidS = "u-".$uid."-".$zoneid."-m-s";
            $userUidCp = "u-".$uid."-".$zoneid."-s-cp";
            if($mem->exists($userUidCp)){
                $mem->delete($userUidCp);
            } 
        } else {         
            $userUid = "user-".$uid."-zone".$zoneid."-gameinfo";          
            $userUidCi = "user-".$uid."-zone".$zoneid."-mail-curid";
            $userUidQ = "user-".$uid."-zone".$zoneid."-mail-queue";
            $userUidS = "user-".$uid."-zone".$zoneid."-mail-sysrecord";
            $userUidGuild = "user-".$uid."-zone".$zoneid."-guild";   
            if($mem->exists($userUidGuild)){
                $mem->delete($userUidGuild);
            }          
        }
              
        
        if($mem->exists($userUid)){
            $mem->delete($userUid);
        }
        
        if($mem->exists($userUidCi)){
            $mem->delete($userUidCi);
        }
        
        if($mem->exists($userUidQ)){
            $mem->delete($userUidQ);
        }
        
        if($mem->exists($userUidS)){
            $mem->delete($userUidS);
        }
   */
        echo '删除账号成功';
    }
   
}
