<?php
/**
 * Created by IntelliJ IDEA.
 * User: solu
 * Date: 2019/2/14
 * Time: 5:17 PM
 */
class BaccaratController extends BaseController {
    public function __construct() {
        parent::__construct(false);
    }

    /**
     * 游戏结果
     * @author solu
     * @param $args
     * @return array
     */
    public function actionGameResult($args) {
        $rules = [
            'old' => ['int', 'desc' => '旧游戏id'],
            'new' => ['int', 'desc' => '新游戏id'],
            'player' => ['string', 'nullable' => true, 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => '用户名'],
        ];
        Param::checkParam2($rules, $args);

        return (new Game())->info($args['old'], $args['new'], $args['player']);
    }

    /**
     * 游戏信息
     * @author solu
     * @param $args
     * @return array
     */
    public function actionGameInfo($args) {
        $rules = [
            'game_id' => ['int', 'nullable' => true, 'desc' => '游戏id'],
        ];
        Param::checkParam2($rules, $args);

        $objGame = new Game();
        $gameId = (int)$args['game_id'];
        $keyword = [
            '_field' => 'id, game_state, ttl, create_time, update_time_int',
        ];
        if ($gameId) {
            $game = $objGame->objTable->getRow(['id' => $gameId], $keyword);
        } else {
            $keyword['_sortKey'] = 'id DESC';
            $keyword['_limit'] = 1;
            $game = $objGame->objTable->getRow($keyword);
            $gameId = $game['id'];
        }
        $game['timeout'] = max(strtotime($game['create_time'])  + $game['ttl'] - time(), 0);

        // 下注信息
        $offers = (new Offer())->getCurrentList($gameId);

        // 近50期开奖记录
        $winData = $objGame->winData(60);

        list($total, $list) = Game::getOnlineInfo();
        $online = compact('total', 'list');

        //gt汇率
        $objSummery = new TableHelper('summery', 'dw_eos');
        $info = $objSummery->getRow();
        $dig_rate = $info['dig_rate'];

        return compact('game', 'offers', 'winData', 'online', 'dig_rate');
    }

    /**
     * 我的下注
     * @author solu
     * @param $args
     * @return array
     */
    public function actionPlayerOffers($args) {
        $rules = [
            'player' => ['string', 'reg' => '/^[a-z0-9\.]{1,13}$/', 'desc' => '用户名'],
            'limit' => ['int', 'nullable' => true, 'range' => '[10,100]', 'default' => 25, 'desc' => '数量'],
            'next' => ['int', 'nullable' => true, 'desc' => '最后一个id'],
        ];
        Param::checkParam2($rules, $args);

        $pageSize = (int)$args['limit'];

        $objOffer = new Offer;
        $list = $objOffer->getList(Offer::LIST_PLAYER, 0, $pageSize, $args['player'], false, $args['next']);
        $more = count($list) >= $pageSize;

        return compact('list', 'more');
    }

    /**
     * 所有下注
     * @author solu
     * @param $args
     * @return array
     */
    public function actionOffers($args) {
        $rules = [
            'limit' => ['int', 'nullable' => true, 'range' => '[10,100]', 'default' => 25, 'desc' => '数量'],
            'next' => ['int', 'nullable' => true, 'desc' => '最后一个id'],
        ];
        Param::checkParam2($rules, $args);

        $pageSize = (int)$args['limit'];
        $objOffer = new Offer;

        $list = $objOffer->getList(Offer::LIST_ALL, 0, $pageSize, '', false, $args['next']);
        $more = count($list) >= $pageSize;

        return compact('list', 'more');
    }

    /**
     * 巨额奖金
     * @author solu
     * @param $args
     * @return array
     */
    public function actionRank($args) {
        $rules = [
            'limit' => ['int', 'nullable' => true, 'range' => '[10,100]', 'default' => 25, 'desc' => '数量'],
            'page' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '页码'],
        ];
        Param::checkParam2($rules, $args);

        $pageSize = (int)$args['limit'];
        $offset = max($args['page'] - 1, 0) * $pageSize;

        $objOffer = new Offer;
        $list = $objOffer->getList(Offer::LIST_RANK, $offset, $pageSize, '', true);
        $more = count($list) >= $pageSize;

        return compact('list', 'more');
    }
}