<?php

namespace app\services\bet;

use app\models\bet\Bet;
use freedom\utils\DiceUtil;
use freedom\utils\SocketSession;

/**
 * TODO ToBetDiceService
 * Class ToBetDiceService
 * @package app\services\bet
 */

class ToBetDiceService{

    /**
     * TODO 处理骰宝下注
     * @param array $event
     * @param array $tableInfo
     * @return void
     */
    public static function toBetDice(array $event, array $tableInfo){
        $ws = app('\think\swoole\WebSocket');
        $fd = $ws->getSender();
        $data = array();
        foreach ($event['amount'] as $k => $v){
            if (intval($v) > 0){
                $data[$k] = intval($v);
            }
        }
        $seat_num = isset($event['seat_num']) && intval($event['seat_num']) > 0 ? intval($event['seat_num']) : 0;
        $time = time();
        $toBetCheck = ToBetCommonService::toBetCheck($tableInfo,$event,$data);
        if ($toBetCheck['status'] == false){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => $toBetCheck['msg']]);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        $numberTabInfo = $toBetCheck['numberTabInfo'];
        $userInfo = $toBetCheck['userInfo'];
        $amount = array_sum($data);
        $prevBetInfo = Bet::getPrevBetInfo($userInfo['id'],$numberTabInfo['id']);
        if($prevBetInfo){
            $beforeAmount = string_to_array($prevBetInfo['dice_amount']);
            $betTotalAmount = DiceUtil::amountInc($beforeAmount, $data);
        }else{
            $betTotalAmount = $data;
        }

        /***** User Limit Start *****/
        $totalAmount = array_sum($betTotalAmount);
        if ($totalAmount > $userInfo['limit_high']){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_user']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        if ($totalAmount > 0 && $totalAmount < $userInfo['limit_low']){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'under_limit_user']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        /***** User Limit End *****/

        /***** Table Limit Start *****/
        $totalAmount = array_sum($betTotalAmount);
        $limitMoneyArray = explode('-',$tableInfo['limit_money']);
        if($totalAmount > $limitMoneyArray[1]){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }
        if(($totalAmount > 0 && $totalAmount < $limitMoneyArray[0]) ){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'under_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }

        //如果桌子设置了单口最高下注，则所有的下注不能超过桌子单口下注限红
        if(($tableInfo['limit_money_total'] > 0 && $totalAmount > $tableInfo['limit_money_total'])){
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'exceeds_limit_table']);
            SocketSession::resetRepeat($fd,'user','isToBet');
            return;
        }

        /***** Table Limit End *****/



        $res = Bet::toBet($tableInfo,$userInfo,$numberTabInfo,$prevBetInfo,$data,$seat_num,$betTotalAmount,$time,0);
        $numberTabAmount = string_to_array($numberTabInfo['dice_amount']);
        if($res){
            $ws->emit('toBet',['status' => true, 'table_id' => $tableInfo['id'], 'bet_amount_msg' => $betTotalAmount, 'money' => ($userInfo['money'] - $amount),'seat_num' => $seat_num, 'msg' => 'to_bet_success']);
            $totalAmountArray = DiceUtil::amountInc($numberTabAmount, $data);
            $round = [
                'amount_total' => $totalAmountArray,
                'amount_item' => $data
            ];
            $ws->to(SocketSession::HOUSE_NAME)->emit('allBetAmount',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
        }else{
            $ws->emit('toBet',['status' => false, 'table_id' => $tableInfo['id'], 'msg' => 'to_bet_fail']);
        }
        SocketSession::resetRepeat($fd,'user','isToBet');
    }

}
