<?php
defined( 'IN_WEB') or die( 'Include Error!');
class ModelSteal {
    public $cfg;
    public $ratioPro =[
        ["id"=>1,"bet"=>0.07,"f_weight"=>0,"weight"=>75,"isRobot"=>0],
        ["id"=>2,"bet"=>0.3,"f_weight"=>20,"weight"=>0,"isRobot"=>0],
        ["id"=>3,"bet"=>0.6,"f_weight"=>40,"weight"=>25,"isRobot"=>0],
        ["id"=>4,"bet"=>0.85,"f_weight"=>20,"weight"=>0,"isRobot"=>0],
        ["id"=>5,"bet"=>1,"f_weight"=>20,"weight"=>0,"isRobot"=>0],
    ];
    public function __construct()
    {
        $this->cfg = oo::cfg('stealattack')['steal'];
    }

    /**
     * Notes:绑定
     * User: wsc
     * Time: 2020/6/28 11:02
     * @param $uid
     * @param $level
     * @return array|mixed
     */
    public function getBindKing($uid,$level){
        $rk = okeys::richestKing($uid);
        $richestKingJson = oo::commonOprRedis('server')->get($rk);
        if(!empty($richestKingJson)){
            $king =json_decode($richestKingJson,true);
            return $king;
        }
        $king = $this->getRichestKing($uid,$level);
        if(empty($king)){
            return [];
        }
        $res = oo::commonOprRedis('server')->setex($rk,json_encode($king),oo::redisRandomExpire(86400));
        oo::commonOprRedis('server')->sAdd(okeys::richestKingTo($king['uid']),$uid);
        $info = oo::commonOprModel('member')->getUserAssetsInfo($king['uid']);//被偷用户当前货币信息
        $this->toOutQueue($king['uid'],$king['levelId'],$info['money']);//尝试出队
        return $king;
    }

    /**
     * Notes:解绑
     * User: wsc
     * Time: 2020/6/28 11:02
     * @param $uid
     * @return bool
     */
    public function clearBindKing($uid){
        $rk = okeys::richestKing($uid);
        $richestKingJson = oo::commonOprRedis('server')->get($rk);
        if(!empty($richestKingJson)){
            $king =json_decode($richestKingJson,true);
            oo::commonOprRedis('server')->delete($rk);
            oo::commonOprRedis('server')->sRemove(okeys::richestKingTo($king['uid']),$uid);
//            oo::commonOprRedis('usercache')->delete(okeys::richestKingCoin($uid,$king['uid']));//删除绑定的钱及倍率(old)
            oo::commonOprRedis('usercache')->delete(okeys::richestKingInfo($uid));//其他人绑定的king信息(new)
        }
        //以防未删除king的详细信息
        $king = oo::commonOprRedis('usercache')->hGetAll(okeys::richestKingInfo($uid));
        if(!empty($king)){
            oo::commonOprRedis('server')->sRemove(okeys::richestKingTo($king['toUid']),$uid);
            oo::commonOprRedis('usercache')->delete(okeys::richestKingInfo($uid));//其他人绑定的king信息(new)
        }

        return true;
    }

    /**
     * Notes:清理与$toUid的绑定关系
     * User: wsc
     * Time: 2020/6/28 12:57
     * @param $toUid
     * @param int $uid 需要排除的用户
     */
    public function clearOtherBind($toUid,$uid=0){
       $kingTokey =  okeys::richestKingTo($toUid);
       $arr =  oo::commonOprRedis('server')->sMembers($kingTokey);
       if(!empty($arr)){
           foreach ($arr as $v){
               if($v!=$uid){
                   oo::commonOprRedis('usercache')->delete(okeys::richestKingInfo($v));//其他人绑定的king信息(new)
                   $multiple = oo::commonOprRedis('usercache')->get(okeys::curUserBet($v));
                   oo::commonOprModel('user')->getRichestKing($v,$multiple,true);//从刷king,
               }
           }
       }
    }
    /**
     * Notes:获取richestkong
     * User: wsc
     * Time: 2020/6/24 18:33
     * @param $uid  -用户id
     * @param $level -uid等级
     * @param bool $onlyRand -是否仅来自全集合
     * @return array|mixed ["uid"=>66,"money"=>666]
     */
    public function getRichestKing($uid,$level,$onlyRand=false){
        $king = $this->getFriend($uid);
        if(!empty($king)){
            $money = $this->isPassMoney($level,$king,1);
            if(intval($money)>100000){
                $king['money'] = $money;
            }else{
                $king = [];
            }
        }
        if(empty($king)||$onlyRand){//优先好友
            $actionLevel = oo::getActionLevel('steal',$level);
            $king = [];
            for ($i=$actionLevel['max'];$i>=$actionLevel['min'];$i--){
                $key = akey::userLevelStealList($i);
                $one = oo::commonOprRedis('server')->zRevRange($key,0,1,true);
                if(!empty($one)){
                    foreach ($one as $k=>$v){
                        if((!isset($king['money'])||intval($king['money'])<$v)&&$k!=$uid){
                            $king['money'] = $v;
                            $king['uid'] = $k;
                            $king['levelId']=$i;
                        }
                    }
                }
            }
            $money = $this->isPassMoney($level,$king);
            if(intval($money)<100000){//若不满足1.8.6版本新的最低金币要求
                $king = [];
            }else{
                $king['money'] = $money;
            }
        }
        return $king;
    }
    public function getRichestKing2($uid,$level,$onlyRand=false){
        $king = $this->getFriend($uid);
        if(empty($king)||$onlyRand){//优先好友
            $actionLevel = oo::getActionLevel('steal',$level);
            $newKey = akey::newStealList($level);
            for ($i=$actionLevel['max'];$i>=$actionLevel['min'];$i--){
                $keyList[] = akey::userLevelStealList($i);
                $keyqList[] = 1;
            }
            $num = oo::commonOprRedis('server')->zUnion($newKey,$keyList,$keyqList,"min");
            if($num<=0){
                return [];
            }
            $kings = oo::commonOprRedis('server')->zRevRange($newKey,0,2,true);
            oo::commonOprRedis('server')->delete($newKey);
            if(!empty($kings)&&is_array($kings)){
                foreach ($kings as $k=>$v){
                    if($k!=$uid){
                        $info = oo::commonOprModel('member')->getUserAssetsInfo($k);
                        $king=["uid"=>$k,"money"=>$v,"levelId"=>$info['levelId']];
                        break;
                    }
                }
            }
        }

        return $king;
    }

