<?php
defined('IN_WEB') or die('Include Error!');
/**
 *	岛屿相关
 */
class Level {
    /**
     * Notes:变更打对象
     * User: wsc
     * Time: 2021/7/28 22:04
     * @param $param
     * @return string
     */
    public function changeAttack($param){
        $uid   = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        if(empty($uid) || empty($toUid)){
            return oo::response(-1);
        }
        $bet = oo::commonOprRedis('usercache')->get(okeys::curUserBet($uid))&&1;
        $levelId = oo::commonOprModel("member")->getUserLevelId($uid);
        $isRobot = oo::commonOprModel('attack')->changeAttack($uid,$toUid);
        $data['attackInfo'] = oo::commonOprModel('attack')->getDefaultAttackerInfo($uid,$levelId,$bet,$toUid,$isRobot);
        return oo::response(1,$data,'',true);
    }
    /**
     * 更换攻击对象(20210728-wsc-弃用)
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 17:55
     */
    public function changeAttack_old($param){
        $uid   = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        if(empty($uid) || empty($toUid)){
            return oo::response(-1);
        }
        $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'] = oo::commonOprModel('member')->getUserFirstName($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']);
        $res['attackInfo']['isRevenge']= in_array($toUid,oo::commonOprModel('member')->getMyEnemyList($uid)??[]);
        oo::commonOprRedis('common')->setex(okeys::slotLock($uid),$toUid,3600);
        oo::commonOprRedis('common')->setex(okeys::beAttactLock($toUid),$uid,300);//添加保护锁
        $userPetSkill = $res['attackInfo']['userPetSkill'] = intval(oo::commonOprModel('pet')->usePetSkill($uid,3,10000)) > 0 ? 1 : 0;
        $toUserPetSkill = oo::commonOprModel('pet')->usePetSkill($toUid, 2); //防御技能
        $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)) && $param['test'] != 'level'){
            $res['attackInfo']['shield'] = max($res['attackInfo']['shield'],1);
        }
        return oo::response(1,$res,'',true);
    }

    /**
     * 删除气泡
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 18:07
     */
    public function delBubble($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        oo::commonOprRedis('common')->delete(okeys::Bubble($uid));
        return oo::response(1);
    }

    /**
     * 建筑广告保护
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2019/10/12 15:34
     */
    public function saleProtect($param){
        $uid = oo::functions()->uint($param['uid']);
        $time = oo::functions()->uint($param['time']);
        $sign = oo::functions()->uint($param['sign']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $serverSign = "{$uid}#2019$"."superant{$time}&^%";
        if(md5($serverSign) != $sign){
            return oo::response(-2,[],"error sign");
        }
        oo::commonOprRedis('common')->setex(okeys::elementSaleProtect($uid),1,60);
        return oo::response(1);
    }

    /**
     * 气泡列表
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 18:12
     */
    public function bubble($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $data = oo::commonOprModel('level')->getBubble($uid);
        return oo::response(1,$data,'',true);
    }

    /**
     * 获取用户岛数据
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 18:17
     */
    public function landData($param){
        $uid = oo::functions()->uint($param['uid']); //用户uid
        $toUid = oo::functions()->uint($param['toUid']); //用户uid
        if( empty($uid) ||  empty($toUid) ){
            return oo::response(-1);
        }
        $LandData = oo::commonOprModel('level')->getLandData($toUid);
        $version = oo::commonOprModel('user')->getUserVersion($uid);
        if(oo::compareVersion($version,'1.7.5')){
            $LandData['pets'] = oo::commonOprModel('pet')->getUserPet($toUid);
        }
        return oo::response(1,$LandData,'获取成功',true);
    }

    /**
     * 帮好友修复
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2021/3/17 19:49
     */
    public function helpRepair($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)){
            return oo::response(-1);
        }

        if(!funs::concurrentLock($uid,'helpRepair',2)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS);
        }

        if(!oo::commonOprModel('friend')->isFriend($uid, $toUid)){
            return oo::response(errorCode::CONDITION_NOT_RNOUGH);
        }
        $info = oo::commonOprModel('level')->getHelpRepairInfo($uid);
        if(isset($info['repair'], $info['repair']['expire']) && $info['repair']['expire'] > time()){
            return oo::response(errorCode::LIMIT_NOT_RNOUGH);
        }
        if(isset($info['berepair'], $info['berepair']['expire']) && $info['berepair']['expire'] > time()){
            return oo::response(errorCode::LIMIT_NOT_RNOUGH);
        }

        $friendInfo = oo::commonOprModel('member')->getUserAssetsInfo($toUid);
        $userInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
        if(empty($friendInfo) || empty($userInfo)){
            return oo::response(errorCode::USER_NOT_EXISTS);
        }
        if($userInfo['levelId'] < $friendInfo['levelId']){
            return oo::response(errorCode::LEVEL_NOT_RNOUGH);
        }
        $landData = oo::commonOprModel('level')->helpRepairOperation($uid,$toUid,$optId);
        return oo::response(1, $landData, '', true);
    }

    /**
     * 帮助建筑
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 18:18
     */
    public function helpBuild($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)){
            return oo::response(-1);
        }
        oo::commonOprModel('level')->helpBuildOperation($uid,$toUid,$optId);
        return oo::response(1,[],'',true);
    }

    /**
     * 玩家攻击操作类
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 18:19
     */
    public function attack($param){
        $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 oo::response(-1);
        }
        $opt_return = oo::commonOprModel('attack')->ToAttack($uid,$toUid,$optId);
