<?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 StoreProc
 *
 * @author c'y'zhao
 */
class StoreProc {

    /**
     * 逻辑分发
     * 所有的Proc中必须有这样一个方法
     * @param Req $req
     */
    public static function procMain($req) {
        switch ($req->cmd) {
            case CmdCode::cmd_store_put:                                        # 6401 放入仓库
                return StoreProc::AddItemInStore();
            case CmdCode::cmd_store_gemSet:                                     # 6402 装备宝石
                return StoreProc::gemSet();
            case CmdCode::cmd_store_gemRemove:                                  # 6403 卸下装备宝石
                return StoreProc::gemRemove();
            case CmdCode::cmd_store_equipUpgrade:                               # 6404 装备升级
                return StoreProc::equipUpgrade();
            case CmdCode::cmd_store_equipUpgrade_MaxLv:                         # 6405 一键升级(装备)
                return StoreProc::equipUpgrade_MaxLv();
            case CmdCode::store_gemCompose:                                     # 6406 宝石合成
                return StoreProc::gemCompose();
            case CmdCode::store_switchEquipPag:                                 # 6408 切换装备分页
                return StoreProc::switchEquipPag();
            case CmdCode::store_gemLockState:                                   # 6409 宝石开锁解锁
                return StoreProc::gemLockState();
            case CmdCode::store_equip_removeEquipUpgradeTip:                    # 6410 移除装备可以升级绿点提示
                return StoreProc::removeEquipUpgradeTip();
            case CmdCode::store_equip_removeNewGemTip:                          # 6411 移除是新宝石绿点提示
                return StoreProc::removeNewGemTip();
            case CmdCode::store_allEquipUpgrade:                                # 6412 所有装备的一键升级
                return StoreProc::allEquipUpgrade();
            case CmdCode::store_gemXiLian:                                      # 6413 洗练
                return StoreProc::gemXiLian();
            default:
                Err(ErrCode::cmd_err);
        }
    }