    /**
     * Notes:获取满足条件的好友
     * User: wsc
     * Time: 2020/6/24 18:35
     * @param $uid
     * @return array|mixed
     */
    public function getFriend($uid){
       $friendlist =  oo::commonOprModel('friend')->getUserAllFriend($uid);
       foreach ($friendlist as $k=>$v){
           $res = $this->verifyIsPass($v,1);
           if($res>0){
               $minLv = max(intval(oo::cfg('stealattack')['steal']['minLv']),4);
               $info = oo::commonOprModel('member')->getUserAssetsInfo($v);
               if(!isset($info['levelId'])||empty($info['levelId'])||$info['levelId']<$minLv){
                   continue;
               }
               $minMoney = oo::commonOprModel('level')->getLevelConf($info['levelId']);
               if($info['money']<intval($minMoney['minMoney'])*0.5){//是否达到可被偷的钱数标准
                   continue;
               }
               $data[] = ["uid"=>$v,"money"=>$info['money'],"levelId"=>$info['levelId']];
           }
       }
       if(!empty($data)){
            array_multisort(array_column($data,'money'),SORT_DESC,$data);
           return $data[0];
       }
       return [];
    }
    /**
     * Notes:入队
     * User: wsc
     * Time: 2020/6/19 11:40
     * @param $uid
     * @param $money
     * @return bool
     */
    public function toJoinQueue($uid,$level,$money){
        $pass  =$this->verifyIsPass($uid);
        if($pass<0){
            $this->joinBTFQueue($uid,$level,$money);//入备选池
            return false;
        }
        if($level<=$this->cfg['minLv']){//是否小于最小可偷等级
            return false;
        }
        oo::commonOprRedis('server')->zDelete(akey::userLevelStealListBTF($level),$uid);//备选池清理
        $minMoney = $this->joinMinMoney($level);//oo::commonOprModel('level')->getLevelConf($level);
        if($money<$minMoney){//是否达到可被偷的钱数标准
            return false;
        }
        oo::commonOprRedis('server')->zAdd(akey::userLevelStealList($level),intval($money),$uid);//入队

        return true;
    }

    /**
     * Notes:出队
     * User: wsc
     * Time: 2020/6/28 12:18
     * @param $uid
     * @param $level
     * @param $money
     * @return bool
     */
    public function toOutQueue($uid,$level,$money){
        $minMoney = $this->joinMinMoney($level);//oo::commonOprModel('level')->getLevelConf($level);
        $res1 = $money<$minMoney?true:false;//当前剩余钱是否满足出队条件
        $queueMoney = intval(oo::commonOprRedis('server')->zScore(akey::userLevelStealList($level),$uid));
        if($queueMoney/10>$money){
            oo::commonOprRedis('server')->zDelete(akey::userLevelStealList($level),$uid);//出队主池
            $this->joinBTFQueue($uid,$level,$money);//进入备选
            $bindUlist =oo::commonOprRedis('server')->sMembers(okeys::richestKingTo($uid));
            if (!empty($bindUlist)) foreach ($bindUlist as $buid){ oo::commonOprModel('user')->getRichestKing($buid,0,true);}
        }
        if($res1){
            oo::commonOprRedis('server')->zDelete(akey::userLevelStealList($level),$uid);//出队主池
            oo::commonOprRedis('server')->zDelete(akey::userLevelStealListBTF($level),$uid);//出队备选池
            return true;
        }
        $res2 = $this->verifyIsPass($uid);
        if($res2<0){//当前状态是否满足出队条件
            oo::commonOprRedis('server')->zDelete(akey::userLevelStealList($level),$uid);//出队
            $this->joinBTFQueue($uid,$level,$money);//进入备选
            return true;
        }
        return false;
    }

    /**
     * Notes:入备选池
     * User: wsc
     * Time: 2020/6/28 15:48
     * @param $uid
     * @param $level
     * @param $money
     * @return bool
     */
    public function joinBTFQueue($uid,$level,$money){
        $minMoney = $this->joinMinMoney($level);//oo::commonOprModel('level')->getLevelConf($level);
        if($money<$minMoney||$level<=$this->cfg['minLv']){//是否达到可被偷的钱数标准,或小于最小可偷等级
            return false;
        }
        oo::commonOprRedis('server')->zAdd(akey::userLevelStealListBTF($level),intval($money),$uid);//入队备选池
    }

    /**
     * Notes:双池出队
     * User: wsc
     * Time: 2020/7/4 14:55
     * @param $uid
     * @param $level
     */
    public function outAllQueue($uid,$level){
        if($level>0){
            oo::commonOprRedis('server')->zDelete(akey::userLevelStealList($level),$uid);//偷选池出队
            oo::commonOprRedis('server')->zDelete(akey::userLevelStealListBTF($level),$uid);//偷备选池出队
        }
    }
    /**
     * Notes:定时备选入队
     * User: wsc
     * Time: 2020/6/28 16:23
     * @return bool
     */
    public function cronToJoinQueue(){
        $maxNum = intval(oo::commonOprModel('readconfig')->getCon('basic','curMaxLevel'));
        for($lv=$this->cfg['minLv'];$lv<$maxNum;$lv++){
            $bkey = akey::userLevelStealListBTF($lv);
            $list = oo::commonOprRedis('server')->zRevRange($bkey,0,-1,true);
            if(!empty($list)&&is_array($list)){
                foreach ($list as $k=>$v){
                    $info = oo::commonOprModel('member')->getUserAssetsInfo($k);
                    if($info['levelId']!=$lv){
                        $this->outAllQueue($k,$lv);
                    }
                    $this->toJoinQueue($k,$info['levelId'],$info['money']);//尝试入队
                }
            }
        }
        return true;
    }

