<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace loyalsoft;

/**
 * Description of ShopProc
 *
 * @author c'y'zhao
 */
class ShopProc {

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法
     * @param Req $req
     */
    public static function procMain($req) {
        switch ($req->cmd) {
            case CmdCode::cmd_shop_BuyGift:                                     # 6501 商城购买东西
                return ShopProc::ShopBuyGift();
            case CmdCode::cmd_shop_BuyTili:                                     # 6502 买体力
                return ShopProc::BuyTili();
            case CmdCode::cmd_shop_GateGift_ShowGateId:                         # 6503 章节礼包 分页选中记录
                return ShopProc::Shop_GateGift_ShowGateId();
            default:
                Err(ErrCode::cmd_err);
        }
    }
    
    /**
     * 6503 章节礼包 分页选中记录
     */
    public static function Shop_GateGift_ShowGateId() {
        list($gateId) = req()->paras;
        
        //my_Assert(StlUtil::dictHasProperty(ctx()->gates->GateList,$gateId), ErrCode::active_const_no_err);
        //$mo = GameConfig::shop_gategift_getItem($gateId);
        //my_Assert($mo != null, ErrCode::err_const_no);
        
        ctx()->privateState->shop_gateGift_showGateId = $gateId;
        
        UserProc::updateUserInfo();
        return Resp::ok(array());
    }

    /**
     * 6502 买体力
     * @return type
     */
    public static function BuyTili() {
        list($type) = req()->paras;

        $addTili = 0;
        if ($type == 1) {//广告
            $maxNum = glc()->GuanggaoGetTili_DailyMaxNum;
            my_Assert(ctx()->privateState->guanggaoGetTiliNum < $maxNum, ErrCode::user_shop_GuanggaoGetTiliNumLimit);
            my_Assert(now() - ctx()->privateState->guanggaoGetTili_ts > 300, ErrCode::user_shop_DownTsLimit);
            $addTili = glc()->GuanggaoGetTili;
            ctx()->baseInfo->Add_tili(glc()->GuanggaoGetTili);
            ctx()->privateData(true)->guanggaoGetTiliNum += 1;

            ctx()->privateData(true)->guanggaoGetTili_ts = now();
            
            TaskProc::OnLookGuanggao();
        } else {
            $maxNum = glc()->BuyTili_DailyMaxNum;
            my_Assert(ctx()->privateState->buyTiliNum < $maxNum, ErrCode::user_shop_buyTiliNumLimit);

            $arr = explode(',', glc()->BuyTiliCost);
            my_Assert(ctx()->baseInfo->cash >= $arr[0], ErrCode::user_shop_NotRepeatBuy);
            ctx()->baseInfo->Consume_Cash($arr[0]);
            $addTili = $arr[1];
            ctx()->baseInfo->Add_tili($arr[1]);

            ctx()->privateData(true)->buyTiliNum += 1;
        }

        TaskProc::OnBuyTiliOrLookGuanggao();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    "privateState" => ctx()->privateState,
                    "baseInfo" => ctx()->baseInfo,
                    "tili" => $addTili,
                    "task" => ctx()->task,
        ));
    }

    /**
     * 商城购买东西
     * @return type
     */
    public static function ShopBuyGift() {
        list($type, $typeId, $buyNum) = req()->paras;
        $user = ctx();

        //$resultArr = array();
        switch ($type) {
            case EnumShopGift::GateGift_Shop: // 1 章节礼包
                self::BuyShop_GateGift($type, $typeId, $buyNum);
                break;
            case EnumShopGift::Daily_Shop:    // 2 每日商店
                self::BuyShop_Daily($type, $typeId, $buyNum);
                break;

            case EnumShopGift::GemBox_Shop :  // 7 宝石宝箱---->2024.8.14 改为可能抽出宝石或是道具
                self::BuyShop_GemBox($type, $typeId, $buyNum);

                break;
            case EnumShopGift::BujiBox_Shop:  // 4 补给箱
                $equip = self::BuyShop_BujiBox($type, $typeId, $buyNum);
                break;
            case EnumShopGift::Cash_Shop:     // 5 商城-钻石
                self::BuyShop_Cash($type, $typeId, $buyNum);
                break;
            case EnumShopGift::Gold_Shop:     // 6 商城-金币--可以多次购买
                self::BuyShop_Gold($type, $typeId, $buyNum);
                break;

            case EnumShopGift::MonthCard_Shop:// 8 月卡
                self::BuyShop_MonthCard($type, $typeId, $buyNum);
                break;
            case EnumShopGift::Equip_Shop :  // 7 宝石宝箱---->2024.8.14 改为可能抽出宝石或是道具
                self::BuyShop_EquipBox($type, $typeId, $buyNum);
            default:
                break;
        }

//        if (ctx()->baseInfo->charge_amt > 0 && ctx()->baseInfo->fRechargePriceReceived == 0) {
//            StoreProc::AddMultiItemInStore(GameConfig::globalsettings()->FirstRechargePrice);
//            ctx()->baseInfo->fRechargePriceReceived = 1;
//        }


        ctx($user);
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    //'gemBox' => $resultArr,
                    //'gateGift' => $gateGift,
                    'cash' => $user->baseInfo->cash,
                    'gold' => $user->baseInfo->gold,
                    'privateState' => $user->privateState,
                    'store' => $user->store,
                    'task' => ctx()->task,
                    'heros' => ctx()->heros,
                    'reward' => StoreProc::$reward,
                    'reward_Gem' => StoreProc::$reward_Gem,
                    'reward_equip' => StoreProc::$reward_equip,
        ));
    }
    
    public static function BattlePass_TextPay($type, $typeId, $buyNum) {
        $mo = GameConfig::shop_getItem($typeId);
        
        switch ($mo->shopItemType) {
            case 'battlePass_gate':
                    ctx()->privateData(true)->battlePass_gate_cost_ts = now();
                    break;
                case 'battlePass_activePoint':
                    ctx()->privateData(true)->battlePass_activePoint_cost_ts = now();
                    break;
                case 'battlePass_tili':
                    ctx()->privateData(true)->battlePass_tili_cost_ts = now();
                    break;
            default:
                break;
        }
        
        
    }

    /*
     * 商城章节礼包购买
     */

    public static function BuyShop_GateGift($type, $typeId, $buyNum) {
        //$gateGift = array();
        my_Assert(StlUtil::dictHasProperty(ctx()->gates->GateList, $typeId), ErrCode::active_const_no_err);
        my_Assert(!in_array($typeId, ctx()->privateState->gateGiftReceived), ErrCode::user_shop_NotRepeatBuy);

        $mo = GameConfig::shop_gategift_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);
        my_Assert(ctx()->baseInfo->cash>= $mo->price, ErrCode::notenough_cash_msg);
        StoreProc::AddMultiItemInStore($mo->reward);
