<?php

namespace loyalsoft;

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

/**
 * 活动模块
 */
class ActiveProc {

    /**
     * 兑换码礼包使用记录
     */
    const tab_toke_gift = 'tab_token_gift';

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法 
     */
    public static function procMain() {
        switch (req()->cmd) {
            case CmdCode::active_day7_drawreward:                               # 6502 领取七日签到奖励
                return self::Day7_DrawReward();
            case CmdCode::active_getzonePublicTs:                               # 6503 查询开服时间戳
                return ActiveProc::GetZonePublicTS();
            case CmdCode::active_getTodayOnlineInfos:                           # 6504 查询当天在线时长
                return self::getTotayOnlineInfos();
            case CmdCode::Task_Tili:                                            # 6508 体力变化
                return ActiveProc::RecoveryTili();
            case CmdCode::active_token_drawReward:                              # 6512 兑换码礼包
                return self::drawActivePackageByCode();
            case CmdCode::active_draw_onlinegift:                               # 6513 领取在线(时长)礼包
                return self::drawOnlineGift();
            case CmdCode::active_draw_reggift:                                  # 6514 领取全服注册礼包
                return self::drawRegGift();
            case CmdCode::active_presentTili:                                   # 6515 领取在线赠送体力
                return self::drawTiliGift();
            case CmdCode::active_get_regnum:                                    # 6516 查询当前全服注册人数
                return self::getRegNumber();
            case CmdCode::active_shenmiaoDrawPrize:                            # 6505 什么抽奖
                return self::shenmiaoDrawPrize();
            case CmdCode::active_GetShenmiaoData:
                return self::GetShenmiaoData();
            default:
                return Resp::err(ErrCode::cmd_err);
        }
    }

    /**
     * 每天重置 
     */
    static function DailyReset() {
        self::DailyResetDay7Task();
        self::ClearOnlineGiftTs();
        self::ClearDailyTiliGift();
    }

// <editor-fold defaultstate="collapsed" desc="    赠送体力    ">
    
    /**
     * 获取神庙的主界面信息
     */
    static function GetShenmiaoData() {
        $req = req();          
        $shenmiaoInfo = $req->userInfo->game->shenmiao;
        
        $shenmiaoInfo->initActiveItem();
        $req->userInfo->game->shenmiao = $shenmiaoInfo;
        
        UserProc::updateUserInfo();
        return Resp::ok(array(#                                                 # 返回值
                    'shenmiaoInfo' => $shenmiaoInfo,                 
        ));
    }
    
