<?php


namespace app\services\bet;

use app\models\bet\Bet;
use app\models\process\NumberTab;
use app\models\user\User;
use freedom\utils\SocketSession;

/**
 * TODO ToRobService
 * Class ToRobService
 * @package app\services\bet
 */
class ToRobService
{
    /**
     * TODO 处理toRob
     * @param array $event
     * @param array $tableInfo
     * @return void
     */
    public static function toTob(array $event, array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $fd = $ws->getSender();
        $numberTabId = isset($event['number_tab_id']) && intval($event['number_tab_id']) > 0 ? intval($event['number_tab_id']) : 0;
        $userId = isset($event['user_id']) && intval($event['user_id']) > 0 ? intval($event['user_id']) : 0;
        $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);
        /** 验证是否和当前铺同一铺 */
        if ($numberTabInfo['id'] != $numberTabId){
            $ws->emit('toRob',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_rob_fail_4']);
            SocketSession::resetRepeat($fd,'user','isToRob');
            return;
        }
        /** 验证是否抢庄时间 */
        if ($numberTabInfo['rob_status'] != 1 || time() > $numberTabInfo['rob_start_time'] + $tableInfo['rob_time']){
            $ws->emit('toRob',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_rob_fail_3']);
            SocketSession::resetRepeat($fd,'user','isToRob');
            return;
        }
        $userInfo = User::get(['id' => $userId, 'status' => 1, 'is_delete' => 0]);
        /** 验证会员有效 */
        if (!$userInfo){
            $ws->emit('toRob',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_rob_fail_2']);
            SocketSession::resetRepeat($fd,'user','isToRob');
            return;
        }
        /** 十万会员不能抢庄 */
        if($userInfo['is_sw'] == 1){
            $ws->emit('toRob',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_rob_fail_5']);
            SocketSession::resetRepeat($fd,'user','isToRob');
            return;
        }
        /** 会员金额必须大于桌子抢庄金额 */
        if($userInfo['money'] < $tableInfo['limit_banker_amount']){
            $ws->emit('toRob',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_rob_fail_1']);
            SocketSession::resetRepeat($fd,'user','isToRob');
            return;
        }
        /** 验证当前局是否有人已经抢庄 */
        if($numberTabInfo['rob_banker_id'] > 0){
            $ws->emit('toRob',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_rob_fail_6']);
            SocketSession::resetRepeat($fd,'user','isToRob');
            return;
        }
        $res = Bet::toRob($userInfo,$numberTabInfo);
        if ($res){
            $RobMsg = ['rob_banker_id' => $userInfo['id'], 'rob_banker_username' => $userInfo['username']];
            $ws->to(SocketSession::HOUSE_NAME)->emit('toRob',['status' => true, 'table_id' => $tableInfo['id'], 'RobMsg' => $RobMsg, 'msg' => '抢庄成功']);
        }else{
            $ws->emit('toRob',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_rob_fail']);
        }
        SocketSession::resetRepeat($fd,'user','isToRob');
    }
}