<?php
namespace App\Lib\Biz\Game\Parse;
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/11/30
 * Time: 16:42
 */
class GameParse extends BaseParse {
	protected $game;
	protected $name;

	/**
	 * 开奖号吗解析
	 */
	function prizeParse($data, $game = '') {
		$this->game = $game;
		$this->name = trans('common.' . $game);
		$prize = json_decode($data['prizes'], 1);
		if (is_array($prize) && count($prize) > 0) {
			$prize = current($prize);
			if (is_array($prize) && count($prize) > 0) {
				foreach ($prize as $k => $v) {
					if (is_array($v) && count($v) > 0) {
						$data[$k] = '';
						foreach ($v as $key => $value) {
							$data[$k] .= trans($this->game . '.' . $k . '-' . $key) . '<br/>';
						}
					}
				}
			}
		}

		unset($data['prizes']);
//        echo '<pre>';
		//        print_r($data);
		//        echo '-------------';
		//解析开奖补充号码
		if (isset($data['extra']) && !empty($data['extra'])) {
			$extra = json_decode($data['extra'], 1);
			if (is_array($extra) && count($extra) > 0) {
				foreach ($extra as $kq => $vq) {
					if (is_numeric($vq)) {
						//处理数字开奖号码
						if ($vq < 0) {
							//处理未开出奖
							$data[$kq] = '';
						} else {
							$data[$kq] = $vq;
						}
					} else {
						//非数字
						if (is_array($vq) && count($vq)) {
							$data[$kq] = '';
							foreach ($vq as $ka => $va) {
								if (is_array($va) && count($va)) {
									foreach ($va as $kw => $vw) {
										$data[$kq] .= trans($this->game . '.' . $kq . '-' . $vw);
									}
								} else {
									$data[$kq] .= trans($this->game . '.' . $va) . ' ';
								}
							}
						} else {
							$data[$kq] = trans($this->game . '.' . $vq);
						}
					}
				}
				if (isset($data['one'])) {
					$data['prizes_code'] = $data['one'] . '+' . $data['two'] . '+' . $data['three'] . '=' . $data['code'];
				} else if (isset($data['prizes_code'])) {
					$data['prizes_code'] = $data['prizes_code'];
				}
			}
		}
		if (!isset($data['prizes_code'])) {
			$data['prizes_code'] = $data['codes'];
		}
		$data['name'] = $this->name;
		$data['game'] = $this->game;
		//中奖撤单未中奖状态的处理
		if (isset($data['game_status'])) {
			if ($data['status'] == 4) {
				$data['game_status'] = 4;
			}
			$lang = trans('status.money_buy.game_status');
			$data['game_status'] = $lang[$data['game_status']];
		}
		unset($data['status']);
		return $data;
	}

	//赔率
    function getSettings($game = 'sixlottery') {
        $game = empty($game)?'sixlottery':$game;
        //权限检测
        $arrys = array(

            'series_twospecial',//二中特
            'series_threetwo'

        );
        if(!checkRriv('/odds/game/'.$game)){
            return responseToJson(array());
        }
        $db = new \App\Models\GameOdds();
        $data = $db->getOdds($game);
        if($data<0){
            return responseToJson($data);
        }
        $data = json_decode($data['odds'], 1);
        $array = array();
        $i = 0;

//        var_dump($arrys,$data);
//        exit;
        foreach ($arrys as $v){
            $bs[$v]=$data[$v];
        }
        return $bs;

    }

	/**
	 *
	 */
	function betParse($data, $game = '') {
		$this->game = $game;
		$this->name = trans('common.' . $game);
		//获取开奖号码
		if (isset($data['extra']) && !empty($data['extra'])) {
			$extra = json_decode($data['extra'], 1);
			if (is_array($extra) && count($extra) > 0) {
				if (isset($extra['one'])) {
					$data['prizes_code'] = $extra['one'] . '+' . $extra['two'] . '+' . $extra['three'] . '=' . $extra['code'];
				} else if (isset($data['prizes_code'])) {
					$data['prizes_code'] = $data['prizes_code'];
				}
			}
		}
		if (!isset($data['prizes_code'])) {
			$data['prizes_code'] = $data['codes'];
		}
		$data['name'] = $this->name;
		$data['game'] = $this->game;
		//解析投注号码
		if (isset($data['extra']) && !empty($data['extra'])) {
			$codes = json_decode($data['codes'], 1);
			if (is_array($codes) && count($codes) > 0) {
				foreach ($codes as $k => $v) {
					if (is_array($v) && count($v) > 0) {
						$data[$k] = '';
						foreach ($v as $ka => $va) {
							$data[$k] .= trans($this->game . '.' . $k . '-' . $ka) . ' ';
						}
					}
				}
			}
		}
		//中奖撤单未中奖状态的处理
		if (isset($data['game_status'])) {
			if ($data['status'] == 4) {
				$data['game_status'] = 4;
			}
			$lang = trans('status.money_buy.game_status');
			$data['game_status'] = $lang[$data['game_status']];
		}
		unset($data['status']);
		return $data;
	}