    /**
     * Notes:是否满足入队条件
     * User: wsc
     * Time: 2020/6/19 11:41
     * @param $uid
     * @return bool
     */
    public function verifyIsPass($uid,$f=0){
        $nowDay = date("Ymd",time());
        $time = oo::commonOprModel('activitynew')->getEndproteTime($uid, $nowDay);
        if(!empty($time)&&$time>time()){//在保护期
            return -1;
        }
        $beStealNum = oo::commonOprRedis('server')->hGet(akey::beStealNumHashKey($nowDay),$uid);
        if($f==1){//好友额外10次
            $this->cfg['maxLimit'] = $this->cfg['maxLimit']+10;
        }
        if($beStealNum>=$this->cfg['maxLimit']){//已达被偷最大次数
            return -2;
        }
        $bindNum = oo::commonOprRedis('server')->sSize(okeys::richestKingTo($uid));
        $maxNum = intval(oo::commonOprModel('readconfig')->getCon('richestKing','MaxNum'));
        if($bindNum>=$maxNum){//超过最大绑定数
            return -3;
        }

        $result = oo::commonOprRedis('common')->get(okeys::beStealLock($uid));
        if($result){//是否被人锁定了
            return -4;
        }

        return 1;
    }
    //清理已绑定的richking
    public function outRichking($uid){
        $level = oo::commonOprModel('member')->getUserLevelId($uid);
        oo::commonOprRedis('server')->zDelete(akey::userLevelStealList($level),$uid);//出队主池
        $bindUlist =oo::commonOprRedis('server')->sMembers(okeys::richestKingTo($uid));
        if (!empty($bindUlist)) foreach ($bindUlist as $buid){ oo::commonOprModel('user')->getRichestKing($buid,0,true);}
    }
    //入队最少金币数
    public function joinMinMoney($lv){
       $levelInfo =  oo::commonOprModel('level')->getLevelConf($lv);
//       $money = intval($levelInfo['minMoney']);
       if($lv>=200){
           $money = intval($levelInfo['minMoney'])*0.2;
       }else if($lv>=101){
           $money = intval($levelInfo['minMoney'])*0.5;
       }else if($lv>=51){
           $money = intval($levelInfo['minMoney'])*0.8;
       }else{
           $money = intval($levelInfo['minMoney']);
       }
       return $money;
    }
    /**
     * Notes:设置保护时间
     * User: wsc
     * Time: 2020/6/19 14:34
     * @param $uid
     * @param $time
     * @return bool
     */
    public function setProteTime($uid,$time){
        $now = time();
        $nowDay = date("Ymd",$now);
        $oldTime = oo::commonOprModel('activitynew')->getEndproteTime($uid, $nowDay);
        if($oldTime>$now&&$time<($oldTime-$now)){//不做处理
//            $ovTime = 0;
            return true;
        }else{
            $ovTime = $now+$time;
        }
        oo::async()->add("oo::commonOprModel('steal')->outRichking",[$uid]);//异步推送更换king,加速响应
        $newDay = date("Ymd",$ovTime);//要设置的时间
        if($newDay>$nowDay){//跨天
            oo::commonOprModel('activitynew')->upEndproteTime($uid, $newDay, $ovTime);
            $key = oo::commonOprModel('activitynew')->getEndproteTimeKey($uid, $newDay);
            $nlen = oo::commonOprRedis('server')->hLen($key);
            if($nlen<2){//第一次设置过期，减少数据堆积
                oo::commonOprModel('activitynew')->upEndproteTimeExpire($uid, $newDay, strtotime($newDay)+86400-1);
            }
        }
        $res = oo::commonOprModel('activitynew')->upEndproteTime($uid, $nowDay, $ovTime);
        $key = oo::commonOprModel('activitynew')->getEndproteTimeKey($uid, $nowDay);
        $len = oo::commonOprRedis('server')->hLen($key);
        if($len<2){//第一次设置过期，减少数据堆积
            oo::commonOprModel('activitynew')->upEndproteTimeExpire($uid, $nowDay, strtotime($nowDay)+86400-1);
        }
        $info = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        $this->toOutQueue($uid,$info['levelId'],$info['money']);
        return $res;
    }

    /**
     * Notes:设置被偷次数
     * User: wsc
     * Time: 2020/6/19 15:45
     * @param $uid
     * @return int
     */
    public function setBeStealNum($uid){
        $nowDay = date("Ymd",time());
        $num = oo::commonOprRedis('server')->hIncrBy(akey::beStealNumHashKey($nowDay),$uid,1);
        $len = oo::commonOprRedis('server')->hLen(akey::beStealNumHashKey($nowDay));
        if($len<2){//第一次设置过期，减少数据堆积
            oo::commonOprRedis('server')->expireAt(akey::beStealNumHashKey($nowDay),oo::redisRandomExpire(strtotime($nowDay)+86400-1));
        }
        return $num;
    }
//========================================================================================
    /**
     * Notes:获取偷取对象数据
     * User: wsc
     * Time: 2021/2/23 18:22
     * @param $uid
     * @return array ["toUid"=>"273230","nick"=>"Vita","s_picture"=>"https://...","u_flag"=>1,"vip"=>0,"vipLv"=>0,"vipExpire"=>0,"avatar"=>0,"coins"=>568000]
     */
    public function getStealData($uid){
        $dataInfo= [];
        $key = okeys::richestKingInfo($uid);
        $info = oo::commonOprRedis('usercache')->hGetAll($key);
        if(!empty($info)){
            return $info;
        }
//        $bet = oo::commonOprRedis('usercache')->get(okeys::curUserBet($uid)) ?? 1;
        $userInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        $kingInfo = $this->getBindKing($uid,$userInfo['levelId']);
        $king  = isset($kingInfo['uid'])?$kingInfo['uid']:'';
        if(!$king){
            $robot = oo::commonOprModel('robot')->getAllRobot();
            $king = $robot[array_rand($robot,1)];
//            $coinConfig = oo::commonOprModel('level')->getCoinConfig(min($userInfo['levelId'],50));
//            $coinConfig = json_decode($coinConfig,true);
            $randMoney = $userInfo['levelId']>9?mt_rand(50000,150000):mt_rand(5000,50000);
            $money = $this->getMaxRaticMoney($userInfo['levelId'])*0.07+$randMoney;
            $money = oo::Thousand($money);
            $isrobot = 1;
        }else{
//            $money = $this->getStealMoney($uid,$kingInfo['money'],$king,$userInfo['levelId']);
            $money = oo::Thousand($kingInfo['money']);
            $isrobot =0;
        }
        $dataInfo = oo::commonOprModel('member')->getUserLevel($king);
        if($isrobot){
            $dataInfo['levelId'] =  max(3,$userInfo['levelId']+mt_rand(-3,5));
            for($i=0;$i<5;$i++){
                $r1 = mt_rand(0,4);
                $r2 = mt_rand($r1,4);
                $r3= $r2!=$r1?1:0;
                $element[] = ["id"=>$i+1,"level"=>$r1,"maxLevel"=>$r2,"status"=>$r3];
            }
            $dataInfo['element'] = json_encode($element);
        }else{
            $dataInfo['element'] =json_encode($dataInfo['element']);
        }

        $userInfoKing = oo::commonOprModel('member')->getUserInfo($king);
        $dataInfo['toUid']   = $king;
        $dataInfo['nick']    = oo::commonOprModel('member')->getUserFirstName($userInfoKing['nick']) ?? 'crazyKing';
        $dataInfo['s_picture'] = $userInfoKing['s_picture'];
        $dataInfo['u_flag']  = $userInfoKing['u_flag'];
        $dataInfo['vip']     = $userInfoKing['vip'];
        $dataInfo['vipLv']   = $userInfoKing['vipLv'];
        $dataInfo['vipExpire']    = $userInfoKing['vip_expire'];
        $dataInfo['avatar']  = intval($userInfoKing['avatar']);
        $dataInfo['coins']   = $money;
        $dataInfo['isrobot'] = $isrobot;
        oo::commonOprRedis('usercache')->hMset($key,$dataInfo);
        oo::commonOprRedis('usercache')->expire($key,3*3600);
        return $dataInfo;
    }

