<?php
declare (strict_types = 1);

namespace app\listener\user;

use app\services\bet\CancelBetService;
use freedom\utils\SocketSession;
use think\swoole\Websocket;

class CancelBet
{
    /**
     * 事件监听处理
     *
     * @return void
     */
    public function handle(array $event, Websocket $ws)
    {
        $preliminaryCheckRes = SocketSession::preliminaryCheck($event,'user');
        $fd = $ws->getSender();
        if($preliminaryCheckRes['status'] == true){
            $tableInfo = $preliminaryCheckRes['data'];
            if(SocketSession::checkRepeat($fd,'user','isToCancelBet')){
                $res = CancelBetService::cancelBet($event,$tableInfo);
                if ($res['status'] == true){
                    $ws->emit('cancelBet',['status' => true, 'table_id' => $tableInfo['id'],'user_id' => $res['user_id'],'manager_id' => $res['manager_id'], 'money' => $res['money'], 'msg' => $res['msg']]);
                    $ws->to(SocketSession::MANAGER_ROOM_NAME)->emit('cancelBet',['status' => true, 'table_id' => $tableInfo['id'],'user_id' => $res['user_id'],'manager_id' => $res['manager_id'], 'money' => $res['money'], 'msg' => $res['msg']]);

                }else{
                    $ws->emit('cancelBet',['status' => false, 'msg' => $res['msg']]);
                }
                SocketSession::resetRepeat($fd,'user','isToCancelBet');
            }else{
                $ws->emit('toBet',['status' => false, 'msg' => 'notice_repeat']);
            }
        }else{
            $ws->emit('changeBoot', ['status' => false, 'msg' => $preliminaryCheckRes['msg']]);
        }
    }
}
