<?php
defined('IN_WEB') or die('Include Error!');
/**
 *	岛屿相关
 */
class Level {
    /**
     *	关卡岛建造升级操作
     * $uid用户id  岛屿id 自己获取 opt[0建造or升级1破坏] optId对哪个建筑操作
     */
    public function levelOperation($param) {
        //旧版本接口异常处理
        $data = array('code' => 1, 'desc' => 'your version is too old');
        return json_encode($data);

        $data = array('code' => -1, 'desc' => '参数错误');
        $uid = oo::functions()->uint($param['uid']);
        $optId = oo::functions()->uint($param['optId']); //操作id
        if( empty($uid)  || empty($optId)){
            return json_encode($data);
        }
        // 升級鎖
        while(oo::commonOprRedis('common')->exists(okeys::LevelLock($uid))){
            $time = mt_rand(50,150);
            usleep($time);
        }
        oo::commonOprRedis('common')->setex(okeys::LevelLock($uid),1,5);
        $opt_return = oo::commonOprModel('level')->levelOperation($uid,$optId);
        oo::commonOprRedis('common')->delete(okeys::LevelLock($uid));
        return json_encode($opt_return);
    }

    /**
     *	关卡岛建造升级操作 -- 不过关
     * $uid用户id  岛屿id 自己获取 opt[0建造or升级1破坏] optId对哪个建筑操作
     */
    public function levelOperationNoUp($param) {
        $data = array('code' => -1, 'desc' => '参数错误');
        $uid = oo::functions()->uint($param['uid']);
        $optId = oo::functions()->uint($param['optId']);
        $level = oo::functions()->uint($param['level']);
        $isVideo = oo::functions()->uint($param['isVideo']);
        if( empty($uid) || empty($optId) || empty($level)){
            return json_encode($data);
        }
        $unKey = uniqid($uid.$optId.$level.mt_rand(0,1000));
        oo::commonOprRedis('common')->rPush(okeys::LevelList($uid),$unKey);
        $i = 10;
        while($unKey != oo::commonOprRedis('common')->lGet(okeys::LevelList($uid),0)){
            if(oo::commonOprRedis('common')->lSize(okeys::LevelList($uid)) == 0){
                return json_encode($data);
            }else{
                usleep(200);
            }
            $i--;
            if($i<=0){
                break;
            }
        }
        $opt_return = oo::commonOprModel('level')->levelOperationNoUp($uid,$optId,$level,$isVideo);
        oo::commonOprRedis('common')->lPop(okeys::LevelList($uid));
        return json_encode($opt_return);
    }

    /**
     * 升级
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/7/9 16:34
     * Description:
     */
    public function Uplevel($param){
        $uid = oo::functions()->uint($param['uid']);
        $type = oo::functions()->escape($param['type']);
        if( empty($uid)){
            return json_encode(['code' => -1, 'desc' => '参数错误']);
        }
		
		$unKey = uniqid($uid.mt_rand(0,1000));
        oo::commonOprRedis('common')->rPush(okeys::UpLevelList($uid),$unKey);
        $i = 10;
        while($unKey != oo::commonOprRedis('common')->lGet(okeys::UpLevelList($uid),0)){
            if(oo::commonOprRedis('common')->lSize(okeys::UpLevelList($uid)) == 0){
                return json_encode(['code' => -1, 'desc' => 'level error']);
            }else{
                usleep(200);
            }
            $i--;
            if($i<=0){
                break;
            }
        }
        $opt_return = oo::commonOprModel('level')->upLevel($uid);
        $outPut = json_encode($opt_return,JSON_NUMERIC_CHECK);
		oo::commonOprRedis('common')->lPop(okeys::UpLevelList($uid));
        return $outPut;
    }