    /**
     * Notes:获取实际可偷总钱(弃用)
     * User: wsc
     * Time: 2021/2/5 18:34
     * @param $uid
     * @param $kingMoney
     * @param $king
     * @param $levelId
     * @return int
     */
    public function getStealMoney($uid,$kingMoney,$king,$levelId){
        $toUserLevelInfo = oo::commonOprModel('member')->getUserLevel($king);
        $levelCfg =  oo::commonOprModel('level')->getLevelConf($levelId);
        $s =   $levelId-$toUserLevelInfo['levelId'];
        if($s>=0){
            $a=1;
        }else if($s>-10){
            $a=1/(abs($s));
        }else{
            $a=0.1;
        }
        $money = $kingMoney*0.75*$a;
        $friendlist =  oo::commonOprModel('friend')->getUserAllFriend($uid);
        if(!in_array($king,$friendlist)){
            $money = 0.5*$money;
        }
        $rand_money = mt_rand(1,50)*1000;
        if($levelId<=30){
            if($money>$levelCfg['sumMoney']/10){
                $money = $levelCfg['sumMoney']/10-$rand_money;
            }
        }else if ($levelId<=100){
            $nlv = $levelId-20;
            if($money>$levelCfg['sumMoney']/$nlv){
                $money = $levelCfg['sumMoney']/$nlv-$rand_money;
            }
        }else if ($levelId<=120){
            $nlv = $levelId*5-420;
            if($money>$levelCfg['sumMoney']/$nlv){
                $money = $levelCfg['sumMoney']/$nlv-$rand_money;
            }
        }else if ($levelId<=200){
            $nlv = $levelId*10-1025;
            if($money>$levelCfg['sumMoney']/$nlv){
                $money = $levelCfg['sumMoney']/$nlv-$rand_money;
            }
        }else{
            $nlv = $levelId*20-3025;
            if($money>$levelCfg['sumMoney']/$nlv){
                $money = $levelCfg['sumMoney']/$nlv-$rand_money;
            }
        }

        return intval($money);
    }

    /**
     * Notes:陌生人rachking 的钱是否满足被偷条件/ 好友钱处理
     * User: wsc
     * Time: 2021/4/20 17:22
     * @param $levelId
     * @param $kingInfo
     * @return bool|float|int
     */
    public function isPassMoney($levelId,$kingInfo,$isfriend=0){
        $ratioProCfgJson = oo::commonOprRedis('config')->get(okeys::stealRaticProCfg());
        $ratioProCfg = $ratioProCfgJson?json_decode($ratioProCfgJson,true):'';
        $ratioProCfg = !empty($ratioProCfg)?$ratioProCfg:$this->ratioPro;
        if(!$isfriend){
            $ratioPro = array_column($ratioProCfg,'weight','id');
            $id = funs::get_randByProb($ratioPro);
            $ratioCfg =array_column($ratioProCfg,null,'id');
            if($ratioCfg[$id]['isRobot']){
                return 0;
            }
            $ratio =$ratioCfg[$id]['bet']?$ratioCfg[$id]['bet']:1;
        }else{
            $ratioPro = array_column($ratioProCfg,'f_weight','id');
            $id = funs::get_randByProb($ratioPro);
            $ratioCfg =array_column($ratioProCfg,null,'id');
            $ratio =$ratioCfg[$id]['bet']?$ratioCfg[$id]['bet']:1;
        }

        $money = $kingInfo['money']*0.75;
        $rn = 100;
        if($levelId<9){
            $rn=5;
        }
        $MaxRaticMoney = $this->getMaxRaticMoney($levelId);
        if(!$isfriend){//非好友
            if($money<$MaxRaticMoney*$ratio){
                return 0;
            }else{
                $MaxRaticMoney =  $MaxRaticMoney*$ratio;
            }
        }else{
            if($money<=$MaxRaticMoney*0.07){
                return 0;
            }
            else if($money>$MaxRaticMoney){//rachking的全部*0.75 大于$MaxRaticMoney
                $MaxRaticMoney = $MaxRaticMoney*$ratio;
            }
            else{
                $rn=1;
                $MaxRaticMoney = $money;
            }
        }
        $randMoney = mt_rand(1,$rn)*1000;
        $lastMoney= $MaxRaticMoney-$randMoney>0?$MaxRaticMoney-$randMoney:$MaxRaticMoney;
        return $lastMoney;
    }
    public function getMaxRaticMoney($levelId){
//        $cfg = oo::cfg("stealattack")["stealMaxMoney"];
        $key =okeys::stealMaxMoney();
        $maxMoney=oo::commonOprRedis('common')->hGet($key,$levelId);
        if(!$maxMoney){
            $tb = otable::commonConfig();
            $sql = "SELECT * FROM {$tb} where  type='stealMaxMoney'";
            $conf = oo::commonOprDb('config')->getOne($sql,1);
            if(!empty($conf)){
                $confArr =json_decode($conf['conf']);
                $confArr2= array_column($confArr,'maxCoin','levelId');
                oo::commonOprRedis('common')->hMset($key,$confArr2);
                $maxMoney = $confArr2[$levelId];
            }
        }
        return $maxMoney;
    }
    public function getMaxRaticMoney_old($levelId){
        $levelCfg =  oo::commonOprModel('level')->getLevelConf($levelId);
        if($levelId<=8){
            $nlv= $levelId+2;
        }
        else if($levelId<=30){
            $nlv = 10;
        }else if ($levelId<=100){
            $nlv = $levelId-20;
        }else if ($levelId<=120){
            $nlv = $levelId*5-420;
        }else if ($levelId<=200){
            $nlv = $levelId*10-1025;
        }else{
            $nlv = $levelId*20-3025;
        }
        $MaxRaticMoney = $levelCfg['sumMoney']/$nlv;//金币上限
        return $MaxRaticMoney;
    }