//        $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,JSON_NUMERIC_CHECK);
    }

    /**
     * 建造
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 18:27
     */
    public function build($param) {
        $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 oo::response(-1);
        }
        $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 oo::response(-2);
            }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);
    }

    /**
     * Notes:领取挖坑而外奖励
     * User: wsc
     * Time: 2020/6/23 16:44
     * @param $param
     * @return false|string
     */
    public function getOptOtherAward($param){
        $uid = oo::functions()->uint($param['uid']);
        $optId = oo::functions()->uint($param['optId']);
        $data = oo::commonOprModel('level')->getOtherStealAward($uid,$optId);
        return json_encode($data);
    }
    /**
     * 结束偷取
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2020/3/27 18:31
     */
    public function overSteal($param){
        $uid = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        $optIds = explode(',',$param['optIds']);
        $res = funs::concurrentLock($uid,'overSteal',2);
        if(empty($uid) || empty($toUid)||empty($optIds)||!$res){
            return oo::response(-1);
        }
        $data = oo::commonOprModel('level')->overSteal($uid,$toUid,$optIds);
        return json_encode($data);
    }

    /**
     * 设置气泡
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/3/27 18:33
     */
    public function setBubble($param){
        $uid = oo::functions()->uint($param['uid']);
        $toUid = oo::functions()->uint($param['toUid']);
        $bubble = oo::functions()->uint($param['bubble']);
        if(empty($uid) || empty($toUid) || empty($bubble)){
            return oo::response(-1);
        }
        oo::commonOprModel('level')->setBubble($uid,$toUid,$bubble);
        return oo::response(1);
    }

    /**
     * 升级
     * @param $param
     * @return false|string
     * Created by: Owen
     * Created on: 2019/7/9 16:34
     * Description:
     */
    public function pass($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        $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 oo::response(-2,[],'level error');
            }else{
                usleep(200);
            }
            $i--;
            if($i<=0){
                break;
            }
        }
        $data = oo::commonOprModel('level')->upLevel($uid);
        oo::commonOprRedis('common')->lPop(okeys::UpLevelList($uid));
        return json_encode($data,JSON_NUMERIC_CHECK);
    }

    /**
     * 一键建造
     * @param $param
     * @return string
     * Created by: Owen
     * Created on: 2020/11/20 19:56
     */
    public function oneClick($param){
        $uid = oo::functions()->uint($param['uid']);
        if(empty($uid)){
            return oo::response(-1);
        }
        if(!funs::concurrentLock($uid,'oneClick',5)){
            return oo::response(errorCode::API_FREQUENT_REQUESTS);
        }
        return oo::commonOprModel('level')->oneClick($uid);
    }
}
