<?php


namespace app\services\reset;

use app\models\process\NumberTab;
use freedom\utils\SocketSession;
use think\swoole\Websocket;

/**
 * TODO Baccarat开结果服务
 * Class ResetBaccaratService
 * @package app\services\opening
 */
class ResetBaccaratService
{
    /**
     * TODO Baccarat修改上一铺结果处理
     * @param array $event
     * @param array $tableInfo
     * @param Websocket $ws
     * @return void
     */
    public static function resetBaccarat(array $event, array $tableInfo, Websocket $ws){

        $ws = app('\think\swoole\WebSocket');
        $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc_two($tableInfo);
        if (!$numberTabInfo){
            $ws->emit('resetBaccarat', ['status' => false, 'msg' => 'not_number_tab_data']);
            return;
        }
        if(count($numberTabInfo) != 2){
            $ws->emit('resetBaccarat', ['status' => false, 'msg' => '上一铺数据不存在']);
            return;
        }
        if($numberTabInfo[0]['boot_id'] != $numberTabInfo[1]['boot_id']){
            $ws->emit('resetBaccarat', ['status' => false, 'msg' => '上一铺不在同一靴']);
            return;
        }

        $numberTabInfo = $numberTabInfo[1];
        if($event['opening'] == $numberTabInfo['result'] && $event['pair'] == $numberTabInfo['pair'] && $event['luck_six'] == $numberTabInfo['luck_six']){
            $ws->emit('resetBaccarat', ['status' => false, 'msg' => '结果一样']);
            return;
        }
        $res = NumberTab::resetBaccarat($event,$numberTabInfo,$tableInfo);
        if (!$res){
            $ws->emit('resetBaccarat', ['status' => false, 'msg' => '修改上一局结果失败']);
            return;
        }

        $ws->to(SocketSession::HOUSE_NAME)->emit('resetBaccarat',[
            'status' => true,
            'table_id' => $tableInfo['id'],
            'round' => [
                'previous_number_tab_id' => $numberTabInfo['id']
            ]
        ]);
    }
}