    public function toSteal($uid,$levelId,$bet){
        $data = [];
        $bet = max($bet,1);
        $kingInfoBash =$kingInfo = $this->getStealData($uid);
        $kingInfoBash['element'] = $kingInfo['element'] = json_decode($kingInfo['element'],true);
        $toUid = $kingInfo['toUid'];
        $money = intval($kingInfo['coins']);
        //临时偷锁(用作极端情况同偷一人时是否真偷的依据)
        $lockRes = oo::commonOprRedis('common')->setnx(okeys::beStealLock($toUid),$uid);
        oo::commonOprRedis('common')->expire(okeys::beStealLock($toUid),300);
        $kingInfo['lockRes'] = $lockRes;
        //通知其他人更换king
        if(!$kingInfo['isrobot']){
            $this->clearOtherBind($toUid,$uid);
        }
        //获取坑位奖励信息
        $stealDataInfo = $this->getOptMoney($uid,$money,$bet,$levelId);//获取坑位金币分布
        $stealData = $stealDataInfo[0];
        $isPerfect=intval($stealData[2])>0?1:0;//是否完美偷
        $petStealInfo = oo::commonOprModel('pet')->usePetSkill($uid,1,array_sum($stealData) * $bet);
        $optInfo = $this->getOptInfo($uid,$levelId,$stealData,$petStealInfo,$bet);//获取坑位详情(包括金币及宝箱,空,宠物加成)
        $kingInfo['optInfo'] = $optInfo;
        $kingInfo['steal_result'] = $stealDataInfo[1];
        //发奖
        $data = $this->sendStealReward($uid,$kingInfo);
        $data['kingInfo']  = $kingInfoBash;
        //返回奖励信息组装
        $data['petCoinsPercent'] = $petStealInfo[1]?$petStealInfo[1]:0;//宠物加成率
        $data['optInfo'] = $optInfo;
        $data['isPerfect'] = $isPerfect;
        return $data;
    }

