<?php
defined( 'IN_WEB') or die( 'Include Error!');
/**
 * 倒计时宝箱相关
 */
class TreasureBox {

    /**
	 * 初始化功能
    */
	public function get($param) {
	    $data = array('code' => -1, 'remainTime' => 0, 'rewardChip' => 0, 'multiple' => 0);
		$uid = oo::functions()->uint($param['uid']);
		if (!$uid) {
			return json_encode($data);
		}
		$timed = oo::commonOprModel('treasurebox')->getChestTime($uid); //宝箱已存时间
		$config = oo::commonOprModel('treasurebox')->getConfig();//获取宝箱配置

		//已领几次的奖励
		$key 	= okeys::mkchestnum($uid);
		$num 	= oo::commonOprRedis('common')->get($key);
		$num	= intval($num);
		//领第几次奖励
		$num++;
		//已经领取所有奖励了
		if($num > $config['limit']){
		    $data['code'] = -2;
			return json_encode($data);
		}
        $data['remainTime'] = $config['config'][$num][1] - $timed;
		//已存时间超过可领奖时间，说明可以领奖了
		if($data['remainTime'] < 0){
            $data['remainTime'] = 0;
		}
        $data['rewardChip'] = $config['config'][$num][2];
		$data['code'] = 1;
		return json_encode($data);
	}



	/**
	 * 点击领取奖励
 	*/
	public function reward($param) {
		$uid = (int)$param['uid'];
		$config = oo::commonOprModel('treasurebox')->getConfig();//获取宝箱配置
		
		$data = array(
			'code' => 0,
			'remainTime' => 0,   //还差多少秒可领
			'nextRemainTime' => 0,	//下一次可领奖时长
			'nextRewardChip' => 0,	//下一次可领奖金币
		);

		$info = oo::commonOprModel('treasurebox')->getChest($uid);
		//uid错误
		if ($info['code'] == -1) {
			$data['code'] = -1;
			return json_encode($data);
		}
		//已领取了全部奖励
		if ($info['code'] == -2) {
			$data['code'] = -2;
			return json_encode($data);
		}
		//已领几次的奖励
		$key 	= okeys::mkchestnum($uid);
		$num 	= oo::commonOprRedis('common')->get($key);
		$num	= (int)$num;
		//领第几次奖励
		$num++;
		
		//未到领奖时间
		if ($info['code'] == -3) {
			$data['code'] = -3;
			$data['remainTime'] = $info['data']['time'];
			$data['nextRemainTime'] = $config['config'][$num][1];
			$data['nextRewardChip'] = $config['config'][$num][2];
			return json_encode($data);
		}
		//领取失败，可以重新再试
		if ($info['code'] == -5) {
			$data['code'] = -4;
			$data['line'] = __LINE__;
			return json_encode($data);
		}
		//领取失败，可以重新再试
		if ($info['code'] == 1) {
			oo::logs()->debug3($info, 'TreasureBox_receiveChest.log');
			$data['code'] = 1;
			$data['remainTime'] = $config['config'][$num][1];
			$data['money'] = $info['data']['addmoney'];
			$data['nextRemainTime'] = isset($config['config'][$num][1])?$config['config'][$num][1]:0;
			$data['nextRewardChip'] = isset($config['config'][$num][2])?$config['config'][$num][2]:'';
			return json_encode($data);
		}else{ //未知错误
			$err['uid'] = $uid;
			$err['num'] = $num;
			oo::logs()->debug3(json_encode($err), 'TreasureBox_receiveChest_Err.log');
			$data['code'] = -7;
			$data['line'] = __LINE__;
			return json_encode($data);
		}

	}


}