    /**
     * 神庙抽奖
     */
    static function ShenmiaoDrawPrize() {
        $req = req();
        list($type,$num) = $req->paras;  
        $shenmiaoInfo = $req->userInfo->game->shenmiao;
        
        my_Assert(!StlUtil::dictHasProperty($shenmiaoInfo->activeDic, $type), ErrCode::active_activeNoExistent);
        
        $list = array();      
        if(Enum_Active::NewWish == $type){                     
            my_Assert($shenmiaoInfo->activeDic->$type->num >= glc()->UserWishNumLimit, ErrCode::active_NewUserActiveNumLimit);
            
            $shenmiaoInfo->activeDic->$type->num += $num;   
            $shenmiaoInfo->activeDic->$type->totalNum += $num;
            
            $shenmo = \GameConfig::shenmiao_getItem($shenmiaoInfo->activeDic->$type->id);
            if($shenmo->discount != 0){
                $sList = explode(',', $shenmo->cost);
                $costVal = $sList[1] - $sList[1]*$shenmo->discount/100;
                $costStr = $sList[0].','.$costVal*$num;
                StoreProc::AddMultiItemInStore($costStr);
            }      
            
            $tag = false;
            $n = 0;
            for ($index = 0; $index < $num; $index++) {
                $n+=1;
                $itemType = self::activeRandom($shenmo->per, $shenmo); 
                if($itemType >= 2){
                    $tag = true;
                }
                if($n==10 && $tag == false){
                    $list[] =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $shenmo);
                } else {
                    $list[] =self::getActiveRandomItem($itemType, $shenmo);
                }                            
            }                             
            if($shenmiaoInfo->activeDic->$type->totalNum == 10 && !in_array($shenmo->must, $list)){//首次10连抽有必出--临时
                $list[0] = $shenmo->must;
            }
                
            if($shenmiaoInfo->activeDic->$type->totalNum >= 20){
                StlUtil::dictRemove($shenmiaoInfo->activeDic, $type);
            }
                                                       
        } else if(Enum_Active::OpenWish == $type){            
            $old = intval($shenmiaoInfo->activeDic->$type->totalNum/90);            
            $shenmiaoInfo->activeDic->$type->totalNum += $num;
            $new = intval($shenmiaoInfo->activeDic->$type->totalNum/90);        
            $shenmo = \GameConfig::shenmiao_getItem($shenmiaoInfo->activeDic->$type->id);            
            if($num == 1){
                $shenmiaoInfo->activeDic->$type->num += $num; 
                StoreProc::AddMultiItemInStore($shenmo->cost);
                  
                if($shenmiaoInfo->activeDic->$type->num%10 == 0){
                    if($shenmiaoInfo->activeDic->$type->num%90 == 0){
                        if($shenmiaoInfo->activeDic->$type->Ur == 0){
                            $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ur, $shenmo);
                            $list[] = $sid;
                        } else {
                            $shenmiaoInfo->activeDic->$type->Ur = 0;
                        }                                             
                    } else {
                        if($shenmiaoInfo->activeDic->$type->Ssr == 0){
                            $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $shenmo);
                            $list[] = $sid;
                        } else {
                            $shenmiaoInfo->activeDic->$type->Ssr = 0;
                        }                      
                    }                                                                                                     
                } else {     
                    $itemType = self::activeRandom($shenmo->per, $shenmo);
                    $aid =self::getActiveRandomItem($type, $shenmo);           
                    if($itemType == Enum_ActiveItemType::Ssr){
                        $shenmiaoInfo->activeDic->$type->Ssr = 1;
                    }
                
                    if($itemType == Enum_ActiveItemType::Ur){
                        $shenmiaoInfo->activeDic->$type->Ssr = 1;
                        $shenmiaoInfo->activeDic->$type->Ur = 1;
                    }
                     $list[] = $aid;
                }             
            } else {//10连抽
                $sList = explode(',', $shenmo->cost);
                $costStr = $sList[0].','.$sList[1]*$num;
                StoreProc::AddMultiItemInStore($costStr);              
                
                $tag = false;
                $n = 0;
                for ($index1 = 0; $index1 < $num; $index1++) {
                   $n+=1;
                    $itemType = self::activeRandom($shenmo->per, $shenmo); 
                    if($itemType >= 2){
                        $tag = true;
                    }
                    if($n==10 && $tag == false){
                        $list[] =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $shenmo);
                    } else {
                        $list[] =self::getActiveRandomItem($itemType, $shenmo);
                        if($itemType == Enum_ActiveItemType::Ur){
                            $shenmiaoInfo->activeDic->$type->Ur = 1;
                        }                      
                    }  
                }
                
                if($old != $new){//说明过了一个90次的抽奖
                    if($shenmiaoInfo->activeDic->$type->Ur == 0){
                        $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ur, $shenmo); 
                        $list[0] = $aid;
                    } else {
                        $shenmiaoInfo->activeDic->$type->Ur = 1;
                    }
                }                                             
            }
            
        } else {
            $old = intval($shenmiaoInfo->activeDic->$type->totalNum/90);        
            $shenmiaoInfo->activeDic->$type->totalNum += $num;
            $new = intval($shenmiaoInfo->activeDic->$type->totalNum/90);
            if($num == 1){
                $shenmiaoInfo->activeDic->$type->num += $num; 
                StoreProc::AddMultiItemInStore($shenmo->cost);
                  
                if($shenmiaoInfo->activeDic->$type->num%10 == 0){
                    if($shenmiaoInfo->activeDic->$type->num%90 == 0){
                        if($shenmiaoInfo->activeDic->$type->Ur == 0){
                            $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ur, $shenmo);
                            $list[] = $sid;
                        } else {
                            $shenmiaoInfo->activeDic->$type->Ur = 0;
                        }                                             
                    } else {
                        if($shenmiaoInfo->activeDic->$type->Ssr == 0){
                            $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $shenmo);
                            $list[] = $sid;
                        } else {
                            $shenmiaoInfo->activeDic->$type->Ssr = 0;
                        }                      
                    }                                                                                                     
                } else {     
                    $itemType = self::activeRandom($shenmo->per, $shenmo);
                    $aid =self::getActiveRandomItem($type, $shenmo);           
                    if($itemType == Enum_ActiveItemType::Ssr){
                        $shenmiaoInfo->activeDic->$type->Ssr = 1;
                    }
                
                    if($itemType == Enum_ActiveItemType::Ur){
                        $shenmiaoInfo->activeDic->$type->Ssr = 1;
                        $shenmiaoInfo->activeDic->$type->Ur = 1;
                    }
                     $list[] = $aid;
                }
            } else {
                $sList = explode(',', $shenmo->cost);
                $costStr = $sList[0].','.$sList[1]*$num;
                StoreProc::AddMultiItemInStore($costStr);              
                
                $tag = false;
                $n = 0;
                for ($index1 = 0; $index1 < $num; $index1++) {
                   $n+=1;
                    $itemType = self::activeRandom($shenmo->per, $shenmo); 
                    if($itemType >= 2){
                        $tag = true;
                    }
                    if($n==10 && $tag == false){
                        $list[] =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $shenmo);
                    } else {
                        $list[] =self::getActiveRandomItem($itemType, $shenmo);
                        if($itemType == Enum_ActiveItemType::Ur){
                            $shenmiaoInfo->activeDic->$type->Ur = 1;
                        }                      
                    }  
                }
                
                if($old != $new){//说明过了一个90次的抽奖
                    if($shenmiaoInfo->activeDic->$type->Ur == 0){
                        $aid =self::getActiveRandomItem(Enum_ActiveItemType::Ur, $shenmo); 
                        $list[0] = $aid;
                    } else {
                        $shenmiaoInfo->activeDic->$type->Ur = 1;
                    }
                }
            }
            
        }   
        $req->userInfo->game->shenmiao = $shenmiaoInfo;
        UserProc::updateUserInfo();
        
        return Resp::ok(array(#                                                 # 返回值
                    'shenmiaoInfo' => $shenmiaoInfo,
                    'list'=>$list,            
        ));
        
    }
    /**
     * 
     * @param type $randCtx
     * @param sm_shenmiao $mo
     * @return int
     */
    public static function activeRandom($randCtx,$mo) {
        $rand = rand(1,10000);
        
        $type = 1;
        $list = explode(',', $randCtx);
        
        $start = 0;
        foreach ($list as $val) {
            $end = $val + $start;
            if($rand > $start && $rand <= $end){
                break;
            }
            $type += 1;
        }
        return $type;
        
//        $id = 0;
//        switch ($type) {
//            case Enum_ActiveItemType::Sr:
//                $id = array_rand($mo->srList);
//                break;
//            case Enum_ActiveItemType::Ssr:
//                $id = array_rand($mo->ssrList);
//
//                break;
//            case Enum_ActiveItemType::Ur:
//                $id = array_rand($mo->urList);
//                break;
//            default:
//                break;
//        }      
//        return $id;
    }
    
    public static function getActiveRandomItem($type,$mo) {            
        $id = 0;
        switch ($type) {
            case Enum_ActiveItemType::Sr:
                $id = array_rand($mo->srList);              
                break;
            case Enum_ActiveItemType::Ssr:
                $id = array_rand($mo->ssrList);              
                break;
            case Enum_ActiveItemType::Ur:
                $id = array_rand($mo->urList);              
                break;
            default:
                break;
        }      
        return $id;
    }
    
    /**
     * 
     * @param type $activeType
     * @param type $num
     * @param type $shenmo
     * @return type
     */
    public function DrawLottery($activeType,$num,$shenmo,$maxNum) {
        $list = array();
        $tag = false;
            $n = 0;
            for ($index = 0; $index < $num; $index++) {
                $n+=1;
                $itemType = self::activeRandom($shenmo->per, $shenmo); 
                if($itemType >= 2){
                    $tag = true;
                }
                if($n==10 && $tag == false){
                    $list[] =self::getActiveRandomItem(Enum_ActiveItemType::Ssr, $mo);
                } else {
                    $list[] =self::getActiveRandomItem($itemType, $mo);
                }                            
            } 
            return $list;
    }
    
    /**
     * [6515] 领取 赠送体力  
     */
    static function drawTiliGift() {
        $user = ctx();
        $privateState = ctx()->privateData();
        $base = $user->base();
        $hour = date('G');
        my_Assert(!in_array($hour, $privateState->dailyDrawedTiliGift), ErrCode::active_hasgetted); # 判断是否已领取
        $arr = explode(',', glc()->activity_presentTili_times);                 # 判断是否满足领取条件
        my_Assert(in_array($hour, $arr), ErrCode::active_illegal);              # 不满足条件 
        $base->Add_tili(glc()->activity_presentTili_number);                    # 增加体力
        $privateState->dailyDrawedTiliGift[] = $hour;
        UserProc::updateUserInfo();                                             # 回存
        return Resp::ok(array(#                                                 # 返回值
                    'key' => 'ok',
                    'record' => $privateState->dailyDrawedTiliGift,
                    'tili' => $base->tili,
                    'time' => $user->privateState->TiliTime,
        ));
    }

