Commit f58371d8 authored by shajiaiming's avatar shajiaiming

Merge branch 'master' into feature/ws_ticker

parents 8cd511c7 9e44df21
...@@ -298,6 +298,15 @@ class Chain33Service ...@@ -298,6 +298,15 @@ class Chain33Service
return $this->send($params, 'Chain33.SignRawTx'); return $this->send($params, 'Chain33.SignRawTx');
} }
public function getBlock2MainInfo($start, $end)
{
$params = [
"Start" => $start,
"End" => $end,
];
return $this->send($params, 'paracross.GetBlock2MainInfo');
}
public function sendTrade($data) public function sendTrade($data)
{ {
$params = [ $params = [
......
...@@ -4,6 +4,7 @@ namespace console\controllers; ...@@ -4,6 +4,7 @@ namespace console\controllers;
use common\business\Chain33Business; use common\business\Chain33Business;
use common\models\psources\CoinGameBet; use common\models\psources\CoinGameBet;
use common\service\chain33\Chain33Service;
use yii\console\Controller; use yii\console\Controller;
use Yii; use Yii;
...@@ -17,48 +18,46 @@ class GameBetController extends Controller ...@@ -17,48 +18,46 @@ class GameBetController extends Controller
public function actionGameStatus() public function actionGameStatus()
{ {
$nodes = \Yii::$app->params['chain_parallel']['wasm']; $nodes = \Yii::$app->params['chain_parallel']['wasm'];
if(empty($nodes)){ if (empty($nodes)) {
echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL; echo date('Y-m-d H:i:s') . '无节点' . PHP_EOL;
return 0; return 0;
} }
foreach ($nodes as $key => $node) { foreach ($nodes as $key => $node) {
$service = new Chain33Business(); $service = new Chain33Business();
$result = $service->getGameStatus($node); $result = $service->getGameStatus($node);
if (0 !== $result['code']) { if (0 !== $result['code']) {
echo $key.':'.date('Y-m-d H:i:s') . $result['msg'].PHP_EOL; echo $key . ':' . date('Y-m-d H:i:s') . $result['msg'] . PHP_EOL;
continue; continue;
} }
$queryResultItems = $result['result'] ?? []; $queryResultItems = $result['result'] ?? [];
if (empty($queryResultItems)) { if (empty($queryResultItems)) {
echo $key.':'.date('Y-m-d H:i:s') . 'error'.PHP_EOL; echo $key . ':' . date('Y-m-d H:i:s') . 'error' . PHP_EOL;
continue; continue;
} }
$resultJSON = json_decode($queryResultItems['queryResultItems'][0]['resultJSON'], true); $resultJSON = json_decode($queryResultItems['queryResultItems'][0]['resultJSON'], true);
$current_round = $resultJSON['current_round']; $current_round = $resultJSON['current_round'];
$current_height = $resultJSON['height']; $cache_current_round = Yii::$app->redis->get('chain33_game_bet_status_' . $key);
$cache_current_round = Yii::$app->redis->get('chain33_game_bet_status_'.$key);
if (empty($cache_current_round)) { if (empty($cache_current_round)) {
$cache_current_round = CoinGameBet::find()->where(['platform' => $key])->max('round'); $cache_current_round = CoinGameBet::find()->where(['platform' => $key])->max('round');
Yii::$app->redis->set('chain33_game_bet_status_'.$key, $cache_current_round, 'EX', 300); Yii::$app->redis->set('chain33_game_bet_status_' . $key, $cache_current_round, 'EX', 300);
} }
$cache_current_round = (false == $cache_current_round ? 0 : $cache_current_round); $cache_current_round = (false == $cache_current_round ? 0 : $cache_current_round);
if ($cache_current_round >= $current_round) { if ($cache_current_round >= $current_round) {
echo $key.':'.date('Y-m-d H:i:s') . '数据已为最新' . PHP_EOL; echo $key . ':' . date('Y-m-d H:i:s') . '数据已为最新' . PHP_EOL;
continue; continue;
} }
Yii::$app->redis->set('chain33_game_bet_status_'.$key, $current_round, 'EX', 300); Yii::$app->redis->set('chain33_game_bet_status_' . $key, $current_round, 'EX', 300);
Yii::$app->redis->set('chain33_game_bet_status_height_'.$key, $current_height, 'EX', 300);
$result = $service->getBetStatus($cache_current_round, $current_round, '', $node); $result = $service->getBetStatus($cache_current_round, $current_round, '', $node);
if (0 !== $result['code']) { if (0 !== $result['code']) {
echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL; echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue; continue;
} }
$queryResultItems = $result['result'] ?? []; $queryResultItems = $result['result'] ?? [];
if (empty($queryResultItems)) { if (empty($queryResultItems)) {
echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL; echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue; continue;
} }
$platform = $key; $platform = $key;
...@@ -70,7 +69,7 @@ class GameBetController extends Controller ...@@ -70,7 +69,7 @@ class GameBetController extends Controller
]; ];
} }
CoinGameBet::loadArray($datas); CoinGameBet::loadArray($datas);
echo $platform.':'.date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL; echo $platform . ':' . date('Y-m-d H:i:s') . '数据更新成功' . PHP_EOL;
continue; continue;
} }
return 0; return 0;
...@@ -78,44 +77,66 @@ class GameBetController extends Controller ...@@ -78,44 +77,66 @@ class GameBetController extends Controller
public function actionBetUpdate() public function actionBetUpdate()
{ {
$service = new Chain33Business();
$node_params = [
'scheme' => 'https',
'host' => 'jiedian1.bityuan.com',
'port' => 8801
];
$result = $service->getLastHeader($node_params);
$main_height = isset($result['result']['height']) ? $result['result']['height'] : 0;
$safe_main_height = $main_height - 14;
$nodes = \Yii::$app->params['chain_parallel']['wasm']; $nodes = \Yii::$app->params['chain_parallel']['wasm'];
if(empty($nodes)){ if (empty($nodes)) {
echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL; echo date('Y-m-d H:i:s') . '无节点' . PHP_EOL;
return 0; return 0;
} }
foreach ($nodes as $key => $node) { foreach ($nodes as $key => $node) {
$service = new Chain33Business(); $service = new Chain33Service($node);
$result = $service->getLastHeader($node); $result = $service->getLastHeader();
$height = $result['result']['height']; $height = $result['result']['height'];
$main_info = $service->getBlock2MainInfo($height - 14, $height);
if (!isset($main_info['result']['items'])) continue;
$items = $main_info['result']['items'];
$items = $this->arraySort($items, 'mainHeight');
$safe_height = 0;
foreach ($items as $item) {
if ($safe_main_height > $item['mainHeight']) {
$safe_height = $item['height'];
break;
}
}
echo $key . ':' . date('Y-m-d H:i:s') . '当前主链高度为:' . $main_height . ';当前平行链高度为:' . $height . ';当前安全高度为:' . $safe_height . PHP_EOL;
$models = CoinGameBet::find()->select('round')->where([ $models = CoinGameBet::find()->select('round')->where([
'and', 'and',
['valid' => CoinGameBet::VAILD_FALSE], ['valid' => CoinGameBet::VAILD_FALSE],
['<', 'height', $height - 12], ['<', 'height', $safe_height],
['platform' => $key] ['platform' => $key]
])->all(); ])->all();
if(empty($models)){ if (empty($models)) {
echo $key.':'.date('Y-m-d H:i:s') . '无需更新的数据'.PHP_EOL; echo $key . ':' . date('Y-m-d H:i:s') . '无需更新的数据' . PHP_EOL;
continue; continue;
} }
$valid_arr = []; $valid_arr = [];
foreach ($models as $model) { foreach ($models as $model) {
$valid_arr[] = $model->round; $valid_arr[] = $model->round;
} }
$result = $service->getBetStatus('', '', $valid_arr, $node); $business = new Chain33Business();
if( 0 !== $result['code']){ $result = $business->getBetStatus('', '', $valid_arr, $node);
echo $key.':'.date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; if (0 !== $result['code']) {
echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue; continue;
} }
$queryResultItems = $result['result'] ?? []; $queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){ if (empty($queryResultItems)) {
echo $key.':'.date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; echo $key . ':' . date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue; continue;
} }
$platform = $key; $platform = $key;
foreach ($queryResultItems['queryResultItems'] as $key => $val){ foreach ($queryResultItems['queryResultItems'] as $key => $val) {
if (false == $val['found']) continue; if (false == $val['found']) continue;
$resultArr = json_decode($val['resultJSON'],true); $resultArr = json_decode($val['resultJSON'], true);
CoinGameBet::updateAll([ CoinGameBet::updateAll([
'amount' => $resultArr['amount'], 'amount' => $resultArr['amount'],
'height' => $resultArr['height'], 'height' => $resultArr['height'],
...@@ -123,15 +144,25 @@ class GameBetController extends Controller ...@@ -123,15 +144,25 @@ class GameBetController extends Controller
'rand_num' => $resultArr['rand_num'], 'rand_num' => $resultArr['rand_num'],
'player_win' => $resultArr['player_win'], 'player_win' => $resultArr['player_win'],
'valid' => CoinGameBet::VAILD_TRUE 'valid' => CoinGameBet::VAILD_TRUE
],[ ], [
'round' => $resultArr['round'], 'round' => $resultArr['round'],
'platform' => $platform 'platform' => $platform
]); ]);
} }
echo $platform.':'.date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL; echo $platform . ':' . date('Y-m-d H:i:s') . '数据更新成功' . PHP_EOL;
continue; continue;
} }
return 0; return 0;
} }
protected function arraySort($array, $keys, $sort = SORT_DESC)
{
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment