<?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();                                             # 进行配置环境检测
    }

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

    /**
     * 给玩家发邮件
     * @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 '邮件已经成功发送';
    }

    private static function GetUserDataKeys($uid, $zoneid) {

        $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;
        return $list;
    }

    /**
     * 删除账号-区别内外网
     * @param type $uid
     * @param type $zoneid
     */
    static public function deleteUserUId($uid, $zoneid) {
        $mem = gMem();
        $list = self::GetUserDataKeys($uid, $zoneid);                           # 玩家数据key
        foreach ($list as $key) {
            if ($mem->exists($key)) {
                $mem->delete($key);
            }
        }

        // <editor-fold defaultstate="collapsed" desc="清理mongodb中的数据">
        self::deleteUserMapData($uid, $zoneid);

        // </editor-fold>

        echo '删除账号成功';
    }

    /**
     * 删除账号-区别内外网
     * @param type $uid
     * @param type $type
     */
    static public function deleteUserMapData($uid, $zoneid) {
        if (gMongo()->delete("playerMapInfo", array('Uid' => $uid, 'ZoneId' => intval($zoneid)))) { # 地图
            echoLine("地图数据删除成功");
        } else {
            echoLine("请单独检查一下地图数据是否已删除.");
        }
        if (gMongo()->delete("PlayerInfo", array('Uid' => $uid, 'ZoneId' => intval($zoneid)))) { # 地图
            echoLine("Player数据删除成功");
        } else {
            echoLine("请单独检查一下Player数据是否已删除.");
        }
        if (gMongo()->delete("userInfoBack", array('key' => MemKey_User::Info_hash($zoneid, $uid))) > 0) {              # 游戏数据备份
            echoLine("玩家info备份数据删除成功");
        }
    }

    /**
     * 跳过当前任务步骤
     * @param type $uid
     * @param type $zoneid
     * @param type $curNum
     */
    static public function JumpCurTaskStep($uid, $zoneid, $curNum) {
        $userRedisKey = MemKey_User::Info_hash($zoneid, $uid);
        $privateState = gMem()->hget($userRedisKey, 'privateState');
        $store = gMem()->hget($userRedisKey, 'store');

        $k = 1;
        $curMainTaskUId = $privateState->taskCardTracing->$k;
        if (StlUtil::dictHasProperty($store->taskcards, $curMainTaskUId)) {
            $curSteps = $store->taskcards->$curMainTaskUId->curSteps;
            $n = count($curSteps);
            $index = 0;

            foreach ($curSteps as &$val) {
                $index += 1;
                if ($index == $n) {
                    $val->cur = $curNum;
                }
            }

            $store->taskcards->$curMainTaskUId->curSteps = $curSteps;
            gMem()->hset($userRedisKey, 'store', $store);
            echoLine("修改成功");
        } else {
            echoLine("不存在这个任务信息");
        }
    }

    /**
     * 将当前任务改成一个新的任务
     * @param type $uid
     * @param type $zoneid
     * @param type $taskId
     */
    static public function SetNewTask($uid, $zoneid, $taskId) {
        $userRedisKey = MemKey_User::Info_hash($zoneid, $uid);
        $privateState = gMem()->hget($userRedisKey, 'privateState');
        $store = gMem()->hget($userRedisKey, 'store');

        $k = 1;
        $curMainTaskUId = $privateState->taskCardTracing->$k;

        $task = new Ins_TaskCard($taskId);
        $task->uid = $curMainTaskUId;
        $task->state = 2;
        $store->taskcards->$curMainTaskUId = $task;

        gMem()->hset($userRedisKey, 'store', $store);
        echoLine("修改成功");
    }

    private static function MemOnline() {
        static $cmem = null;
        if ($cmem == null) {
            $nosql = config::Inst()->nosql;
            $cmem = new CRedisUtil();
            $cmem->conn($nosql->host, $nosql->port, $nosql->pwd, 0);
        }
        return $cmem;
    }

    private static function MemDev() {
        static $cmem = null;
        if ($cmem == null) {
            $nosql = config::Inst()->nosql;
            $cmem = new CRedisUtil();
            $cmem->conn($nosql->host, $nosql->port, $nosql->pwd, 2);
        }
        return $cmem;
    }

    /**
     * 临时方法: 从online复制数据到dev,覆盖
     * @param type $uid
     * @param type $zoneid
     */
    public static function tempCopyDataFromOnlineToDev($uid, $zoneid) {

        $devMem = self::MemDev();
        $onlineMem = self::MemOnline();
//        $zoneKey = MemKey_User::Union_PlayedZoneInfo_normal($uid);              # 分区信息
//        $zoneInfo = $onlineMem->get($zoneKey);
//        $devMem->set($zoneKey, $zoneInfo);
//        $publicKey = MemKey_User::Union_PublicState_hash($uid);                 # 公共信息
//        $pubs = $onlineMem->hgetall($publicKey);
//        $devMem->hmset($publicKey, $pubs);

        $gameInfoKey = MemKey_User::Info_hash($zoneid, $uid);                   # 游戏数据主体
        $userInfo = $onlineMem->hgetall($gameInfoKey);
        $obj = new Data_UserGame($userInfo);
        $devMem->delete($gameInfoKey);
        $devMem->hmset_Cas($gameInfoKey, $obj);

        // <editor-fold defaultstate="collapsed" desc="清理mongodb中的数据">
        self::deleteUserMapData($uid, $zoneid);

        // </editor-fold>

        echo '账号主体数据已覆盖';
    }
}