////
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="    全服注册礼包    ">

    /**
     * [6516] 查询当前全服注册人数 
     */
    static function getRegNumber() {
        return Resp::ok(array(
                    'num' => gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid))
        ));
    }

    /**
     * [6514] 领取全服注册礼包 
     */
    static function drawRegGift() {
        $user = new Data_UserGame(req()->userInfo->game);
        $privateState = new Info_PrivateState($user->privateState);
        $giftId = req()->paras[0];                                               # 礼包id
        $giftModel = GameConfig::activity_reggift_getItem($giftId);             # 取礼包常亮数据
        if (null == $giftModel) {
            return Resp::err(ErrCode::err_const_no);
        }
        if (in_array($giftId, $privateState->drawedRegGift)) {                  # 判断是否已领取
            return Resp::err(ErrCode::active_hasgetted);
        }
        // 判断是否满足领取条件
        if (gMem()->get(MemKey_GameRun::Stat_UserCountByZone_int(req()->zoneid)) < $giftModel->regNumber) {
            return Resp::err(ErrCode::active_illegal);              # 不满足条件
        }
        $err = StoreProc::AddMultiItemInStore($giftModel->rewardstr);           # 发放奖励
        if ($err) {
            return Resp::err($err);                                 # 失败
        }
        $privateState->drawedRegGift[] = $giftId;
        $user->privateState = $privateState;
        req()->userInfo->game = $user;
        UserProc::updateUserInfo();                                         # 回存
        return Resp::ok(array(#                                     # 返回值
                    'key' => 'ok',
                    'store' => $user->store
        ));
    }

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="    在线礼包   ">

    /**
     * 【6504】查询当天在线时长信息 
     */
    static function getTotayOnlineInfos() {

        return Resp::ok(array(
                    "onlineGiftIDs" => req()->userInfo->game->privateState->onlineGiftIDs,
                    "onlineGiftts" => req()->userInfo->game->privateState->onlineGiftts
        ));
    }

    /**
     * [6513] 领取连续在线礼包 
     */
    static function drawOnlineGift() {
        $giftid = req()->paras[0];                                               # 提取参数: 礼包的编号
        $user = ctx();
        $privateState = ctx()->privateData();
        $giftData = GameConfig::activity_onlinegift_getItem($giftid);           # 在线礼包常量数据
        my_Assert(null != $giftData, ErrCode::onlinegift_constno_err);          # 防御找不到配置数据
        my_Assert($privateState->onlineGiftts >= $giftData->onlineSec, #        # 时间未到，不可以领取
                ErrCode::onlinegift_timenotenough);

        my_default_Arr($privateState->onlineGiftIDs);                           # 防御未初始化领取记录
        my_Assert(!in_array($giftid, $privateState->onlineGiftIDs), #            # 判断礼包ID是否已经领取
                ErrCode::active_hasgetted);
        $err = StoreProc::AddMultiItemInStore($giftData->rewardstr);            # 发放奖励
        my_Assert(ErrCode::ok == $err, $err);                                   # 发放奖励失败 
        $privateState->onlineGiftIDs[] = $giftid;                               # 记录已领礼包ID     
        UserProc::updateUserInfo();                                             # 回写用户数据
        return Resp::ok(array(
                    'onlineGiftts' => $privateState->onlineGiftts,
                    'onlineGiftIDs' => $privateState->onlineGiftIDs,
                    "reward" => $giftData->rewardstr,
                    'gold' => $user->base()->gold,
                    'cash' => $user->base()->cash,
                    'tili' => $user->base()->tili,
                    'store' => $user->store(),
                    'hero' => $user->heros()
        ));                                                                     # 设置返回值
    }

    /**
     * 清理/重置 在线礼包时间戳 
     */
    static private function ClearOnlineGiftTs() {
        req()->userInfo->game->privateState->onlineGiftts = 0;
        req()->userInfo->game->privateState->onlineGiftIDs = array();
    }

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="    兑换码     ">

    /**
     * [6512]凭兑换码领取礼包 
     */
    static function drawActivePackageByCode() {
        $user = new Data_UserGame(req()->userInfo->game);
        $privateState = new Info_PrivateState($user->privateState);             # 快速访问
        list( $activeId, $codestring) = req()->paras;                            # 取参数 活动id, 兑换码
        $active = GameConfig::activity_getItem($activeId);                      # 活动数据
        my_Assert(null != $active, ErrCode::active_const_no_err);
        my_Assert($active->startts <= now() && $active->endts >= now(), ErrCode::active_time); # 校验开放时间
        my_Assert(preg_match("/^[a-kmnp-z2-9]{10}$/", $codestring), ErrCode::active_activecode_format); # 基础格式校验(10个特定字符)
        $activeCode = CipheredBase32::Decode($codestring);                      # 解码
        $codePlatStr = GameConstants::GetPlatStringByActivteCode($activeCode);  # platstr
        my_Assert(GameConstants::AllPlatStr == $codePlatStr #                   # 忽略全平台礼包
                || req()->userInfo->getPlatStr() == $codePlatStr, #              # 平台字符串必须相符
                ErrCode::active_activecode_plat); #                             # 平台错误

        my_Assert(is_int($activeCode->number) #                                 # 编号为int值
                && $activeCode->number >= 1 && $activeCode->number <= 50000, #  # 检查 兑换码的编号范围0~50000
                ErrCode::active_activecode_format);

        $packageID = $activeCode->package;                                      # 礼包id
        $packageInfo = GameConfig::tokenGift_getItem($packageID);               # 礼包常量数据
        my_Assert(null != $packageInfo, ErrCode::err_const_no);                 # 防御
        my_Assert($packageInfo->expirets >= now() && $packageInfo->startTs <= now(),
                ErrCode::active_activecode_outtime);                            # 激活码已经失效,或者礼包尚未开启
        my_Assert(!in_array($packageID, $privateState->usedTokens), ErrCode::active_hasgetted); # 已经领取过该礼包了
        my_Assert(!self::checkActiveCodeIsUsed($activeCode), ErrCode::active_activecode_used);  # 检查 该激活码是否已经使用过了
        $err = StoreProc::AddMultiItemInStore($packageInfo->reward);            # 发放礼包
        my_Assert(ErrCode::ok == $err, $err);                                   # 防御发放礼包过程出错
        req()->userInfo->game->privateState->usedTokens[] = $packageID;          # 记录领取记录
        $ok = self::setActiveCodeUserRecord($activeCode, req()->uid);            # 插入数据库
        my_Assert($ok, ErrCode::err_db);                                        # 数据库操作失败- 重试
        UserProc::updateUserInfo();                                             # 回存玩家数据
        $ret = array(#                                                          # 返回值
            "plat" => $codePlatStr,
            "packageId" => $activeCode->package,
            "reward" => $packageInfo->reward,
            'gold' => $user->base()->gold,
            'cash' => $user->base()->cash,
            'tili' => $user->base()->tili,
            'store' => $user->store,
            'hero' => $user->heros
        );
        return Resp::ok($ret);                                                  # 返回成功信息
    }

    /**
     * 检查兑换码是否已经使用过了
     * @param ActiveCode $activeCode
     * @return boolean true : 已经用过了, false : 激活码尚未使用过.
     */
    static function checkActiveCodeIsUsed($activeCode) {
        $sql = sprintf("`plat`='%d' and `package`='%d' and `number`='%d'", #       # where 子句
                $activeCode->plat, $activeCode->package, $activeCode->number);
        $rows = daoInst()
                ->select()
                ->from(self::tab_toke_gift)                                     # 表名
                ->where($sql)
                ->count();
        return $rows > 0;
    }

    /**
     * 将兑换码添加到使用记录中
     * @param ActiveCode $actvieCode
     * @param string $uid
     * @return boolean true: 操作成功, false: 操作失败
     */
    static function setActiveCodeUserRecord($actvieCode, $uid) {
        $actvieCode->uid = $uid;                                                # 添加uid
        $ret = daoInst()
                ->insert(self::tab_toke_gift)                                   # 表名
                ->data($actvieCode)
                ->exec();
        return $ret > 0;
    }