    /**
     * 获取用户岛数据 wsc-4.2(废弃)
     * uid当前用户
     */
//    public function getLandData($param){
//        $data = array('code' => -1, 'desc' => '参数错误');
//        $uid = oo::functions()->uint($param['uid']); //用户uid
//        if( empty($uid) ){
//            return json_encode($data);
//        }
//        $LandData = oo::commonOprModel('level')->getLandData($uid);
//        return json_encode($LandData,JSON_NUMERIC_CHECK);
//    }
    /**
     * Notes:用户岛信息
     * User: wsc
     * Time: 2020/4/2 15:49
     * @param $param
     * @return false|string
     */
    public function getLandDatas($param)
    {
        $data = array('code' => -1, 'desc' => '参数错误');
        $uid = oo::functions()->uint($param['uid']); //用户uid
        $toUid = oo::functions()->uint($param['toUid']); //用户uid
        if (empty($uid) || empty($toUid)) {
            return json_encode($data);
        }
        //刷新用户体力
        if($uid==$toUid){
            $refreSpins = oo::commonOprModel('user')->refreshSpins($uid,1);
        }
        $LandData = oo::commonOprModel('level')->getLandData($toUid);
        $LandDataList = array('code' => 1, 'desc' => '获取成功','data'=>$LandData);
        return json_encode($LandDataList);
    }
    /**
     * 获取用户岛数据  wsc-4.2(废弃)
     * uid当前用户
     */
    public function getLandDatasold($param){
        $data = array('code' => -1, 'desc' => '参数错误');
        $uid = oo::functions()->uint($param['uid']); //用户uid
        $toUid = oo::functions()->uint($param['toUid']); //用户uid
        if( empty($uid) ||  empty($toUid) ){
            return json_encode($data);
        }
        $LandData = oo::commonOprModel('level')->getLandData($toUid);
      	$outList  = oo::commonOprRedis('common')->get(okeys::EnemyList($uid));
        if(!empty($outList)){
            $outList = json_decode($outList,true);
            $key = array_search($toUid,$outList['game_list']);
            if($key !== false && $outList['list_info'][$key]['robot'] == 1){
                $LandData['element'] = json_decode($outList['list_info'][$key]['element'],true);
            }
        }
        $LandDataList = array('code' => 1, 'desc' => '获取成功','data'=>$LandData);
        return json_encode($LandDataList);
    }

    /**
     * 返回被攻击
     * uid当前用户
     */
    public function getAttackLandData($param){
        $data = array('code' => -1, 'desc' => '参数错误');
        $uid = oo::functions()->uint($param['uid']); //用户uid
        if( empty($uid) ){
            return json_encode($data);
        }
        $LandData = oo::commonOprModel('member')->getEnemyData($uid);
        return json_encode($LandData);
    }

    /**
     * 玩家攻击操作类
     * 当前用户uid ， 对方sid，对方建筑id，倍数bet，
     */
    public function levelAttackOperation($param){
        $data = array('code' => -1, 'desc' => '参数错误');

        $uid = oo::functions()->uint($param['uid']); //用户uid
        $toUid = oo::functions()->uint($param['toUid']); //对方uid
        $optId = oo::functions()->uint($param['optId']); //攻击的建筑物id
        $bet = oo::functions()->uint($param['bet']); //倍数

        if( empty($uid)  || empty($toUid)|| empty($optId)){
            return json_encode($data);
        }
        /*
          // 连续攻击锁
        $lock = oo::commonOprRedis('common')->get(okeys::levelAttackLock($uid));
        if($lock){
            return json_encode(['code' => -1, 'desc' => '攻击速度异常']);
        }
        oo::commonOprRedis('common')->setex(okeys::levelAttackLock($uid),1,10);
          */
        $opt_return = oo::commonOprModel('level')->AttackOperation($uid,$toUid,$optId,$bet);

        oo::commonOprRedis('common')->setex(okeys::lastObject($uid,'Attack'),$toUid,300);

        return json_encode($opt_return);

    }

