<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
*	破产统计相关
*/
class Broke extends MY_Controller{

	public $postdata = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('cookie');
		$this->load->library('curl');

		//请求地址
		$this->env = get_cookie($this->_wenenv);
		$this->_url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
	}

	public function index()
	{
		$data = array('stime' => '', 'etime' => '');

		//时间查询
        $dateArr = array();

        if( $stime = get_cookie('brokeindex_stime') ){
            $data['stime'] = $stime;
            $dateArr['stime'] = strtotime($stime.'0:0:1');
        }else{
            $dateArr['stime'] = strtotime('- 7 days 0:0:1');//默认开始时间
            $data['stime'] = date('Y-m-d', $dateArr['stime']);
        }

        if( $etime = get_cookie('brokeindex_etime') ){
            $data['etime'] = $etime;
            $dateArr['etime'] = strtotime($etime.'23:59:59');
        }else{
            $dateArr['etime'] = strtotime('-1 days 23:59:59');//默认结束时间
            $data['etime'] = date('Y-m-d', $dateArr['etime']);
        }

        //获取数据
        $postData = array_merge($dateArr, $this->postdata);
        $postData['mod'] = 'Broke';
        $postData['act'] = 'statistics';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        //统计数据
        if( !empty($retData) ){
            $data['dayList'] = $retData['dayList'];
            $data['brokeTimes'] = $retData['brokeTimes'];
            $data['brokeUser'] = $retData['brokeUser'];
            $data['totalLogin'] = $retData['totalLogin'];
            //计算破产率
            $data['bankruptcy'] = [];
            foreach ($data['brokeUser'] as $key => $value) {
                $data['bankruptcy'][] = round($value / $data['totalLogin'][$key], 2); 
            }

            //处理场次人数
            $brokeSmallBlind = $retData['brokeSmallBlind'];
            $levelStr = '';
            foreach ($brokeSmallBlind as $smallBlind => $info) {
                if($smallBlind > 5000){
                    $levelStr .= "{name:'".$smallBlind."盲住',visible:false,data:[";
                }else {
                    $levelStr .= "{name:'".$smallBlind."盲住',data:[";
                }

            		$tempStr = implode(',',array_values($info));
            		$levelStr .= $tempStr.']},';
            }
            $data['levelStr'] = $levelStr;

            //处理破产玩家等级
            $brokePlayerExp = $retData['brokePlayerExp'];
            $expStr = '';
            foreach ($brokePlayerExp as $exp => $nums) {
                if($exp >= 10){
                    $visible = 'visible: false,';
                }
                $expStr .= "{name:'等级".$exp."',{$visible}data:[";
                $tempexpStr = implode(',',array_values($nums));
                $expStr .= $tempexpStr.']},';
            }
            $data['expStr'] = $expStr;
        }
        $this->load->view('broke/index', $data);
	}


    public function statistics($gameId=0, $flag = 1)
    {
        $data = array('stime' => '', 'etime' => '');

        //时间查询
        $dateArr = array();

        if( $stime = get_cookie('brokeindex_stime') ){
            $data['stime'] = $stime;
            $dateArr['stime'] = strtotime($stime.'0:0:1');
        }else{
            $dateArr['stime'] = strtotime('- 7 days 0:0:1');//默认开始时间
            $data['stime'] = date('Y-m-d', $dateArr['stime']);
        }

        if( $etime = get_cookie('brokeindex_etime') ){
            $data['etime'] = $etime;
            $dateArr['etime'] = strtotime($etime.'23:59:59');
        }else{
            $dateArr['etime'] = strtotime('-1 days 23:59:59');//默认结束时间
            $data['etime'] = date('Y-m-d', $dateArr['etime']);
        }

        //获取数据
        $postData = array_merge($dateArr, $this->postdata);
        $postData['gameId'] =$gameId == 5 ? 0 : intval($gameId);
        $postData['flag'] = $flag;
        $postData['mod'] = 'Broke';
        $postData['act'] = 'newstatistics';
        $this->env == 2 && $postData['demo'] = 1;
        $res = $this->curl->req($this->_url, $postData);
        $retData = json_decode($res, TRUE);
        //统计数据
        if( !empty($retData) ){
            $data['dayList'] = $retData['dayList'];
            $data['brokeTimes'] = array_values($retData['brokeTimes']);
            $data['brokeUser'] = array_values($retData['brokeUser']);
            $data['totalLogin'] = $retData['totalLogin'];
            //计算破产率
            $data['bankruptcy'] = [];
            foreach ($data['brokeUser'] as $key => $value) {
                $data['bankruptcy'][] = empty($data['totalLogin'][$key]) ? 0 : round($value / $data['totalLogin'][$key], 2);
            }

            //处理场次人数
            $brokeSmallBlind = $retData['brokeSmallBlind'];
            $levelStr = '';
            foreach ($brokeSmallBlind as $smallBlind => $info) {
                if($smallBlind > 5000){
                    $levelStr .= "{name:'".$smallBlind."盲住',visible:false,data:[";
                }else {
                    $levelStr .= "{name:'".$smallBlind."盲住',data:[";
                }
                $tempStr = implode(',',array_values($info));
                $levelStr .= $tempStr.']},';
            }
            $data['levelStr'] = $levelStr;

            //处理破产玩家等级
            $brokePlayerExp = $retData['brokePlayerExp'];
            $expStr = '';
            foreach ($brokePlayerExp as $exp => $nums) {
                if($exp >= 10){
                    $visible = 'visible: false,';
                }
                $expStr .= "{name:'等级".$exp."',{$visible}data:[";
                $tempexpStr = implode(',',array_values($nums));
                $expStr .= $tempexpStr.']},';
            }
            $data['expStr'] = $expStr;
        }
        $data['type'] = $gameId;
        switch ($gameId) {
            case 1:
                $gameName = '德州场';
                break;
            case 3:
                $gameName = '奥马哈场';
                break;
            case 4:
                $gameName = '百人场';
                break;
            case 5:
                $gameName = '新手破产总计';
                break;
            default:
                $gameName = '破产总计';
                break;
        }
        $data['gameName'] = $gameName;
        $this->load->view('broke/statistics', $data);
    }
}