<?php


namespace app\services\scan;


use freedom\utils\CardPosition;
use freedom\utils\RedisUtil;

/**
 * TODO Dt扫描
 * Class ScanDtService
 * @package app\services\scan
 */
class ScanDtService
{
    /**
     * TODO Dt扫描处理
     * @param array $event
     * @param array $tableInfo
     * @return array
     */
    public static function doScanDt(array $event, array $tableInfo): array{
        $checkScanRes = ScanCommonService::checkScan($event,$tableInfo);
        if ($checkScanRes['status'] == false){
            return $checkScanRes;
        }else{
            $numberTabInfo = $checkScanRes['numberTabInfo'];
            $cardInfo = $checkScanRes['cardInfo'];
        }
        if (!isset($event['card']) || !isset($event['position'])) return ['status' => false, 'msg' => 'CardPosition Unable Distinguish'];
        $card = CardPosition::interchangeCard($event['card']);
        if (!$card) return ['status' => false, 'msg' => 'CardPosition Unable Distinguish'];
        $position = CardPosition::interchangePosition($event['position']);
        if (!$position) return ['status' => false, 'msg' => 'Position Error'];

        $cardInfo[$position] = intval($event['card']);
        RedisUtil::saveCardPosition($numberTabInfo['id'],intval($event['position']),intval($event['card']));
        $round = array(
            'tid' => intval($tableInfo['id']),
            'number_tab_id' => $numberTabInfo['id'],
            'card' => intval($event['card']),
            'number' => $card,
            'position' => intval($event['position']),
            'boot_id' => intval($numberTabInfo['boot_id']),
            'boot_num' => intval($numberTabInfo['boot_num']),
            'number_tab_number' => intval($numberTabInfo['number']),
            'card_info' => $cardInfo
        );
        if ($numberTabInfo['bet_status'] == 2) {
            $round['is_scan'] = true;
        } else {
            $round['is_scan'] = false;
        }
        return ['status' => true, 'data' => $round];
    }
}