<?php
declare (strict_types = 1);

namespace app\listener;

use app\models\bet\Bet;
use app\models\process\NumberTab;
use app\services\connect\GetCardService;
use freedom\utils\SocketSession;
use think\swoole\Websocket;

class GetState
{
    /**
     * 事件监听处理
     *
     * @return void
     */
    public function handle(array $event, Websocket $ws)
    {
        $tableFd = app('swoole.table.fd');
        pre('getState:ws:'.$ws->getSender());
        $fdInfo = $tableFd->get((string) $ws->getSender());
        pre($fdInfo);
        $preliminaryCheckRes = SocketSession::preliminaryCheck($event,$fdInfo['mode']);
        if($preliminaryCheckRes['status'] == true){
            $tableInfo = $preliminaryCheckRes['data'];
            $numberTabInfo = NumberTab::getByTableIdOrderByIdDesc($tableInfo);
            $field = '*';
            if($numberTabInfo['game_id'] == 1){
                $field = 'table_id,game_id,user_id,banker_amount,player_amount,tie_amount,banker_pair_amount,player_pair_amount,luck_six_amount,big_amount,small_amount';
            }elseif($numberTabInfo['game_id'] == 2){
                $field = 'table_id,game_id,user_id,banker_amount,player_amount,tie_amount';
            }elseif($numberTabInfo['game_id'] == 4 || $numberTabInfo['game_id'] == 5){
                $field = 'table_id,game_id,user_id,
                          amount_player_1,amount_player_1_times,withhold_player_1_times,amount_player_1_banker,amount_player_1_banker_times,withhold_player_1_banker_times,
                          amount_player_2,amount_player_2_times,withhold_player_2_times,amount_player_2_banker,amount_player_2_banker_times,withhold_player_2_banker_times,
                          amount_player_3,amount_player_3_times,withhold_player_3_times,amount_player_3_banker,amount_player_3_banker_times,withhold_player_3_banker_times';
            }elseif($numberTabInfo['game_id'] == 6){
                $field = 'table_id,game_id,user_id,toning_amount';
            }elseif($numberTabInfo['game_id'] == 7){
                $field = 'table_id,game_id,user_id,dice_amount';
            }elseif($numberTabInfo['game_id'] == 8){
                $field = 'table_id,game_id,user_id,roulette_european_amount,roulette_french_amount';
            }
            $bets = $numberTabInfo ? Bet::getByNumberTabIdValid($numberTabInfo['id'], $field) : [];
            if($bets){
                if($numberTabInfo['game_id'] == 6){
                    foreach ($bets as $key => $val){
                        $val['toning_amount'] = string_to_array($val['toning_amount']);
                        $bets[$key] = $val;
                    }
                }

                if($numberTabInfo['game_id'] == 7){
                    foreach ($bets as $key => $val){
                        $val['dice_amount'] = string_to_array($val['dice_amount']);
                        $bets[$key] = $val;
                    }
                }
                if($numberTabInfo['game_id'] == 8){
                    foreach ($bets as $key => $val){
                        if($val['roulette_european_amount']){
                            $val['roulette_type'] = 'european';
                            $val['roulette_amount'] = string_to_array($val['roulette_european_amount']);
                        }elseif($val['roulette_french_amount']){
                            $val['roulette_type'] = 'french';
                            $val['roulette_amount'] = string_to_array($val['roulette_french_amount']);
                        }else{
                            $val['roulette_type'] = '';
                            $val['roulette_amount'] = array();
                        }
                        unset($val['roulette_european_amount']);
                        unset($val['roulette_french_amount']);
                        $bets[$key] = $val;
                    }
                }
            }
            $round = [
                // 靴ID
                'boot_id' => $numberTabInfo ? $numberTabInfo['boot_id'] : 0,
                // 靴号
                'boot_num' => $numberTabInfo ? $numberTabInfo['boot_num'] : 1,
                // 铺ID
                'number_tab_id' => $numberTabInfo ? $numberTabInfo['id'] : 0,
                // 铺号
                'number_tab_number' => $numberTabInfo ? $numberTabInfo['number'] : 1,
                // 铺状态 0、未开始 1、接受下注 2、下注结束 3、开出结果
                'bet_status' => $numberTabInfo ? $numberTabInfo['bet_status'] : 0,
                // 注单数据
                'bet' => $bets,
                // 牌数据
                'show_card' => $numberTabInfo ? GetCardService::getCard($tableInfo,$numberTabInfo) : []
            ];
            $ws->emit('getState',['status' => true, 'table_id' => $tableInfo['id'], 'round' => $round]);
        }else{
            $ws->emit('getState', ['status' => false, 'msg' => $preliminaryCheckRes['msg']]);
        }
    }
}
