<?php


namespace app\services\process;


use app\models\process\NumberTab;
use app\services\connect\InitTableService;
use freedom\utils\SocketSession;

/**
 * TODO 作废
 * Class ResetNumberTabService
 * @package app\services\process
 */
class ResetNumberTabService
{
    /**
     * TODO 处理作废
     * @param array $tableInfo
     * @return void
     */
    public static function resetNumberTabService(array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        // 使用 InitTableService::initTable 获取当前靴的最新一局，与 onlineLogin 保持一致
        $numberTabInfo = InitTableService::initTable($tableInfo);
        if (!$numberTabInfo){
            $ws->emit('resetNumberTab', ['status' => false, 'msg' => 'not_number_tab_data']);
            return;
        }
        // 调试日志
        \think\facade\Log::write('resetNumberTab debug: bet_status=' . $numberTabInfo['bet_status'] . ', rob_status=' . $numberTabInfo['rob_status'] . ', id=' . $numberTabInfo['id'] . ', boot_id=' . $numberTabInfo['boot_id'], 'info');
        if($numberTabInfo['bet_status'] == 1 || $numberTabInfo['bet_status'] == 2 || $numberTabInfo['bet_status'] == 3 || ($numberTabInfo['bet_status'] == 0 && ($numberTabInfo['rob_status'] == 1 || $numberTabInfo['rob_status'] == 2))){
            //事务处理
            $res = NumberTab::resetNumberTab($numberTabInfo);
            \think\facade\Log::write('resetNumberTab result: ' . ($res ? 'true' : 'false'), 'info');
            if (!$res){
                $ws->emit('resetNumberTab', ['status' => false, 'msg' => 'reset_number_fail']);
                return;
            }
            $numberTabInfo['bet_status'] = 0;
            $responseData = [
                'status' => true,
                'table_id' => $tableInfo['id'],
                'round' => [
                    'boot_id' => $numberTabInfo['boot_id'],
                    'boot_num' => $numberTabInfo['boot_num'],
                    'number_tab_id' => $numberTabInfo['id'],
                    'number_tab_number' => $numberTabInfo['number'],
                    'in_checkout' => $tableInfo['in_checkout'],
                    'number_tab_status' => InitTableService::numberTabStatus($numberTabInfo)
                ]
            ];
            // 广播到 house 房间
            $ws->to(SocketSession::HOUSE_NAME)->emit('resetNumberTab', $responseData);
            // 同时发送给请求者
            $ws->emit('resetNumberTab', $responseData);
        }else{
            $ws->emit('resetNumberTab', ['status' => false, 'msg' => 'reset_number_fail']);
        }
    }
}