<?php

require_once realpath(dirname(__FILE__)) . '/../common.php';

ini_set("display_errors", "On");
error_reporting(E_ALL & ~E_NOTICE);

//$objLock = new Lock('changeBalance', 30);
//$objLock->lockWait('test', 3);
//sleep(4);
//$objLock->unlock('test');
//exit;


$objEos = new Eos();
$objRedis = dwRedis::init('dw_box');
$objGame = new TableHelper('game', 'dw_box');
$startId = 0;
$errorNum = 0;

for ($i = 0; $i < 100000; $i++) {
    if (!$startId) {
        $game = $objGame->getRow(['_sortKey' => 'id DESC']);
        $startId = (int) $game['id'] ?: 201809140551;
    } else {
        $game = null;
    }

    $id = $startId + $i;
    if ($game) {
        $source = $game['source'];
        $commitment = $game['commitment'];
    } else {
        $cmd = "openssl rand 32 -hex";
        $source = exec($cmd);

        $cmd = "echo '$source' | xxd -r -p | shasum -a 256 -b | awk '{print $1}'";
        $commitment = exec($cmd);

        $key = Eos::PRE_SIGN_MAP . $commitment;
        // 把原串信息缓存在redis
        $objRedis->setex($key, 1800, $source);
    }

    $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeBaccarat']} creategame '[ {$id}, \"{$commitment}\" ]' -p {$GLOBALS['codeBaccarat']}";
    $output = [];
    exec($cmd, $output);
    var_dump($output);

    $cmd = "cleos -u {$GLOBALS['eosUrl']} push action {$GLOBALS['codeBaccarat']} reveal '[ {$id}, \"{$source}\" ]' -p {$GLOBALS['codeBaccarat']}";
    exec($cmd, $output);
    var_dump($output);

    if (!$output && $errorNum++ > 20) {
        break;
    }

    if ($i % 5 == 0) {
//        $objEos->syncGame();
        $objSyncGame = new Sync_Game();
        $objSyncGame->pubSubscribe();
    }
}