// </editor-fold>

    /**
     * [6503] 查询当前分区开放时间戳 
     */
    private static function GetZonePublicTS() {
        $zoneInfo = GameConfig::zonelist_getItem(req()->zoneid);
        return Resp::ok($zoneInfo->publicTs);
    }

// <editor-fold defaultstate="collapsed" desc="  体力  ">

    /**
     * [6508]  
     */
    public static function RecoveryTili() {
        $useTili = req()->paras[0];
        ActiveProc::ChangeTili($useTili);

        UserProc::updateUserInfo();
        $result = array(
            'time' => req()->userInfo->game->privateState->TiliTime,
            'tili' => req()->userInfo->game->baseInfo->tili
        );
        $resp = Resp::ok($result);
        return $resp;
    }

    /**
     * 清理每天的赠送体力领取记录 
     */
    private static function ClearDailyTiliGift() {
        req()->userInfo->game->privateState->dailyDrawedTiliGift = ArrayInit();
    }

    /**
     * 修改玩家体力
     * @param int $useTili $useTili>0代表增加体力,   $useTili<0代表消耗体力 
     */
    public static function ChangeTili($useTili) {
        $user = req()->userInfo->game;
        $moreTili = 0;                                                          # 超上限的体力值
// 如果没有tilitime这个值,说明是以前老用户新登录的,为了纠错在判断里面补值.(正常用户不会进入判断内部)
        if (!CommUtil::isPropertyExists($user->privateState, "TiliTime")) {
            $user->privateState->TiliTime = time() - 120;
        }
        if ($user->baseInfo->tili > 120) {                                                # 如果该账户体力值本身是大于上限的,获取一下超出的体力值
            $moreTili = $user->baseInfo->tili - 120;                                      # 用moreTili保存一下额外的体力值
        }
        $useTime = time() - $user->privateState->TiliTime;                      # 距离上次修改体力值的时间
        if ($useTime > 300) {                                                   # 该账户超过了单位体力时间,需要重新计算体力值
            if ($moreTili <= 0) {
                $user->baseInfo->tili += (int) ( (int) ($useTime / 300));
                if ($user->baseInfo->tili > 120) {                                        # 体力值上限
                    $user->baseInfo->tili = 120;
                } // 这代码真烂,以后用的时候再修复吧... -gwang 20200110154858
            }
            $user->privateState->TiliTime = time();
        }
        if ($useTili < 0) {                                                     # 如果是消耗体力
            my_Assert(abs($useTili) < $user->baseInfo->tili, ErrCode::notenough_tili);    # 体力不足
            $user->baseInfo->tili += $useTili;                                            # 扣除体力
        } else if ($useTili > 0) {                                              # 如果是购买增加体力
            $user->baseInfo->tili += $useTili;
        }
        return ErrCode::ok;
    }