    /**
     * 洗练
     * @return Resp
     */
    public static function gemXiLian() {
        list($uid) = req()->paras;

        //把 qual pos u一样的宝石开锁关锁
        $gemStore = ctx()->store->gemStore;
        my_Assert(StlUtil::dictHasProperty($gemStore, $uid), ErrCode::user_store_NotExistGem);

        $gem = new Ins_Gem($gemStore->$uid);
        my_Assert($gem->mo()->qual >= 5, ErrCode::user_store_GemNotXilian);

        $arr = explode(';', glc()->XILianGemlNeedXILianStone);
        $needStone = 0;
        foreach ($arr as $val) {
            $str = explode(':', $val);
            if ($str[0] == $gem->mo()->qual) {
                $needStone = $str[1];
                break;
            }
        }

        my_Assert(ctx()->baseInfo->xilianStone >= $needStone, ErrCode::user_store_XilianStoneNoEnough);

        $dic = GameConfig::predicate();
        $pList = array();
        foreach ($dic as $id => $pMo) {
            if ($pMo->positions != null && $pMo->qual != null) {
                $list = explode(',', $pMo->positions);
                if ($pMo->id != $gem->predicateId && $pMo->qual == $gem->mo()->qual && in_array($gem->mo()->position, $list)) {
                    $pList[] = $pMo->id;
                }
            }
        }
        my_Assert(count($pList) > 0, ErrCode::user_store_NoFindPredicate);
        $rand = random_int(0, count($pList) - 1);
        $pId = $pList[$rand];
        ctx()->store->gemStore->$uid->predicateId = $pId;

        ctx()->baseInfo->xilianStone -= $needStone;

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'xilianStone' => ctx()->baseInfo->xilianStone,
                    'predicateId' => $pId,
        ));
    }

    /**
     * 宝石开锁解锁
     * @return Resp
     */
    public static function gemLockState() {
        list($uid) = req()->paras;
        $user = ctx();

        //把 qual pos u一样的宝石开锁关锁
        $gemStore = $user->store->gemStore;
        $gem = $gemStore->$uid;
        $tag = 0;
        if ($gem->isUnlock == 0) {
            $gem->isUnlock = 1;
            $tag = 1;
        } else {
            $gem->isUnlock = 0;
            $tag = 0;
        }
        $gemStore->$uid = $gem;
        foreach ($gemStore as $k => $ins_gem) {
            if ($ins_gem->typeId == $gem->typeId && $ins_gem->predicateId == $gem->predicateId) {
                $gemStore->$k->isUnlock = $tag;
            }
        }
        $user->store->gemStore = $gemStore;

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

    /**
     * 切换装备分页
     * @return Resp
     */
    public static function switchEquipPag() {
        list($index) = req()->paras;
        $user = ctx();

        $user->store->equipPag = $index;

        ctx($user);
        UserProc::updateUserInfo();
        return Resp::ok(array());
    }

    /**
     * 所有装备的一键升级
     * 规则:等级不同从等级小到大,等级一样按照部位顺序
     *
     * @return Resp
     */
    public static function allEquipUpgrade() {
        //list() = req()->paras;
        $user = ctx();

        $equipDic = $user->store->equip;

        $arr = array();
        if ($equipDic != null) {
            foreach ($equipDic as $k => &$equip) {
                $equip = new Ins_Equip($equip);

                $lvMo = GameConfig::equip_levelupgrade_getItem($equip->level);
                $needGold = $lvMo->needGold;
                $needItemNum = $lvMo->needItemNum;
                $neeItemId = $equip->mo()->costTuzhiId;

                if ($user->baseInfo->gold >= $needGold && StlUtil::dictHasProperty($user->store->items, $neeItemId) && $user->store->items->$neeItemId >= $needItemNum) {
                    $arr[] = $equip;
                }
            }
        }
        $upLevelArr = array();
        if (count($arr) > 0) {
            //进行排序
            $len = count($arr);
            for ($i = 0; $i < $len - 1; $i++) {
                for ($j = 0; $j < $len - $i - 1; $j++) {
                    if ($arr[$j]->level > $arr[$j + 1]->level) {//从小到大
                        $temp = $arr[$j];
                        $arr[$j] = $arr[$j + 1];
                        $arr[$j + 1] = $temp;
                    } elseif ($arr[$j]->level == $arr[$j + 1]->level) {
                        $j_equip = new Ins_Equip($arr[$j]);
                        $jj_equip = new Ins_Equip($arr[$j + 1]);
                        if ($j_equip->mo()->position > $jj_equip->mo()->position) {
                            $temp = $arr[$j];
                            $arr[$j] = $arr[$j + 1];
                            $arr[$j + 1] = $temp;
                        }
                    }
                }
            }

            //升级，扣除金币和图纸
            while (true) {
                $tag = 9999;
                foreach ($arr as $index => &$equip) {
                    $ins_equip = new Ins_Equip($equip);
                    $lvMo = GameConfig::equip_levelupgrade_getItem($ins_equip->level);
                    $needGold = $lvMo->needGold;
                    $needItemNum = $lvMo->needItemNum;
                    $neeItemId = $ins_equip->mo()->costTuzhiId;
                    if ($user->baseInfo->gold >= $needGold && StlUtil::dictHasProperty($user->store->items, $neeItemId) && $user->store->items->$neeItemId >= $needItemNum) {
                        $id = $ins_equip->typeId;
                        if (!in_array($id, $upLevelArr)) {
                            $upLevelArr[] = $id;
                        }

                        $ins_equip->level += 1;
                        $equipDic->$id->level += 1;
                        $user->baseInfo->Consume_Gold($needGold);
                        $user->store->removeItem($neeItemId, $needItemNum);
                        if ($ins_equip->level >= 50) {
                            $tag = $index;
                            break;
                        }
                    } else {
                        $tag = $index;
                        break;
                    }
                }

                if ($tag != 9999) {
                    if (array_key_exists($tag, $arr)) {
                        StlUtil::arrayRemoveAt($arr, $tag);
                    }
                }

                if (count($arr) <= 0) {
                    break;
                }
            }
        }

        TaskProc::OnAnyEquipUpLevel_X();
        TaskProc::OnAllEquipUpLevel_X();
        TaskProc::OnEquipLevelUpNum();
        ctx($user);

        FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'upLevelArr' => $upLevelArr,
                    'gold' => $user->baseInfo->gold,
                    'store' => $user->store,
                    'task' => $user->task,
        ));
    }

    /**
     * 移除是新宝石绿点提示
     * @return Resp
     */
    public static function removeNewGemTip() {
        list($uid) = req()->paras; //宝石uid
        $user = ctx();

        my_Assert(StlUtil::dictHasProperty($user->store->gemStore, $uid), ErrCode::user_store_NoItem);

        //$ins_gem = new Ins_Gem($user->store->gemStore->$uid);
        $user->store->gemStore->$uid->isNew = 0;

//        $equipPag = $user->store->equipPag;
        $typeId = $user->store->gemStore->$uid->typeId;
        $mo = GameConfig::gem_getItem($typeId);
        my_Assert($mo != null, ErrCode::err_const_no);
        $position = $mo->position;
        $gemEquip = $user->store->gemEquip;
        //$tag = 0;

        foreach ($gemEquip as $pag => $val) {
            foreach ($val as $posId => $dic) {
                if ($posId != $position) {
                    continue;
                }
                foreach ($dic as $index => $gem) {
                    if ($gem->uid == $uid) {
                        $user->store->gemEquip->$pag->$posId->$index->isNew = 0;
                        break;
                    }
                }
            }
        }

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

    /**
     * 移除装备可以升级绿点提示
     * @return Resp
     */
    public static function removeEquipUpgradeTip() {
        list($posId) = req()->paras; //装备部位
        $user = ctx();

        my_Assert(StlUtil::dictHasProperty($user->store->equip, $posId), ErrCode::user_store_NoEquip);

        $user->store->equip->$posId->tip = 0;

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

    /**
     * 6406 合成
     * @return type
     */
    public static function gemCompose() {
        list($gemIds) = req()->paras;
        $user = ctx();

        //做校验
        $composeArr = array();

        //$gemIds = substr($gemIds, 0, -1);
        $list = explode(';', $gemIds);
        krsort($list);

        foreach ($list as $str) {
            $str = substr($str, 0, -1);
            $gemUidList = explode(',', $str);
            $firstGemUId = $gemUidList[0];
            $qual = 0;
            $posId = 0;
            if (StlUtil::dictHasProperty($user->store->gemStore, $firstGemUId)) {
                $ins_gem = $user->store->gemStore->$firstGemUId;
                $mo = GameConfig::gem_getItem($ins_gem->typeId);
                $qual = $mo->qual;
                $posId = $mo->position;
            }
            $isCompose = true;
            //在做一次校验
            foreach ($gemUidList as $uid) {
                if (StlUtil::dictHasProperty($user->store->gemStore, $uid)) {
                    $ins_gem = $user->store->gemStore->$uid;
                    $mo = GameConfig::gem_getItem($ins_gem->typeId);

                    if ($mo->qual != $qual || $mo->position != $posId) {
                        $isCompose = false;
                        break;
                    }
                }
            }
            my_Assert($isCompose == true, ErrCode::user_store_GemCanotCompose);

            //查找要合成的宝石配置信息
            $gemConf = self::GemComposeSynthesis($qual, $posId);
            my_Assert($gemConf != null, ErrCode::err_const_no);
            $synthesisList = explode('_', $gemConf->synthesis);
            $composePosition = $synthesisList[0];
            $composeQual = $synthesisList[1];
            $composeNum = $synthesisList[2];

            foreach ($composeArr as $val) {
                $mo = GameConfig::gem_getItem($val->typeId);
                if ($mo->qual == $composeQual && $mo->position == $composePosition) {
                    $gemUidList[] = $val->uid;
                }
            }

            if (count($gemUidList) >= $composeNum) {
                if ($gemConf != null) {
                    $maxGemNum = intval(count($gemUidList) / $composeNum);
                    $index = 0;
                    $res = 0;
                    foreach ($gemUidList as $uid) {
                        my_Assert(self::GemIsCanCompose($uid) == false && $user->store->gemStore->$uid->isUnlock == 0, ErrCode::user_store_GemCanotCompose);

                        $index += 1;
                        self::RemoveGemInStore($uid);
                        //$composeArr 有的话删除
                        for ($i = 0; $i < count($composeArr); $i++) {
                            if ($composeArr[$i]->uid == $uid) {
                                StlUtil::arrayRemoveAt($composeArr, $i);
                                break;
                            }
                        }

                        if ($index % $composeNum == 0) {
                            $res += 1;
                            $composeGem = self::initGem($gemConf->typeId);
                            self::PutGemInStore2($composeGem);
                            $composeArr[] = $composeGem; //临时放这
                        }

                        if ($res >= $maxGemNum) {
                            break;
                        }
                    }
                }
            }
        }
        $resultArr = array();
        foreach ($composeArr as $value) {           
            SystemProc::GetGem_GreaterOrangeQual_GemCompose(req()->zoneid, ctx()->baseInfo->name, $value->typeId);//广播
            $resultArr[] = $value->uid;
        }
        TaskProc::OnComposeNumGem();
        ctx($user);
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'composeGemIds' => $resultArr,
                    'store' => $user->store,
                    'task' => $user->task,
        ));
    }

    /**
     * 该宝石是否可以参与合成，其他页镶嵌的，已经被锁的都不能参与
     * @param type $uid
     * @return bool
     */
    private static function GemIsCanCompose($uid) {
        $Ins_gemItem = new Ins_Gem(ctx()->store->gemStore->$uid);

        $equipGemDic = ctx()->store->gemEquip;
        $equipPag = ctx()->store->equipPag;

        $isExist = false;
        foreach ($equipGemDic as $pag => $dic) {
            if ($equipPag == $pag) {
                continue;
            }
            if (StlUtil::dictHasProperty($dic, $Ins_gemItem->mo()->position)) {
                $posId = $Ins_gemItem->mo()->position;
                $tDic = $dic->$posId;
                foreach ($tDic as $index => $ins_gem) {
                    if ($ins_gem->uid == $Ins_gemItem->uid) {
                        $isExist = true;
                        break;
                    }
                }
            }

            if ($isExist) {
                break;
            }
        }

        return $isExist;
    }

    private static function GemComposeSynthesis($qual, $posId) {
        $gem = GameConfig::gem();
        foreach ($gem as $key => $value) {
            if ($value->synthesis != null) {
                $synthesisList = explode('_', $value->synthesis);
                $composePosition = $synthesisList[0];
                $composeQual = $synthesisList[1];
                $composeNum = $synthesisList[2];

                if ($composeQual != 0 && $composePosition != 0 && $composeQual == $qual && $posId == $composePosition) {
                    return $value;
                }
            }
        }

        return null;
    }

    /**
     * 装备宝石
     * @return type
     */
    public static function gemSet() {
        list($uid, $type, $replaceUId) = req()->paras; //宝石uid  手动的时候记得校验u
        $user = ctx();

        my_Assert(StlUtil::dictHasProperty($user->store->gemStore, $uid), ErrCode::user_store_NoItem);
        $ins_gem = new Ins_Gem($user->store->gemStore->$uid);

        $pag = $user->store->equipPag;
        $posId = $ins_gem->mo()->position;
        $dic = $user->store->gemEquip->$pag->$posId;

        $tag_index = 0;
        $tag_gem = null;
        foreach ($dic as $index => &$gem) {
            $gem = new Ins_Gem($gem);
            if ($gem->predicateMo()->uniqueKey == $ins_gem->predicateMo()->uniqueKey) {
                $tag_index = $index;
                $tag_gem = $gem;
                break;
            }
        }

        switch ($type) {
            case 1://镶嵌
                my_Assert($tag_gem == null, ErrCode::user_store_GemCanotSet);
                for ($i = 1; $i <= 5; $i++) {
                    if (!StlUtil::dictHasProperty($dic, $i)) {
                        $dic->$i = $ins_gem;
                        //StlUtil::dictRemove($user->store->gemStore, $uid);
                        break;
                    }
                }
                //$user->store->gemEquip->$pag->$posId = $dic;
                break;
            case 2://替换
                //满不满都高替低品阶
                my_Assert(StlUtil::dictHasProperty($dic, $replaceUId), ErrCode::user_store_NoExistGemReplace);
                $replace_ins_Gem = new Ins_Gem($dic->$replaceUId); //这个uid是5个坑位的index id
                my_Assert($replace_ins_Gem->mo()->qual < $ins_gem->mo()->qual, ErrCode::user_store_NoExistGemReplace);
                $dic->$replaceUId = $ins_gem;

//                $id = $replace_ins_Gem->uid;
//                $user->store->gemStore->$id = $replace_ins_Gem;
//                $dic->$replaceUId = $ins_gem;
//                StlUtil::dictRemove($user->store->gemStore, $uid);
                //$user->store->gemEquip->$pag->$posId = $dic;
                break;
            case 3://手动
                my_Assert(count((array) $dic) >= 5, ErrCode::user_store_GemCanotSet);
                my_Assert(StlUtil::dictHasProperty($user->store->gemStore, $replaceUId), ErrCode::user_store_NoExistGemReplace);
                $replace_ins_Gem = new Ins_Gem($user->store->gemStore->$replaceUId);
                //$id = $replace_ins_Gem->uid;
                if ($tag_gem != null) {//如果有u那
                    my_Assert($replace_ins_Gem->uid == $tag_gem->uid, ErrCode::user_store_SameGemCanotEquip); //点的不是u一样的，则提示同一件装备无法镶嵌多个技能相同的宝石 【错误】
                    //有U一样的，则点击正好是这个直接替换
                    //$user->store->gemStore->$id = $replace_ins_Gem;
                    $dic->$tag_index = $ins_gem;
                } else {
                    $go_index = 0;
                    foreach ($dic as $index => $g) {
                        if ($g->uid == $replaceUId) {
                            $go_index = $index;
                            break;
                        }
                    }

                    my_Assert($go_index > 0, ErrCode::user_store_NoExistGemReplace);
                    $dic->$go_index = $ins_gem;
                }

                break;
            case 4://不能
                //1.小品阶替大品阶  或是  相等的，在不满的时候不允许    2. //满的  相等不允许替
                my_Assert($uid == 0, ErrCode::user_store_SameGemCanotEquip); //点的不是u一样的，则提示同一件装备无法镶嵌多个技能相同的宝石 【错误】
                break;
            default:
                break;
        }

        $user->store->gemEquip->$pag->$posId = $dic;

        TaskProc::OnSetSpecialQualGem();

        FightProc::Ranking_FightPower();
        ctx($user);
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'gold' => 0,
                    'store' => $user->store,
                    'task' => $user->task,
        ));
    }

    /**
     * 卸下装备
     * @return type
     */
    public static function gemRemove() {
        list($uid) = req()->paras; //装备宝石id
        $user = ctx();

        $pag = $user->store->equipPag;
        $posDic = $user->store->gemEquip->$pag;

        $tag_pos = 0;
        $tag_index = 0;
        $tag_gem = null;
        foreach ($posDic as $posId => $dic) {
            foreach ($dic as $index => $gem) {
                if ($gem->uid == $uid) {
                    $tag_pos = $posId;
                    $tag_index = $index;
                    $tag_gem = $gem;
                    break 2;
                }
            }
        }

        if ($tag_index > 0 && $tag_gem != null) {
            StlUtil::dictRemove($posDic->$tag_pos, $tag_index);
            $user->store->gemEquip->$pag = $posDic;
            //$user->store->gemStore->$uid = $tag_gem;
        }
        ctx($user);

        FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'gold' => 0,
                    'store' => $user->store,));
    }

    /**
     * 升级装备
     * @return type
     */
    public static function equipUpgrade() {
        list($posId) = req()->paras; //装备的部位id
        $user = ctx();

        $lv = $user->store->equip->$posId->level;
        $mo = GameConfig::equip_levelupgrade_getItem($lv);
        my_Assert($user->baseInfo->gold >= $mo->needGold, ErrCode::notenough_gold_msg);
        $costTuzhiId = GameConfig::equip_getItem($posId)->costTuzhiId;

        $tuzhiNum = 0;
        if (StlUtil::dictHasProperty($user->store->items, $costTuzhiId)) {
            $tuzhiNum = $user->store->items->$costTuzhiId;
        }
        my_Assert($tuzhiNum >= $mo->needItemNum, ErrCode::notenough_item);
        $user->store->removeItem($costTuzhiId, $mo->needItemNum);
        ctx()->baseInfo->Consume_Gold($mo->needGold);

        $user->store->equip->$posId->level += 1;

        TaskProc::OnAnyEquipUpLevel_X();
        TaskProc::OnAllEquipUpLevel_X();
        TaskProc::OnEquipLevelUpNum();

        FightProc::Ranking_FightPower();
        ctx($user);
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'gold' => $user->baseInfo->gold,
                    'store' => $user->store,
                    'task' => $user->task,
        ));
    }

    /**
     * 单个装备部位一键升级
     * @return type
     */
    public static function equipUpgrade_MaxLv() {
        list($posId) = req()->paras; //mask = 1:表示战斗中掉落
        $user = ctx();

        $equip = $user->store->equip->$posId;

        $lv = $equip->level;
        $levelupgrade = GameConfig::equip_levelupgrade();
        $maxLv = count((array) $levelupgrade);

        $need_gold = 0;
        $need_item = 0;
        $tuzhiId = GameConfig::equip_getItem($posId)->costTuzhiId;

        $itemNum_store = 0;
        if (StlUtil::dictHasProperty($user->store->items, $tuzhiId)) {
            $itemNum_store = $user->store->items->$tuzhiId;
        }

        $up_Gold = 0;
        $up_needItem = 0;
        $up_lv = 0;
        for ($i = $lv; $i <= $maxLv; $i++) {
            $up_Gold += GameConfig::equip_levelupgrade_getItem($i)->needGold;
            $up_needItem += GameConfig::equip_levelupgrade_getItem($i)->needItemNum;
            if ($user->baseInfo->gold >= $up_Gold && $itemNum_store >= $up_needItem) {
                $need_gold = $up_Gold;
                $need_item = $up_needItem;
                $up_lv = $i;
                continue;
            }

            break;
        }

        $user->baseInfo->gold -= $need_gold;
        $user->store->removeItem($tuzhiId, $need_item);
        if($up_lv >0){
            $user->store->equip->$posId->level = $up_lv;
        }
        
        TaskProc::OnAnyEquipUpLevel_X();
        TaskProc::OnAllEquipUpLevel_X();
        TaskProc::OnEquipLevelUpNum();

        FightProc::Ranking_FightPower();
        ctx($user);
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'gold' => $user->baseInfo->gold,
                    'store' => $user->store,
                    'task' => $user->task,
        ));
    }

    public static function AddItemInStore() {
        list($rwdStr) = req()->paras; //mask = 1:表示战斗中掉落
        $user = ctx();

        $err = self::AddMultiItemInStore($rwdStr);

        my_Assert(ErrCode::ok == $err, $err);

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    //'gold' => $user->baseInfo->gold,
                    //'tili' => $user->baseInfo->tili,
                    //'cash' => $user->baseInfo->cash,
                    'store' => $user->store));
    }
    
    static $reward = array();

    /**
     * 具体奖励存入背包
     * @param type $goodsStr
     * @param type $src
     */
    public static function AddMultiItemInStore($goodsStr, $src = 1) {
        if ($goodsStr == null) {
            return;
        }
        $ary = explode(";", $goodsStr);
        foreach ($ary as $value) {
            $val = explode(",", $value);
            my_Assert(count($val) > 1, "解析奖励字符串出错");

            list($itemId, $num) = $val;                                         # ID, 数量           
            $itemMo = GameConfig::item_getItem($itemId);
            if($itemMo->itemType != 701){
                self::$reward[] = $value;                               
            }           
            
            switch ($itemMo->itemType) {
                case 1:
                    ctx()->baseInfo->Add_Gold($num);
                    self::checkEquipUpgradeTip();
                    break;
                case 2:
                    ctx()->baseInfo->Add_Cash($num);
                    break;
                case 3:
                    ctx()->baseInfo->Add_tili($num);
                    break;
                case 4:
                    ctx()->baseInfo->Add_Exp($num);
                    break;
                case 100://图纸
                case 401://启灵石
                case 301://洗练石
                case 501://人物碎片
                    self::PutItemsInStore($itemId, $num);
                    if ($itemMo->itemType == 100) {//图纸
                        self::checkEquipUpgradeTip();
                    }
                    break;
                case 201://宝石                      
                    self::PutGemInStore($itemId, $num);
                    break;
                case 701://道具宝箱
                    for ($i = 0; $i < $num; $i++) {
                        self::DistributeItemsBox($itemId);
                    }
                    
                    
                    break;
                default:

                    break;
            }
        }
    }

    public static function PutItemsInStore($itemId, $num) {
        $items = ctx()->store->items;
        if (StlUtil::dictHasProperty($items, $itemId)) {
            $items->$itemId += $num;
        } else {
            $items->$itemId = $num;
        }

        ctx()->store->items = $items;
    }

    /**
     * 拆分宝箱盒子
     * @param type $reward
     */
    public static function DistributeItemsBox($itemId) {
        $itemBoxMo = GameConfig::item_2023_box_getItem($itemId);
        my_Assert($itemBoxMo != null, ErrCode::err_const_no);
        if ($itemBoxMo->type == 1) {
            $strList = explode(';', $itemBoxMo->contents);
            $numList = explode('-', $strList[0]);
            $randNum = rand($numList[0], $numList[1]);

            $ctxList = explode(',', $strList[1]);
            $per = 0;
            foreach ($ctxList as $value) {
                $ctx = explode(':', $value);
                $per += $ctx[1];
            }

            $reward = "";
            for ($i = 0; $i < $randNum; $i++) {
                $ctxPer = rand(1, $per);
                $start = 0;
                $end = 0;

                $id = 0;
                foreach ($ctxList as $value) {
                    $ctx = explode(':', $value);               
                    $end += $ctx[1];
                    if ($ctxPer > $start && $ctxPer <= $end) {
                        $id = $ctx[0];
                        break;
                    }
                    $start = $end;
                }

                if ($id != 0) {
                    $str = $id . ',1';
                    if ($reward == "") {
                        $reward = $str;
                    } else {
                        $reward = $reward . ';' . $str;
                    }
                }
            }

            self::AddMultiItemInStore($reward);
        } else {
            self::AddMultiItemInStore($itemBoxMo->contents);
        }
    }