    /**
     * Notes:发放偷奖励
     * User: wsc
     * Time: 2021/2/26 15:27
     * @param $uid
     * @param $kingInfo
     * @return mixed
     */
    public function sendStealReward($uid,$kingInfo){
        $sumStealMoney = 0;//已乘倍率的偷取金币总数
        $sumPetStealMoney =0;//已乘倍率的宠物偷取金币总数
        $isPerfect =1;//是否完美偷
        $treasureType=0;//宝箱类型
        $toUid = $kingInfo['toUid'];
        $optInfo  = $kingInfo['optInfo'];
        foreach ($optInfo as $k=>$v){
            $sumStealMoney += $v['coins'];
            $sumPetStealMoney+=$v['pet'];
            $bet = max($v['bet'],1);
            if($v['coins']==0){
                $isPerfect=0;
            }
            if(intval($v['treasureType'])>0){
                $treasureType = intval($v['treasureType']);
            }
        }
        $basicsStealMoney=$sumStealMoney/$bet;//人偷基数
        $basicsPetStealMoney=$sumPetStealMoney/$bet;//宠物偷基数
        $steal_num = $basicsStealMoney+$basicsPetStealMoney;//被偷总基数
        $userGetCoinNum = $sumStealMoney+$sumPetStealMoney;//用户总获得金币数
        $data['haveMinePlace'] = 0;//有雷的坑位
        //--金币发放--
        if($kingInfo['isrobot'] == 1){
            oo::commonOprModel('statistics')->addWarning($uid,"返回RichestKing数据失败，追加机器人：{$toUid} 金币 {$steal_num}",0,$steal_num);
            oo::commonOprModel('member')->optProperty($uid,array('money'=>$userGetCoinNum),'+',9,"机器人补发:偷到用户:{$toUid}的金币,基数:{$steal_num},宠物:{$basicsPetStealMoney},bet:{$bet}");
            $systemPay = true;
        }else{
            $ttl = oo::commonOprModel('activitynew')->getEndproteTime($toUid)-time();
            if($ttl > 0||!$kingInfo['lockRes']){
                oo::commonOprModel('member')->optProperty($uid,array('money'=>$userGetCoinNum),'+',10,"偷到用户:{$toUid}的金币,用户被保护,系统发放");
                $systemPay = true;
            }else{
                $userMoney = oo::commonOprModel('member')->getUserAssetsInfo($toUid)['money'];
                $n=intval($sumPetStealMoney)>0?0.85:0.75;
                $canBeSteal = ceil($userMoney*$n);//用户可被偷的最大钱数
                if($canBeSteal>$steal_num){//用户钱够被偷
                    $beStealMoney =$steal_num;//实际被偷
                    $petSteal = $basicsPetStealMoney;
                    $personSteal = $basicsStealMoney;
                }else{//用户钱不够被偷
                    $beStealMoney =$canBeSteal;//实际被偷
                    $petSteal = 0;
                    $personSteal = $canBeSteal;
                }
                oo::commonOprModel('member')->optProperty($toUid,array('money'=>$beStealMoney),'-',7,"被用户:{$uid}偷取金币,被用户偷了{$personSteal},宠物偷了{$petSteal}");
                oo::commonOprModel('member')->optProperty($uid,array('money'=>$beStealMoney),'+',4,"偷到用户:{$toUid}的金币");
                //其余部分系统补
                $fromSys = $userGetCoinNum -$beStealMoney;

                if($fromSys>0){
                    oo::commonOprModel('member')->optProperty($uid,array('money'=>$fromSys),'+',10,"倍率偷取:系统补发（当前倍率：{$bet}，偷到用户{$beStealMoney},宠物偷了{$basicsPetStealMoney}，系统补发{$fromSys},实际发放{$userGetCoinNum}）");
                }
                //被偷者大于1.8.5且有雷,返回雷所在坑位
                $toVersion = oo::commonOprModel('member')->getUserInfo($toUid)['version'];
                if(oo::compareVersion($toVersion,'1.8.5')){
                    $aid = oo::commonOprModel('activitynew')->activityLock($toUid,'change_collect');
                    if($aid){
                        $toUidItemInfo = oo::commonOprRedis('activity')->hGetAll(okeys::activityUserCollectChangeInfo($toUid,$aid));
                        $data['haveMinePlace'] =  intval($toUidItemInfo['place']);
                    }
                }

            }
        }
        // VIP 加成
        $stealCoinsPro = oo::commonOprModel('newvip')->vipRewardConf($uid,'stealCoinsPro');
        $data['vipCoins']= $vipCoins = $sumStealMoney* $stealCoinsPro;
        $vipCoins > 0 && oo::commonOprModel('member')->optProperty($uid,['money'=>$vipCoins],'+',353,"VIP加成：偷取 {$toUid}");
        //金币爆炸活动加成
        $bet2 = oo::commonOprModel('activitynew')->getCoinBlastBetByType($uid,'steal');
        $data['coin_blast_Coins'] = $basicsStealMoney*$bet*$bet2;
        if($bet2>0){
            oo::commonOprModel('member')->optProperty($uid,array('money'=>$data['coin_blast_Coins']),'+',219,"偷取 {$toUid}");
        }

        //--宝箱发放--
        if($treasureType>0){
            oo::commonOprModel('member')->sendReward($uid,$treasureType,1,10,'偷挖坑出宝箱');
        }
        //--活动类--
        //通行证
        $data['trafficPermit'] = oo::commonOprModel('activitynew')->getUserActData($uid,'traffic_permit');
        oo::commonOprModel('activitynew')->addTrafficPermitNum($uid,3,$bet);//通行证加经验
        if($sumPetStealMoney>0){//存在宠物参与偷
            //通行证任务5
            oo::commonOprModel('activitynew')->addTrafficPermitNum($uid,5,$bet);
            //宠物参与统计
            $flag = (int)oo::commonOprRedis('statistics')->get(okeys::participationPetKey($uid, 1));
            oo::commonOprModel('currency')->participation($uid, 'steal', $flag);
        }

        //超级收集
        $ts=$isPerfect?1:2;
        $retData=oo::commonOprModel('activitynew')->setRateByDo($uid,'steal',$ts,$bet);
        if(oo::commonOprModel('activitynew')->activityLock($uid,'super_collect')){
            $symbol = oo::commonOprModel('activitynew')->getUserActData($uid,'super_collect');
        }

        $data['symbol'] = $symbol;
//        $data['stealAttack'] =  oo::commonOprModel('activitynew')->getUserActData($uid,'steal');
        if(isset($retData["rankCollectNum"])&&$retData["rankCollectNum"]>0){
            $data['AcRankData'] =  oo::commonOprModel('activitynew')->getUserActData($uid,'rank');
            $data['AcRankNoProData'] =  oo::commonOprModel('activitynew')->getUserActData($uid,'rank2');
        }


        //--清理各类绑定信息--
        oo::commonOprRedis('common')->delete(okeys::beStealLock($toUid));//同偷一人极端情况锁
        if($kingInfo['isrobot'] == 1){
            oo::commonOprRedis('usercache')->delete(okeys::richestKingInfo($uid));//其他人绑定的king信息(new)
        }else{
            $this->clearBindKing($uid);//清除$uid与$toUid所有绑定关系,及绑定的用户信息
        }


        //--关联被偷用户处理--
        if(!$systemPay){
            //仇人列表追加
            oo::commonOprRedis('common')->zAdd(okeys::NewEnemyUidList($toUid),time(),$uid);
            //推消息
            $toUidInfo = oo::commonOprModel('member')->getUserAssetsInfo($toUid); //被偷用户资产
            $uidInfo = oo::commonOprModel('member')->getUserInfo($uid);
            $nick = oo::commonOprModel('member')->getUserFirstName($uidInfo['nick']);
            //挖雷奖励信息
            $data['preMineReward']=0;
            if(oo::compareVersion($uidInfo['version'],'1.8.5')&&$toUidItemInfo['spareNum']>0){
                $data['preMineReward']  =oo::commonOprModel('activitynew')->getCoinRewardByTypeAndLevel('change_collect',$toUidInfo['levelId']);
            }
            //被偷用户是否在线(发在线消息,设置保护时间)
            $isOnline = oo::commonOprModel('Workerman')->isUidOnline($toUid);
            if($isOnline){
                $nowDay = date("Ymd",time());
                $stealLimit= oo::commonOprRedis('server')->hGet(akey::beStealNumHashKey($nowDay),$uid);//当日被偷次数
                if($stealLimit>$this->cfg['proteCfg']['changeProteTimeLimit']){//被偷5次保护时间加长
                    $times = $this->cfg['proteCfg']['changeOnlineProteTime']?$this->cfg['proteCfg']['changeOnlineProteTime']:720*60;
                }else{
                    $times = $this->cfg['proteCfg']['onlineProteTime']?$this->cfg['proteCfg']['onlineProteTime']:30*60;
                }
                oo::commonOprModel('member')->setUserStealProtect($toUid,$times,true);

                oo::commonOprModel('Workerman')->push($toUid,ocmd::$SVR_SELF_RAID_MESSAGE,[
                    'nick' =>$nick,
                    'vip'  =>$uidInfo['vip'],
                    'vipLv'=>$uidInfo['vipLv'],
                    'vipExpire'=>$uidInfo['vip_expire'],
                    'avatar'=>intval($uidInfo['avatar']),
                    'u_flag'=>$uidInfo['u_flag'],
                    'url'=>$uidInfo['s_picture'],
                    'coins'=>intval($beStealMoney),
                    'selfCoin'=>intval($toUidInfo['money']),
                    'petCoin'=>intval($basicsPetStealMoney)
                ],true);
            }else{
                $userKinfoInfo = oo::commonOprModel('member')->getUserInfo($uid);//被偷玩家基本信息
                $longTime = $this->cfg['proteCfg']['longNoOnlineTime']?$this->cfg['proteCfg']['longNoOnlineTime']:2880 * 60;
                if(time() - $userKinfoInfo['lasttime'] > $longTime){//长时间不在线
                    $times = $this->cfg['proteCfg']['longNoOnlineProteTime']?$this->cfg['proteCfg']['longNoOnlineProteTime']:23*60*60;
                }else{
                    $times = $this->cfg['proteCfg']['noOnlineProteTime']?$this->cfg['proteCfg']['noOnlineProteTime']:120*60;
                }
                oo::commonOprModel('member')->setUserStealProtect($toUid,$times,true);
            }
            oo::commonOprModel('member')->setUserStealProtect($uid,60);//偷别人保护一下自己

            oo::commonOprModel('push')->pushNews($toUid,'push.title.steal','push.content.steal', [$nick,funs::CoinFormat($beStealMoney)],'steal');
            oo::commonOprModel('message')->addMessage($toUid,$uid,$nick,$uidInfo['s_picture'],$beStealMoney,1,0,[1=>$basicsPetStealMoney]);
            //触发挖雷
            if(!oo::compareVersion($uidInfo['version'],'1.8.5')){//挖坑者小于1.8.5版本,自动100%触发
                oo::commonOprModel('activitynew')->sendItemReward($uid,$toUid,0,1);
            }
        }

        //记录类
        oo::commonOprModel('statistics')->attackStealSta('steal',$steal_num,$sumStealMoney,$uid);//打偷统计
        oo::commonOprRedis('User')->setex(okeys::LastStealCoinsNum($uid),$sumStealMoney,2*60*60);//用作广告加倍
        $data['petGetExp']=intval(oo::commonOprModel('pet')->addUserPetExpBy($uid,1,"stealAddExp",$bet,122,"偷宠物触发"));//宠物经验
        //数数事件-数据组装-完成偷
        $isFriend = oo::commonOprModel('friend')->isfriend($uid,$toUid);
        $petInfo = oo::commonOprModel('pet')->getUserPet($uid,1);
        $steal_pet_remain_time =intval($petInfo['feedEffecteTime'])>time()?$petInfo['feedEffecteTime']-time():0;
        $eventData=[
            "steal_target_user"=>$toUid,
            "steal_relation"=>$kingInfo['isrobot']?1:($isFriend?3:2),
            "steal_result"=>$kingInfo['steal_result'].",".$treasureType,
            "steal_bet"=>$bet,
            "steal_collect_symbol"=>intval($retData['collectNum']),
            "steal_rank_symbol"=>intval($retData['rankCollectNum']),
            "steal_coin_basic"=>$kingInfo['coins'],
            "steal_coin_final"=>$userGetCoinNum+intval($vipCoins)+intval($data['coin_blast_Coins']),//(人+宠物)+vip+金币爆炸活动
            "steal_pet_remain_time"=>$steal_pet_remain_time,
            "steal_pet_level"=>intval($petInfo['lv']),
        ];
        $data['eventData'] = $eventData;
        return $data;
    }

