<?php
defined('IN_WEB') or die('Include Error!');
/**
* 机器人相关
*/

class Robot
{

	private $key = 'RDSRBTL';

	protected $smallBlind = array(50, 100, 200, 500, 1000, 2000, 3000, 5000, 10000, 20000);//小盲
	protected $tableListArr = array();
	

    public function __construct()
    {
		 $this->tableListArr[50] = array( 1 => 101, 2 => 111, 3 => 121, 4 => 131);
		 $this->tableListArr[100] = array( 1 => 102, 2 => 112, 3 => 122, 4 => 132);
		 $this->tableListArr[200] = array( 1 => 103, 2 => 113, 3 => 123, 4 => 133);
		 $this->tableListArr[500] = array( 1 => 104, 2 => 114, 3 => 124, 4 => 134);
		 $this->tableListArr[1000] = array( 1 => 105, 2 => 115, 3 => 125, 4 => 135);
		 $this->tableListArr[2000] = array( 1 => 106, 2 => 116, 3 => 126, 4 => 136);
		 $this->tableListArr[3000] = array( 1 => 201, 2 => 211, 3 => 221, 4 => 231);
		 $this->tableListArr[5000] = array( 1 => 202, 2 => 212, 3 => 222, 4 => 232);
		 $this->tableListArr[10000] = array( 1 => 203, 2 => 213, 3 => 223, 4 => 233);
		 $this->tableListArr[20000] = array( 1 => 204, 2 => 214, 3 => 224, 4 => 234);
		 $this->tableListArr[] = array( 1 => 151, 2 => 161, 3 => 171, 4 => 181);//第二组开始
		 $this->tableListArr[] = array( 1 => 152, 2 => 162, 3 => 172, 4 => 182);
		 $this->tableListArr[] = array( 1 => 153, 2 => 163, 3 => 173, 4 => 183);
		 $this->tableListArr[] = array( 1 => 154, 2 => 164, 3 => 174, 4 => 184);
		 $this->tableListArr[] = array( 1 => 155, 2 => 165, 3 => 175, 4 => 185);
		 $this->tableListArr[] = array( 1 => 156, 2 => 166, 3 => 176, 4 => 186);
    }

    /**
	*	机器人场次列表
	*/
	public function getlist($param)
	{
		$data = array();
		$smallBlind = oo::functions()->uint($param['smallblind']);
		if (!in_array($smallBlind, $this->smallBlind)) {
			return json_encode($data);
		}

		$levelList = array();//level列表
		foreach ($this->tableListArr[$smallBlind] as $level) {
			array_push($levelList, $level);
		}

		//玩家信息
		$uidList = $uidLevel = array();
		$i = 1;
		foreach ($levelList as $v) {
			$rediskey = $this->key.$v;
			$rs = oo::commonOprRedis('robot')->sMembers($rediskey);
			if( empty($rs) ){
				continue;
			}

			foreach ($rs as $info) {
				$temp = explode('|', $info);
				array_push($uidList, $temp[0]);
				$uidLevel[$temp[0]] = $v;
			}

			if( $i%5 == 0 ){
				usleep(100);
			}
			$i++;
		}

		//获取玩家筹码信息
		foreach ($uidList as $uid) {
			$property = oo::commonOprModel('member')->getUserAssetsInfo($uid);
			if( empty($property) ){
				continue;
			}
			$data[$uid]['money'] = $property['money'];
			$data[$uid]['level'] = $uidLevel[$uid];
		}
		return json_encode($data);
	}




	/**
	*	机器人批量加钱
	*/
	public function actionmultiRobotmoney($param)
	{
		$smallBlind = oo::functions()->uint($param['smallBlind']);
		$addMoney = oo::functions()->uint($param['robotmoney']);
		oo::commonOprModel('robot')->autoAddMoney($smallBlind, $addMoney);
		return 1;
	}





    /**
     * 机器人输赢统计
     * @param $param
     * @return string
     */
    public function loseWin($param)
    {
        $stime = oo::functions()->uint($param['stime']);
        $etime = oo::functions()->uint($param['etime']);

        //写入时间是后一天，所以查询时相应加一天
        if(empty($stime)){
            $stime = strtotime("0:0:0");
            $sdate = date('Ymd');
        }else{
            $sdate = date('Ymd', $stime);
//            $stime = strtotime("$date +1 day ");
        }

        if(empty($etime)){
            $etime = strtotime("23:59:59");
            $edate = date('Ymd');
        }else{
            $edate = date('Ymd', $etime);
//            $etime = strtotime("$date +1 day ");
        }


        $bcreportTable = otable::robotLoseWin();
        $where = " date >= {$sdate} ";
        $where .= " AND date <= {$edate} ";

        $sql = "SELECT * FROM (
                      SELECT gameid, level, sum(effective_bet) as effective_bet, sum(record_num) as record_num,sum(total_num) as total_num, sum(wincoins) as wincoins, base, robot_type, fee 
                      FROM  {$bcreportTable} 
                      WHERE {$where} GROUP BY gameid, base  ORDER BY gameid, level DESC) AS aa
                 ORDER BY gameid, base ASC";

        $ret = oo::commonOprDb('common')->getAll($sql , 1);

        $data['list'] = $ret;
        return json_encode($data);
    }


    /**
     * 机器人输赢统计
     * @param $param
     * @return string
     */
    public function loseWinTotalByDate($param)
    {
        $stime = oo::functions()->uint($param['stime']);
        $etime = oo::functions()->uint($param['etime']);

        //写入时间是后一天，所以查询时相应加一天
        if(empty($stime)){
            $stime = strtotime("0:0:0");
            $sdate = date('Ymd');
        }else{
            $sdate = date('Ymd', $stime);
//            $stime = strtotime("$date +1 day ");
        }

        if(empty($etime)){
            $etime = strtotime("23:59:59");
            $edate = date('Ymd');
        }else{
            $edate = date('Ymd', $etime);
//            $etime = strtotime("$date +1 day ");
        }


        $bcreportTable = otable::robotLoseWin();
        $where = " date >= {$sdate} ";
        $where .= " AND date <= {$edate} ";

        $sql = "SELECT  date,  sum(wincoins) as wincoins
                      FROM  {$bcreportTable} 
                      WHERE {$where} GROUP BY date";

        $ret = oo::commonOprDb('common')->getAll($sql , 1);

        $list = [];
        foreach ($ret as $val){
            $list[$val['date']] = $val['wincoins'] ?? 0;
        }

        $data['list'] = $list;
        return json_encode($data);
    }



}