//    public static function PutEquipInStore($equipId, $num) {
//        if ($equipId == 0) {
//            return;
//        }
//        $n = count((array) ctx()->store->equip) + 1;
//
//        for ($index = 0; $index < $num; $index++) {
//            $Equip = new Ins_Equip();
//            $Equip->uid = $n;
//            $Equip->typeId = $equipId;
//            $Equip->qual = GameConfig::equip_getItem($equipId)->qual;
//            ctx()->store->equip->$n = $Equip;
//            $n += 1;
//        }
//    }

    /**
     * 得到金币或是图纸的时候校验下，是否满足装备升级条件，满足则tip字段设置为1
     */
    public static function checkEquipUpgradeTip() {
        $equipDic = ctx()->store->equip;
        if ($equipDic != null) {
            foreach ($equipDic as $k => &$equip) {
                $equip = new Ins_Equip($equip);
                $lvMo = GameConfig::equip_levelupgrade_getItem($equip->level);
                $needGold = $lvMo->needGold;
                $needItemNum = $lvMo->needItemNum;
                $neeItemId = $equip->mo()->costTuzhiId;

                if (ctx()->baseInfo->gold >= $needGold && StlUtil::dictHasProperty(ctx()->store->items, $neeItemId) && ctx()->store->items->$neeItemId >= $needItemNum) {
                    $equipDic->$k->tip = 1; //绿点是每次得到金币或是图纸了，只要满足升级条件都变绿
                }
            }
        }
        ctx()->store->equip = $equipDic;
    }

    /**
     * 新宝石入库
     */
    public static function PutGemInStore($id, $num = 1) {
        for ($i = 0; $i < $num; $i++) {
            $gem = self::initGem($id);
            $length = $gem->uid;
            ctx()->store->gemStore->$length = $gem;
        }
    }

    public static function PutGemInStore2($gem, $num = 1) {
        for ($i = 0; $i < $num; $i++) {
            $length = $gem->uid;
            ctx()->store->gemStore->$length = $gem;
        }
    }

    public static function initGem($id) {
        $length = ctx()->store->gemLength;
        $length += 1;
        $gem = new Ins_Gem();
        $gem->uid = $length;
        $gem->typeId = $id;
        $gem->predicateId = self::RandomGemPredicateId($id);
        $tag = self::CheckNewGemTip($gem);
        $gem->isNew = $tag;

        ctx()->store->gemLength = $length;

        return $gem;
    }

    /**
     * 从仓库移除宝石
     */
    public static function RemoveGemInStore($uid) {
        if (StlUtil::dictHasProperty(ctx()->store->gemStore, $uid)) {
            StlUtil::dictRemove(ctx()->store->gemStore, $uid);
        }

        return 0;
    }

    /**
     * 新宝石随机词条 临时等刚哥
     */
    public static function RandomGemPredicateId($id) {
        $mo = GameConfig::gem_getItem($id);
        my_Assert(null != $mo, "找不到宝石{$id}的配置数据");
        if ($mo->isfixed_predicateId == 1) {
            return $mo->predicateId;
        }

        $posId = $mo->position;
        $qual = $mo->qual;
        $arr = array();
        $predicateMo = GameConfig::predicate();
        foreach ($predicateMo as $key => $value) {
            if ($value->positions != null) {
                $list = explode(',', $value->positions);
                if ($value->qual == $qual && in_array($posId, $list) && $value->attachTarget == "gem") {
                    $arr[] = $value->id;
                }
            }
        }

        if (count($arr) <= 0) {
            return 40;
        }
        my_Assert(count($arr) > 0, "宝石没有对应词条信息");
        $n = mt_rand(0, count($arr) - 1);
        return $arr[$n];

        //return 40;
    }

    /**
     * 新宝石提示
     */
    public static function CheckNewGemTip($gem) {
        $gemStore = ctx()->store->gemStore;
        $tag = false;
        foreach ($gemStore as $k => $ins_gem) {
            if ($ins_gem->typeId == $gem->typeId && $ins_gem->predicateId == $gem->predicateId) {
                $tag = true;
                break;
            }
        }

        return $tag == true ? 0 : 1;
    }

    //临时代码
//    public static function InitGemInfo() {
//        if(count((array)ctx()->store->gemStore) > 0){
//            return;
//        }
//
//        $index = 0;
//        $gem = GameConfig::gem();
//
//        for ($i = 1; $i <=21; $i++) {
//            foreach ($gem as $key => $value) {
//                if($value->id == $i){
//                    self::PutGemInStore($value->typeId);
//                }
//
//            }
//        }
//
//
//
//
//    }
}