    /**
     * 玩家偷操作类
     * 当前用户uid ， 对方toUid，洞 id，倍数bet，
     */
    public function levelStealOperation($param){
        $uid   = oo::functions()->uint($param['uid']); //用户uid
        $toUid = oo::functions()->uint($param['toUid']); //偷取的用户uid
        $optId = oo::functions()->uint($param['optId']); //
        $bet   = oo::functions()->uint($param['bet']); //
        if( empty($uid)  || empty($toUid) ){
            return json_encode(array('code' => -1, 'desc' => '参数错误'));
        }

        $unKey = uniqid($uid.mt_rand(0,1000));
        oo::commonOprRedis('common')->rPush(okeys::StealOperationList($uid),$unKey);
        oo::commonOprRedis('common')->expire(okeys::StealOperationList($uid),300);
        $i = 10;
        while($unKey != oo::commonOprRedis('common')->lGet(okeys::StealOperationList($uid),0)){
            if(oo::commonOprRedis('common')->lSize(okeys::StealOperationList($uid)) == 0){
                return json_encode(['code' => -1, 'desc' => 'level error']);
            }else{
                usleep(200);
            }
            $i--;
            if($i<=0){
                break;
            }
        }
//        $cache = ocache::t_userinfo($uid)->get(okeys::StealOperationCache($uid,$toUid,$optId));//过度-上线后删除
//        if(empty($cache)){//过度-上线后删除
            $cache = oo::commonOprRedis('usercache')->get(okeys::StealOperationCache($uid,$toUid,$optId));
//        }//过度-上线后删除
        if(!empty($cache)){
            return $cache;
        }

        $datas = oo::commonOprModel('level')->StealOperation_130($uid,$toUid,max(0,$optId-1),$bet);
//        oo::commonOprRedis('common')->setex(okeys::lastObject($uid,'Steal'),$toUid,300);
        oo::commonOprRedis('common')->lPop(okeys::StealOperationList($uid));
        $returnData = json_encode($datas);
//        ocache::t_userinfo($uid)->set(okeys::StealOperationCache($uid,$toUid,$optId),$returnData,3*60);
        oo::commonOprRedis('usercache')->setex(okeys::StealOperationCache($uid,$toUid,$optId),$returnData,3*60);
        return $returnData;
    }


    public function overStealOperation($param){
        $data = array('code' => -1, 'desc' => '参数错误');
        return json_encode($data);
//        $uid = oo::functions()->uint($param['uid']); //用户uid
//        $toUid = oo::functions()->uint($param['toUid']); //偷取的用户uid
//        if( empty($uid)  || empty($toUid) ){
//            $data = array('code' => -1, 'desc' => '参数错误');
//            return json_encode($data);
//        }
//        $data = oo::commonOprModel('level')->overSteal_130($uid,$toUid);
//        for($i=0;$i<=5;$i++){
////            ocache::t_userinfo($uid)->delete(okeys::StealOperationCache($uid,$toUid,$i));//过度-上线后删除
//            oo::commonOprRedis('usercache')->delete(okeys::StealOperationCache($uid,$toUid,$i));
//        }
        return json_encode($data);
    }


    /**
     * 过关奖励
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/6/11 20:27
     * Description:
     */
    public function levelReward($param){
        $uid = oo::functions()->uint($param['uid']); //用户uid
        if( empty($uid)){
            $data = array('code' => -1, 'desc' => '参数错误');
            return json_encode($data);
        }
        $data = oo::commonOprModel('level')->levelReward($uid);
        return json_encode($data);
    }

    public function setBubble($param){
        $uid = oo::functions()->uint($param['uid']); //用户uid
        $toUid = oo::functions()->uint($param['toUid']); //用户uid
        $bubble = oo::functions()->uint($param['bubble']); //用户uid
        if(empty($uid) || empty($toUid) || empty($bubble)){
            $data = array('code' => -1, 'desc' => '参数错误');
            return json_encode($data);
        }

        oo::commonOprModel('level')->setBubble($uid,$toUid,$bubble);
        return json_encode(['code' => 1],JSON_NUMERIC_CHECK);
    }

    public function getBubble($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            $data = array('code' => -1, 'desc' => '参数错误');
            return json_encode($data);
        }

