<?php
/**
* 破产统计相关
*/
defined('IN_WEB') or die('Include Error!');
include_once dirname(__FILE__) . "/Base.php";
class Broke extends Base
{
	private $_levelSmallBlind;//场次分部

	private $_brokelevel = 'BROKELEVEL:';//破产等级分布

	private $_broketimes = 'BROKEUSER:';//破产次数 程序写错了,这个key本来是要计算破产人数的

	private $_brokeuser = 'BROKEUSERTIMES:';//破产人数 程序写错了,这个key本来是要计算破产次数的

	private $_brokePlayerExp = 'BROKE:PLAYEREXP:';//破产玩家等级

	function __construct()
	{
		$this->_levelSmallBlind = $this->getGameLevel();
	}

//	/**
//	*	获取场次
//	*/
//	protected function getGameLevel()
//	{
//		$key = okeys::levelSmallBlind();
//		if ( $cacheData = oo::commonOprRedis('brokelevel')->get($key) ) {
//			return json_decode($cacheData, 1);
//		}
//
//		$levelInfo = array();
//		$table = otable::srv_tableconfig();
//		$sql = " SELECT level, SmallBlind FROM {$table} WHERE level >= 101 AND level <= 500";
//		$list = oo::commonOprDb('admin')->getAll($sql, 1);
//		if ($list) {
//			foreach ($list as $info) {
//				$tempLevel = $info['level'];
//				$smallBlind = $info['SmallBlind'];
//				$levelInfo[$tempLevel] = $smallBlind;
//			}
//			$jsonData = json_encode($levelInfo);
//			oo::commonOprRedis('brokelevel')->set($key, $jsonData);
//		}
//		return $levelInfo;
//	}


	/**
	*	新统计
	*/
	public function newstatistics($param)
	{
		$data = array();
		$stime = oo::functions()->uint($param['stime']);
        $etime = oo::functions()->uint($param['etime']);
        $gid = oo::functions()->uint($param['gameId']);
        $gameArr = empty($gid)  ? [1,2,3] : [$gid];
        $flag = oo::functions()->uint($param['flag']);
//        empty($gameId) && $gameId = 1;

        $dayList = $this->getDays($stime, $etime, 2);
        $brokeUser = $brokeTimes = $brokeSmallBlind = $brokePlayerExp = array();
        //获取等级
        $levelConfig = include_once PATH_CFG.'config.level.php';
        $levelList = array_keys($levelConfig['level']);

        foreach ($dayList as $day) {
            foreach ($gameArr as $gameId) {
                $this->dealBrokeData($day, $levelList, $gameId, $brokePlayerExp, $brokeSmallBlind, $brokeUser, $brokeTimes, $flag);
            }

            //统计DAU
            $stime = strtotime($day. ' 0:0:1');
            $etime = strtotime($day. ' 23:59:59');
            if($flag == 2){
                $infoLogin = oo::commonOprModel('statistics')->getOneNewPlayer($stime, $etime);
            }else {
                $infoLogin = oo::commonOprModel('statistics')->getOneLoginPlayer($stime, $etime);
            }

            if ($infoLogin) {
                $data['totalLogin'][] = $infoLogin['total'];
            }else{
                $data['totalLogin'][] = 0;
            }

    }

        $data['dayList'] = $dayList;
//        $data['brokeLevel'] = $brokeLevel;
        $data['brokeUser'] = $brokeUser;
        $data['brokeTimes'] = $brokeTimes;
        $data['brokeSmallBlind'] = $brokeSmallBlind;
        $data['levelList'] = $levelList;
        $data['brokePlayerExp'] = $brokePlayerExp;

        return json_encode($data);
	}


//    /**
//     * @param $day
//     * @param $levelList
//     * @param $gameId
//     * @param $brokePlayerExp
//     * @param $brokeSmallBlind
//     * @param $brokeUser
//     * @param $brokeTimes
//     * @param int $flag  --2 新手 其他 全部玩家
//     * @return array
//     */
//	private function dealBrokeData($day, $levelList, $gameId, &$brokePlayerExp, &$brokeSmallBlind, &$brokeUser, &$brokeTimes, $flag = 1)
//    {
//            $data = [];
//            $blindList = array_unique(array_values($this->_levelSmallBlind));
//
//            //获取场次
//            foreach ($blindList as $smallBlind) {
//                $tempLevelKey = ($flag == 2) ? okeys::brokeblind_np($gameId, $smallBlind, $day) :  okeys::brokeblindnew($gameId, $smallBlind, $day);
//                $tempLevelNum = intval(oo::commonOprRedis('brokelevel')->get($tempLevelKey));//当天场次人数
////        		$brokeLevel[$day][$level] = $tempLevelNum;
//                $brokeSmallBlind[$smallBlind][$day] += $tempLevelNum;
//            }
//
//            //获取人数
//            $tempUserKey = ($flag == 2) ? okeys::brokeuser_np($gameId, $day) : okeys::brokeusernew($gameId, $day);
//            $brokeUser[$day] +=  intval(oo::commonOprRedis('brokelevel')->sSize($tempUserKey));
//
//            //获取次数
//            $tempTimesKey =  ($flag == 2) ? okeys::brokeusertimes_np($gameId, $day) : okeys::brokeusertimesnew($gameId, $day);
//            $brokeTimes[$day] +=  intval( oo::commonOprRedis('brokelevel')->get($tempTimesKey) );
//
//            //获取破产等级人数
//            foreach ($levelList as $exp) {
//                $brokePlayerLevel = ($flag == 2) ? okeys::brokeuserexp_np($exp, $gameId, $day) : okeys::brokeuserexpnew($exp, $gameId, $day);
//                $tmpExp = intval( oo::commonOprRedis('brokelevel')->get($brokePlayerLevel) );
//                $brokePlayerExp[$exp][$day] +=  $tmpExp ;
//            }
//
//        return $data;
//    }


