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

    /**
     * 开奖号吗解析
     */
    function prizeParse($data,$game=''){
        $prize=json_decode($data['prizes'],1);
        if(is_array($prize)&&count($prize)>0){
            $prize=current($prize);
            foreach ($prize as $k=>$v){
                if(is_array($prize)&&count($prize)>0){
                    foreach ($v as $key=>$value){
                        $data[$k]=trans($this->game.'.'.$k.'-'.$key);
                    }
                }
            }
        }
        unset($data['prizes']);
        //解析开奖补充号码
        if(isset($data['extra'])&&!empty($data['extra'])){
            $extra=json_decode($data['extra'],1);
            foreach ($extra as $kq=>$vq){
                if(!is_numeric($vq)){
                    $data[$kq]=trans($this->game.'.'.$vq);
                }else{
                    $data[$kq]=$vq;
                }
            }
            $data['prizes_code']=$extra['one'].'+'.$extra['two'].'+'.$extra['three'].'='.$extra['code'];
            $data['leopard']=$extra['leopard'];
            $data['limit']=trans($this->game.'.'.$extra['limit']);
        }
        unset( $data['specialCode']);
        unset($data['extra']);
//        unset($data['one']);
//        unset($data['two']);
//        unset($data['three']);
//        unset($data['code']);
        $data['name']=$this->name;
        //中奖撤单未中奖状态的处理
        if($data['status']==4){
            $data['game_status']=4;
        }
        unset($data['status']);
        return $data;
    }

    /**
     *
     */
    function betParse($data,$game=''){
    }

    //投注内容
    function packbet($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'];
                    $data['sum_code']=$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_'] = '';
        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 . '¥'.$va[0].' ';
                            } else {
                                $data['bet_'] .= trans($this->game . '.' . $ka) . '¥'.$va[0].' ';
                            }
                        }
                        $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 winMomey($data){
        $winMoney=0;
        $data=json_decode($data,1);
        foreach ($data as $k=>$v){
            if(is_array($v)&&count($v)>0){
               foreach ($v as $key=>$value){
                   if(isset($value['settings']['multiple']['max'])){
                       $winMoney=$value[0]*$value['settings']['multiple']['max'];
                   }else{
                       $winMoney=$value[0]*$value['settings']['multiple'];
                   }
               }
            }
        }
       return $winMoney;
    }
    /**
     * 单注号码解析
     * @param $data 号码json
     * @param $game
     * @return array
     */
    function dataChange($data){
        $args=array();
        $buy_code = json_decode($data['codes'],1);//该注购买号码
        $prize_code =json_decode($data['prize'],1);//改注中奖号码
        //购买号码解析
        if(is_array($buy_code) && count($buy_code)>0)
        {
            $i=0;
            foreach($buy_code as $k=>$v)
            {
                if(is_array($v) && count($v)>0)
                {
                    foreach($v as $k1=>$v1)
                    {
                        $args[$i]=array(
                            'gameType'=>trans($data['game_name'].'.'.$k),
                            'codes'=>trans($data['game_name'].'.'.$k.'-'.$k1),
                            'money'=>$v1[0],
                            'maybe'=>$v1[0]*$v1['settings']['multiple'],
                            'play'=>$k,
                            'p_code'=>$k1,
                        );
                        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']='撤单';
                        }else if($data['game_status']==0){
                            $args[$i]['prize_status']='待开奖';
                        }
                        $i++;
                    }

                }

            }
        }
        //中奖判断
        if(is_array($prize_code)&&count($prize_code)){
            //中奖号码
            foreach ($prize_code 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($v['p_code']==$p_v2){
                                    $args[$num]['prize_status']='中奖';
                                }else{
                                    $args[$num]['prize_status']='未中奖';
                                }
                            }
                        }
                    }
                }
            }
        }
        return $args;
    }
}