	/**
	 * 单注可赢金额计算
	 */
	function winMomey($data) {
		$winMoney = 0;
        $stmoney = 0;
        $stnum = 0;
        $stodds = 0;
        $n1 = 0;
        $n2 = 0;
        $num = 0;
        $groupnum = array();
		$data = json_decode($data, 1);
        /*echo '<pre>';
        print_r($data);
        echo '</pre>';*/
		foreach ($data as $k => $v) {
			if (is_array($v) && count($v) > 0) {
				foreach ($v as $key => $value) {
                    $stmoney = $value[0];
                    $stodds = $value['settings']['multiple'];
                    if($k=="specialthree"){
                        $stnum++;
                        continue;
                    }
                    if($k=="tsnumro"){
                        (strlen($key)==1)?$n1++:$n2++;
                        continue;
                    }
                    if(in_array($k, $this->typeGroup())){
                        $stnum++;
                        $num = $this->numTg($k);
                        continue;
                    }
                    if(in_array($k, $this->reGroupPlaytype())){
                        $seat = explode('_', $key);
                        if(isset($groupnum[$seat[0]])){
                            $groupnum[$seat[0]] += 1;
                        }else{
                            $groupnum[$seat[0]] = 1;
                        }
                        continue;
                    }
					if (isset($value['settings']['multiple']['max'])) {
						$winMoney += $value[0] * $value['settings']['multiple']['max'];
					} else {
						$winMoney += $value[0] * $value['settings']['multiple'];
					}
				}
			}
		}
        //echo '<br>n1:'.$n1.';n2:'.$n2;
        //echo '<br>stmoney:'.$stmoney.';stodds:'.$stodds;
        if($stnum>2){
            $stnum = $stnum*($stnum-1)*($stnum-2)/(3*2);
            $winMoney += $stmoney * $stnum * $stodds;
        }else if($stnum>=$num && $stnum>0){
            $t1=1;
            $t2=1;
            for($i=0;$i<$num;$i++){
                $t1 *= ($stnum-$i);
                $t2 *= ($i+1);
            }
            $winMoney += $stmoney * ($t1/$t2) * $stodds;
        }
        //echo '<br>ws:'.$winMoney;
        if($n1>0 && $n2>0){
            $winMoney += $stmoney * $n1*$n2 * $stodds;
        }
        if(!empty($groupnum)){
            $groupsum = 1;
            foreach ($groupnum as $key => $value) {
                $groupsum *= $value;
            }
            $winMoney += $stmoney * $groupsum * $stodds;
        }
        //echo '<br>winmoney:'.$winMoney;
		return $winMoney;
	}

    //通用组合玩法
    private function typeGroup(){
        return array('twodiffnum','thdiffnum','specialthree');
    }

    //组合玩法每组个数
    private function numTg($type){
        $arr = array(
            'twodiffnum' => 2,
            'thdiffnum' => 3,
            'specialthree' => 3,
        );
        return $arr[$type];
    }

    //可重复分组玩法
    private function reGroupPlaytype(){
        return array('fronttwostar','fronttwostar','behindthreestar','frontthreestar','fourstar','fivestar');
    }

    //处理28开13或14游戏赔率
    private function speseHanddle($data){
        //判断是否投注混合类型玩法
        $buycode = json_decode($data['codes'],1);
        if(!isset($buycode['mix']))return -1;

        //获取用户当前游戏本期投注总额
        $summoney = \App\Models\MoneyBuy::where('account_identity',$data['account_identity'])->where('game_no',$data['game_no'])->where('game_name',$data['game_name'])->where('status','<>',4)->sum('money');
        if($summoney<=0)return -1;

        //获取混合玩法对应赔率
        $odds = \App\Models\GameOdds::select('odds')->where('game_type',$data['game_name'])->first();
        if(!$odds || count($odds->toArray())<1)return -1;
        $odds = json_decode($odds->odds,1);
        
        foreach ($buycode['mix'] as $k => $v) {
            $tmoney = 0;
            $key = 'mix-' . $k;
            if(isset($odds[$key]['settings']['cost'])){
                foreach ($odds[$key]['settings']['cost'] as $ok => $ov) {
                    if($summoney<=$ov['max'] && $summoney>$tmoney){
                        $buycode['mix'][$k]['settings']['multiple'] = $ov['multiple'];
                        $tmoney = $ov['max'];
                    }
                }
            }
        }
        $data['codes'] = json_encode($buycode);
        return $data;
    }

