<?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();
            case CmdCode::store_equiped:                                        # 6413 装备
                return StoreProc::Equiped();
            case CmdCode::store_remove_equip:                                   # 6413 卸下装备
                return StoreProc::RemoveEquip();
            case CmdCode::store_equip_gemSlotBuy:                               # 6416 购买装备宝石槽         
                return StoreProc::Gem_BuySlot();
            case CmdCode::store_equip_compose:                                  # 6417 装备合成
                return StoreProc::Equip_Compose();
            case CmdCode::store_equip_OnekeyCompose:                            # 6418 装备一键合成
                return StoreProc::Equip_OnekeyCompose_new();
            case CmdCode::store_equip_downGradingLevel:                         # 6419 降级
                return StoreProc::Equip_DownGradingLevel();
            case CmdCode::store_equip_downGradingQual:                          # 6420 降品
                return StoreProc::Equip_DownGradingQual();
            default:
                Err(ErrCode::cmd_err);
        }
    }

    /**
     * 6420 降品
     */
    public static function Equip_DownGradingQual() {
        list($uid) = req()->paras;

        $equipDic = ctx()->store->equip;
        my_Assert(StlUtil::dictHasProperty($equipDic, $uid), ErrCode::user_store_NotExistEquip);
        $ins_equip = new Ins_Equip($equipDic->$uid);

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

        self::GoldTuzhi_Fallback_Level($ins_equip);
        self::ComposeMaterial_Fallback($ins_equip->mo()->position, $ins_equip->qual);

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

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

    private static function ComposeMaterial_Fallback($posId, $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::equip_compose_getItem($i);
                    $num += explode(',', $mo->compose_condition)[2];
                }

                $typeId = self::ComposeMaterial($posId, $min);
                StoreProc::AddMultiItemInStore($typeId . ',' . $num);
                break;
            }
        }
    }

    private static function ComposeMaterial($posId, $qual) {
        $itemDic = GameConfig::item();
        foreach ($itemDic as $typeId => $mo) {
            if ($mo->itemType == 901 && $typeId % 100 == $posId && floor($typeId / 100) % 100 == $qual) {
                return $typeId;
            }
        }

        return null;
    }

    private static function GoldTuzhi_Fallback_Level($ins_equip) {
        $gold = 0;
        $itemNum = 0;
        for ($i = 1; $i < $ins_equip->level; $i++) {
            $mo = GameConfig::equip_levelupgrade_getItem($ins_equip->mo()->rarity, $ins_equip->qual, $ins_equip->mo()->position, $i);
            $gold += $mo->needGold;
            $itemNum += $mo->needItemNum;
        }

        $tuzhiId = GameConfig::equip_position_getItem($ins_equip->mo()->position)->costTuzhiId;
        StoreProc::AddMultiItemInStore($tuzhiId . ',' . $itemNum);

        ctx()->base(true)->Add_Gold($gold);
    }

    /*
     * 6419 降级
     */

    public static function Equip_DownGradingLevel() {
        list($uid) = req()->paras; //装备uid

        $equipDic = ctx()->store->equip;
        my_Assert(StlUtil::dictHasProperty($equipDic, $uid), ErrCode::user_store_NotExistEquip);
        $ins_equip = new Ins_Equip($equipDic->$uid);

        my_Assert($ins_equip->level > 1, ErrCode::user_store_equip_minlevel);
        self::GoldTuzhi_Fallback_Level($ins_equip);

        $ins_equip->level = 1;
        ctx()->store->equip->$uid = $ins_equip;
        
        FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'gold' => ctx()->baseInfo->gold,
                    'store' => ctx()->store,
        ));
    }

    /**
     * 6417 装备合成
     * @return type
     */
    public static function Equip_Compose() {
        list($uid, $equipUids_cost, $composeMaterial_typeIds) = req()->paras;
        $store = ctx()->store(true);
        $equipDic = ctx()->store->equip;
        my_Assert(StlUtil::dictHasProperty($equipDic, $uid), ErrCode::user_store_NotExistEquip);
        $ins_equip = new Ins_Equip($equipDic->$uid);

        my_Assert($ins_equip->qual<16, ErrCode::user_store_equip_qualMaxLimit);
        $composeMo = GameConfig::equip_compose_getItem($ins_equip->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 = self::ComposeMaterial($ins_equip->mo()->position, $costQual);
                my_Assert($composeMaterial_itemTypeId == $s, ErrCode::user_store_equip_composeMaterialErr);
                $all_num += 1;
            }
        }

        if ($equipUids_cost != null) {
            $costEquipsArr = explode(',', $equipUids_cost);
            $all_num += count($costEquipsArr);

            foreach ($costEquipsArr as $eqUid) {
                my_Assert(StlUtil::dictHasProperty($equipDic, $eqUid), ErrCode::user_store_NotExistEquip);

                $costIns_Equip = new Ins_Equip($equipDic->$eqUid);
                my_Assert($costIns_Equip->qual == $costQual, ErrCode::user_store_equipMaterialQualErr);
                if ($type == 1) {//本体                
                    my_Assert($costIns_Equip->typeId == $ins_equip->typeId, ErrCode::user_store_equipMaterialTypeErr);
                } else {
                    my_Assert($costIns_Equip->mo()->position == $ins_equip->mo()->position, ErrCode::user_store_equipMaterialTypeErr);
                }
            }
        }
        my_Assert($all_num == $num, ErrCode::user_store_equipMaterialNumErr);
        ctx()->store(true)->equip->$uid = self::ComposeNewEquip($ins_equip);

        //消耗的装备 回收         
        if ($equipUids_cost != null) {
            $costEquipsArr = explode(',', $equipUids_cost);
            foreach ($costEquipsArr as $eqUid) {
                $costIns_Equip = new Ins_Equip($equipDic->$eqUid);
                self::equipLevelUp_Material_Recovery($costIns_Equip->mo()->rarity, $costIns_Equip->qual, $costIns_Equip->mo()->position, $costIns_Equip->level);
                StlUtil::dictRemove(ctx()->store(true)->equip, $eqUid);
                for ($i = 1; $i <= 6; $i++) {                                   # 清理装备信息
                    if ($store->equipLocation->$i == $eqUid) {
                        StlUtil::dictRemove($store->equipLocation, $i);
                    }
                }
            }
        }

        if ($materialItemType != 0) {
            ctx()->store(true)->removeItem($materialItemType, $materialItemNum);
        }

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

    /**
     * 装备等级提升 消耗的金币图纸回收
     * @param type $rarity
     * @param type $qual
     * @param type $posId
     * @param type $level
     */
    public static function equipLevelUp_Material_Recovery($rarity, $qual, $posId, $level) {
        $gold = 0;
        $tuzhi = 0;

        $tuzhiId = GameConfig::equip_position_getItem($posId)->costTuzhiId;
        for ($i = 1; $i <= $level - 1; $i++) {
            $mo = GameConfig::equip_levelupgrade_getItem($rarity, $qual, $posId, $i);
            $gold += $mo->needGold;
            $tuzhi += $mo->needItemNum;
        }
        if ($gold > 0) {
            StoreProc::AddMultiItemInStore("1," . $gold);
            StoreProc::AddMultiItemInStore($tuzhiId . ',' . $tuzhi);
        }
    }

    /**
     * 合成一个新的装备
     * @param type Ins_Equip
     */
    public static function ComposeNewEquip(&$ins_equip) {
        $newEquipTypeId = $ins_equip->typeId;
        $qual = $ins_equip->qual + 1;
        if ($ins_equip->qual < 4) {
            $newEquipTypeId = substr($ins_equip->typeId, 0, strlen($ins_equip->typeId) - 5);
            $rarity = substr($ins_equip->typeId, -3);
            $newEquipTypeId = $newEquipTypeId . "0" . $qual . $rarity;
        }

        $ins_equip->typeId = $newEquipTypeId;
        $ins_equip->qual = $qual;

        return $ins_equip;
    }

    /**
     * 6418 装备一键合成 (gwang 2024年11月16日)
     * @return type
     */
    public static function Equip_OnekeyCompose_new() {
        $store = ctx()->store(true);
        $equipDic = $store->equip;
        $arr = array();                                                         # 待处理装备临时数组
        $composeEquip = array();                                                # 合成记录
        foreach ($equipDic as $equip) {
            if ($equip->qual < 4) {                                            # 4 品以上的不参与合成
                $arr[] = new Ins_Equip($equip);
            }
        }
        usort($arr, function ($a, $b) {                                         # 按等级大小排序, 等级大的放最后
            if ($a->level > $b->level) {
                return 1;
            } else if ($a->level == $b->level) {
                return 0;
            } else {
                return -1;
            }
        });

        while (count($arr) > 0) {                                               # 循环处理每一个装备
            $equip_compose = array_pop($arr);                                   # 从末尾弹出一个元素做合成运算
            if (null == $equip_compose) {                                       # 防御空对象
                continue;
            }
            $eqQualCfg = GameConfig::equip_compose_getItem($equip_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 == $equip_compose->typeId #   # type: 1 与本体相同 2 同部位即可
                                    : $v->mo()->position == $equip_compose->mo()->position) {
                        $composeArr[] = $v;                                     # 成为消耗品
                    }
                    if (count($composeArr) >= $num) {                           # 达到消耗数量就停
                        break;
                    }
                }
            }
            if (count($composeArr) >= $num) {                                   # 合成的回收 不合成的可不能给回收了
                $uid = $equip_compose->uid;
                $equipDic->$uid = self::ComposeNewEquip($equip_compose);        # 原地合成
                $composeEquip[] = $uid;                                         # 添加合成记录
                foreach ($composeArr as $val) {                                 # 回收消耗的装备中所含材料
                    self::equipLevelUp_Material_Recovery($val->mo()->rarity, $val->qual, $val->mo()->position, $val->level); # 回收材料
                    StlUtil::dictRemove(ctx()->store(true)->equip, $val->uid);  # 背包删除
                    for ($i = 1; $i <= 6; $i++) {                               # 清理装备信息
                        if ($store->equipLocation->$i == $val->uid) {
                            StlUtil::dictRemove($store->equipLocation, $i);
                        }
                    }
                    StlUtil::arrayRemove($arr, $val); # 从arr中删掉已消耗装备
                }
            }
        }
        
        TaskProc::OnEquipComposeNum();
        FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();                                             # 回存玩家数据
        return Resp::ok(array(#                                                 # 返回给客户端的数据
                    'gold' => ctx()->baseInfo->gold,
                    'store' => ctx()->store,
                    'composeEquip' => $composeEquip,
                    'task' => ctx()->task,
        ));
    }

    /**
     * 6418 装备一键合成
     * @return type
     */
    public static function Equip_OnekeyCompose() {
        //list() = req()->paras;

        $equipDic = ctx()->store->equip;
        $arr = array();
        foreach ($equipDic as $uid => $equip) {
            $ins_equip = new Ins_Equip($equip);
            if ($ins_equip->qual > 4) {
                continue;
            }
            $arr[] = $ins_equip;
        }
//等级从大到小
        $len = count($arr);
        for ($i = 0; $i < $len - 1; $i++) {
            for ($j = $len - 1; $j > $i; $j--) {
                if ($arr[$j]->level > $arr[$j - 1]->level) {
                    $temp = $arr[$j];
                    $arr[$j] = $arr[$j - 1];
                    $arr[$j - 1] = $temp;
                }
            }
        }

        $composeEquip = array();

        if ($len > 0) {
            while (true) {
                $equip_compose = $arr[0];
                $str = explode(',', GameConfig::equip_compose_getItem($equip_compose->qual)->compose_condition);
                $type = $str[0];
                $costQual = $str[1];
                $num = $str[2];

                $n = count($arr);
                $composeArr = array();
                $tag = false;

                $ing_compose = false;
                for ($k = $n - 1; $k >= 0; $k--) {
                    if ($arr[$k]->uid == $equip_compose->uid) {
                        continue;
                    }
                    if ($type == 1 && $arr[$k]->qual == $costQual && $arr[$k]->typeId == $equip_compose->typeId) {
                        $tag = true;
                    } else if ($type == 2 && $arr[$k]->qual == $costQual && $arr[$k]->mo()->position == $equip_compose->mo()->position) {
                        $tag = true;
                    }

                    if ($tag) {
                        $composeArr[] = $arr[$k];
                        if (count($composeArr) >= $num) {
                            break;
                        }
                    }
                }

                if (count($composeArr) >= $num) {
                    $newEquip = self::ComposeNewEquip($equip_compose);
                    $uid = $newEquip->uid;
                    ctx()->store(true)->equip->$uid = $newEquip;
                    $composeEquip[] = $uid;
                    $ing_compose = true;
                }
                //合成的 材料不够不能合成的都要删除
                foreach ($composeArr as $val) {
                    if ($ing_compose) {//合成的回收 不合成的可不能给回收了
                        self::equipLevelUp_Material_Recovery($val->mo()->rarity, $val->qual, $val->mo()->position, $val->level);
                        StlUtil::dictRemove(ctx()->store(true)->equip, $val->uid);
                    }
                    StlUtil::arrayRemove($arr, $val);
                }
                StlUtil::arrayRemove($arr, $equip_compose);
                if (count($arr) <= 0) {
                    break;
                }
            }
        }

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

    /**
     *  6416 购买装备宝石槽         
     * @return type
     */
    public static function Gem_BuySlot() {
        list($uid) = req()->paras;

        $equipDic = ctx()->store->equip;
        my_Assert(StlUtil::dictHasProperty($equipDic, $uid), ErrCode::user_store_NotExistEquip);
        $ins_equip = new Ins_Equip($equipDic->$uid);

        $mo = GameConfig::gem_slotposition_getItem($ins_equip->qual);
        my_Assert(null != $mo, ErrCode::err_const_no);
        $initNum = $mo->initNum_slot;
        $buyNum = $mo->buyNum_slot;

        $unlockIndex = 0;
        if ($buyNum > 0) {
            for ($i = $initNum + 1; $i <= $initNum + $buyNum; $i++) {
                if (!StlUtil::dictHasProperty($ins_equip->gemSetSlot, $i)) {
                    $ins_equip->gemSetSlot->$i = 0;
                    break;
                }
                $unlockIndex += 1;
            }
        }
        $costArr = explode(';', $mo->cost);
        $cash = explode(',', $costArr[$unlockIndex]);
        my_Assert(ctx()->base(true)->cash >= $cash[1], ErrCode::notenough_cash_msg);

        ctx()->base(true)->Consume_Cash($cash[1]);
        ctx()->store(true)->equip->$uid = $ins_equip;

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

    /**
     * 6413 装备
     * @return type
     */
    public static function Equiped() {
        list($uid) = req()->paras;

        $equipDic = ctx()->store->equip;
        my_Assert(StlUtil::dictHasProperty($equipDic, $uid), ErrCode::user_store_NotExistEquip);
        $ins_equip = new Ins_Equip($equipDic->$uid);
        $posId = $ins_equip->mo()->position;
        ctx()->store(true)->equipLocation->$posId = $uid;
        FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array());
    }

    /**
     * 6413 卸下装备
     * @return type
     */
    public static function RemoveEquip() {
        list($uid) = req()->paras;

        $equipDic = ctx()->store->equip;
        my_Assert(StlUtil::dictHasProperty($equipDic, $uid), ErrCode::user_store_NotExistEquip);
        $ins_equip = new Ins_Equip($equipDic->$uid);
        $posId = $ins_equip->mo()->position;
        StlUtil::dictRemove(ctx()->store(true)->equipLocation, $posId);

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

    /**
     * 洗练
     * @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_XilianStoneNoEnough);

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

                $needStone = $s[1];
                $xilianStoneId = $s[0];
                break;
            }
        }

        my_Assert(StlUtil::dictHasProperty(ctx()->store->items, $xilianStoneId) && ctx()->store->items->$xilianStoneId >= $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) && $pMo->attachTarget == "gem") {
                    $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()->store->removeItem($xilianStoneId, $needStone);
        $xilianStoneNum = 0;
        if (StlUtil::dictHasProperty(ctx()->store->items, $xilianStoneId)) {
            $xilianStoneNum = ctx()->store->items->$xilianStoneId;
        }

        FightProc::Ranking_FightPower();
        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'xilianStoneId' => $xilianStoneId,
                    'xilianStone' => $xilianStoneNum,
                    '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->equipPosition;
//
//        $equip_levelDic = GameConfig::equip_levelupgrade();
//        $maxLv = count(StlUtil::dictToArray($equip_levelDic));
//        $arr = array();
//        if ($equipDic != null) {
//            foreach ($equipDic as $k => &$equip) {
//                $equip = new Ins_EquipPosition($equip);
//
//                if ($equip->level >= $maxLv) {
//                    continue;
//                }
//
//                $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_EquipPosition($arr[$j]);
//                        $jj_equip = new Ins_EquipPosition($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_EquipPosition($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 >= 99) {
//                            $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,
        ));
    }

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

        my_Assert(StlUtil::dictHasProperty(ctx()->store->gemStore, $uid), ErrCode::user_store_NoItem);
        ctx()->store(true)->gemStore->$uid->isNew = 0;

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

    /**
     * 移除装备可以升级绿点提示 -------废弃
     * @return Resp
     */
    public static function RemoveEquipUpgradeTip() {
//        list($posId) = req()->paras; //装备部位
//        $user = ctx();
//
//        my_Assert(StlUtil::dictHasProperty($user->store->equipPosition, $posId), ErrCode::user_store_NoEquip);
//
//        $user->store->equipPosition->$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;

        $list = explode(';', $gemIds);

        $gemStore = ctx()->store->gemStore;
        $composeArr = array();
        foreach ($list as $gems) {
            $gemArr = explode('-', $gems);
            $length1 = strlen($gemArr[0]);
            $length2 = strlen($gemArr[1]);
            $gem_composeCost = explode(',', substr($gemArr[0], 1, $length1 - 1)); //去掉逗号
            $gem_composeIds = explode(',', substr($gemArr[1], 1, $length2 - 1)); //去掉逗号

            $gemMo = GameConfig::gem_getItem($gem_composeIds[0]);
            my_Assert(null != $gemMo, ErrCode::err_const_no);

            $isCompose = true;
            foreach ($gem_composeCost as $gemUid) {
                if (!StlUtil::dictHasProperty($gemStore, $gemUid)) {
                    $isCompose = false;
                    break;
                }

                $ins_gem = new Ins_Gem($gemStore->$gemUid);
                if ($ins_gem->mo()->qual + 1 != $gemMo->qual || $ins_gem->mo()->position != $gemMo->position) {
                    $isCompose = false;
                    break;
                }

                if (!self::GemIsCanCompose($gemUid)) {
                    $isCompose = false;
                    break;
                }
            }
            my_Assert($isCompose == true, ErrCode::user_store_GemCanotCompose);

            foreach ($gem_composeCost as $gemUid) {
                self::RemoveGemInStore($gemUid);
            }

            foreach ($gem_composeIds as $gemTypeId) {
                $composeGem = self::initGem($gemTypeId);
                self::PutGemInStore($composeGem);
                $composeArr[] = $composeGem->uid; //临时放这
            }
        }

        TaskProc::OnComposeNumGem();
        TaskProc::OnComposeNumGem_state();

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

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

        $isExist = true;
        $equip = ctx()->store->equip;
        foreach ($equip as $uid => $item) {
            $arr = get_object_vars($item->gemSetSlot);
            $values = array_values($arr);
            if (in_array($uid, $values)) {
                $isExist = false;
                break;
            }
        }

        return $isExist;
    }

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

        my_Assert(StlUtil::dictHasProperty(ctx()->store->gemStore, $uid), ErrCode::user_store_NoItem);
        my_Assert(StlUtil::dictHasProperty(ctx()->store->equip, $equipUid), ErrCode::user_store_NoEquip);
        $ins_equip = new Ins_Equip(ctx()->store->equip->$equipUid);
        $ins_gem = new Ins_Gem(ctx()->store->gemStore->$uid);
        my_Assert($ins_equip->mo()->position == $ins_gem->mo()->position, ErrCode::user_store_PositionNotFit);

        $gemSetSlotDic = $ins_equip->gemSetSlot;
        $tag_index = 0;
        $tag_gem = null;
        foreach ($gemSetSlotDic as $slotId => $gemUid) {
            if ($gemUid == 0) {
                continue;
            }
            $gem = new Ins_Gem(ctx()->store->gemStore->$gemUid);
            if ($gem->predicateMo()->uniqueKey == $ins_gem->predicateMo()->uniqueKey) {
                $tag_index = $slotId;
                $tag_gem = $gem;
                break;
            }
        }
        $mo = GameConfig::gem_slotposition_getItem($ins_equip->qual);
        my_Assert(null != $mo, ErrCode::err_const_no);
        switch ($type) {
            case 1://镶嵌
                my_Assert($tag_gem == null, ErrCode::user_store_GemCanotSet);
                $go_index = 1;
                for ($i = 1; $i <= $mo->initNum_slot + $mo->buyNum_slot; $i++) {
                    if (!StlUtil::dictHasProperty($gemSetSlotDic, $i)) {
                        //$gemSetSlotDic->$i = $ins_gem;
                        $go_index = $i;
                        break;
                    }
                    if ($gemSetSlotDic->$i == 0) {
                        $go_index = $i;
                        break;
                    }
                }
                $gemSetSlotDic->$go_index = $uid;
                break;
            case 2://替换
                //满不满都高替低品阶  
                my_Assert(StlUtil::dictHasProperty($gemSetSlotDic, $replaceUId), ErrCode::user_store_NoExistGemReplace);
                $gUid = $gemSetSlotDic->$replaceUId;
                $replace_ins_Gem = new Ins_Gem(ctx()->store->gemStore->$gUid); //这个$replaceUId是坑位的index id
                my_Assert($replace_ins_Gem->mo()->qual < $ins_gem->mo()->qual, ErrCode::user_store_NoExistGemReplace);
                //$gemSetSlotDic->$replaceUId = $ins_gem;
                $gemSetSlotDic->$replaceUId = $uid;
                break;
            case 3://手动
                //my_Assert(count((array) $gemSetSlotDic) >= $mo->initNum_slot + $mo->buyNum_slot, ErrCode::user_store_GemCanotSet);
                my_Assert(StlUtil::dictHasProperty($gemSetSlotDic, $replaceUId), ErrCode::user_store_NoExistGemReplace);
                $gUid = $gemSetSlotDic->$replaceUId;
                my_Assert(StlUtil::dictHasProperty($gemSetSlotDic, $replaceUId), ErrCode::user_store_NoExistGemReplace);
                $replace_ins_Gem = new Ins_Gem(ctx()->store->gemStore->$gUid);
                //$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一样的，则点击正好是这个直接替换                
                    //$gemSetSlotDic->$tag_index = $ins_gem;
                    $gemSetSlotDic->$tag_index = $uid;
                } else {
//                    $go_index = 0;
//                    foreach ($gemSetSlotDic as $slotId => $gemUid) {
//                        if ($slotId == $replaceUId) {
//                            $go_index = $slotId;
//                            break;
//                        }
//                    }
//
//                    my_Assert($go_index > 0, ErrCode::user_store_NoExistGemReplace);
                    //$gemSetSlotDic->$go_index = $ins_gem;
                    $gemSetSlotDic->$replaceUId = $uid;
                }

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

        $ins_equip->gemSetSlot = $gemSetSlotDic;
        ctx()->store(true)->equip->$equipUid = $ins_equip;

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

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

        $equips = ctx()->store->equip;
        foreach ($equips as $eUid => $ins_equip) {
            $dic = $ins_equip->gemSetSlot;
            foreach ($dic as $slotId => $gemUid) {
                if ($uid == $gemUid) {
                    ctx()->store->equip->$eUid->gemSetSlot->$slotId = 0;
                    break 2;
                }
            }
        }


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

    /**
     * 6404 升级装备
     * @return type
     */
    public static function EquipUpgrade() {
        list($uid) = req()->paras; //装备uid

        my_Assert(StlUtil::dictHasProperty(ctx()->store->equip, $uid), ErrCode::user_store_NoEquip);
        $ins_equip = new Ins_Equip(ctx()->store->equip->$uid);
        my_Assert($ins_equip->level < $ins_equip->Equip_MaxLevel(), ErrCode::user_store_equipLevelLimit);

        $mo = GameConfig::equip_levelupgrade_getItem($ins_equip->mo()->rarity, $ins_equip->qual, $ins_equip->mo()->position, $ins_equip->level);
        my_Assert(null != $mo, ErrCode::err_const_no);
        my_Assert(ctx()->baseInfo->gold >= $mo->needGold, ErrCode::notenough_gold_msg);

        $costTuzhiId = GameConfig::equip_position_getItem($ins_equip->mo()->position)->costTuzhiId;
        $tuzhiNum = 0;
        if (StlUtil::dictHasProperty(ctx()->store->items, $costTuzhiId)) {
            $tuzhiNum = ctx()->store->items->$costTuzhiId;
        }
        my_Assert($tuzhiNum >= $mo->needItemNum, ErrCode::notenough_item);

        ctx()->store(true)->removeItem($costTuzhiId, $mo->needItemNum);
        ctx()->base(true)->Consume_Gold($mo->needGold);

        $ins_equip->level += 1;
        ctx()->store->equip->$uid = $ins_equip;

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

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

    /**
     * 6405 单个装备部位一键升级
     * @return type
     */
    public static function EquipUpgrade_MaxLv() {
        list($uid) = req()->paras;

        my_Assert(StlUtil::dictHasProperty(ctx()->store->equip, $uid), ErrCode::user_store_NoEquip);
        $ins_equip = new Ins_Equip(ctx()->store->equip->$uid);
        $maxLevel = $ins_equip->Equip_MaxLevel();
        my_Assert($ins_equip->level < $ins_equip->Equip_MaxLevel(), ErrCode::user_store_equipLevelLimit);

        $tuzhiId = GameConfig::equip_position_getItem($ins_equip->mo()->position)->costTuzhiId;
        $itemNum_store = 0;
        if (StlUtil::dictHasProperty(ctx()->store->items, $tuzhiId)) {
            $itemNum_store = ctx()->store->items->$tuzhiId;
        }

        $up_Gold = 0;
        $up_needItem = 0;
        $up_lv = 0;

        $noUp_gold = false;
        $noUp_item = false;
        $need_gold = 0;
        $need_item = 0;
        for ($i = $ins_equip->level; $i <= $maxLevel; $i++) {
            $up_Gold += GameConfig::equip_levelupgrade_getItem($ins_equip->mo()->rarity, $ins_equip->qual, $ins_equip->mo()->position, $i)->needGold;
            $up_needItem += GameConfig::equip_levelupgrade_getItem($ins_equip->mo()->rarity, $ins_equip->qual, $ins_equip->mo()->position, $i)->needItemNum;

            if (ctx()->baseInfo->gold >= $up_Gold) {
                $noUp_gold = true;
            }
            if ($itemNum_store >= $up_needItem) {
                $noUp_item = true;
            }
            $up_lv = $i;
            if (ctx()->baseInfo->gold >= $up_Gold && $itemNum_store >= $up_needItem) {
                $need_gold = $up_Gold;
                $need_item = $up_needItem;               
                continue;
            }
            
            break;
        }

        if ($up_lv == 0) {
            my_Assert($noUp_gold, ErrCode::notenough_gold_msg);
            my_Assert($noUp_item, ErrCode::notenough_item);
        }

        ctx()->base(true)->Consume_Gold($need_gold);
        ctx()->store(true)->removeItem($tuzhiId, $need_item);
        if ($up_lv > 0) {
            ctx()->store->equip->$uid->level = $up_lv;
        }

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

        FightProc::Ranking_FightPower();

        UserProc::updateUserInfo();
        return Resp::ok(array(
                    'gold' => ctx()->baseInfo->gold,
                    'store' => ctx()->store,
                    'task' => ctx()->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();
    static $reward_Gem = array();
    static $reward_equip = array();

    //static $reward_hero = array();

    /**
     * 具体奖励存入背包
     * @param type $goodsStr
     * @param type $src
     */
    public static function AddMultiItemInStore($goodsStr, $src = 0) {
        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 && $itemMo->itemType != 201 && $itemMo->itemType != 502 && $itemMo->itemType != 101 && $src != 1) {
                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 103://钥匙    
                case 401://启灵石
                case 301://洗练石
                case 501://人物碎片
                case 601://人身果  
                case 801://寻宝券  
                case 901:
                    self::PutItemsInStore($itemId, $num);
//                    if ($itemMo->itemType == 100) {//图纸
//                        self::checkEquipUpgradeTip();//废弃
//                    }
                    break;
                case 502://角色卡
                    HeroProc::RoleCardUnlockHero($itemId, $num);

                    break;
                case 201://宝石           
                    for ($i = 0; $i < $num; $i++) {
                        $gem = self::initGem($itemId);
                        if ($src != 1) {
                            self::$reward_Gem[] = $gem->uid;
                        }
                        self::PutGemInStore($gem);
                        if ($src == Enum_StoreSourceType::ShopBox) {
                            SystemProc::GetGem_GreaterOrangeQual_ShopBox(req()->zoneid, ctx()->baseInfo->name, $gem->typeId);
                        }
                    }

                    break;

                case 101:
                    //self::PutEquipInStore($itemId, $num);
                    for ($i = 0; $i < $num; $i++) {
                        FightProc::funUnlock_equip_firstTime();
                        
                        $equip = self::initEquip($itemId);
                        $uid = $equip->uid;
                        ctx()->store(true)->equip->$uid = $equip;                       
                        self::$reward_equip[] = $uid;
                    }
                    break;
                case 701://道具宝箱
                    for ($i = 0; $i < $num; $i++) {
                        self::DistributeItemsBox($itemId);
                    }

                    break;
                default:

                    break;
            }
        }
    }

    /**
     * 装备回存
     * @param type $itemId
     * @param type $num
     */
    public static function PutEquipInStore($itemId, $num) {
        for ($i = 0; $i < $num; $i++) {
            $equip = self::initEquip($itemId);
            $uid = $equip->uid;
            ctx()->store(true)->equip->$uid = $equip;
        }
    }

    public static function initEquip($typeId, $uid = 0) {
        if ($uid == 0) {
            $uid = ctx()->store->nextUID();
        }
        $ins_equip = new Ins_Equip();
        $ins_equip->uid = $uid;
        $ins_equip->typeId = $typeId;
        $ins_equip->qual = GameConfig::equip_getItem($typeId)->qual;
        return $ins_equip;
    }

    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) {
            $reward = self::Distribute_Rewards($itemBoxMo->contents);
            self::AddMultiItemInStore($reward);
        } else {
            self::AddMultiItemInStore($itemBoxMo->contents);
        }
    }

    public static function Distribute_Rewards($contents) {
        $strList = explode(';', $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;
                }
            }
        }
        return $reward;
    }

//    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->equipPosition;
//        if ($equipDic != null) {
//            foreach ($equipDic as $k => &$equip) {
//                $equip = new Ins_EquipPosition($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->equipPosition = $equipDic;
//    }

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

    public static function PutGemInStore($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);
//                }
//
//            }
//        }
//
//
//
//
//    }
}
