<?php

namespace loyalsoft;

/**
 * Description of PetProc
 *
 * @author Administrator
 */
class PetProc {

    public static function InitArea() {
        $isUnlock = FightProc::isFunUnlock(Enum_FunType::Pet);
        if (!$isUnlock) {
            return;
        }
        $dic = GameConfig::pet_area();
        if ($dic != null && count(StlUtil::dictToArray(ctx()->store->exploreArea, 1)) == 0) {
            foreach ($dic as $areaId => $arr) {
                $ins_exploreArea = new Ins_ExploreArea();
                $ins_exploreArea->areaId = $areaId;
                ctx()->store()->exploreArea->$areaId = $ins_exploreArea;
            }
        }
    }

    /**
     * 6433 区域等级升级
     * @return Resp
     */
    public static function Pet_AreaLevelUpgrade() {
        list($areaId) = req()->paras;

        if (!StlUtil::dictHasProperty(ctx()->store->exploreArea, $areaId)) {
            $ins_ExploreArea = new Ins_ExploreArea();
            $ins_ExploreArea->areaId = $areaId;
            ctx()->store->exploreArea->$areaId = $ins_ExploreArea;
        }
        $vo = new Ins_ExploreArea(ctx()->store->exploreArea->$areaId);
        $vo->level += 1;

        my_Assert(ctx()->baseInfo->gold >= $vo->mo()->unlockArea_gold, ErrCode::notenough_gold_msg);
        my_Assert(ctx()->baseInfo->level >= $vo->mo()->unlockArea_userLv, ErrCode::user_levelnotenough_msg);

        ctx()->base()->Consume_Gold($vo->mo()->unlockArea_gold);
        ctx()->store->exploreArea->$areaId = $vo;
        UserProc::updateUserInfo();

        return Resp::ok(array(
                    'gold' => ctx()->baseInfo->gold,
                    'store' => ctx()->store,
        ));
    }

    /**
     * 
     * @param Ins_ExploreArea $ins_ExploreArea
     */
    public static function GetAreaOutPutInfo(&$ins_ExploreArea, $outPut_type) {
        $num = $ins_ExploreArea->mo()->output_num; //等级带来的
        $ts = $ins_ExploreArea->mo()->output_time; //等级带来的

        if (count($ins_ExploreArea->explorePetList) > 0) {
            $list = $ins_ExploreArea->explorePetList;
            foreach ($list as $petUid) {
                $ins_pet = new Ins_Pet(ctx()->store->$petUid);
                $typeId = $ins_pet->mo()->type;
                $qual = $ins_pet->qual;
                
                $pet_exploreoutputMo = GameConfig::pet_exploreoutput_getItem($typeId, $qual);
                if ($pet_exploreoutputMo->exploreOutput != null) {
                    $sList = explode(';', $pet_exploreoutputMo->exploreOutput);
                    foreach ($sList as $str) {
                        $s = explode(',', $str);
                        if ((int) $s[0] == $ins_ExploreArea->areaId) {
                            $num += (int) $s[1];
                            $ts += (int) $s[2];
                        }
                    }
                }
            }
        }

        return ($outPut_type == 1) ? $num : $ts;
    }