        $data = oo::commonOprModel('level')->getBubble($uid);
        return json_encode(['code' => 1,'data'=>$data],JSON_NUMERIC_CHECK);
    }

    public function delBubble($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            $data = array('code' => -1, 'desc' => '参数错误');
            return json_encode($data);
        }
        oo::commonOprRedis('common')->delete(okeys::Bubble($uid));
        return json_encode(['code' => 1],JSON_NUMERIC_CHECK);
    }
  
  	/**
     * 建筑广告保护
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/10/12 15:34
     */
    public function elementSaleProtect($param){
        $uid = oo::functions()->uint($param['uid']);
        $time = oo::functions()->uint($param['time']);
        $sign = oo::functions()->uint($param['sign']);
        if(empty($uid)){
            $data = array('code' => -1, 'desc' => '参数错误');
            return json_encode($data);
        }
        
        $serverSign = "{$uid}#2019$"."superant{$time}&^%";
        if(md5($serverSign) != $sign){
            $data = array('code' => -2, 'desc' => 'error sign');
            return json_encode($data);
        }
        $ttl = oo::commonOprRedis('common')->ttl(okeys::AttackLock($uid));
        if($ttl<=60){
            oo::commonOprRedis('common')->setex(okeys::AttackLock($uid),1,60);
        }
        return json_encode(['code' => 1],JSON_NUMERIC_CHECK);
    }

    public function helpBuildOperation($param){
        $uid   = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        $optId = oo::functions()->uint($param['optId']);
        if(empty($uid) || empty($toUid) || empty($optId)){
            $data = array('code' => -1, 'desc' => '参数错误');
            return json_encode($data);
        }
        oo::commonOprModel('level')->helpBuildOperation($uid,$toUid,$optId);
        return oo::response(1,[],'',true);
    }

    public function changeAttackObject($param){
        $uid   = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        $userInfo = oo::commonOprModel('member')->getUserInfo($toUid);
        $list_info = oo::commonOprModel('member')->getUserLevel($toUid);
        $dataInfo = [
            'uid'  =>$toUid,
            'toUid'  =>$toUid,
            'levelId'=>$list_info['levelId'],
            'element'=> $list_info['element'],
        ];
        $res['attackInfo'] = $dataInfo;
        $res['attackInfo']['nick'] = $userInfo['nick'];
        $res['attackInfo']['s_picture'] = $userInfo['s_picture'];
        $res['attackInfo']['vip'] = $userInfo['vip'];
        $res['attackInfo']['vipLv'] = $userInfo['vipLv'];
        $res['attackInfo']['avatar'] = intval($userInfo['avatar']);
        $attackAssetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($toUid);
        $res['attackInfo']['shield'] = intval($attackAssetsInfo['shield']);
        $oldToUid = oo::commonOprRedis('common')->get(okeys::slotLock($uid));//原先被绑的用户

        oo::commonOprRedis('common')->delete(okeys::beAttactLock($oldToUid));//删除原先默认攻击对象绑定关系锁
        oo::commonOprRedis('common')->setex(okeys::slotLock($uid),$toUid,3600);//新对象锁定
        oo::commonOprRedis('common')->setex(okeys::beAttactLock($toUid),$uid,300);//新对象添加保护锁
        if($res['attackInfo']['shield'] != 0){
            $userPetSkill = oo::commonOprModel('pet')->usePetSkill($uid,3);     //攻击技能
            if($userPetSkill){
                oo::commonOprRedis('usercache')->setex(okeys::PetSkill($uid, 3),1,5*60);
            }
        }
        $toUserPetSkill = oo::commonOprModel('pet')->usePetSkill($toUid, 2); //防御技能
        if($toUserPetSkill){
            oo::commonOprRedis('usercache')->setex(okeys::PetSkill($toUid, 2),1,5*60);
        }
        $res['attackInfo']['userPetSkill']   = $userPetSkill?1:0;
        $res['attackInfo']['toUserPetSkill'] = $toUserPetSkill?1:0;

        if(oo::commonOprRedis('common')->get(okeys::AttackLock($toUid)) && $_POST['test'] != 'level'){
            $res['attackInfo']['shield'] = max($res['attackInfo']['shield'],1);
        }
        return oo::response(1,$res,'',true);
    }
}
