<?php


namespace freedom\utils;

/**
 * TODO 露珠类
 * Class Waybill
 * @package freedom\utils
 */
class Waybill
{

    /**
     * TODO Toning露珠获取定位方法
     * @param array $ns 局数组
     * @return array;
     */
    public static function waybillToning(array $ns): array
    {
        /**************************** 计算 showRoad start ***************************/
        $showRoad = array_chunk($ns,6);
        $showRoadLocation = array();
        foreach($showRoad as $k => $v){
            foreach($v as $key => $val){
                $pushData = array('show_x' => $k + 1, 'show_y' =>$key + 1, 'result' => $val['toning_result']);
                array_push($showRoadLocation,$pushData);
            }
        }
        /**************************** 计算 showRoad end ***************************/
        return (['status'=>true,'msg'=>'数据存在','waybill'=>$showRoadLocation]);
    }

    /**
     * TODO Roulette露珠获取定位方法
     * @param array $ns 局数组
     * @return array;
     */
    public static function waybillRoulette(array $ns): array
    {
        /**************************** 计算 showRoad start ***************************/
        $showRoad = array_chunk($ns,6);
        $showRoadLocation = array();
        foreach($showRoad as $k => $v){
            foreach($v as $key => $val){
                $pushData = array('show_x' => $k + 1, 'show_y' =>$key + 1, 'result' => $val['roulette_result']);
                array_push($showRoadLocation,$pushData);
            }
        }
        /**************************** 计算 showRoad end ***************************/
        return (['status'=>true,'msg'=>'数据存在','waybill'=>$showRoadLocation]);
    }
    /**
     * TODO Nn&Tc露珠获取定位方法
     * @param array $ns 局数组
     * @return array;
     */
    public static function waybillNn(array $ns): array
    {
        $showRoadLocation = array();
        foreach($ns as $k => $v){
            $pushData2 = array('show_x' => $k + 1, 'show_y' =>2,'type' => 2, 'result' => $v['result_player_1'], 'is_win' => $v['win_player_1']);
            $pushData3 = array('show_x' => $k + 1, 'show_y' =>3,'type' => 2, 'result' => $v['result_player_2'], 'is_win' => $v['win_player_2']);
            $pushData4 = array('show_x' => $k + 1, 'show_y' =>4,'type' => 2, 'result' => $v['result_player_3'], 'is_win' => $v['win_player_3']);
            if($v['win_player_1'] == 0 && $v['win_player_2'] == 0 && $v['win_player_3'] == 0){
                $banker_win = 1;
            }else{
                $banker_win = 0;
            }
            $pushData1 = array('show_x' => $k + 1, 'show_y' =>1,'type' => 1, 'result' => $v['result_banker'], 'is_win' => $banker_win);
            array_push($showRoadLocation,$pushData1);
            array_push($showRoadLocation,$pushData2);
            array_push($showRoadLocation,$pushData3);
            array_push($showRoadLocation,$pushData4);
        }
        return (['status'=>true,'msg'=>'数据存在','waybill'=>$showRoadLocation]);
    }
    /**
     * TODO DT&Baccarat露珠获取定位方法
     * @param array $ns 局数组
     * @return array;
     */
    public static function waybill(array $ns): array
    {
        /**************************** 计算 sanxingRoad start ***************************/
        $sanxingRoad = array();
        $firstTieNum = 0;
        $isFirst = true;
        $num = 0;
        foreach($ns AS $v){
            if($isFirst == true){
                if($v['result'] == 3){
                    $firstTieNum++;
                }elseif($v['result'] == 1 || $v['result'] == 2){
                    $v['tie_num'] = $firstTieNum;
                    $sanxingRoad[$num] = $v;
                    $isFirst = false;
                    $num++;
                }
            }else{
                if($v['result'] == 3){
                    $sanxingRoad[$num-1]['tie_num'] = $sanxingRoad[$num-1]['tie_num'] + 1;
                }elseif($v['result'] == 1 || $v['result'] == 2){
                    $v['tie_num'] = 0;
                    $sanxingRoad[] = $v;
                    $num++;
                }
            }
        }
        $sanxingRoad = array_chunk($sanxingRoad,3);
        $sanxingRoadLocation = array();
        foreach($sanxingRoad AS $k => $v){
            foreach($v as $key => $val){
                $pushData = array('show_x' => $k + 1, 'show_y' =>$key + 1, 'result' => $val['result'], 'pair' => $val['pair'], 'tie_num' => $val['tie_num']);
                array_push($sanxingRoadLocation,$pushData);
            }
        }
        /**************************** 计算 sanxingRoad end   ***************************/
        /**************************** 计算 showRoad start ***************************/
        $showRoad = array_chunk($ns,6);
        $showRoadLocation = array();
        foreach($showRoad as $k => $v){
            foreach($v as $key => $val){
                $pushData = array('show_x' => $k + 1, 'show_y' =>$key + 1, 'result' => $val['result'], 'pair' => $val['pair']);
                array_push($showRoadLocation,$pushData);
            }
        }
        /**************************** 计算 showRoad end ***************************/
        $bigRoad = array();
        $bigEyeRoad = array();
        $pathway = array();
        $roach = array();
        /**************************** 计算 bigRoad start ***************************/
        //列
        $yKey = 0;
        //行
        $xKey = 0;
        $last = array();
        foreach($ns AS $key => $value){
            if($value['pair'] == 1){
                $pair = 1;
            }elseif($value['pair'] == 2){
                $pair = 2;
            }elseif($value['pair'] == 3){
                $pair = 3;
            }else{
                $pair = 0;
            }
            if($key == 0 && $value['result'] == 3){
                $bigRoad[$yKey][$xKey] = array('result' => 3, 'tie_num' => 1, 'pair' => $pair);
                $last = array('yKey' => $yKey, 'xKey' => $xKey);
            }elseif($yKey == 0 && $xKey == 0 && !empty($last) && $value['result'] != 3){
                $bigRoad[$last['yKey']][$last['xKey']]['result'] = $value['result'];
                $bigRoad[$last['yKey']][$last['xKey']]['pair'] = $value['pair'];
                if(isset($ns[$key+1]) && $ns[$key+1]['result'] != $bigRoad[$last['yKey']][$last['xKey']]['result'] && $ns[$key+1]['result'] != 3){
                    $yKey++;
                    $xKey = 0;
                }elseif(isset($ns[$key+1]) && $ns[$key+1]['result'] == $bigRoad[$last['yKey']][$last['xKey']]['result'] && $ns[$key+1]['result'] != 3){
                    $xKey++;
                }
                $last = array('yKey' => $yKey, 'xKey' => $xKey);
            }elseif($key > 0 && $value['result'] == 3){
                $bigRoad[$last['yKey']][$last['xKey']]['tie_num'] = $bigRoad[$last['yKey']][$last['xKey']]['tie_num'] + 1;
                if(isset($ns[$key+1]) && $ns[$key+1]['result'] != $bigRoad[$last['yKey']][$last['xKey']]['result'] && $ns[$key+1]['result'] != 3 && $bigRoad[$last['yKey']][$last['xKey']]['result'] != 3){
                    $yKey++;
                    $xKey = 0;
                }elseif(isset($ns[$key+1]) && $ns[$key+1]['result'] == $bigRoad[$last['yKey']][$last['xKey']]['result'] && $ns[$key+1]['result'] != 3 && $bigRoad[$last['yKey']][$last['xKey']]['result'] != 3){
                    $xKey++;
                }
                $last = array('yKey' => $last['yKey'], 'xKey' => $last['xKey']);
            }else{
                $bigRoad[$yKey][$xKey] = array('result' => $value['result'], 'tie_num' => 0, 'pair' => $pair);
                if(isset($ns[$key+1]) && $ns[$key+1]['result'] != $bigRoad[$yKey][$xKey]['result'] && $ns[$key+1]['result'] != 3){
                    $yKey++;
                    $xKey = 0;
                }elseif(isset($ns[$key+1]) && $ns[$key+1]['result'] == $bigRoad[$yKey][$xKey]['result'] && $ns[$key+1]['result'] != 3){
                    $xKey++;
                }
                $last = array('yKey' => $yKey, 'xKey' => $xKey);
            }
        }
        //重新计算坐标
        $bigRoadLocation = array();
        $occupy = array();
        foreach($bigRoad AS $key => $value){
            $swerve = false;
            $swerveY = $key;
            foreach($value AS $k => $v){
                $show_y = $key;
                $show_x = $k;
                if($show_x > 5 && $swerve === false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = 5;
                    array_push($occupy,$show_y.'-'.$show_x);
                }elseif(in_array($show_y.'-'.$show_x,$occupy)){
                    if($swerve === false){
                        $swerve = $show_x - 1;
                    }
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupy,$show_y.'-'.$show_x);
                }elseif($swerve !== false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupy,$show_y.'-'.$show_x);
                }
                $pushArray = array('show_x' => $show_y+1, 'show_y' => $show_x+1, 'result' => $v['result'], 'pair' => $v['pair'], 'tie_num' => $v['tie_num']);
                array_push($bigRoadLocation,$pushArray);
            }
        }
        /**************************** 计算 bigRoad end ***************************/
        /**************************** 计算 bigEyeRoad start ***************************/
        $bigEyeRoadStart = false;
        $bigEyeRoadYKey = 0;
        $bigEyeRoadXKey = 0;
        $bigEyeRoadLast = array();
        foreach($bigRoad AS $key => $value){
            foreach($value AS $k => $v){
                if($key == 1 && $k == 1 && isset($bigRoad[1][1])){
                    if(isset($bigRoad[0][1])){
                        $bigEyeRoad[0][0] = array('result' => 1);
                    }else{
                        $bigEyeRoad[0][0] = array('result' => 2);
                    }
                    $bigEyeRoadStart = true;
                    $bigEyeRoadLast = $bigEyeRoad[0][0];
                    continue;
                }
                if($key == 2 && $k == 0 && !isset($bigEyeRoad[0][0])){
                    if(isset($bigRoad[0]) && isset($bigRoad[1]) && count($bigRoad[0]) == count($bigRoad[1])){
                        $bigEyeRoad[0][0] = array('result' => 1);
                    }else{
                        $bigEyeRoad[0][0] = array('result' => 2);
                    }
                    $bigEyeRoadStart = true;
                    $bigEyeRoadLast = $bigEyeRoad[0][0];
                    continue;
                }
                if($bigEyeRoadStart == true){
                    if($k == 0){		//第一个
                        $p1 = $key - 1;
                        $p2 = $key - 2;
                        if(count($bigRoad[$p1]) == count($bigRoad[$p2])){
                            $bigEyeRoadPushData = array('result' => 1);
                        }else{
                            $bigEyeRoadPushData = array('result' => 2);
                        }
                    }elseif($k == 1){	//第二个
                        if(isset($bigRoad[$key-1][$k])){
                            $bigEyeRoadPushData = array('result' => 1);
                        }else{
                            $bigEyeRoadPushData = array('result' => 2);
                        }
                    }else{				//第三个或者之后那些
                        if(isset($bigRoad[$key-1][$k-1]) && !isset($bigRoad[$key-1][$k])){
                            $bigEyeRoadPushData = array('result' => 2);
                        }else{
                            $bigEyeRoadPushData = array('result' => 1);
                        }
                    }
                    if($bigEyeRoadLast['result'] == $bigEyeRoadPushData['result']){
                        $bigEyeRoadXKey++;
                    }else{
                        $bigEyeRoadYKey++;
                        $bigEyeRoadXKey = 0;
                    }
                    $bigEyeRoad[$bigEyeRoadYKey][$bigEyeRoadXKey] = $bigEyeRoadPushData;
                    $bigEyeRoadLast = $bigEyeRoadPushData;
                }
            }
        }
        //重新计算坐标
        $bigEyeRoadLocation = array();
        $occupyEye = array();
        foreach($bigEyeRoad AS $key => $value){
            $swerve = false;
            $swerveY = $key;
            foreach($value AS $k => $v){
                $show_y = $key;
                $show_x = $k;
                if($show_x > 5 && $swerve === false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = 5;
                    array_push($occupyEye,$show_y.'-'.$show_x);
                }elseif(in_array($show_y.'-'.$show_x,$occupyEye)){
                    if($swerve === false){
                        $swerve = $show_x - 1;
                    }
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupyEye,$show_y.'-'.$show_x);
                }elseif($swerve !== false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupyEye,$show_y.'-'.$show_x);
                }
                $pushArray = array('show_x' => $show_y+1, 'show_y' => $show_x+1, 'result' => $v['result']);
                array_push($bigEyeRoadLocation,$pushArray);
            }
        }
        /**************************** 计算 bigEyeRoad end ***************************/
        /**************************** 计算 pathway start ***************************/
        $pathwayStart = false;
        $pathwayYKey = 0;
        $pathwayXKey = 0;
        $pathwayLast = array();
        foreach($bigRoad AS $key => $value){
            foreach($value AS $k => $v){
                if($key == 2 && $k == 1 && isset($bigRoad[2][1])){
                    if(isset($bigRoad[0][1])){
                        $pathway[0][0] = array('result' => 1);
                    }else{
                        $pathway[0][0] = array('result' => 2);
                    }
                    $pathwayStart = true;
                    $pathwayLast = $pathway[0][0];
                    continue;
                }
                if($key == 3 && $k == 0 && !isset($pathway[0][0])){
                    if(isset($bigRoad[0]) && isset($bigRoad[2]) && count($bigRoad[0]) == count($bigRoad[2])){
                        $pathway[0][0] = array('result' => 1);
                    }else{
                        $pathway[0][0] = array('result' => 2);
                    }
                    $pathwayStart = true;
                    $pathwayLast = $pathway[0][0];
                    continue;
                }
                if($pathwayStart == true){
                    if($k == 0){		//第一个
                        $p1 = $key - 1;
                        $p2 = $key - 3;
                        if(count($bigRoad[$p1]) == count($bigRoad[$p2])){
                            $pushData = array('result' => 1);
                        }else{
                            $pushData = array('result' => 2);
                        }
                    }elseif($k == 1){	//第二个
                        if(isset($bigRoad[$key-2][$k])){
                            $pushData = array('result' => 1);
                        }else{
                            $pushData = array('result' => 2);
                        }
                    }else{				//第三个或者之后那些
                        if(isset($bigRoad[$key-2][$k-1]) && !isset($bigRoad[$key-2][$k])){
                            $pushData = array('result' => 2);
                        }else{
                            $pushData = array('result' => 1);
                        }
                    }
                    if($pathwayLast['result'] == $pushData['result']){
                        $pathwayXKey++;
                    }else{
                        $pathwayYKey++;
                        $pathwayXKey = 0;
                    }
                    $pathway[$pathwayYKey][$pathwayXKey] = $pushData;
                    $pathwayLast = $pushData;
                }
            }
        }
        //echo "<pre>";
        //print_r($pathway);
        //echo "</pre>";
        //exit();
        //重新计算坐标
        $pathwayLocation = array();
        $occupyPathway = array();
        foreach($pathway AS $key => $value){
            $swerve = false;
            $swerveY = $key;
            foreach($value AS $k => $v){
                $show_y = $key;
                $show_x = $k;
                if($show_x > 5 && $swerve === false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = 5;
                    array_push($occupyPathway,$show_y.'-'.$show_x);
                }elseif(in_array($show_y.'-'.$show_x,$occupyPathway)){
                    if($swerve === false){
                        $swerve = $show_x - 1;
                    }
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupyPathway,$show_y.'-'.$show_x);
                }elseif($swerve !== false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupyPathway,$show_y.'-'.$show_x);
                }
                $pushArray = array('show_x' => $show_y+1, 'show_y' => $show_x+1, 'result' => $v['result']);
                array_push($pathwayLocation,$pushArray);
            }
        }
        /**************************** 计算 pathway end ***************************/
        /**************************** 计算 roach start ***************************/
        $roachStart = false;
        $roachYKey = 0;
        $roachXKey = 0;
        $roachLast = array();
        foreach($bigRoad AS $key => $value){
            foreach($value AS $k => $v){
                if($key == 3 && $k == 1 && isset($bigRoad[3][1])){
                    if(isset($bigRoad[0][1])){
                        $roach[0][0] = array('result' => 1);
                    }else{
                        $roach[0][0] = array('result' => 2);
                    }
                    $roachStart = true;
                    $roachLast = $roach[0][0];
                    continue;
                }
                if($key == 4 && $k == 0 && !isset($roach[0][0])){
                    if(isset($bigRoad[0]) && isset($bigRoad[3]) && count($bigRoad[0]) == count($bigRoad[3])){
                        $roach[0][0] = array('result' => 1);
                    }else{
                        $roach[0][0] = array('result' => 2);
                    }
                    $roachStart = true;
                    $roachLast = $roach[0][0];
                    continue;
                }
                if($roachStart == true){
                    if($k == 0){		//第一个
                        $p1 = $key - 1;
                        $p2 = $key - 4;
                        if(count($bigRoad[$p1]) == count($bigRoad[$p2])){
                            $pushData = array('result' => 1);
                        }else{
                            $pushData = array('result' => 2);
                        }
                    }elseif($k == 1){	//第二个
                        if(isset($bigRoad[$key-3][$k])){
                            $pushData = array('result' => 1);
                        }else{
                            $pushData = array('result' => 2);
                        }
                    }else{				//第三个或者之后那些
                        if(isset($bigRoad[$key-3][$k-1]) && !isset($bigRoad[$key-3][$k])){
                            $pushData = array('result' => 2);
                        }else{
                            $pushData = array('result' => 1);
                        }
                    }
                    if($roachLast['result'] == $pushData['result']){
                        $roachXKey++;
                    }else{
                        $roachYKey++;
                        $roachXKey = 0;
                    }
                    $roach[$roachYKey][$roachXKey] = $pushData;
                    $roachLast = $pushData;
                }
            }
        }
        //重新计算坐标
        $roachLocation = array();
        $occupyRoach = array();
        foreach($roach AS $key => $value){
            $swerve = false;
            $swerveY = $key;
            foreach($value AS $k => $v){
                $show_y = $key;
                $show_x = $k;
                if($show_x > 5 && $swerve === false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = 5;
                    array_push($occupyRoach,$show_y.'-'.$show_x);
                }elseif(in_array($show_y.'-'.$show_x,$occupyRoach)){
                    if($swerve === false){
                        $swerve = $show_x - 1;
                    }
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupyRoach,$show_y.'-'.$show_x);
                }elseif($swerve !== false){
                    $swerveY = $swerveY + 1;
                    $show_y = $swerveY;
                    $show_x = $swerve;
                    array_push($occupyRoach,$show_y.'-'.$show_x);
                }
                $pushArray = array('show_x' => $show_y+1, 'show_y' => $show_x+1, 'result' => $v['result']);
                array_push($roachLocation,$pushArray);
            }
        }
        $data = array();
        $data['showRoad'] = $showRoadLocation;
        $data['bigRoad'] = $bigRoadLocation;
        $data['bigEyeRoad'] = $bigEyeRoadLocation;
        $data['pathway'] = $pathwayLocation;
        $data['roach'] = $roachLocation;
        $data['sanxingRoad'] = $sanxingRoadLocation;
        return ['status'=>true,'msg'=>'数据存在','waybill'=>$data];
    }
}