// </editor-fold>
// 
// 
// <editor-fold defaultstate="collapsed" desc="    7日签到   ">

    /**
     *  重置7日签到相关计数器 
     */
    static function DailyResetDay7Task() {
        req()->userInfo->game->gates->Times = 0;
        $typeID = 6;                                                            #  7日活动
        $ac = GameConfig::activity_getItem($typeID);
        my_Assert(null != $ac, ErrCode::err_const_no);
        $startDay = tsDay($ac->startts);
        $newLoginDays = array_filter(req()->userInfo->game->privateState->LoginDays, # 剔除不符合时间的登录记录
                function ($tsday)use ($startDay) {
                    return $startDay <= $tsday;
                }
        );
        req()->userInfo->game->privateState->LoginDays = $newLoginDays;          # 
        if (count(req()->userInfo->game->privateState->LoginDays) < 7) {
            req()->userInfo->game->privateState->LoginDays[] = tsDay();
        }
    }

    /**
     * [6502] 领取七日签到奖励 
     */
    static function Day7_DrawReward() {
        // 设计7日数据结构
        list($day) = req()->paras;                                               # 参数: 领取第x天的奖励
        $user = req()->userInfo->game;
        $private = new Info_PrivateState($user->privateState);   # 私有数据
        my_Assert(count($private->LoginDays) >= $day, ErrCode::active_day7_totaldays);
        my_Assert(!in_array($day, $private->day7_drawed), ErrCode::active_hasgetted);
        $day_rwd = GameConfig::activity_day7_getItem($day);                     # 查询奖励数据
        my_Assert(null != $day_rwd, ErrCode::err_const_no);                     # 防御找不到配置
        StoreProc::AddMultiItemInStore($day_rwd->reward);                       # 发放奖励
        $private->day7_drawed[] = $day;                                         # 添加领取记录
        $user->privateState = $private;                          # 更新数据
        UserProc::updateUserInfo();                                             # 回存
        return Resp::ok(array(
                    'gold' => $user->baseInfo->gold,
                    'cash' => $user->baseInfo->cash,
                    'tili' => $user->baseInfo->tili,
                    'store' => $user->store,
                    'hero' => $user->heros
        ));                                                                     # 返回值
    }

// </editor-fold>
//    
    
    
    
}