    //偷坑结果权重 a完美偷 b(65+25+其他) c(65+10+其他) d(25+其他+其他)
    public $stealRetWeight = [
        ["id"=>1,"slv"=>1,"elv"=>10,"weight"=>["a"=>25,"b"=>25,"c"=>25,"d"=>25]],
        ["id"=>2,"slv"=>11,"elv"=>50,"weight"=>["a"=>25,"b"=>25,"c"=>25,"d"=>25]],
        ["id"=>3,"slv"=>51,"elv"=>100,"weight"=>["a"=>20,"b"=>25,"c"=>30,"d"=>25]],
        ["id"=>4,"slv"=>101,"elv"=>150,"weight"=>["a"=>15,"b"=>25,"c"=>30,"d"=>30]],
        ["id"=>5,"slv"=>151,"elv"=>200,"weight"=>["a"=>15,"b"=>25,"c"=>30,"d"=>30]],
        ["id"=>6,"slv"=>201,"elv"=>1000,"weight"=>["a"=>15,"b"=>25,"c"=>30,"d"=>30]],
    ];
    //空位宝箱的权重分配 a空 b木箱子 c白银箱子 d黄金箱子
    public $boxRetWeight = [
        ["id"=>1,"weight"=>["a"=>100,"b"=>0,"c"=>0,"d"=>0]],
        ["id"=>2,"weight"=>["a"=>90,"b"=>7,"c"=>2,"d"=>1]],
        ["id"=>3,"weight"=>["a"=>85,"b"=>11,"c"=>3,"d"=>1]],
        ["id"=>4,"weight"=>["a"=>80,"b"=>14,"c"=>4,"d"=>2]],
        ["id"=>5,"weight"=>["a"=>75,"b"=>18,"c"=>5,"d"=>2]],
        ["id"=>6,"weight"=>["a"=>70,"b"=>21,"c"=>6,"d"=>3]],
    ];
    //倍率调整偷金币坑权重系数
    public $betRetWeight =[
        ["id"=>1,"bet"=>1,"coefficient"=>["a"=>1,"b"=>1,"c"=>1,"d"=>1]],
        ["id"=>2,"bet"=>2,"coefficient"=>["a"=>1,"b"=>1,"c"=>1,"d"=>1]],
        ["id"=>3,"bet"=>3,"coefficient"=>["a"=>1,"b"=>1,"c"=>1,"d"=>1]],
        ["id"=>4,"bet"=>5,"coefficient"=>["a"=>1,"b"=>1,"c"=>1,"d"=>1]],
        ["id"=>5,"bet"=>10,"coefficient"=>["a"=>1,"b"=>1.2,"c"=>1.2,"d"=>1.2]],
        ["id"=>6,"bet"=>20,"coefficient"=>["a"=>1,"b"=>1.2,"c"=>1.2,"d"=>1.2]],
        ["id"=>7,"bet"=>40,"coefficient"=>["a"=>1,"b"=>1.5,"c"=>1.5,"d"=>1.5]],
        ["id"=>8,"bet"=>80,"coefficient"=>["a"=>1,"b"=>1.5,"c"=>1.5,"d"=>1.5]],
        ["id"=>9,"bet"=>100,"coefficient"=>["a"=>1,"b"=>2,"c"=>2,"d"=>2]],
        ["id"=>10,"bet"=>200,"coefficient"=>["a"=>1,"b"=>3,"c"=>3,"d"=>3]],
    ];
    //倍率调整空位宝箱权重系数
    public $betBoxRetWeight =[
        ["id"=>1,"bet"=>1,"coefficient"=>["a"=>1,"b"=>0,"c"=>0,"d"=>0]],
        ["id"=>2,"bet"=>2,"coefficient"=>["a"=>1,"b"=>0,"c"=>0,"d"=>0]],
        ["id"=>3,"bet"=>3,"coefficient"=>["a"=>1,"b"=>1,"c"=>1,"d"=>1]],
        ["id"=>4,"bet"=>5,"coefficient"=>["a"=>1,"b"=>1,"c"=>1,"d"=>1]],
        ["id"=>5,"bet"=>10,"coefficient"=>["a"=>1,"b"=>1.2,"c"=>1.2,"d"=>1.2]],
        ["id"=>6,"bet"=>20,"coefficient"=>["a"=>1,"b"=>1.2,"c"=>1.2,"d"=>1.2]],
        ["id"=>7,"bet"=>40,"coefficient"=>["a"=>1,"b"=>1.5,"c"=>1.5,"d"=>1.5]],
        ["id"=>8,"bet"=>80,"coefficient"=>["a"=>1,"b"=>1.5,"c"=>1.5,"d"=>1.5]],
        ["id"=>9,"bet"=>100,"coefficient"=>["a"=>1,"b"=>2,"c"=>2,"d"=>2]],
        ["id"=>10,"bet"=>200,"coefficient"=>["a"=>1,"b"=>3,"c"=>3,"d"=>3]],
    ];
    /**
     * Notes:获取坑位钱分布
     * User: wsc
     * Time: 2020/10/12 14:48
     * @param $uid
     * @param $king
     * @param $money
     * @param $bet
     * @param $robot
     * @return array
     */
    public function getOptMoney($uid,$kingMoney,$bet,$levelId){
        $levelId = max($levelId,1);
        $bet = max($bet,1);
        $money = oo::Thousand($kingMoney);
        $probId = 1;//当前选中的权重id
        $prob = ["id"=>1,"slv"=>1,"elv"=>10,"weight"=>["a"=>500,"b"=>300,"c"=>100,"d"=>100]];
        $stealRetWeight = oo::commonOprModel('config')->getSlotRetConfig("stealRetWeight");
        $stealRetWeight =$stealRetWeight?$stealRetWeight:$this->stealRetWeight;
        if(!empty($stealRetWeight)){
            foreach ($stealRetWeight as $v){
                if($levelId>=$v['slv']&&$levelId<=$v['elv']){
                    $prob = $v;
                    $probId = $v['id'];
                    break;
                }
            }
        }
        //调整偷到坑位结果权重=权重*系数
        $betRetWeight = oo::commonOprModel('config')->getSlotRetConfig("betRetWeight");
        $betRetWeight = $betRetWeight?$betRetWeight:$this->betRetWeight;
        $coefficientCfgArr = array_column($betRetWeight,'coefficient','bet');
        $coefficientCfg = !empty($coefficientCfgArr[$bet])&&is_array($coefficientCfgArr[$bet])?$coefficientCfgArr[$bet]:$betRetWeight[0]['coefficient'];
        foreach ($prob['weight'] as $k=>$v){
            $retWeight[$k] = $coefficientCfg[$k]*$v;
        }
        $ret1 = funs::get_randByProb($retWeight);
        switch ($ret1){
            case 'a'://完美偷
                $stealData = [$money*0.65,$money*0.25,$money*0.10];
                break;
            case 'b'://	65+25+其他
                $stealData = [$money*0.65,$money*0.25,0];
                break;
            case 'c'://65+10+其他
                $stealData = [$money*0.65,$money*0.10,0];
                break;
            case 'd'://25+10+其他
                $stealData = [$money*0.25,$money*0.10,0];
                break;
            default:
                $stealData = [$money*0.65,$money*0.10,0];
                $ret1="c";
                break;
        }
        array_push($stealData,$probId);
        return [$stealData,$ret1];
    }

