<?php


namespace app\services\opening;


use app\models\bet\Bet;
use app\models\process\NumberTab;
use app\models\user\User;
use app\services\connect\InitTableService;
use freedom\utils\CardPositionTc;
use freedom\utils\RedisUtil;
use freedom\utils\SocketSession;
use think\swoole\Websocket;

class OpeningTcService
{
    /**
     * TODO Dt开结果处理
     * @param array $event
     * @param array $tableInfo
     * @param Websocket $ws
     * @return void
     */
    public static function openingTc(array $event, array $tableInfo, Websocket $ws){
        //判断结果并推送结果
        $res = self::doOpening($event,$tableInfo);
        if ($res['status'] == false){
            $ws->emit('openingTc',['status' => false, 'msg' => $res['msg']]);
            return;
        }
        list($data,$lastNumberTabInfo,$newNumberTabInfo) = $res['data'];
        $round = [
            'boot_id' => $newNumberTabInfo['boot_id'],
            'boot_num' => $newNumberTabInfo['boot_num'],
            'number_tab_id' => $newNumberTabInfo['id'],
            'previous_number_tab_id' => $lastNumberTabInfo['id'],
            'number_tab_number' => $newNumberTabInfo['number'] + 1,
            'number_tab_status' => InitTableService::numberTabStatus($newNumberTabInfo)
        ];
        $round = array_merge($round,$data);
        $ws->emit('openingTc',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
        $ws->to(SocketSession::HOUSE_NAME)->emit('openingTcResult',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
        //处理注单
        self::doBet($ws,$tableInfo,$lastNumberTabInfo,$data);
    }

    /**
     * TODO 判断结果
     * @param array $event
     * @param array $tableInfo
     * @return array
     */
    public static function doOpening(array $event, array $tableInfo): array
    {
        if (!isset($event['number_tab_id'])) return ['status' => false, 'msg' => 'opening_fail_2'];
        $numberTabId = intval($event['number_tab_id']);
        $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);
        if (!$numberTabInfo || $numberTabId != $numberTabInfo['id']) return ['status' => false, 'msg' => 'opening_fail_2'];
        if ($numberTabInfo['bet_status'] != 2) return ['status' => false, 'msg' => 'opening_fail_4'];
        //TC只有扫描台,不是扫描台返回false
        if ($tableInfo['is_scavenging'] != 1) return ['status' => false, 'msg' => 'opening_fail'];
        $cardInfo = RedisUtil::getCard($numberTabId);
        if (!$cardInfo) return ['status' => false, 'msg' => 'opening_fail_6'];
        // 判断是否所有的牌都已经扫描
        foreach ($cardInfo AS $v){
            if ($v == 0){
                return ['status' => false, 'msg' => 'opening_fail_6'];
            }
        }
        // 比较结果
        $player1Card[0] = $cardInfo['player_1_card_1'];
        $player1Card[1] = $cardInfo['player_1_card_2'];
        $player1Card[2] = $cardInfo['player_1_card_3'];
        $player2Card[0] = $cardInfo['player_2_card_1'];
        $player2Card[1] = $cardInfo['player_2_card_2'];
        $player2Card[2] = $cardInfo['player_2_card_3'];
        $player3Card[0] = $cardInfo['player_3_card_1'];
        $player3Card[1] = $cardInfo['player_3_card_2'];
        $player3Card[2] = $cardInfo['player_3_card_3'];
        $bankerCard[0] = $cardInfo['banker_card_1'];
        $bankerCard[1] = $cardInfo['banker_card_2'];
        $bankerCard[2] = $cardInfo['banker_card_3'];
        $player1Result = CardPositionTc::ThredCardCowCow($player1Card);
        $player2Result = CardPositionTc::ThredCardCowCow($player2Card);
        $player3Result = CardPositionTc::ThredCardCowCow($player3Card);
        $bankerResult = CardPositionTc::ThredCardCowCow($bankerCard);

        $data = [];
        $data['result_player_1'] = $player1Result['cow'];
        $data['result_player_2'] = $player2Result['cow'];
        $data['result_player_3'] = $player3Result['cow'];
        $data['result_banker'] = $bankerResult['cow'];
        if($data['result_player_1'] > $data['result_banker']){
            $data['win_player_1'] = 1;
        }else if($data['result_player_1'] < $data['result_banker']){
            $data['win_player_1'] = 0;
        }else if($data['result_player_1'] == $data['result_banker']){
            $data['win_player_1'] = CardPositionTc::compareCardTc($player1Result['max'], $bankerResult['max'],$data['result_player_1']);
        }
        if($data['result_player_2'] > $data['result_banker']){
            $data['win_player_2'] = 1;
        }else if($data['result_player_2'] < $data['result_banker']){
            $data['win_player_2'] = 0;
        }else if($data['result_player_2'] == $data['result_banker']){
            $data['win_player_2'] = CardPositionTc::compareCardTc($player2Result['max'], $bankerResult['max'],$data['result_player_2']);
        }

        if($data['result_player_3'] > $data['result_banker']){
            $data['win_player_3'] = 1;
        }else if($data['result_player_3'] < $data['result_banker']){
            $data['win_player_3'] = 0;
        }else if($data['result_player_3'] == $data['result_banker']){
            $data['win_player_3'] = CardPositionTc::compareCardTc($player3Result['max'], $bankerResult['max'],$data['result_player_3']);
        }
        //开盘
        $numberTabUpdate = [
            'result_player_1' => $data['result_player_1'],
            'result_player_2' => $data['result_player_2'],
            'result_player_3' => $data['result_player_3'],
            'result_banker' => $data['result_banker'],
            'win_player_1' => $data['win_player_1'],
            'win_player_2' => $data['win_player_2'],
            'win_player_3' => $data['win_player_3'],
            'end_time' => time(),
            'bet_status' => 3,
        ];
        $newNumberTabInfo = NumberTab::next($numberTabInfo,$numberTabUpdate,$tableInfo);
        if ($newNumberTabInfo){
            $lastNumberTabInfo = array_merge($numberTabInfo,$numberTabUpdate);
            return ['status' => true, 'data' => [$data,$lastNumberTabInfo,$newNumberTabInfo]];
        }else{
            return ['status' => false, 'msg' => 'opening_fail'];
        }
    }
    /**
     * TODO Bet处理
     * @param Websocket $ws  桌子信息
     * @param array $tableInfo  桌子信息
     * @param array $numberTabInfo  开结果的当前局信息
     * @param array $data   输赢数据
     * @return void
     */
    public static function doBet(Websocket $ws, array $tableInfo, array $numberTabInfo, array $data){
        $bankerWinTotal = 0;
        if($numberTabInfo['rob_banker_id'] > 0 ){
            $betInfo = Bet::getByNumberTabIdNotRob($numberTabInfo['id'],$numberTabInfo['rob_banker_id']);
            $bankerBetInfo = Bet::getByNumberTabIdRob($numberTabInfo['id'],$numberTabInfo['rob_banker_id']);
        }else{
            $betInfo = Bet::getByNumberTabIdValid($numberTabInfo['id']);
        }
        foreach($betInfo AS $v){
            $userInfo = User::get(intval($v['user_id']));
            if (!$userInfo){
                continue;
            }
            $amount = $v['amount_player_1'] +
                $v['amount_player_1_times'] +
                $v['amount_player_1_banker'] +
                $v['amount_player_1_banker_times'] +
                $v['amount_player_2'] +
                $v['amount_player_2_times'] +
                $v['amount_player_2_banker'] +
                $v['amount_player_2_banker_times'] +
                $v['amount_player_3'] +
                $v['amount_player_3_times'] +
                $v['amount_player_3_banker'] +
                $v['amount_player_3_banker_times'];
            $withholdAmount = $v['withhold_player_1_times'] +
                $v['withhold_player_1_banker_times'] +
                $v['withhold_player_2_times'] +
                $v['withhold_player_2_banker_times'] +
                $v['withhold_player_3_times'] +
                $v['withhold_player_3_banker_times'];
            $newAmount = 0;
            $winTotal = 0;
            $winTotalActual = 0;
            $timesPlayer1 = 1;
            $timesPlayer2 = 1;
            $timesPlayer3 = 1;
            $rebate = 0;
            // 双边洗码 （对冲 庄正闲负绝对值）
            $rebatePlayer1 = 0;
            $rebatePlayer2 = 0;
            $rebatePlayer3 = 0;
            
            //闲1
            if($data['win_player_1'] == 1){
                if($v['amount_player_1'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal += round($v['amount_player_1'] * $userInfo['price_tc_n1'],2);
                    $winTotalActual += round($v['amount_player_1'] * $times,2);
                    $newAmount += $v['amount_player_1'];
                    $rebatePlayer1 -= $v['amount_player_1'];
                }
                if($v['amount_player_1_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_player_1']);
                    if($data['result_player_1'] == 1){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n1'],2);
                        $timesPlayer1 = $userInfo['price_tc_n1'];
                    }elseif($data['result_player_1'] == 2){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n2'],2);
                        $timesPlayer1 = $userInfo['price_tc_n2'];
                    }elseif($data['result_player_1'] == 3){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n3'],2);
                        $timesPlayer1 = $userInfo['price_tc_n3'];
                    }elseif($data['result_player_1'] == 4){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n4'],2);
                        $timesPlayer1 = $userInfo['price_tc_n4'];
                    }elseif($data['result_player_1'] == 5){
                        $winTotal += round($v['amount_player_1_times'] *  $userInfo['price_tc_n5'],2);
                        $timesPlayer1 = $userInfo['price_tc_n5'];
                    }elseif($data['result_player_1'] == 6){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n6'],2);
                        $timesPlayer1 = $userInfo['price_tc_n6'];
                    }elseif($data['result_player_1'] == 7){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n7'],2);
                        $timesPlayer1 = $userInfo['price_tc_n7'];
                    }elseif($data['result_player_1'] == 8){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n8'],2);
                        $timesPlayer1 = $userInfo['price_tc_n8'];
                    }elseif($data['result_player_1'] == 9){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_n9'],2);
                        $timesPlayer1 = $userInfo['price_tc_n9'];
                    }elseif($data['result_player_1'] == 10){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_nn'],2);
                        $timesPlayer1 = $userInfo['price_tc_nn'];
                    }elseif($data['result_player_1'] == 11){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_bz'],2);
                        $timesPlayer1 = $userInfo['price_tc_bz'];
                    }elseif($data['result_player_1'] == 12){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_ths'],2);
                        $timesPlayer1 = $userInfo['price_tc_ths'];
                    }elseif($data['result_player_1'] == 13){
                        $winTotal += round($v['amount_player_1_times'] * $userInfo['price_tc_hjths'],2);
                        $timesPlayer1 = $userInfo['price_tc_hjths'];
                    }
                    $winTotalActual += round($v['amount_player_1_times'] * $times,2);
                    $newAmount += $v['amount_player_1_times'];
                    $rebatePlayer1 -= $v['amount_player_1_times'];
                }

                if($v['amount_player_1_banker'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal -= round($v['amount_player_1_banker'] * $times,2);
                    $rebatePlayer1 += round($v['amount_player_1_banker'] * $times,2);
                    $newAmount += $v['amount_player_1_banker'] * $times;
                    $rebate += $v['amount_player_1_banker'] * $times;
                }
                if($v['amount_player_1_banker_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_player_1']);
                    $winTotal -= round($v['amount_player_1_banker_times'] * $times,2);
                    $rebatePlayer1 += round($v['amount_player_1_banker_times'] * $times,2);
                    $newAmount += $v['amount_player_1_banker_times'] * $times;
                    $rebate += $v['amount_player_1_banker_times'] * $times;
                }

            }elseif($data['win_player_1'] == 0){
                if($v['amount_player_1_banker'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal += round($v['amount_player_1_banker'] * $userInfo['price_tc_n1'],2);
                    $newAmount += $v['amount_player_1_banker'] * $times;
                    $rebatePlayer1 += $v['amount_player_1_banker'] * $times;
                }
                if($v['amount_player_1_banker_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_banker']);
                    if($data['result_banker'] == 1){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n1'],2);
                        $timesPlayer1 = $userInfo['price_tc_n1'];
                    }elseif($data['result_banker'] == 2){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n2'],2);
                        $timesPlayer1 = $userInfo['price_tc_n2'];
                    }elseif($data['result_banker'] == 3){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n3'],2);
                        $timesPlayer1 = $userInfo['price_tc_n3'];
                    }elseif($data['result_banker'] == 4){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n4'],2);
                        $timesPlayer1 = $userInfo['price_tc_n4'];
                    }elseif($data['result_banker'] == 5){
                        $winTotal += round($v['amount_player_1_banker_times'] *  $userInfo['price_tc_n5'],2);
                        $timesPlayer1 = $userInfo['price_tc_n5'];
                    }elseif($data['result_banker'] == 6){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n6'],2);
                        $timesPlayer1 = $userInfo['price_tc_n6'];
                    }elseif($data['result_banker'] == 7){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n7'],2);
                        $timesPlayer1 = $userInfo['price_tc_n7'];
                    }elseif($data['result_banker'] == 8){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n8'],2);
                        $timesPlayer1 = $userInfo['price_tc_n8'];
                    }elseif($data['result_banker'] == 9){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_n9'],2);
                        $timesPlayer1 = $userInfo['price_tc_n9'];
                    }elseif($data['result_banker'] == 10){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_nn'],2);
                        $timesPlayer1 = $userInfo['price_tc_nn'];
                    }elseif($data['result_banker'] == 11){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_bz'],2);
                        $timesPlayer1 = $userInfo['price_tc_bz'];
                    }elseif($data['result_banker'] == 12){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_ths'],2);
                        $timesPlayer1 = $userInfo['price_tc_ths'];
                    }elseif($data['result_banker'] == 13){
                        $winTotal += round($v['amount_player_1_banker_times'] * $userInfo['price_tc_hjths'],2);
                        $timesPlayer1 = $userInfo['price_tc_hjths'];
                    }
                    $newAmount += $v['amount_player_1_banker_times'];
                    $rebatePlayer1 += $v['amount_player_1_banker_times'];
                }

                if($v['amount_player_1'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal -= round($v['amount_player_1'] * $times,2);
                    $winTotalActual -= round($v['amount_player_1'] * $times,2);
                    $rebatePlayer1 -= round($v['amount_player_1'] * $times,2);
                    $rebate += $v['amount_player_1'] * $times;
                    $newAmount += $v['amount_player_1'] * $times;
                }
                if($v['amount_player_1_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_banker']);
                    $winTotal -= round($v['amount_player_1_times'] * $times,2);
                    $winTotalActual -= round($v['amount_player_1_times'] * $times,2);
                    $rebatePlayer1 -= round($v['amount_player_1_times'] * $times,2);
                    $rebate += $v['amount_player_1_times'] * $times;
                    $newAmount += $v['amount_player_1_times'] * $times;
                }
            }
            //闲2
            if($data['win_player_2'] == 1){
                if($v['amount_player_2'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal += round($v['amount_player_2'] * $userInfo['price_tc_n1'],2);
                    $winTotalActual += round($v['amount_player_2'] * $times,2);
                    $newAmount += $v['amount_player_2'];
                    $rebatePlayer2 -= $v['amount_player_2'];
                }
                if($v['amount_player_2_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_player_2']);
                    if($data['result_player_2'] == 1){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n1'],2);
                        $timesPlayer2 = $userInfo['price_tc_n1'];
                    }elseif($data['result_player_2'] == 2){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n2'],2);
                        $timesPlayer2 = $userInfo['price_tc_n2'];
                    }elseif($data['result_player_2'] == 3){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n3'],2);
                        $timesPlayer2 = $userInfo['price_tc_n3'];
                    }elseif($data['result_player_2'] == 4){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n4'],2);
                        $timesPlayer2 = $userInfo['price_tc_n4'];
                    }elseif($data['result_player_2'] == 5){
                        $winTotal += round($v['amount_player_2_times'] *  $userInfo['price_tc_n5'],2);
                        $timesPlayer2 = $userInfo['price_tc_n5'];
                    }elseif($data['result_player_2'] == 6){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n6'],2);
                        $timesPlayer2 = $userInfo['price_tc_n6'];
                    }elseif($data['result_player_2'] == 7){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n7'],2);
                        $timesPlayer2 = $userInfo['price_tc_n7'];
                    }elseif($data['result_player_2'] == 8){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n8'],2);
                        $timesPlayer2 = $userInfo['price_tc_n8'];
                    }elseif($data['result_player_2'] == 9){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_n9'],2);
                        $timesPlayer2 = $userInfo['price_tc_n9'];
                    }elseif($data['result_player_2'] == 10){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_nn'],2);
                        $timesPlayer2 = $userInfo['price_tc_nn'];
                    }elseif($data['result_player_2'] == 11){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_bz'],2);
                        $timesPlayer2 = $userInfo['price_tc_bz'];
                    }elseif($data['result_player_2'] == 12){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_ths'],2);
                        $timesPlayer2 = $userInfo['price_tc_ths'];
                    }elseif($data['result_player_2'] == 13){
                        $winTotal += round($v['amount_player_2_times'] * $userInfo['price_tc_hjths'],2);
                        $timesPlayer2 = $userInfo['price_tc_hjths'];
                    }
                    $winTotalActual += round($v['amount_player_2_times'] * $times,2);
                    $newAmount += $v['amount_player_2_times'];
                    $rebatePlayer2 -= $v['amount_player_2_times'];
                }

                if($v['amount_player_2_banker'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal -= round($v['amount_player_2_banker'] * $times,2);
                    $rebatePlayer2 += round($v['amount_player_2_banker'] * $times,2);
                    $rebate += $v['amount_player_2_banker'] * $times;
                    $newAmount += $v['amount_player_2_banker'] * $times;
                }
                if($v['amount_player_2_banker_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_player_2']);
                    $winTotal -= round($v['amount_player_2_banker_times'] * $times,2);
                    $rebatePlayer2 += round($v['amount_player_2_banker_times'] * $times,2);
                    $rebate += $v['amount_player_2_banker_times'] * $times;
                    $newAmount += $v['amount_player_2_banker_times'] * $times;
                }

            }elseif($data['win_player_2'] == 0){
                if($v['amount_player_2_banker'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal += round($v['amount_player_2_banker'] * $userInfo['price_tc_n1'],2);
                    $rebatePlayer2 += $v['amount_player_2_banker'] * $times;
                    $newAmount += $v['amount_player_2_banker'] * $times;
                }
                if($v['amount_player_2_banker_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_banker']);
                    if($data['result_banker'] == 1){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n1'],2);
                        $timesPlayer2 = $userInfo['price_tc_n1'];
                    }elseif($data['result_banker'] == 2){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n2'],2);
                        $timesPlayer2 = $userInfo['price_tc_n2'];
                    }elseif($data['result_banker'] == 3){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n3'],2);
                        $timesPlayer2 = $userInfo['price_tc_n3'];
                    }elseif($data['result_banker'] == 4){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n4'],2);
                        $timesPlayer2 = $userInfo['price_tc_n4'];
                    }elseif($data['result_banker'] == 5){
                        $winTotal += round($v['amount_player_2_banker_times'] *  $userInfo['price_tc_n5'],2);
                        $timesPlayer2 = $userInfo['price_tc_n5'];
                    }elseif($data['result_banker'] == 6){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n6'],2);
                        $timesPlayer2 = $userInfo['price_tc_n6'];
                    }elseif($data['result_banker'] == 7){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n7'],2);
                        $timesPlayer2 = $userInfo['price_tc_n7'];
                    }elseif($data['result_banker'] == 8){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n8'],2);
                        $timesPlayer2 = $userInfo['price_tc_n8'];
                    }elseif($data['result_banker'] == 9){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_n9'],2);
                        $timesPlayer2 = $userInfo['price_tc_n9'];
                    }elseif($data['result_banker'] == 10){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_nn'],2);
                        $timesPlayer2 = $userInfo['price_tc_nn'];
                    }elseif($data['result_banker'] == 11){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_bz'],2);
                        $timesPlayer2 = $userInfo['price_tc_bz'];
                    }elseif($data['result_banker'] == 12){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_ths'],2);
                        $timesPlayer2 = $userInfo['price_tc_ths'];
                    }elseif($data['result_banker'] == 13){
                        $winTotal += round($v['amount_player_2_banker_times'] * $userInfo['price_tc_hjths'],2);
                        $timesPlayer2 = $userInfo['price_tc_hjths'];
                    }
                    $rebatePlayer2 += $v['amount_player_2_banker_times'];
                    $newAmount += $v['amount_player_2_banker_times'];
                }

                if($v['amount_player_2'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal -= round($v['amount_player_2'] * $times,2);
                    $winTotalActual -= round($v['amount_player_2'] * $times,2);
                    $rebatePlayer2 -= round($v['amount_player_2'] * $times,2);
                    $rebate += $v['amount_player_2'] * $times;
                    $newAmount += $v['amount_player_2'] * $times;
                }
                if($v['amount_player_2_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_banker']);
                    $winTotal -= round($v['amount_player_2_times'] * $times,2);
                    $winTotalActual -= round($v['amount_player_2_times'] * $times,2);
                    $rebatePlayer2 -= round($v['amount_player_2_times'] * $times,2);
                    $rebate += $v['amount_player_2_times'] * $times;
                    $newAmount += $v['amount_player_2_times'] * $times;
                }
            }

            //闲3
            if($data['win_player_3'] == 1){
                if($v['amount_player_3'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal += round($v['amount_player_3'] * $userInfo['price_tc_n1'],2);
                    $winTotalActual += round($v['amount_player_3'] * $times,2);
                    $rebatePlayer3 -= $v['amount_player_3'];
                    $newAmount += $v['amount_player_3'];
                }
                if($v['amount_player_3_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_player_3']);
                    if($data['result_player_3'] == 1){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n1'],2);
                        $timesPlayer3 = $userInfo['price_tc_n1'];
                    }elseif($data['result_player_3'] == 2){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n2'],2);
                        $timesPlayer3 = $userInfo['price_tc_n2'];
                    }elseif($data['result_player_3'] == 3){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n3'],2);
                        $timesPlayer3 = $userInfo['price_tc_n3'];
                    }elseif($data['result_player_3'] == 4){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n4'],2);
                        $timesPlayer3 = $userInfo['price_tc_n4'];
                    }elseif($data['result_player_3'] == 5){
                        $winTotal += round($v['amount_player_3_times'] *  $userInfo['price_tc_n5'],2);
                        $timesPlayer3 = $userInfo['price_tc_n5'];
                    }elseif($data['result_player_3'] == 6){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n6'],2);
                        $timesPlayer3 = $userInfo['price_tc_n6'];
                    }elseif($data['result_player_3'] == 7){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n7'],2);
                        $timesPlayer3 = $userInfo['price_tc_n7'];
                    }elseif($data['result_player_3'] == 8){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n8'],2);
                        $timesPlayer3 = $userInfo['price_tc_n8'];
                    }elseif($data['result_player_3'] == 9){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_n9'],2);
                        $timesPlayer3 = $userInfo['price_tc_n9'];
                    }elseif($data['result_player_3'] == 10){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_nn'],2);
                        $timesPlayer3 = $userInfo['price_tc_nn'];
                    }elseif($data['result_player_3'] == 11){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_bz'],2);
                        $timesPlayer3 = $userInfo['price_tc_bz'];
                    }elseif($data['result_player_3'] == 12){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_ths'],2);
                        $timesPlayer3 = $userInfo['price_tc_ths'];
                    }elseif($data['result_player_3'] == 13){
                        $winTotal += round($v['amount_player_3_times'] * $userInfo['price_tc_hjths'],2);
                        $timesPlayer3 = $userInfo['price_tc_hjths'];
                    }
                    $winTotalActual += round($v['amount_player_3_times'] * $times,2);
                    $rebatePlayer3 -= $v['amount_player_3_times'];
                    $newAmount += $v['amount_player_3_times'];
                }

                if($v['amount_player_3_banker'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal -= round($v['amount_player_3_banker'] * $times,2);
                    $rebatePlayer2 += round($v['amount_player_3_banker'] * $times,2);
                    $rebate += $v['amount_player_3_banker'] * $times;
                    $newAmount += $v['amount_player_3_banker'] * $times;
                }
                if($v['amount_player_3_banker_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_player_3']);
                    $winTotal -= round($v['amount_player_3_banker_times'] * $times,2);
                    $rebatePlayer1 += round($v['amount_player_3_banker_times'] * $times,2);
                    $rebate += $v['amount_player_3_banker_times'] * $times;
                    $newAmount += $v['amount_player_3_banker_times'] * $times;
                }

            }elseif($data['win_player_3'] == 0){
                if($v['amount_player_3_banker'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal += round($v['amount_player_3_banker'] * $userInfo['price_tc_n1'],2);
                    $rebatePlayer3 += $v['amount_player_3_banker'] * $times;
                    $newAmount += $v['amount_player_3_banker'] * $times;
                }
                if($v['amount_player_3_banker_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_banker']);
                    if($data['result_banker'] == 1){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n1'],2);
                        $timesPlayer3 = $userInfo['price_tc_n1'];
                    }elseif($data['result_banker'] == 2){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n2'],2);
                        $timesPlayer3 = $userInfo['price_tc_n2'];
                    }elseif($data['result_banker'] == 3){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n3'],2);
                        $timesPlayer3 = $userInfo['price_tc_n3'];
                    }elseif($data['result_banker'] == 4){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n4'],2);
                        $timesPlayer3 = $userInfo['price_tc_n4'];
                    }elseif($data['result_banker'] == 5){
                        $winTotal += round($v['amount_player_3_banker_times'] *  $userInfo['price_tc_n5'],2);
                        $timesPlayer3 = $userInfo['price_tc_n5'];
                    }elseif($data['result_banker'] == 6){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n6'],2);
                        $timesPlayer3 = $userInfo['price_tc_n6'];
                    }elseif($data['result_banker'] == 7){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n7'],2);
                        $timesPlayer3 = $userInfo['price_tc_n7'];
                    }elseif($data['result_banker'] == 8){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n8'],2);
                        $timesPlayer3 = $userInfo['price_tc_n8'];
                    }elseif($data['result_banker'] == 9){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_n9'],2);
                        $timesPlayer3 = $userInfo['price_tc_n9'];
                    }elseif($data['result_banker'] == 10){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_nn'],2);
                        $timesPlayer3 = $userInfo['price_tc_nn'];
                    }elseif($data['result_banker'] == 11){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_bz'],2);
                        $timesPlayer3 = $userInfo['price_tc_bz'];
                    }elseif($data['result_banker'] == 12){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_ths'],2);
                        $timesPlayer3 = $userInfo['price_tc_ths'];
                    }elseif($data['result_banker'] == 13){
                        $winTotal += round($v['amount_player_3_banker_times'] * $userInfo['price_tc_hjths'],2);
                        $timesPlayer3 = $userInfo['price_tc_hjths'];
                    }
                    $rebatePlayer3 += $v['amount_player_3_banker_times'];
                    $newAmount += $v['amount_player_3_banker_times'];
                }

                if($v['amount_player_3'] > 0){
                    $times = CardPositionTc::getTimes(1);
                    $winTotal -= round($v['amount_player_3'] * $times,2);
                    $winTotalActual -= round($v['amount_player_3'] * $times,2);
                    $rebatePlayer3 -= round($v['amount_player_3'] * $times,2);
                    $rebate += $v['amount_player_3'] * $times;
                    $newAmount += $v['amount_player_3'] * $times;
                }
                if($v['amount_player_3_times'] > 0){
                    $times = CardPositionTc::getTimes($data['result_banker']);
                    $winTotal -= round($v['amount_player_3_times'] * $times,2);
                    $winTotalActual -= round($v['amount_player_3_times'] * $times,2);
                    $rebatePlayer3 -= round($v['amount_player_3_times'] * $times,2);
                    $rebate += $v['amount_player_3_times'] * $times;
                    $newAmount += $v['amount_player_3_times'] * $times;
                }
            }

            // 双边洗码
            if($userInfo['type_xima'] == 1){
                $rebate = abs($rebatePlayer1) + abs($rebatePlayer2) + abs($rebatePlayer3);
            }

            //计算庄家输赢
            if($numberTabInfo['rob_banker_username']){
                if($userInfo['is_sw'] == 0){
                    $bankerWinTotal = $winTotalActual + $bankerWinTotal;
                }
            }
            $v['position_first'] = $numberTabInfo['position_first'];
            $betInfoItem = array_merge($v,$data);
            $betInfoItem['times_player_1'] = $timesPlayer1;
            $betInfoItem['times_player_2'] = $timesPlayer2;
            $betInfoItem['times_player_3'] = $timesPlayer3;
            $res = Bet::openingBet($tableInfo,$betInfoItem,$userInfo,$numberTabInfo,$amount,$winTotal,$rebate,$withholdAmount);
            if ($res['status']){
                $tableUser = app('swoole.table.user');
                $userSession = $tableUser->get((string) $v['user_id']);
                if ($userSession && is_array($userSession)){
                    $ws->setSender(0)->to($userSession['fd'])->emit('opening',[
                        'status' => true,
                        'table_id' => $tableInfo['id'],
                        'round' => [
                            'money' => $res['money'],
                            'win_total' => $winTotal,
                            'previous_number_tab_id' => $v['number_tab_id']
                        ]
                    ]);
                }
            }
        }

        /**
         * 计算抢庄
         */
        if($numberTabInfo['rob_banker_id'] > 0){
            $userInfo = User::get(intval($numberTabInfo['rob_banker_id']));
            //更新user表余额
            $winTotal = to_number($bankerWinTotal);
            if($winTotal > 0){
                $winTotal = $winTotal * 0.94;
            }
            $amount = 0;
            $rebate = abs($bankerWinTotal);
            $bankerBetInfo = array_merge($bankerBetInfo,$data);
            $res = Bet::openingBet($tableInfo,$bankerBetInfo,$userInfo,$numberTabInfo,$amount,$winTotal,$rebate);
            if ($res['status']){
                $tableUser = app('swoole.table.user');
                $userSession = $tableUser->get((string) $userInfo['id']);
                if ($userSession && is_array($userSession)) {
                    $ws->setSender(0)->to($userSession['fd'])->emit('opening', [
                        'status' => true,
                        'table_id' => $tableInfo['id'],
                        'round' => [
                            'money' => $res['money'],
                            'previous_number_tab_id' => $bankerBetInfo['number_tab_id']
                        ]
                    ]);
                }
            }
        }
    }
}