//        $price = explode(';', $mo->reward);
//        foreach ($price as $value) {
//            $item = explode(',', $value);
//            if (GameConfig::item_getItem($item[0])->itemType == EnumItemType::tuzhiBox) {
//                $dic = GameConfig::item();
//                $list = array();
//                foreach ($dic as $id => $val) {
//                    if ($val->itemType == EnumItemType::tuzhi) {
//                        $list[] = $val->typeId;
//                    }
//                }
//
//                $randNum = rand(0, count($list) - 1);
//                $tuzhiId = $list[$randNum];
//                $gateGift[] = $tuzhiId . ',1';
//            } else {
//                $gateGift[] = $value;
//            }
//        }
//        foreach ($gateGift as $str) {
//            StoreProc::AddMultiItemInStore($str);
//        }
        ctx()->privateData(true)->gateGiftReceived[] = $typeId;       
        ctx()->baseInfo->Consume_Cash($mo->price);

        //return $gateGift;
    }

    /**
     * 每日商店
     * @param type $type
     * @param type $typeId
     * @param type $buyNum
     */
    public static function BuyShop_Daily($type, $typeId, $buyNum) {
        $mo = GameConfig::shop_daily_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        my_Assert(in_array($typeId, ctx()->privateState->dailyShopRandItems), ErrCode::err_const_no);
        if ($mo->type == 1) {
            $getNum = 0;
            foreach (ctx()->privateState->dailyShopReceived as $id) {
                if ($id == $typeId) {
                    $getNum += 1;
                }
            }

            if ($getNum >= $mo->freeNum) {
                my_Assert(ctx()->privateState->dailyShop_GuangGaoNum < $mo->num, ErrCode::user_shop_GuanggaoFreeNumLimit);
                if (ctx()->privateState->dailyShop_GuangGaoNum >= 1) {
                    my_Assert(now() - ctx()->privateState->dailyShop_GuangGaoTs >= $mo->downTs, ErrCode::user_shop_DownTsLimit);
                }
                ctx()->privateState->dailyShop_GuangGaoNum += 1;
                ctx()->privateState->dailyShop_GuangGaoTs = now();
                
                TaskProc::OnLookGuanggao();
            }
        } else {
            my_Assert(!in_array($typeId, ctx()->privateState->dailyShopReceived), ErrCode::user_shop_NotRepeatBuy);

            switch ($mo->costType) {
                case EnumShopCost::Gold:
                    my_Assert(ctx()->baseInfo->gold >= $mo->costNum, ErrCode::notenough_gold_msg);
                    ctx()->baseInfo->Consume_Gold($mo->costNum);
                    break;
                case EnumShopCost::Cash:
                    my_Assert(ctx()->baseInfo->cash >= $mo->costNum, ErrCode::notenough_cash_msg);
                    ctx()->baseInfo->Consume_Cash($mo->costNum);
                    break;
                default:
                    break;
            }

            TaskProc::OnBuyNumDailyShop();
        }

        StoreProc::AddMultiItemInStore($mo->reward);
        ctx()->privateState->dailyShopReceived[] = $typeId;
        TaskProc::OnBuyNumDailyShop_state();
    }
    
    /**
     * 宝石宝箱---->2024.8.14 改为可能抽出宝石或是道具
     */
    public static function BuyShop_EquipBox($type, $typeId, $buyNum) {
        $mo = GameConfig::shop_box_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        if ($buyNum == 1) {

            $yaoshiArr = explode(',', $mo->cost_one_yaoshi);
            $itemId = $yaoshiArr[0];
            if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
                ctx()->store->removeItem($itemId, $yaoshiArr[1]);
            } else {
                $cost = $mo->cost_one;
                my_Assert(ctx()->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
                ctx()->baseInfo->Consume_Cash($cost);
            }
        } elseif ($buyNum == 10) {
            $yaoshiArr = explode(',', $mo->cost_ten_yaoshi);
            $itemId = $yaoshiArr[0];
            if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
                ctx()->store->removeItem($itemId, $yaoshiArr[1]);
            } else {
                $cost = $mo->cost_ten;
                my_Assert(ctx()->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
                ctx()->baseInfo->Consume_Cash($cost);
            }
        }

        if ($mo->type == 3) {
            if ($buyNum == 0) {
                $buyNum = 1;
                my_Assert(now() - ctx()->privateState->lastFreeGetTs_ShopBoxCommon_equip >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
                ctx()->privateState->lastFreeGetTs_ShopBoxCommon_equip = now();
                TaskProc::OnLookGuanggao();
            }
        } else if ($mo->type == 4) {
            if ($buyNum == 0) {
                $buyNum = 1;
                my_Assert(now() - ctx()->privateState->lastFreeGetTs_ShopBoxBright_equip >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
                ctx()->privateState->lastFreeGetTs_ShopBoxBright_equip = now();
                TaskProc::OnLookGuanggao();
            }
                        
        }

        $arr = array();
        $arr[] = 1;
        $arr[] = 10;
        if (!in_array($buyNum, $arr)) {
            $buyNum = 1;
        }
        //TaskProc::OnOpenNumBrightBox($typeId, $buyNum);
        TaskProc::OnOpenNumAnyBox($buyNum);
        //FightProc::funUnlock_Gem();

        $perList = explode(';', $mo->percent);
        $allNum = 0;
        foreach ($perList as $key => $value) {
            $str = explode(',', $value);
            $allNum += $str[1];
        }

        $allNum *= 1000;
        for ($i = 0; $i < $buyNum; $i++) {
            mt_srand((double) microtime() * 1000000);
            $buyCount = 0;
            if ($mo->type == 3) {
                ctx()->privateState->buyNum_ShopBoxCommon_equip += 1;
                $buyCount = ctx()->privateState->buyNum_ShopBoxCommon_equip;
            } else {
                ctx()->privateState->buyNum_ShopBoxBright_equip += 1;
                $buyCount = ctx()->privateState->buyNum_ShopBoxBright_equip;
            }

            $bichu1 = explode(',', $mo->bichu1);
            $qual = 0; //改成道具盒子了 但是这个变量不动了
            if ($buyCount % $bichu1[0] == 0) {
                $qual = $bichu1[1];
            }
            if ($mo->bichu2 != null) {
                $bichu2 = explode(',', $mo->bichu2);
                if ($buyCount % $bichu2[0] == 0) {
                    $qual = $bichu2[1];
                }
            }

            if ($qual == 0) {
                $start = 0;
                $end = 0;
                $randNum = mt_rand(1, $allNum);
                foreach ($perList as $k => $val) {
                    $str = explode(',', $val);
                    $end += $str[1] * 1000;
                    if ($randNum > $start && $randNum <= $end) {
                        $qual = $str[0];
                        break;
                    }
                    $start = $end;
                }
            }

            if ($qual > 0) {
                StoreProc::AddMultiItemInStore($qual . ",1", Enum_StoreSourceType::ShopBox);
            }
        }
        TaskProc::OnOpenNumBrightBox_state($typeId, $buyNum);
        //FightProc::funUnlock_equip_firstTime();
    }

    /**
     * 宝石宝箱---->2024.8.14 改为可能抽出宝石或是道具
     */
    public static function BuyShop_GemBox($type, $typeId, $buyNum) {
        $mo = GameConfig::shop_box_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        if ($buyNum == 1) {

            $yaoshiArr = explode(',', $mo->cost_one_yaoshi);
            $itemId = $yaoshiArr[0];
            if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
                ctx()->store->removeItem($itemId, $yaoshiArr[1]);
            } else {
                $cost = $mo->cost_one;
                my_Assert(ctx()->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
                ctx()->baseInfo->Consume_Cash($cost);
            }
        } elseif ($buyNum == 10) {
            $yaoshiArr = explode(',', $mo->cost_ten_yaoshi);
            $itemId = $yaoshiArr[0];
            if (StlUtil::dictHasProperty(ctx()->store->items, $itemId) && ctx()->store->items->$itemId >= $yaoshiArr[1]) {
                ctx()->store->removeItem($itemId, $yaoshiArr[1]);
            } else {
                $cost = $mo->cost_ten;
                my_Assert(ctx()->baseInfo->cash >= $cost, ErrCode::notenough_cash_msg);
                ctx()->baseInfo->Consume_Cash($cost);
            }
        }

        if ($mo->type == 1) {
            if ($buyNum == 0) {
                $buyNum = 1;
                my_Assert(now() - ctx()->privateState->lastFreeGetTs_ShopBoxCommon >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
                ctx()->privateState->lastFreeGetTs_ShopBoxCommon = now();
                TaskProc::OnLookGuanggao();
            }
        } else if ($mo->type == 2) {
            if ($buyNum == 0) {
                $buyNum = 1;
                my_Assert(now() - ctx()->privateState->lastFreeGetTs_ShopBoxBright >= $mo->freeDownTs * 24 * 60 * 60, ErrCode::user_shop_FreeNumLimit);
                ctx()->privateState->lastFreeGetTs_ShopBoxBright = now();
                TaskProc::OnLookGuanggao();
            }
                        
        }

        $arr = array();
        $arr[] = 1;
        $arr[] = 10;
        if (!in_array($buyNum, $arr)) {
            $buyNum = 1;
        }
        TaskProc::OnOpenNumBrightBox($typeId, $buyNum);
        TaskProc::OnOpenNumAnyBox($buyNum);
        //FightProc::funUnlock_Gem();

        $perList = explode(';', $mo->percent);
        $allNum = 0;
        foreach ($perList as $key => $value) {
            $str = explode(',', $value);
            $allNum += $str[1];
        }

        $allNum *= 1000;
        for ($i = 0; $i < $buyNum; $i++) {
            mt_srand((double) microtime() * 1000000);
            $buyCount = 0;
            if ($mo->type == 1) {
                ctx()->privateState->buyNum_ShopBoxCommon += 1;
                $buyCount = ctx()->privateState->buyNum_ShopBoxCommon;
            } else {
                ctx()->privateState->buyNum_ShopBoxBright += 1;
                $buyCount = ctx()->privateState->buyNum_ShopBoxBright;
            }

            $bichu1 = explode(',', $mo->bichu1);
            $qual = 0; //改成道具盒子了 但是这个变量不动了
            if ($buyCount % $bichu1[0] == 0) {
                $qual = $bichu1[1];
            }
            if ($mo->bichu2 != null) {
                $bichu2 = explode(',', $mo->bichu2);
                if ($buyCount % $bichu2[0] == 0) {
                    $qual = $bichu2[1];
                }
            }

            if ($qual == 0) {
                $start = 0;
                $end = 0;
                $randNum = mt_rand(1, $allNum);
                foreach ($perList as $k => $val) {
                    $str = explode(',', $val);
                    $end += $str[1] * 1000;
                    if ($randNum > $start && $randNum <= $end) {
                        $qual = $str[0];
                        break;
                    }
                    $start = $end;
                }
            }

            if ($qual > 0) {
                StoreProc::AddMultiItemInStore($qual . ",1", Enum_StoreSourceType::ShopBox);
            }
        }
        TaskProc::OnOpenNumBrightBox_state($typeId, $buyNum);       
    }

    /**
     * 补给箱
     * @param type $type
     * @param type $typeId
     * @param type $buyNum
     */
    public static function BuyShop_BujiBox($type, $typeId, $buyNum) {
        $equip = array();
      
        return $equip;
    }

    /**
     * 钻石商城
     * @param type $type
     * @param type $typeId
     * @param type $buyNum
     */
    public static function BuyShop_Cash($type, $typeId, $buyNum) {
        $mo = GameConfig::shop_cash_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        $tag = false;
        if (in_array($typeId, ctx()->privateState->cashShopReceived)) {
            $tag = true;
        }

        $reward = $mo->reward;
        $pList = explode(',', $reward);
        $addYuanbao = $pList[1];
        if (!$tag) {
            $reward = $mo->reward . ';' . $mo->reward;
            $addYuanbao *= 2;
        }

        StoreProc::AddMultiItemInStore($reward);
        ctx()->baseInfo->accumulateYuanBao += $addYuanbao;
        //ctx()->baseInfo->charge_amt += $mo->price;
        ctx()->privateState->cashShopReceived[] = $typeId;
      
        TaskProc::OnRecharge();
        return $reward;
    }

    /**
     * 金币商城
     * @param type $type
     * @param type $typeId
     * @param type $buyNum
     */
    public static function BuyShop_Gold($type, $typeId, $buyNum) {
        $mo = GameConfig::shop_gold_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        $tempNum = 0;
        foreach (ctx()->privateState->goldShopReceived as $key) {
            if ($key == $typeId) {
                $tempNum += 1;
            }
        }

        if ($tempNum >= $mo->freeNum) {
            $ts = 0;
            switch ($typeId) {
                case 1:
                    $ts = ctx()->privateState->goldShop_CoolDownTs_1;
                    break;
                case 2:
                    $ts = ctx()->privateState->goldShop_CoolDownTs_2;
                    break;
                case 3:
                    $ts = ctx()->privateState->goldShop_CoolDownTs_3;
                    break;
            }

            //收费买
            if ($typeId == 1 && ctx()->privateState->goldShop_GuangGaoNum < $mo->guanggaoNum) {
                ctx()->privateState->goldShop_GuangGaoNum += 1;
                
                TaskProc::OnLookGuanggao();
            } else {
                my_Assert(ctx()->baseInfo->cash >= $mo->price, ErrCode::notenough_cash_msg);
                ctx()->baseInfo->Consume_Cash($mo->price);
            }

            if ($ts == 0) {
                switch ($typeId) {
                    case 1:
                        ctx()->privateState->goldShop_CoolDownTs_1 = now();
                        break;
                    case 2:
                        ctx()->privateState->goldShop_CoolDownTs_2 = now();
                        break;
                    case 3:
                        ctx()->privateState->goldShop_CoolDownTs_3 = now();
                        break;
                }

                //$user->privateState->goldShopReceived[] = $typeId;
            } else {
                my_Assert(now() - $ts >= $mo->downTs, ErrCode::user_shop_DownTsLimit);
                switch ($typeId) {
                    case 1:
                        ctx()->privateState->goldShop_CoolDownTs_1 = now();
                        break;
                    case 2:
                        ctx()->privateState->goldShop_CoolDownTs_2 = now();
                        break;
                    case 3:
                        ctx()->privateState->goldShop_CoolDownTs_3 = now();
                        break;
                }
            }

            if ($typeId == 1 && ctx()->privateState->goldShop_GuangGaoNum == $mo->guanggaoNum) {
                ctx()->privateState->goldShop_GuangGaoNum += 1;
                ctx()->privateState->goldShop_CoolDownTs_1 = 0;
            }
        }


        ctx()->privateState->goldShopReceived[] = $typeId;
        TaskProc::OnBuyGold();
        StoreProc::AddMultiItemInStore($mo->reward);
    }

    public static function BuyShop_MonthCard($type, $typeId, $buyNum) {
        $mo = GameConfig::shop_monthcard_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);

        //$user->privateState->monthCardShop_Received[] = $typeId;
        if ($typeId == 1) {
            my_Assert(ctx()->privateState->monthCardShop_ts == 0, ErrCode::err_const_no);
            ctx()->privateState->monthCardShop_ts = now();
            StoreProc::AddMultiItemInStore($mo->buy_reward);
            EmailProc::SendMonthCardRewardMail(req()->zoneid, req()->uid, $mo->daily_reward);
        } else {
            my_Assert(ctx()->privateState->honourCardShop_ts == 0, ErrCode::err_const_no);
            ctx()->privateState->honourCardShop_ts = now();
        }
    }

    /**
     * 商城每日重置
     */
    public static function ShopDailyClear() {
        ctx()->privateState->dailyShopReceived = array();
        ctx()->privateState->goldShopReceived = array();
        $num = count(ctx()->privateState->goldShopReceived);

        $arr = ctx()->privateState->goldShopReceived;
        for ($i = 0; $i < $num; $i++) {
            if ($arr[$i] == 1) {
                StlUtil::arrayRemoveAt($arr, $i);
            }
        }

        ctx()->privateState->goldShop_CoolDownTs_1 = 0;
        ctx()->privateState->goldShop_GuangGaoNum = 0;

        
        ctx()->privateState->dailyShop_GuangGaoNum = 0;
        ctx()->privateState->dailyShop_GuangGaoTs = 0;

        ctx()->privateState->guanggaoGetTiliNum = 0;
        ctx()->privateState->buyTiliNum = 0;

        ctx()->privateState->lastFreeGetTs_ShopBoxBright = 0;
        ctx()->privateState->lastFreeGetTs_ShopBoxCommon = 0;
        self::ShopMonthClear();
    }

    /**
     * 月卡按照时间记录的一个月后清理
     */
    public static function ShopMonthClear() {
        $monthTs = 30 * 24 * 60 * 60;

        if (ctx()->privateState->monthCardShop_ts > 0) {
            if (now() - ctx()->privateState->monthCardShop_ts >= $monthTs) {
                ctx()->privateState->monthCardShop_ts = 0;
            } else {
                $mo = GameConfig::shop_monthcard_getItem(1);
                EmailProc::SendMonthCardRewardMail(req()->zoneid, req()->uid, $mo->daily_reward);
            }
        }

        if (ctx()->privateState->honourCardShop_ts > 0 && now() - ctx()->privateState->honourCardShop_ts >= $monthTs) {
            ctx()->privateState->honourCardShop_ts = 0;
        }
    }

    /**
     * 每日商城随机6个道具
     * @return type
     */
    public static function DailyShopItemRand() {
        $dataDic = new \stdClass();

        $dic = GameConfig::shop_daily();
        if ($dic == null) {
            return;
        }

        foreach ($dic as $typeId => $mo) {
            $type = $mo->type;
            if (StlUtil::dictHasProperty($dataDic, $type)) {
                $list = $dataDic->$type;
                $list[] = $typeId;
                $dataDic->$type = $list;
            } else {
                $list = array();
                $list[] = $typeId;
                $dataDic->$type = $list;
            }
        }
        $result = array();
        $arrType = array(1,2,5,2,5,3);
        foreach ($arrType as $k) {
            if (StlUtil::dictHasProperty($dataDic, $k) && count($dataDic->$k) > 0) {
                $arr2 = $dataDic->$k;
                if ($k == 1) {
                    $result[] = $arr2[0];
                    continue;
                }

                $allNum = 0;
                foreach ($arr2 as $giftTypeId) {
                    $allNum += GameConfig::shop_daily_getItem($giftTypeId)->per;
                }

                $randNum = rand(1, $allNum);
                $start = 0;
                $end = 0;
                $index = 0;
                foreach ($arr2 as $itemId) {
                    $per = GameConfig::shop_daily_getItem($itemId)->per;
                    $end += $per;
                    if ($randNum >= $start && $randNum < $end) {
                        $result[] = $itemId;
                        break;
                    }
                    $start = $end;
                    $index += 1;
                }
                unset($arr2[$index]);

                $dataDic->$k = array_values($arr2);
            }
        }


        ctx()->privateState->dailyShopRandItems = $result;
        UserProc::updateUserInfo();
    }
}