    /**
     *	统计
     */
    public function statistics($param)
    {
        $data = array();
        $stime = oo::functions()->uint($param['stime']);
        $etime = oo::functions()->uint($param['etime']);

        $dayList = $this->getDays($stime, $etime);
        $brokeLevel = $brokeUser = $brokeTimes = $brokeSmallBlind = array();
        //获取等级
        $levelConfig = include_once PATH_CFG.'config.level.php';
        $levelList = array_keys($levelConfig['level']);

        foreach ($dayList as $day) {
            $day = str_replace('-', '', $day);
            //获取场次
            foreach ($this->_levelSmallBlind as $level => $smallBlind) {
                $tempLevelKey = $this->_brokelevel.$level.':'.$day;
                $tempLevelNum = oo::commonOprRedis('brokelevel')->get($tempLevelKey);//当天场次人数
                $brokeLevel[$day][$level] = $tempLevelNum;

                $brokeSmallBlind[$smallBlind][$day] += $tempLevelNum;
            }

            //获取人数
            $tempUserKey = $this->_brokeuser.$day;
            $brokeUser[] =  oo::commonOprRedis('brokelevel')->sSize($tempUserKey);

            //获取次数
            $tempTimesKey = $this->_broketimes.$day;
            $brokeTimes[] =  intval( oo::commonOprRedis('brokelevel')->get($tempTimesKey) );

            //获取破产等级人数
            $newDay = str_replace('-', '', $day);
            foreach ($levelList as $exp) {
                $brokePlayerLevel = $this->_brokePlayerExp.$day.':'.$exp;
                $brokePlayerExp[$exp][$newDay] =  intval( oo::commonOprRedis('brokelevel')->get($brokePlayerLevel) );
            }

            //统计DAU
            $stime = strtotime($day. ' 0:0:1');
            $etime = strtotime($day. ' 23:59:59');
            $infoLogin = oo::commonOprModel('statistics')->getOneLoginPlayer($stime, $etime);
            if ($infoLogin) {
                $data['totalLogin'][] = $infoLogin['total'];
            }else{
                $data['totalLogin'][] = 0;
            }
        }
        $data['dayList'] = $dayList;
        $data['brokeLevel'] = $brokeLevel;
        $data['brokeUser'] = $brokeUser;
        $data['brokeTimes'] = $brokeTimes;
        $data['brokeSmallBlind'] = $brokeSmallBlind;
        $data['levelList'] = $levelList;
        $data['brokePlayerExp'] = $brokePlayerExp;

        return json_encode($data);
    }

}