    /**
     * 6434 更换派遣宠物
     * @return Resp
     */
    public static function ReplaceDispatchPet_old() {
        list($areaId, $petUid, $exploreType) = req()->paras;

        $vo = new Ins_ExploreArea(ctx()->store->exploreArea->$areaId);

        if (StlUtil::arrayContains($vo->explorePetList, $petUid)) {
            StlUtil::arrayRemove($vo->explorePetList, $petUid);
        } else {
            my_Assert(count($vo->explorePetList) < $vo->mo()->dispatchPetNum, ErrCode::user_store_dispatchPetNumLimit);
            $petDic = ctx()->store->pet;
            my_Assert(StlUtil::dictHasProperty($petDic, $petUid), ErrCode::user_store_NotExistPet);

            $ins_pet = new Ins_Pet($petDic->$petUid);
            $mo = GameConfig::pet_exploreoutput_getItem($ins_pet->mo()->type, $ins_pet->qual);
            my_Assert(null != $mo, ErrCode::err_const_no);

            $sList = explode(';', $mo->exploreOutput);
            $tag = false;
            foreach ($sList as $key => $value) {
                $str = explode(',', $value);
                if ($str[0] == $areaId) {
                    $tag = true;
                    break;
                }
            }
            my_Assert($tag, ErrCode::user_store_dispatchPetErr);
            $vo->explorePetList[] = $petUid;
        }

        my_Assert($exploreType == 1 && now() - $vo->startTs < 10 * 60, ErrCode::user_store_CannotReplaced_tsLimit);

        //开始探索
        if (count($vo->explorePetList) == 0) {
            $vo->startTs = 0;
            $vo->endTs = 0;
            $vo->output_count = 0;
            $vo->output_ts = 0;
        } else {
            if ($vo->startTs == 0) {
                $vo->startTs = now();
            }

            $ts = self::GetAreaOutPutInfo($vo, 2);
            $num = self::GetAreaOutPutInfo($vo, 1);
            $vo->endTs = $vo->startTs + $ts;
            $vo->output_count = $num;
            $vo->output_ts = $vo->endTs - $vo->startTs;
        }

        ctx()->store->exploreArea->$areaId = $vo;

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'store' => ctx()->store,
        ));
    }

    /**
     * 6434 更换派遣宠物  废弃
     * @return Resp
     */
    public static function ReplaceDispatchPet() {
        list($areaId, $petUid) = req()->paras;

        $vo = new Ins_ExploreArea(ctx()->store->exploreArea->$areaId);

        if (StlUtil::arrayContains($vo->explorePetList, $petUid)) {
            StlUtil::arrayRemove($vo->explorePetList, $petUid);
        } else {
            my_Assert(count($vo->explorePetList) < $vo->mo()->dispatchPetNum, ErrCode::user_store_dispatchPetNumLimit);
            $petDic = ctx()->store->pet;
            my_Assert(StlUtil::dictHasProperty($petDic, $petUid), ErrCode::user_store_NotExistPet);

            $ins_pet = new Ins_Pet($petDic->$petUid);
            $mo = GameConfig::pet_exploreoutput_getItem($ins_pet->mo()->type, $ins_pet->qual);
            my_Assert(null != $mo, ErrCode::err_const_no);

            $sList = explode(';', $mo->exploreOutput);
            $tag = false;
            foreach ($sList as $key => $value) {
                $str = explode(',', $value);
                if ($str[0] == $areaId) {
                    $tag = true;
                    break;
                }
            }
            my_Assert($tag, ErrCode::user_store_dispatchPetErr);
            $vo->explorePetList[] = $petUid;
        }

        my_Assert($exploreType == 1 && now() - $vo->startTs < 10 * 60, ErrCode::user_store_CannotReplaced_tsLimit);

        ctx()->store->exploreArea->$areaId = $vo;

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'store' => ctx()->store,
        ));
    }

    /**
     * 6437 点击派遣宠物
     * @return Resp
     */
    public static function DispatchPet() {
        list($areaId, $petUidList) = req()->paras;

        $ins_ExploreArea = new Ins_ExploreArea(ctx()->store->exploreArea->$areaId);
        $ins_ExploreArea->explorePetList = $petUidList;

        if ($ins_ExploreArea->startTs > 0 && $ins_ExploreArea->endTs > 0) {//说明是更换
            //my_Assert(count($ins_ExploreArea->explorePetList) > 0, ErrCode::user_store_addDispatchPet);                  
            my_Assert(now() - $ins_ExploreArea->startTs < 10 * 60, ErrCode::user_store_CannotReplaced_tsLimit);
        }

        if (count($petUidList) == 0) {
            $ins_ExploreArea->startTs = 0;
            $ins_ExploreArea->endTs = 0;
            $ins_ExploreArea->output_count = 0;
            $ins_ExploreArea->output_ts = 0;
        } else {
            if ($ins_ExploreArea->startTs == 0) {
                $ins_ExploreArea->startTs = now();
            }

            $ts = self::GetAreaOutPutInfo($ins_ExploreArea, 2);
            $num = self::GetAreaOutPutInfo($ins_ExploreArea, 1);
            $ins_ExploreArea->endTs = $ins_ExploreArea->startTs + $ts;
            $ins_ExploreArea->output_count = $num;
            $ins_ExploreArea->output_ts = $ins_ExploreArea->endTs - $ins_ExploreArea->startTs;
        }
   
        ctx()->store->exploreArea->$areaId = $ins_ExploreArea;

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'store' => ctx()->store,
        ));
    }

    /**
     * 6438 某区域的派遣宠物信息
     * @return Resp
     */
    public static function ResetDispatchPet() {
        list($areaId) = req()->paras;
        
        $ins_ExploreArea = new Ins_ExploreArea(ctx()->store->exploreArea->$areaId);
        
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'explorePetList' =>$ins_ExploreArea->explorePetList,
        ));
    }
    
    /**
     * 6435 删除全部派遣宠物
     */
    public static function RemoveDispatchPet() {
        list($areaId) = req()->paras;

        $vo = new Ins_ExploreArea(ctx()->store->exploreArea->$areaId);
        $vo->explorePetList = array();
        $vo->startTs = 0;
        $vo->endTs = 0;
        $vo->output_count = 0;
        $vo->output_ts = 0;

        ctx()->store->exploreArea->$areaId = $vo;

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'store' => ctx()->store,
        ));
    }

    /**
     * 6436 领取探索奖励
     */
    public static function Pet_ReceiveReward() {
        list($areaId) = req()->paras;

        $vo = new Ins_ExploreArea(ctx()->store->exploreArea->$areaId);

        my_Assert($vo->startTs > 0 && now() - $vo->startTs > 10 * 60, ErrCode::user_store_CannotReceiveReward);

        $tt = now() - $vo->startTs;
        if (now() >= $vo->endTs) {
            $tt = $vo->endTs - $vo->startTs;
        }

        $ts_n = $vo->output_ts / $vo->output_count;
        $num = intval($tt *= $ts_n);
        $goodsStr = $vo->mo()->reward_fixed . ',' . $num;
        StoreProc::AddMultiItemInStore($goodsStr);

        $reward = explode(',', $vo->mo()->reward_extra);
        $randNum = rand(1, $reward[2]);
        if ($randNum <= $reward[2]) {
            StoreProc::AddMultiItemInStore($reward[0] . ',' . $reward[1]);
        }

        //$vo->explorePetList = array();             
        $vo->startTs = 0;
        $vo->endTs = 0;
        $vo->output_count = 0;
        $vo->output_ts = 0;

        ctx()->store->exploreArea->$areaId = $vo;

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'gold' => ctx()->baseInfo->gold,
                    'cash' => ctx()->baseInfo->cash,
                    'reward' => StoreProc::$reward,
                    'reward_Gem' => StoreProc::$reward_Gem,
                    'reward_equip' => StoreProc::$reward_equip,
                    'store' => ctx()->store,
        ));
    }
    
    /**
     * 6432 解锁孵化位置
     * @return Resp
     */
    public static function Pet_UnlockBreedLocation() {
        list($slotId) = req()->paras;
        
        $list = explode(';',GameConfig::glc2()->Pet_BreedLocationUnlockInfo);
        foreach ($list as $value) {
            $s = explode(',', $value);
            if($s[0] == $slotId){
                my_Assert(ctx()->baseInfo->cash >= $s[1], ErrCode::notenough_cash_msg);
                ctx()->base()->Consume_Cash($s[1]);
                $breed = new Ins_BreedEggSlot(ctx()->store->breedEggLocation->$slotId);
                $breed->unlock = 1;
                ctx()->store()->breedEggLocation->$slotId = $breed;
                break;
            }               
        }
        
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'cash' => ctx()->baseInfo->cash,
                    'store' => ctx()->store,                   
        ));
    }
    
    /**
     * 6431 正常孵化完成点击领取
     * @return Resp
     */
    public static function Pet_ReceiveBreedPet() {
        list($slotId) = req()->paras;
        
        $ins_breed = new Ins_BreedEggSlot(ctx()->store()->breedEggLocation->$slotId);    
        my_Assert($ins_breed->eggId != 0, ErrCode::user_store_NoExistBreedEgg);
        my_Assert($ins_breed->unlock == 1, ErrCode::user_store_SlotNotUnlocked);                  
        my_Assert(now() >= $ins_breed->endTs, ErrCode::user_store_NoCompleteBreed);
                    
        StoreProc::AddMultiItemInStore($ins_breed->mo()->reward);
        
        //随机一个灵宠      
        $str = explode(';', $ins_breed->mo()->per);
        $newPet = self::RandObtainPet($str);
                     
        $uid = 0;
        if($newPet != null){           
            $uid = $newPet->uid;
            ctx()->store()->pet->$uid = $newPet;
        }
        
        $ins_breed->eggId = 0;
        $ins_breed->endTs = 0;
        ctx()->store()->breedEggLocation->$slotId = $ins_breed;
        
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'newPetUid'=>$uid,
                    'cash' => ctx()->baseInfo->cash,
                    'store' => ctx()->store,                   
        ));
    }
    
    /**
     * 6430 加速孵化宠物
     * @return Resp
     */
    public static function Pet_FastBreedEgg() {
        list($slotId) = req()->paras;
        $ins_breed = new Ins_BreedEggSlot(ctx()->store()->breedEggLocation->$slotId);    
        my_Assert($ins_breed->eggId != 0, ErrCode::user_store_NoExistBreedEgg);
        my_Assert($ins_breed->unlock == 1, ErrCode::user_store_SlotNotUnlocked);                  
        my_Assert(now() < $ins_breed->endTs, ErrCode::user_store_CompleteBreed);
        
        $num = $ins_breed->mo()->breedTs/60/10;
        $cash_one = $ins_breed->mo()->fastBreed_cost/$num;
        
        $ts_shengyu = $ins_breed->endTs - now();
        $cost_Ts = $ins_breed->mo()->breedTs - $ts_shengyu;
        $costNum = intval($cost_Ts/60/10);
        
        $costCash = $ins_breed->mo()->fastBreed_cost - $costNum*$cash_one;
        
        my_Assert(ctx()->baseInfo->cash >= $costCash, ErrCode::notenough_cash_msg);
        ctx()->base()->Consume_Cash($costCash);
        
        $ins_breed->endTs = 0;
        ctx()->store()->breedEggLocation->$slotId = $ins_breed;
        //StoreProc::AddMultiItemInStore($ins_breed->mo()->reward);
        
        //随机一个灵宠      
        //$str = explode(';', $ins_breed->mo()->per);
        //$newPet = self::RandObtainPet($str);
                     
//        $uid = 0;
//        if($newPet != null){           
//            $uid = $newPet->uid;
//            ctx()->store()->pet->$uid = $newPet;
//        }
        
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'cash' => ctx()->baseInfo->cash,
                    'store' => ctx()->store,
                    //'newPetUid'=>$uid,
        ));
    }
    
    /**
     * 初始化一个宠物
     * @param type $rarity
     * @param type $qual
     * @param type $petType
     */
    public static function RandObtainPet($str) {
        //随机一个灵宠
        $randNum = rand(1,10000);             
        $start = 0;
        $end = 0;
        
        $rarity = 0;
        $qual = 0;
        $petType = 0;
        foreach ($str as $key => $value) {
            $s = explode(',', $value);
            $end += $s[3]*100; 
            if($randNum > $start && $randNum <= $end){
                $rarity = $s[0]; 
                $qual = $s[1];
                $petType = $s[2];
                break;
            }
            $start = $end;
        }
        
        $dic = GameConfig::pet();
        $petArr = array();
        foreach ($dic as $typeId => $mo) {
            if($mo->petType == $petType && $mo->qual == $qual && $mo->rarity == $rarity && $mo->type != 902){
                $petArr[] = $typeId;
            }
        }
        $typeId = 0;
        if(count($petArr) > 0){
            $num = rand(0,count($petArr)-1);
            $typeId = $petArr[$num];                                
        }
        if($typeId != 0){
            return StoreProc::initPet($typeId);
        }
                
        return null;
    }
    
    /**
     * 6429 孵化宠物
     * @return Resp
     */
    public static function Pet_BreedEgg() {
        list($slotId,$eggId) = req()->paras;
        
        my_Assert(StlUtil::dictHasProperty(ctx()->store->items, $eggId), ErrCode::user_store_NoItem);
        $mo = GameConfig::item_getItem($eggId);
        my_Assert($mo != null, ErrCode::err_const_no);
        my_Assert($mo->itemType == 105, ErrCode::user_store_NoItem);             
        
        $ins_breed = new Ins_BreedEggSlot(ctx()->store()->breedEggLocation->$slotId);       
        my_Assert($ins_breed->eggId == 0 && $ins_breed->unlock == 1, ErrCode::user_store_NoBreedSlot);
        ctx()->store()->removeItem($eggId, 1);
        
        //$ins_breed = ctx()->store()->breedEggLocation->$slot;
 
        $ins_breed->eggId = $eggId;       
        $ts = $ins_breed->mo()->breedTs;
        $ins_breed->endTs = now()+$ts;  
        ctx()->store()->breedEggLocation->$slotId = $ins_breed;
               
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    //'gold' => ctx()->baseInfo->gold,
                    'store' => ctx()->store,
        ));
    }
    
    
    /**
     * 6428 宠物降品   
     */
    public static function Pet_DownGradingQual() {
        list($uid) = req()->paras;

        $petDic = ctx()->store->pet;
        my_Assert(StlUtil::dictHasProperty($petDic, $uid), ErrCode::user_store_NotExistPet);
        $ins_pet = new Ins_Pet($petDic->$uid);

        $NotDownGradingList = explode(',', glc()->equipQual_NotDownGrading);
        my_Assert(!in_array($ins_pet->qual, $NotDownGradingList), ErrCode::user_store_equip_minQual);

        $itemNum = 0;
        for ($i = 1; $i < $ins_pet->level; $i++) {
            $mo = GameConfig::pet_levelupgrade_getItem($ins_pet->mo()->rarity, $ins_pet->qual, $i);       
            $itemNum += $mo->costItem;
        }       
        StoreProc::AddMultiItemInStore(6 . ',' . $itemNum);
               
        self::ComposeMaterial_Fallback_Pet($ins_pet->mo()->petType, $ins_pet->qual);

        $ins_pet->level = 1;
        $sList2 = explode(';', glc()->equipQualUnlockSkillId);
        foreach ($sList2 as $str3) {
            $s = explode(',', $str3);
            if (in_array($ins_pet->qual, $s)) {
                $ins_pet->qual = $s[0];
                break;
            }
        }

        ctx()->store->pet->$uid = $ins_pet;
        //FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    //'gold' => ctx()->baseInfo->gold,
                    'store' => ctx()->store,
        ));
    }

    private static function ComposeMaterial_Fallback_Pet($type, $qual) {
        $sList2 = explode(';', glc()->equipQualUnlockSkillId);
        foreach ($sList2 as $str) {
            $s = explode(',', $str);
            if (in_array($qual, $s)) {
                $max = $qual;
                $min = $s[0];
                $num = 0;
                for ($i = $min; $i < $max; $i++) {
                    $mo = GameConfig::pet_compose_getItem($i);
                    $num += explode(',', $mo->compose_condition)[2];
                }

                $typeId = StoreProc::ComposeMaterial($type, $min,902);
                StoreProc::AddMultiItemInStore($typeId . ',' . $num);
                break;
            }
        }
    }
    
    
    /*
     * 6427 宠物降级
     */

    public static function Pet_DownGradingLevel() {
        list($uid) = req()->paras; //宠物uid

        $petDic = ctx()->store->pet;
        my_Assert(StlUtil::dictHasProperty($petDic, $uid), ErrCode::user_store_NotExistPet);
        $ins_pet = new Ins_Pet($petDic->$uid);

        my_Assert($ins_pet->level > 1, ErrCode::user_store_equip_minlevel);      
     
        $itemNum = 0;
        for ($i = 1; $i < $ins_pet->level; $i++) {
            $mo = GameConfig::pet_levelupgrade_getItem($ins_pet->mo()->rarity, $ins_pet->qual, $i);       
            $itemNum += $mo->costItem;
        }       
        StoreProc::AddMultiItemInStore(6 . ',' . $itemNum);

       
        
        $ins_pet->level = 1;
        ctx()->store->pet->$uid = $ins_pet;

        //FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    //'gold' => ctx()->baseInfo->gold,
                    'store' => ctx()->store,
        ));
    }
    
    /**
     * 6426 宠物一键合成    
     * @return type
     */
    public static function Pet_OnekeyCompose() {
        $store = ctx()->store(true);
        $petDic = $store->pet;
        $arr = array();                                                         # 待处理装备临时数组
        $composePet = array();                                                  # 合成记录
        foreach ($petDic as $pet) {
            if ($pet->qual < 4) {                                               # 4 品以上的不参与合成
                $arr[] = new Ins_Pet($pet);
            }
        }      

        while (count($arr) > 0) {                                               # 循环处理每一个装备
            $pet_compose = array_pop($arr);                                     # 从末尾弹出一个元素做合成运算
            if (null == $pet_compose) {                                         # 防御空对象
                break;
            }
            $eqQualCfg = GameConfig::pet_compose_getItem($pet_compose->qual); # 按照品阶查询合成配置数据
            my_Assert(null != $eqQualCfg, ErrCode::err_const_no);               # 防御配置数据错误
            list($type, $costQual, $num) = explode(',', $eqQualCfg->compose_condition); # 从配置提取合成条件
            $composeArr = array();                                              # 消耗集合
            foreach ($arr as $v) {                                              # 从剩余元素中查找合成所需
                if ($v->qual == $costQual) {                                    # 同品阶
                    if (($type == 1) ? $v->typeId == $pet_compose->typeId #     # type: 1 与本体相同 2 同部位即可
                                    : $v->mo()->petType == $pet_compose->mo()->petType) {
                        $composeArr[] = $v;                                     # 成为消耗品
                    }
                    if (count($composeArr) >= $num) {                           # 达到消耗数量就停
                        break;
                    }
                }
            }
            if (count($composeArr) >= $num) {                                   # 合成的回收 不合成的可不能给回收了
                $uid = $pet_compose->uid;
                $newPet = self::ComposeNewPet($pet_compose);                    # 原地合成
                self::petLevelChange_Material_Recovery($newPet->mo()->rarity, $newPet->qual, $newPet->level); # 回收材料
                $newPet->level = 1;
                $petDic->$uid = $newPet;
                               
                $composePet[] = $uid;                                           # 添加合成记录
                foreach ($composeArr as $val) {                                 # 回收消耗的装备中所含材料
                    self::petLevelChange_Material_Recovery($val->mo()->rarity, $val->qual, $val->level); # 回收材料
                    StlUtil::dictRemove(ctx()->store(true)->pet, $val->uid);    # 背包删除
                    if(ctx()->store(true)->petUid_fight_position == $val->uid){
                        ctx()->store(true)->petUid_fight_position = 0;
                    }                 
                    foreach (ctx()->store(true)->petUids_supportFight_position as $k => $pUid) {
                        if($pUid == $val->uid){
                            StlUtil::dictRemove(ctx()->store(true)->petUids_supportFight_position, $k);                            
                            break;
                        }
                    }
                                      
                    StlUtil::arrayRemove($arr, $val); # 从arr中删掉已消耗装备
                }
            }
        }

       
        UserProc::updateUserInfo();                                             # 回存玩家数据
        return Resp::ok(array(#                                                 # 返回给客户端的数据                   
                    'store' => ctx()->store,
                    'composeEquip' => $composePet,
                    'task' => ctx()->task,
        ));
    }
    
    /**
     * 6425 宠物合成    
     * @return Resp
     */
    public static function Pet_Compose() {
        list($uid, $petUids_cost, $composeMaterial_typeIds) = req()->paras;
       
        $petDic = ctx()->store->pet;
        my_Assert(StlUtil::dictHasProperty($petDic, $uid), ErrCode::user_store_NotExistPet);
        $ins_pet = new Ins_Pet($petDic->$uid);

        my_Assert($ins_pet->qual < 11, ErrCode::user_store_equip_qualMaxLimit);
        $composeMo = GameConfig::pet_compose_getItem($ins_pet->qual);
        my_Assert(null != $composeMo, ErrCode::err_const_no);
        $arr = explode(',', $composeMo->compose_condition);

        $type = $arr[0];
        $costQual = $arr[1];
        $num = $arr[2];

        $all_num = 0;

        $materialItemType = 0;
        $materialItemNum = 0;
        if ($type == 2 && $composeMaterial_typeIds != null) {//部位去校验
            $str = explode(',', $composeMaterial_typeIds);
            $materialItemType = $str[0];
            $materialItemNum = count($str);
            foreach ($str as $s) {
                $composeMaterial_itemTypeId = StoreProc::ComposeMaterial($ins_pet->mo()->petType, $costQual,902);
                my_Assert($composeMaterial_itemTypeId == $s, ErrCode::user_store_equip_composeMaterialErr);
                $all_num += 1;
            }
        }

        if ($petUids_cost != null) {
            $costPetsArr = explode(',', $petUids_cost);
            $all_num += count($costPetsArr);

            foreach ($costPetsArr as $pUid) {
                my_Assert(StlUtil::dictHasProperty($petDic, $pUid), ErrCode::user_store_NotExistPet);

                my_Assert(ctx()->store(true)->petUid_fight_position != $pUid, ErrCode::user_store_NotSelectPet_fight);
                $supportDic = ctx()->store(true)->petUids_supportFight_position;
                if(StlUtil::dictHasProperty($supportDic, 1)){
                    $k1 = 1;
                    my_Assert($pUid != $supportDic->$k1, ErrCode::user_store_NotSelectPet_supportfight);
                }
                if(StlUtil::dictHasProperty($supportDic, 2)){
                    $k2 = 2;
                    my_Assert($pUid != $supportDic->$k2, ErrCode::user_store_NotSelectPet_supportfight);
                }
                
                
                $costIns_pet = new Ins_Pet($petDic->$pUid);
                my_Assert($costIns_pet->qual == $costQual, ErrCode::user_store_equipMaterialQualErr);
                if ($type == 1) {//本体                
                    my_Assert($costIns_pet->typeId == $ins_pet->typeId, ErrCode::user_store_equipMaterialTypeErr);
                } else {
                    my_Assert($costIns_pet->mo()->petType == $ins_pet->mo()->petType, ErrCode::user_store_equipMaterialTypeErr);
                }
            }
        }
        my_Assert($all_num == $num, ErrCode::user_store_equipMaterialNumErr);
        ctx()->store(true)->pet->$uid = self::ComposeNewPet($ins_pet);

        //消耗的装备 回收         
        if ($petUids_cost != null) {
            $costPetsArr = explode(',', $petUids_cost);
            foreach ($costPetsArr as $pUid) {
                $costIns_Equip = new Ins_Pet($petDic->$pUid);
                self::petLevelChange_Material_Recovery($costIns_Equip->mo()->rarity, $costIns_Equip->qual, $costIns_Equip->level);
                StlUtil::dictRemove(ctx()->store(true)->pet, $pUid);              
            }
        }

        if ($materialItemType != 0) {
            ctx()->store(true)->removeItem($materialItemType, $materialItemNum);
        }
        
        UserProc::updateUserInfo();
        return Resp::ok(array(                 
                    'store' => ctx()->store,
                    'task' => ctx()->task,
        ));
    }
    
    /**
     * 装备等级提升 消耗的金币图纸回收
     * @param type $rarity
     * @param type $qual
     * @param type $posId
     * @param type $level
     */
    public static function petLevelChange_Material_Recovery($rarity, $qual,$level) {    
        $num = 0;
       
        for ($i = 1; $i <= $level - 1; $i++) {
            $mo = GameConfig::pet_levelupgrade_getItem($rarity, $qual, $i);         
            $num += $mo->costItem;
        }
        if ($num > 0) {        
            StoreProc::AddMultiItemInStore(6 . ',' . $num);
        }
    }
    
    /**
     * 合成一个新的装备
     * @param type Ins_Equip
     */
    public static function ComposeNewPet(&$ins_pet) {
        $newPetTypeId = $ins_pet->typeId;
        $qual = $ins_pet->qual + 1;
        if ($ins_pet->qual < 4) {
            //$newEquipTypeId = substr($ins_pet->typeId, 0, strlen($ins_equip->typeId) - 5);
            $rarity = substr($ins_pet->typeId, -2);
            $mo = GameConfig::pet_getItem($ins_pet->typeId);
            $newPetTypeId = $mo->type. "0" . $qual . $rarity;
        }

        $ins_pet->typeId = $newPetTypeId;
        $ins_pet->qual = $qual;

        return $ins_pet;
    }
    
    
    /**
     *6424 灵宠等级提升   
     * @return Resp
     */
    public static function Pet_LevelUpgrade(){
        list($uid) = req()->paras; //装备uid

        my_Assert(StlUtil::dictHasProperty(ctx()->store->pet, $uid), ErrCode::user_store_NotExistPet);
        $ins_pet = new Ins_Pet(ctx()->store->pet->$uid);
        my_Assert($ins_pet->level < $ins_pet->Pet_MaxLevel(), ErrCode::user_store_LevelLimit);

        $mo = $ins_pet->petLevelMo();

        $costTuzhiId = 6;
        $tuzhiNum = 0;
        if (StlUtil::dictHasProperty(ctx()->store->items, $costTuzhiId)) {
            $tuzhiNum = ctx()->store->items->$costTuzhiId;
        }
        my_Assert($tuzhiNum >= $mo->costItem, ErrCode::notenough_item);

        ctx()->store(true)->removeItem($costTuzhiId,$mo->costItem);
     
        $ins_pet->level += 1;
        ctx()->store->pet->$uid = $ins_pet;
     
        UserProc::updateUserInfo();
        return Resp::ok(array(                   
                    'store' => ctx()->store,              
        ));
    }
    
    /**
     * 6423 展示空闲灵宠
     * @return Resp
     */
    public static function Pet_SelectShowPet(){
        list($uids) = req()->paras;
        
        $petDic = ctx()->store->pet;
        
        if(count($uids) == 0){
            ctx()->store()->mainShowPetList = $uids;
        } else {
            my_Assert(count($uids)<= GameConfig::glc2()->Pet_MainShowPetList , ErrCode::user_store_ShowPetNumLimit);
            
            foreach ($uids as $uid) {
                my_Assert(StlUtil::dictHasProperty($petDic, $uid), ErrCode::user_store_NotExistPet);
                my_Assert(ctx()->store->petUid_fight_position != $uid, ErrCode::user_store_NotSelectPet_fight);
                $tag = false;
                foreach (ctx()->store->petUids_supportFight_position as $k => $val) {
                    if($val == $uid){
                        $tag = true;
                        break;
                    }
                }
                
                my_Assert($tag == false, ErrCode::user_store_NotSelectPet_supportfight);              
            }
            
            ctx()->store()->mainShowPetList = $uids;
        }
             
        UserProc::updateUserInfo();
        return Resp::ok(array(                   
                    'store' => ctx()->store,              
        ));
    }

    /**
     * 6422 出战/替换/上阵
     */
    public static function Pet_ChangeFightPosition() {
        list($type, $uid, $index) = req()->paras;

        $dic = ctx()->store(true)->petUids_supportFight_position;
        if ($type == 1) {//出战、替换
            ctx()->store(true)->petUid_fight_position = $uid;
            
            foreach ($dic as $k => $petUid) {
                if($petUid == $uid){
                    ctx()->store(true)->petUids_supportFight_position->$k = 0;
                }
            }
        } else if ($type == 2) {
            $tag = false;
            foreach ($dic as $k => $petUid) {
                if($petUid == $uid && $k != $index){
                    $tag = true;
                    break;
                }
            }
            my_Assert(!$tag, ErrCode::user_store_NoReplacePet);
            
            ctx()->store(true)->petUids_supportFight_position->$index = $uid;
            if(ctx()->store(true)->petUid_fight_position == $uid){
                ctx()->store(true)->petUid_fight_position = 0;
            }           
        }

//        if ($type == 0 && $index == 0 && $uid != 0) {//上阵点击空白的地方
//            if (ctx()->store(true)->petUid_fight_position == 0) {
//                ctx()->store(true)->petUid_fight_position = $uid;
//            } else {
//                $list = StlUtil::dictToArray(ctx()->store(true)->petUids_supportFight_position);    
//                if (count($list) === 0) {
//                    $key = 1;
//                    ctx()->store(true)->petUids_supportFight_position->$key = $uid;
//                } else {
//                    for ($i = 1; $i <= 2; $i++) {
//                        if (!StlUtil::dictHasProperty(ctx()->store(true)->petUids_supportFight_position, $i) || ctx()->store(true)->petUids_supportFight_position->$i == 0) {
//                            ctx()->store(true)->petUids_supportFight_position->$i = $uid;
//                            break;
//                        }
//                    }
//                }
//            }
//        }

        UserProc::updateUserInfo();
        return Resp::ok(array());
    }
    

    /**
     * 6421 移除宠物上的新的标志
     */
    public static function Pet_RemoveNewTip() {
        list($uid) = req()->paras;

        $petDic = ctx()->store->pet;
        my_Assert(StlUtil::dictHasProperty($petDic, $uid), ErrCode::user_store_NotExistPet);

        ctx()->store->pet->$uid->isNew = 0;

        UserProc::updateUserInfo();
        return Resp::ok(array());
    }
    
    /**
     * 6439 宠物休息
     */
    public static function RemoveFightPosition() {
        list($uid) = req()->paras;

        if (ctx()->store->petUid_fight_position == $uid)
        {
            ctx()->store()->petUid_fight_position = 0;
        }

        for ($i = 1; $i <= 2; $i++) {
            if (StlUtil::dictHasProperty(ctx()->store->petUids_supportFight_position, $i) && ctx()->store->petUids_supportFight_position->$i == $uid)
            {
                ctx()->store()->petUids_supportFight_position->$i = 0;
                break;
            }
        }

        UserProc::updateUserInfo();
        return Resp::ok(array());
    }
    
}
