getGameStatus(); if( 0 !== $result['code']){ echo date('Y-m-d H:i:s') . $result['msg'].PHP_EOL; return 0; } $queryResultItems = $result['result'] ?? []; if(empty($queryResultItems)){ echo date('Y-m-d H:i:s') . 'error'.PHP_EOL; return 0; } $resultJSON = json_decode($queryResultItems['queryResultItems'][0]['resultJSON'],true); $current_round = $resultJSON['current_round']; $cache_current_round = Yii::$app->redis->get('chain33_game_bet_status'); if(empty($cache_current_round)){ $cache_current_round = CoinGameBet::find()->max('round'); Yii::$app->redis->set('chain33_game_bet_status',$cache_current_round,'EX',300); } $cache_current_round = (false == $cache_current_round ? 0 : $cache_current_round); if($cache_current_round >= $current_round){ echo date('Y-m-d H:i:s') . '数据已为最新'.PHP_EOL; return 0; } Yii::$app->redis->set('chain33_game_bet_status',$current_round,'EX',300); $result = $service->getBetStatus($cache_current_round, $current_round); if( 0 !== $result['code']){ echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; return 0; } $queryResultItems = $result['result'] ?? []; if(empty($queryResultItems)){ echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; return 0; } foreach ($queryResultItems['queryResultItems'] as $key => $val){ if( false == $val['found'] ) continue; $resultArr = json_decode($val['resultJSON'],true); $datas[] = [ $resultArr['round'], $resultArr['player'], $resultArr['amount'], $resultArr['height'], $resultArr['guess_num'], $resultArr['rand_num'], $resultArr['player_win'] ]; } CoinGameBet::loadArray($datas); echo date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL; return 0; } public function actionBetUpdate() { $service = new Chain33Business(); $result = $service->getLastHeader(); $result = $result['result'] ?? []; if(empty($result)){ echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; return 0; } $height = $result['height']; $models = CoinGameBet::find()->select('round')->where([ 'and', ['valid' => CoinGameBet::VAILD_FALSE], ['<', 'height', $height - 12] ])->all(); if(empty($models)){ echo date('Y-m-d H:i:s') . '无需更新的数据'.PHP_EOL; return 0; } $valid_arr = []; foreach ($models as $model) { $valid_arr[] = $model->round; } $result = $service->getBetStatus('', '', $valid_arr); if( 0 !== $result['code']){ echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; return 0; } $queryResultItems = $result['result'] ?? []; if(empty($queryResultItems)){ echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; return 0; } foreach ($queryResultItems['queryResultItems'] as $key => $val){ if (false == $val['found']) continue; $resultArr = json_decode($val['resultJSON'],true); CoinGameBet::updateAll([ 'amount' => $resultArr['amount'], 'height' => $resultArr['height'], 'guess_num' => $resultArr['guess_num'], 'rand_num' => $resultArr['rand_num'], 'player_win' => $resultArr['player_win'], 'valid' => CoinGameBet::VAILD_TRUE ],[ 'round' => $resultArr['round'] ]); } echo date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL; return 0; } }