	/**
	 * 单注号码解析
	 * @param $data 号码json
	 * @param $game
	 * @param $sum_code 号码开奖
	 * @return array
	 */
    function dataChange($data,$Prescode,$sum_code='',$sum_money=0) {
        //echo '<pre>';
        /*print_r($data);
        print_r($Prescode);*/
        $point = [
            '1001'=>'pc投注',
            '1002'=>'wap投注',
            '1003'=>'长龙投注'
        ];
        $args = array();
        //28开13或14处理赔率
        if(!empty($sum_code)&&($sum_code==13||$sum_code==14)){
            $odds28 = $this->speseHanddle($data);
            if(is_array($odds28))$data=$odds28;
        }

        if ($data['game_name'] == 'diceone') {
            $buy_code = json_decode($data['buycodes'], 1); //该注购买号码
            $prize_codesd = json_decode($data['prizes'], 1); //改注中奖号码
            $prize_code = $prize_codesd['data']; //改注中奖号码
        }else{
            $buy_code = json_decode($data['codes'], 1); //该注购买号码
            $prize_code = json_decode($data['prize'], 1); //改注中奖号码
        }

        //$grouptype = $this->getGroupType();
        $sscgrouptype = $this->SscGroupType();//时时彩组合玩法
        $groupdata = array();
        $groupnum = array();
        foreach ($buy_code as $bk => $bv) {
            //处理时时彩的
            if(array_key_exists($bk,$sscgrouptype)){
                foreach ($bv as $ck => $cv) {
                    $seat = explode('_', $ck);
                    $groupnum[$seat[0]][] = $ck;
                    if(!empty($data['remark'])){
                        $remark = explode('|',$data['remark']);
                        if(isset($remark[0]) && isset($remark[1]) && !empty($remark[1]) && isset($remark[2]) && isset($point[$remark[1]])){
                            $newremark = $remark[0].'|'.$point[$remark[1]].'|'.$remark[2];
                        }else{
                            $newremark = '';
                        }
                        
                    }else{
                        $newremark = '';
                    }
                    
                    if(isset($groupdata[0]['play'])){
                        $groupdata[0]['p_code'] .= ','.$ck;
                        $groupdata[0]['codes'] .= ' '.trans($data['game_name'] . '.' . $ck);
                    }else{
                        $a = trans($data['game_name'] . '.' . $ck);
                        $tempdata = array(
                            'money' => $cv['0'],
                            'odds' => $cv['settings']['multiple'],
                            'play' => $bk,
                        );

                        $groupdata[0] = array(
                            'odds' => $cv['settings']['multiple'],
                            'back' => empty($cv['settings']['back'])?0:$cv['settings']['back'],
                            'codes' => trans($data['game_name'] . '.' . $bk). '-' . $a,
                            'money' => $cv['0'],
                            'maybe' => 0,
                            'p_code' => $ck,
                            'get_money' => 0,
                            'play' => $bk,
                            'gameType' => trans($data['game_name'] . '.' . $bk),
                            'remark' =>$newremark
                        );
                    }
                }

                $getmaybemoney = $this->getMaybeMoney($groupnum,$tempdata);//计算投注金额与可赢金额
                $groupdata[0]['money'] = $getmaybemoney['money'];
                $groupdata[0]['maybe'] = $getmaybemoney['maybe'];

                if ($data['status'] == 4) {
                    $groupdata[0]['prize_status'] = '撤单';
                    $groupdata[0]['get_money']=0;
                } else if ($data['game_status'] == 0) {
                    $groupdata[0]['prize_status'] = '待开奖';
                    $groupdata[0]['get_money']=0;
                }else{
                    if(empty($prize_code)){
                        $groupdata[0]['get_money'] = -$groupdata[0]['money'];
                        $groupdata[0]['prize_status'] = '未中奖';
                    }else{
                        $prizedata = $this->getPrizedata($groupnum,$tempdata,$prize_code);
                        $groupdata[0]['get_money'] = $prizedata['prize_money']-$groupdata[0]['money'];
                        $groupdata[0]['prize_status'] = $prizedata['prize_status'];
                    }
                }
                
            }                   
        }

        if(!empty($groupdata)){
            return $groupdata;
        }

        if (array_key_exists("head_three", $buy_code)) {
            $paramt = $this->CodesData($buy_code, 3, 3, 2);
        } elseif (array_key_exists("head_two", $buy_code)) {
            $paramt = $this->CodesData($buy_code, 2, 2, 2);
        }elseif (array_key_exists("thdiffnum", $buy_code)) {
            $paramt = $this->CodesData($buy_code, 3, 3, 1);
        }elseif (array_key_exists("twodiffnum", $buy_code)) {
            $paramt = $this->CodesData($buy_code, 2, 2, 1);
        }elseif (array_key_exists("tsnumro", $buy_code)) {
            $paramt = $this->CodesData($buy_code, 2, 2, 1);
        } elseif (array_key_exists("specialthree", $buy_code)) {
            $st_bcode['specialthree'] = $buy_code['specialthree'];
            $paramt = $this->CodesData($st_bcode, 3, 3, 1);
            $buy_code['specialthree'] = $paramt['specialthree'];
            $paramt = $buy_code;
        } else{
            $paramt= $buy_code;
        }
        //print_r($paramt);exit;
        $setting=array();//赔率数组存储
        //购买号码解析
        if (is_array($paramt) && count($paramt) > 0) {
            $i = 0;
            $a="";
            
            foreach ($paramt as $k => $v) {
                if (is_array($v) && count($v) > 0) {
                    foreach ($v as $k1 => $v1) {
                        $name =trans($data['game_name'] . '.' . $k). '-' . trans($data['game_name'] . '.' . $k1);
                        $setting[$i]=$v1['settings'];
                        $newremark = '';
                        if(!empty($data['remark'])){
                            $remark = explode('|',$data['remark']);
                            if(is_array($remark) && count($remark)>1){
                                if(isset($point[$remark[1]])){
                                    $newremark = $remark[0] . '|' . $point[$remark[1]] . '|' . $remark[2];
                                }                                
                            }else{
                                $newremark = '';
                            }
                            
                           
                        }else{
                            $newremark = '';
                        }
                        if(strpos($name,$data['game_name']) !== false){
                            $datas=explode(',',$k1);
                            foreach ($datas as $value){

                                if ($value > 0 || strlen($value) == 1){
                                    $a.=$value.",";
                                }else{
                                    $a.=trans($data['game_name'] . '.' . $value).",";
                                }


                            }
                            $a= rtrim($a, ',');
                            $args[$i] = array(
                                'gameType' => trans($data['game_name'] . '.' . $k),
                                'codes' => trans($data['game_name'] . '.' . $k). '-' . $a,
                                'money' => $v1[0],
                                'maybe' => $v1[0] * $v1['settings']['multiple'],
                                'play' => $k,
                                'p_code' => $k1,
                                'back' => empty($v1['settings']['back']) ? 0 : $v1['settings']['back'],
                                'remark' =>$newremark
                            );
                        }else{
                            $args[$i] = array(
                                'gameType' => trans($data['game_name'] . '.' . $k),
                                'codes' => trans($data['game_name'] . '.' . $k). '-' . trans($data['game_name'] . '.' . $k1),
                                'money' => $v1[0],
                                'maybe' => $v1[0] * $v1['settings']['multiple'],
                                'play' => $k,
                                'p_code' => $k1,
                                'back' => empty($v1['settings']['back']) ? 0 : $v1['settings']['back'],
                                'remark' =>$newremark
                            );
                        }

                        if (isset($v1['settings']['multiple']['max'])) {
                            $args[$i]['odds'] = $v1['settings']['multiple']['max'];
                        } else {
                            $args[$i]['odds'] = $v1['settings']['multiple'];
                        }


                        if ($data['status'] == 4) {
                            $args[$i]['prize_status'] = '撤单';
                            $args[$i]['get_money']=0;
                        } else if ($data['game_status'] == 0) {
                            $args[$i]['prize_status'] = '待开奖';
                            $args[$i]['get_money']=0;
                        }else{
                            $args[$i]['prize_status']='未中奖';
                            $args[$i]['get_money']=-$v1[0];
                        }
                        $i++;
                        $a="";
                    }
                }

            }
        }

		//echo '中奖号码';
		//print_r($setting);
        //中奖判断

        if ($data['status'] != 4){
            if (is_array($prize_code) && count($prize_code)) {

                foreach ($prize_code as $kk => $vv) {
                    $name = $k;
                }

                if (is_array($Prescode) && count($Prescode) > 0){
                    $color_code = json_decode($Prescode['prizes'], 1);
                    $code_num[$name]=isset($color_code['data']['poscode'])?$color_code['data']['poscode']:'';
                    $thdiffnum[$name]=isset($color_code['data']['thdiffnum'])?$color_code['data']['thdiffnum']:'';
                    $twodiffnum[$name]=isset($color_code['data']['twodiffnum'])?$color_code['data']['twodiffnum']:'';
                    $tsnumro[$name]=isset($color_code['data']['tsnumro'])?$color_code['data']['tsnumro']:'';
                    //$thdiffnumdt[$name]=isset($color_code['data']['thdiffnumdt'])?$color_code['data']['thdiffnumdt']:'';
                }
                $color_code=current($color_code);

                if (array_key_exists("head_three", $buy_code)) {
                    $paramt = $this->CodesDatas($code_num,3, 3, 1);
                } elseif (array_key_exists("head_two", $buy_code)) {
                    $paramt = $this->CodesDatas($code_num,2, 2, 1);
                }elseif (array_key_exists("thdiffnum", $buy_code)) {
                    $paramt = $this->CodesDatas($thdiffnum,3, 3, 1);
                }elseif (array_key_exists("twodiffnum", $buy_code)) {
                    $paramt = $this->CodesDatas($twodiffnum,2, 2, 1);
                }elseif (array_key_exists("tsnumro", $buy_code)) {
                    $paramt = $this->CodesDatas($tsnumro,2, 2, 1);
                }else{
                    $paramt = $prize_code;
                }
                //print_r($paramt);

                //中奖号码
                foreach ($paramt as $p_k => $p_v) {
                    //购买号码
                    foreach ($args as $num => $v) {
                        if ($v['play'] == $p_k) {//玩法相同
                            if (is_array($p_v) && count($p_v) > 0) {
                                foreach ($p_v as $p_k1 => $p_v2) {
                                    if ($data['game_name'] == 'diceone') {
                                        $p_v2=$p_k1;
                                    }

                                    if ($v['p_code'] == $p_v2) {//号码相同
                                        $args[$num]['prize_status'] = '中奖';
                                        //开出和的时候显示和，不含单双
                                        if(isset($color_code[$p_k]['equal'])&&$p_v2!='single'&&$p_v2!='dobule'){
                                            $args[$num]['prize_status'] = '和';
                                        }
                                        $args[$num]['get_money']=$v['money']*$v['odds']-$v['money'];
                                        //28的总消费限制
                                        if(isset($setting[$num]['cost'])&&is_array($setting[$num]['cost'])&&count($setting[$num]['cost'])>0){
                                            if(!empty($sum_code)&&($sum_code==13||$sum_code==14)){
                                                foreach ($setting[$num]['cost'] as $v_p_l){
                                                    if($v_p_l['min']<=$sum_money&&$v_p_l['max']>=$sum_money){
                                                        $args[$num]['odds']=$v_p_l['multiple'];
                                                        $args[$num]['maybe']=$v['money']*$v_p_l['multiple'];
                                                        $args[$num]['get_money']=$v['money']*$v_p_l['multiple']-$v['money'];
                                                    }
                                                }
                                            }
                                        }

                                        break;
                                    } elseif ($p_k == 'specialthree') {
                                        foreach ($p_v as $p_k1 => $p_v2) {
                                            $stcodes = explode(',', $v['p_code']);
                                            if(in_array($p_v2, $stcodes)){
                                                $args[$num]['prize_status'] = ($p_v2==13 || $p_v2==14)?'和':'中奖';
                                                $v['odds'] = ($p_v2==13 || $p_v2==14)?1:$v['odds'];
                                                $args[$num]['get_money'] = $v['money'] * $v['odds'] - $v['money'];
                                                break;
                                            } else {
                                                $args[$num]['prize_status'] = '未中奖';
                                                $args[$num]['get_money'] = -$v['money'];
                                            }
                                        }
                                    }else if($p_k ==''){

                                    }else {
                                        $args[$num]['prize_status'] = '未中奖';
                                        $args[$num]['get_money']=-$v['money'];
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        //echo '</pre>';
        return $args;
    }

    //计算投注金额与可赢金额
    private function getMaybeMoney($groupnum,$codedata){
        $resmoney = array();
        $sumnum = 1;
        foreach ($groupnum as $key => $value) {
            $sumnum *= count($value);
        }
        $resmoney = array(
            'money' => $sumnum * $codedata['money'],
            'maybe' => $sumnum * $codedata['money'] * $codedata['odds'],
        );
        return $resmoney;
    }

    //号码中奖判断处理
    private function getPrizedata($groupnum,$codedata,$prize_code){
        $groupdata = array(
            'prize_money' => $codedata['money'] * $codedata['odds'],
            'prize_status' => '中奖',
        );
        
        foreach ($prize_code[$codedata['play']] as $key => $value) {
            $seat = explode('_', $value);
            if(isset($groupnum[$seat[0]]) && !(in_array($value, $groupnum[$seat[0]]))){
                $groupdata['prize_status'] = '未中奖';
                $groupdata['prize_money'] = 0;
                break; 
            }
        }
        return $groupdata;
    }
    

	//购买号码
	function buyCodes($data, $game) {
	}
	//投注内容
	function packbet($data, $game) {
        $this->game = $game;
        $this->name = trans('common.' . $game);
        switch ($game) {
            case 'xy28':
                $_parse=new Xy28Parse;
                break;
            case 'jnd28':
                $_parse=new Jnd28Parse;
                break;
            case 'gpxy28':
                $_parse=new Gpxy28Parse;
                break;
            case 'gpjnd28':
                $_parse=new Gpjnd28Parse;
                break;
            case 'hs28':
                $_parse=new Hs28Parse;
                break;
            case 'dj28':
                $_parse=new Dj28Parse;
                break;
            case 'xjp28':
                $_parse=new Xjp28Parse;
                break;
            case 'tw28':
                $_parse=new Tw28Parse;
                break;
            case 'one28':
                $_parse=new One28Parse;
                break;
            default:
                break;
        }
        if(in_array($game, array('xy28','jnd28','gpxy28','gpjnd28','hs28','dj28','xjp28','tw28','one28'))){
            return $_parse->packbet($data,$game);
        }

        if($game=='diceone'){
            $data['bet_'] = '';
            if (isset($data['buycodes']) && !empty($data['buycodes'])) {
                $codes = json_decode($data['buycodes'], 1);
                if (is_array($codes) && count($codes) > 0) {
                    foreach ($codes as $k => $v) {
                        if (is_array($v) && count($v) > 0) {
                            $data['bet_'] .= '<b>' . trans($game . '.' . $k) . '</b>:'; //玩法
                            foreach ($v as $ka => $va) {
                                if (is_numeric($ka) || preg_match("/^\d+_\d+$/", $ka)) {
                                    $data['bet_'] .= $ka .   '￥'.$va[0];
                                } else {
                                    $data['bet_'] .= trans($game . '.' . $ka) . '￥'.$va[0];
                                }
                            }
                            $data['bet_'] .= '<br/>';
                        }
                    }
                }
            }
            if (isset($data['betstatus'])) {
                if ($data['betstatus'] == 2) {
                    $data['winstatus'] = 4;
                }
                $lang = trans('status.money_buy.game_status');
                $data['game_status'] = $lang[$data['winstatus']];
            }
            $data['name'] = $this->name;
            $data['game'] = $this->game;
            unset($data['status']);
            return $data;
        }

		//获取开奖号码
		if (isset($data['extra']) && !empty($data['extra'])) {
			$extra = json_decode($data['extra'], 1);
			if (is_array($extra) && count($extra) > 0) {
				if (isset($extra['one'])) {
					$data['prizes_code'] = $extra['one'] . '+' . $extra['two'] . '+' . $extra['three'] . '=' . $extra['code'];
				} else if (isset($data['prizes_code'])) {
					$data['prizes_code'] = $data['prizes_code'];
				}
			}
		}
		if (!isset($data['prizes_code'])) {
			$data['prizes_code'] = $data['codes'];
		}
		$data['name'] = $this->name;
		$data['game'] = $this->game;
		//投注号码解析
		$data['bet_'] = '';
        $grouppt = $this->GroupPtname();
		if (isset($data['codes']) && !empty($data['codes'])) {
			$codes = json_decode($data['codes'], 1);
			if (is_array($codes) && count($codes) > 0) {
				foreach ($codes as $k => $v) {
					if (is_array($v) && count($v) > 0) {
						$data['bet_'] .= '<b>' . trans($this->game . '.' . $k) . '</b>:'; //玩法
						foreach ($v as $ka => $va) {
							if (is_numeric($ka) || preg_match("/^\d+_\d+$/", $ka)) {
								$data['bet_'] .= $ka;
							} else {
                                if($k=='dragon')$ka = 'dragon-'.$ka;
								$data['bet_'] .= trans($this->game . '.' . $ka);
							}
                            if(!in_array($k, $grouppt))$data['bet_'] .= '¥'.$va[0];
                            $data['bet_'] .= ' ';
						}
						$data['bet_'] .= '<br/>';
					}
				}
			}
		}    

		$data['ceshi'] = $data['game_status'];
		//中奖撤单未中奖状态的处理
		if (isset($data['game_status'])) {
			if ($data['status'] == 4) {
				$data['game_status'] = 4;
			}
			$lang = trans('status.money_buy.game_status');
			$data['game_status'] = $lang[$data['game_status']];
		}

		unset($data['status']);
		return $data;

	}

    function CodesData($data, $num, $is_prize = '', $is_str = '', $game = '')
    {
        $codes = array();
        foreach ($data as $k => $v) {
            $type = $k;
            if (is_array($v) && count($v) > 0) {
                foreach ($v as $k1 => $v1) {
                    $money = $v[$k1][0];
                    if ($k == 'series_twospecial'){
                        $codes[]= str_replace('g_','',$k1);
                    }else{
                        $codes[] = $k1;
                    }
                }
            }


        }

        $datas=array();
        $datad=array();
        sort($codes);
        if($is_str==2){ //前二前三号码不同位置不重复组合处理
            $a = $this->getPartPosition($codes, $num);
        }else{
            $a = $this->Isniu($codes, $num);
        }

        if($type=='tsnumro'){
            $b = array();
            $i = 0;
            foreach ($a as $k => $v) {
                $t = $v;
                $v = explode(',', $v);
                if(strlen($v[0])!=strlen($v[1])){
                    $b[$i] = $t;
                    $i++;
                }
            }
            if(!empty($b))$a = $b;
        }
        
        foreach ($data as $val){
            foreach ($val as $value){
                foreach ($a as $k=>$v){
                    $datas[$v]=$value;
                }
            }
        }

        $datad[$type]=$datas;
        return $datad;
    }

    //号码分组不同位置不重复号码分组（前二前三）
    function getPartPosition($arr, $m)
    {
        $result = array();
        $temp = array();
        foreach ($arr as $k => $v) {
            $tc = explode('_', $v);
            $temp[$tc[0]][] = $tc[1];
        }
        $i=0;
        $j=0;
        $h = 0;
        $tarr = array();
        //print_r($temp);
        foreach ($temp as $k => $v) {
            $j = count($result);
            $tarr = $result;
            $h = 0;
            //print_r($tarr);
            foreach ($v as $sk => $sv) {
                if($j>0){
                    $i = 0; 
                    $an = $h + $j;
                    for($h;$h<$an;$h++){
                        if(isset($result[$h])){
                            if(in_array($sv, $result[$h])){
                                unset($result[$h]);
                            }else{
                                $result[$h][$k] = $sv;
                            }
                        }else{
                            if(isset($tarr[$i]) && !in_array($sv, $tarr[$i])){
                                $result[$h] = $tarr[$i];
                                $result[$h][$k] = $sv;
                            }
                            $i++;
                        }
                    }
                }else{
                    $result[$sk][$k] = $sv;
                }
            }
        }

        foreach ($result as $k => $v) {
            $tstr = '';
            foreach ($v as $key => $value) {
                $tstr .= empty($tstr)?($key.'_'.$value):(','.$key.'_'.$value);
            }
            $result[$k] = $tstr;
        }
        //print_r($result);exit;
        return $result;
    }

    //组合玩法类型
    private function GroupPtname(){
        return array('head_three','head_two','fronttwostar','fourstar','frontthreestar','behindthreestar','fivestar');
    }

    //时时彩组合玩法
    private function SscGroupType(){
        return array(
            'fronttwostar' => array(2,2,1),
            'fourstar' => array(4,4,1),
            'frontthreestar' => array(3,3,1),
            'behindthreestar' => array(3,3,1),
            'fivestar' => array(5,5,1)
        );
    }

    //组合玩法
    private function getGroupType(){
        return array(
            'head_three' => array(3,3,1),
            'head_two' => array(2,2,1),
            'fronttwostar' => array(2,2,1),
            'fourstar' => array(4,4,1),
            'frontthreestar' => array(3,3,1),
            'behindthreestar' => array(3,3,1),
            'fivestar' => array(5,5,1)
        );
    }

    function CodeData($data)
    {
        $codes = array();
        foreach ($data as $k => $v) {
            $type = $k;
            if (is_array($v) && count($v) > 0) {
                foreach ($v as $k1 => $v1) {
                    $temp = explode('_', $k1);
                    $money = $v[$k1][0];
                    if (!array_key_exists($temp[0] - 1, $codes)) {
                        $codes[$temp[0] - 1] = array();
                        array_push($codes[$temp[0] - 1], intval($temp[1]));
                    } else {
                        array_push($codes[$temp[0] - 1], intval($temp[1]));
                    }
                }
            }
        }
        $tempArr = $this->countNum($codes, $money, $type);
        return $tempArr;

    }

    function CodesDatas($data, $num, $is_prize = '', $is_str = '', $game = '')
    {

        $codes = array();
        foreach ($data as $k => $v) {
            $type = $k;
            if (is_array($v) && count($v) > 0) {
                foreach ($v as $k1 => $v1) {
                    $money = $v[$k1][0];
                    $codes[] = $k1;
                }
            }
        }

//        var_dump($codes,$data,$type);
//        exit;
        $datas=array();
        $datad=array();
        sort($codes);
        $a = $this->Isniu($codes, $num);


                foreach ($a as $k=>$v){
                    $datas[$k]=$v;

                }

//            var_dump($a);
//        exit;
        $datad[$type]=$datas;

//        foreach ($a as $k => $v) {
//            $codelists[$k]['codes'] = $v;
//            $codelists[$k]['money'] = $money;
//            $codelists[$k]['codeDate'] = $type;
//            if (!empty($is_prize)) {
//                $codelists[$k]['is_prize'] = $is_prize;
//            }
//            if (!empty($is_str)) {
//                $codelists[$k]['is_str'] = $is_str;
//            }
//            if (!empty($game)) {
//                $codelists[$k]['game'] = $game;
//            }
//        }
        return $datad;

    }
    function getCombinationToString($arr, $m)
    {
        $result = array();
        if ($m == 1) {
            return $arr;
        }
        if ($m == count($arr)) {
            $result[] = implode(',', $arr);
            return $result;
        }
        $temp_firstelement = isset($arr[0]) ? $arr[0] : '';
        unset($arr[0]);
        $arr = array_values($arr);
        $temp_list1 = $this->getCombinationToString($arr, ($m - 1));
        foreach ($temp_list1 as $s) {
            $s = $temp_firstelement . ',' . $s;
            $result[] = $s;
        }
        unset($temp_list1);
        $temp_list2 = $this->getCombinationToString($arr, $m);
        foreach ($temp_list2 as $s) {
            $result[] = $s;
        }
        unset($temp_list2);
        return $result;
    }

    function Isniu($codes, $num)
    {
        $r = $this->getCombinationToString($codes, $num);
        return $r;
    }


    function CodesDatasd($data, $num, $game_name)
    {
        $codes = array();
        foreach ($data as $k => $v) {
            $type = $k;
            if (is_array($v) && count($v) > 0) {
                foreach ($v as $k1 => $v1) {
                    $money = $v[$k1][0];
                    $codes[] = $k1;

                }
            }
        }
//         dump($codes);
        foreach ($codes as $v) {
            $coded[] = lang($game_name)->get($v);
        }
//        dump($coded);
        $a = $this->Isniu($codes, $num);
//        dump($a);
        foreach ($a as $k => $v) {
            $codelists[$k]['codes'] = $v;
            $codelists[$k]['money'] = $money;
            $codelists[$k]['codeDate'] = $type;
        }
//        dump($codelists);
        return $codelists;

    }
    //六合彩正码过关解析
    function sixGenPass($data){
        $buy_code = json_decode($data['codes'], 1); //该注购买号码
        $prize_code = json_decode($data['prize'], 1); //改注中奖号码
        $game=$data['game_name'];
        if (is_array($buy_code) && count($buy_code) > 0) {
                $return_data=array();
                //投注数组
            foreach ($buy_code as $k=>$v){
                $return_data['gameType']=trans($game. '.' . $k);//玩法
                    if (is_array($v) && count($v) > 0) {
                        $codes_content='';//号码
                        $money=0;//投注金额
                        $maybe=0;//可赢金额
                        $odds=1;//倍率
                        foreach ($v as $ka => $va) {
                            $codes_content.=trans($game . '.' . $ka).' ';//号码
                            $money+=$va[0];
                            $maybe += $va[0] * $va['settings']['multiple'];
                            $back = empty($va['settings']['back']) ? 0 : $va['settings']['back'];
                            $return_data['play'] = $k;
                            $return_data['p_code'] = $ka;
                            if (isset($va['settings']['multiple']['max'])) {
                                $odds *= $va['settings']['multiple']['max'];
                            } else {
                                $odds *= $va['settings']['multiple'];
                            }
                        }
                        //中奖状态
                        if ($data['status'] == 4) {
                            $return_data['prize_status'] = '撤单';
                        } else if ($data['game_status'] == 0) {
                            $return_data['prize_status'] = '待开奖';
                        }else if ($data['game_status'] == 1){
                            $return_data['prize_status']='中奖';
                            $odds=round($data['prize_money']/$data['money'],2);
                        } else if ($data['game_status'] == 2){
                            $return_data['prize_status']='未中奖';
                        }
                        $maybe=$odds*$data['money'];
                        $return_data['codes']=$codes_content;
                        $return_data['money']=round($data['money'],2);
                        $return_data['maybe']=round($maybe,2);
                        $return_data['odds']=round($odds,2);
                        $return_data['get_money']=round($data['get_money'],2);
                    }
                $res_data[]=$return_data;
                }
        }
        return $res_data;
    }
}