<?php


namespace app\services\reset;

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

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

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

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

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