    /**
     * Notes:坑位奖励详情
     * User: wsc
     * Time: 2021/2/23 15:50
     * @param $uid
     * @param $levelId
     * @param $stealData
     * @param $petStealCoin
     * @param $bet
     * @return array
     */
    public function getOptInfo($uid,$levelId,$stealData,$petStealInfo,$bet){
        //宠物偷信息
        $data = [];
        $optData = array_slice($stealData,0,3);//3个坑
        $petCoinsPercent = max($petStealInfo[1],0);//宠物加成系数
        foreach ($optData as $k=>$v){
            $data[$k]['bet']  = $bet;//倍率
            $data[$k]['coins'] = $v* $bet;//每个坑的钱，已加倍
            $data[$k]['pet']  = $v*$petCoinsPercent*$bet;//宠物触发的额外收益，已加倍
            $data[$k]['petFeed'] = 0;//宠物食物
            $data[$k]['treasureType'] =0;//宝箱类型 0空 1木 2白银 3黄金
            $data[$k]['optId'] =$k;//坑位
            //空位补充宝箱
            if($stealData[$k]==0&&$levelId > 2){
                $boxRetWeight    = oo::commonOprModel('config')->getSlotRetConfig("boxRetWeight");
                $boxRetWeight    = $boxRetWeight?$boxRetWeight:$this->boxRetWeight;
                $betBoxRetWeight = oo::commonOprModel('config')->getSlotRetConfig("betBoxRetWeight");
                $betBoxRetWeight = $betBoxRetWeight?$betBoxRetWeight:$this->betBoxRetWeight;
                $probInfo = array_column($boxRetWeight,'weight','id');
                $prob = !empty($probInfo[$stealData[3]])?$probInfo[$stealData[3]]:$boxRetWeight[0]['weight'];
                //调整偷到宝箱坑位结果权重=权重*系数
                $coefficientCfgArr = array_column($betBoxRetWeight,'coefficient','bet');
                $coefficientCfg = !empty($coefficientCfgArr[$bet])&&is_array($coefficientCfgArr[$bet])?$coefficientCfgArr[$bet]:$betBoxRetWeight[0]['coefficient'];
                $sum =0;
                foreach ($prob as $k2=>$v2){
                    $coefficientCfg[$k2] = $coefficientCfg[$k2]?$coefficientCfg[$k2]:1;
                    $retWeight[$k2] = $coefficientCfg[$k2]*$v2;
                    if($retWeight[$k2]==0) $sum++;
                }
                if($sum==count($retWeight)){//避免配置异常参数bug
                    $data[$k]['treasureType']=0;
                    $ret1 ='a';
                }else{
                    $ret1 = funs::get_randByProb($retWeight);
                }
                switch ($ret1){
                    case 'a'://空
                        $data[$k]['treasureType']=0;
                        break;
                    case 'b'://木宝箱
                        $data[$k]['treasureType']=1;
                        break;
                    case 'c'://白银宝箱
                        $data[$k]['treasureType']=2;
                        break;
                    case 'd'://黄金宝箱
                        $data[$k]['treasureType']=3;
                        break;
                    default:
                        $data[$k]['treasureType']=0;
                        break;
                }
            }
        }
//        shuffle($data);
        return $data;
    }
}
