Commit 726e5c9c authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/game' into 'master'

Feature/game See merge request !69
parents 330fabcf 7a15756a
...@@ -10,61 +10,12 @@ use yii\data\Pagination; ...@@ -10,61 +10,12 @@ use yii\data\Pagination;
class GameBetController extends BaseController class GameBetController extends BaseController
{ {
/**
* 获取游戏状态
*
* @return array
*/
public function actionGameStatus()
{
$service = new Chain33Business();
$result = $service->getGameStatus();
if( 0 !== $result['code']){
return ['code' => -1,'data' => [], 'msg' => $result['msg']];
}
$queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){
return ['code' => -1,'data' => [], 'msg' => 'error'];
}
$resultJSON = json_decode($queryResultItems['queryResultItems'][0]['resultJSON'],true);
$current_round = $resultJSON['current_round'];
$cache_current_round = Yii::$app->redis->get('chain33_game_status');
if(empty($cache_current_round)){
$cache_current_round = CoinGameBet::find()->max('round');
Yii::$app->redis->set('chain33_game_status',$cache_current_round,'EX',300);
}
$cache_current_round = (false == $cache_current_round ? 0 : $cache_current_round);
if($cache_current_round >= $current_round){
return ['code' => -1,'data' => [], 'msg' => '数据已为最新'];
}
Yii::$app->redis->set('chain33_game_status',$current_round,'EX',300);
$result = $service->getBetStatus($cache_current_round, $current_round);
if( 0 !== $result['code']){
return ['code' => -1,'data' => [], 'msg' => '数据错误'];
}
$queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){
return ['code' => -1,'data' => [], 'msg' => '数据错误'];
}
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);
return ['code' => 1,'data' => [], 'msg' => '数据更新成功'];
}
public function actionBetStatus() public function actionBetStatus()
{ {
$platform = Yii::$app->request->get('platform', 'ts_wallet');
$player = Yii::$app->request->get('player', ''); $player = Yii::$app->request->get('player', '');
$page = Yii::$app->request->get('page', 1); $page = Yii::$app->request->get('page', 1);
if(empty($player)){ if(empty($player) || empty($platform)){
$msg = '请求参数错误'; $msg = '请求参数错误';
$code = -1; $code = -1;
$data = null; $data = null;
...@@ -72,8 +23,9 @@ class GameBetController extends BaseController ...@@ -72,8 +23,9 @@ class GameBetController extends BaseController
} }
$query = CoinGameBet::find() $query = CoinGameBet::find()
->select('round, player, amount, height, guess_num, guess_num, rand_num, player_win') ->select('round, player, amount, height, guess_num, guess_num, rand_num, player_win, platform')
->where('player= :player',[':player' => $player]) ->where('player= :player',[':player' => $player])
->andWhere(['platform' => $platform])
#->andWhere(['valid' => CoinGameBet::VAILD_TRUE]) #->andWhere(['valid' => CoinGameBet::VAILD_TRUE])
->orderBy('update_time desc'); ->orderBy('update_time desc');
......
...@@ -11,46 +11,90 @@ namespace api\controllers; ...@@ -11,46 +11,90 @@ namespace api\controllers;
use Yii; use Yii;
use api\base\BaseController; use api\base\BaseController;
use common\models\pwallet\GameUser; use common\models\pwallet\GameUser;
use common\models\pwallet\GameUserAddress;
class GameController extends BaseController { class GameController extends BaseController
/** {
* 保存地址与昵称 /**
*/ * 保存地址与昵称
public function actionSaveAddressAndNickname(){ */
$post = Yii::$app->request->post(); public function actionSaveAddressAndNickname()
$address = $post['address']??''; {
$nickname = $post['nickname']??''; $post = Yii::$app->request->post();
if (empty($address) || empty($nickname)) { $address = $post['address'] ?? '';
return ['code' => -1, 'msg' => '地址和昵称不能为空', 'data' => '']; $nickname = $post['nickname'] ?? '';
} if (empty($address) || empty($nickname)) {
//判断重复 return ['code' => -1, 'msg' => '地址和昵称不能为空', 'data' => ''];
$count = GameUser::find()->where(['nickname' => $nickname])->count(); }
if ($count > 0) { //判断重复
return ['code' => -1, 'msg' => '昵称重复']; $count = GameUser::find()->where(['nickname' => $nickname])->count();
} if ($count > 0) {
// todo 保存 return ['code' => -1, 'msg' => '昵称重复'];
$models = new GameUser(); }
$models->nickname = $nickname; // todo 保存
$models->address = $address; $models = new GameUser();
if ($models->save()) { $models->nickname = $nickname;
return ['code' => 0, 'msg' => 'Succeed', 'data' => $models->id]; $models->address = $address;
} if ($models->save()) {
return ['code' => -1, 'msg' => 'Failed']; return ['code' => 0, 'msg' => 'Succeed', 'data' => $models->id];
} }
return ['code' => -1, 'msg' => 'Failed'];
}
/** /**
* 根据地址获取昵称 * 根据地址获取昵称
*/ */
public function actionGetNicknameByAddress(){ public function actionGetNicknameByAddress()
$address = Yii::$app->request->post('address', ''); {
if (empty($address)) { $address = Yii::$app->request->post('address', '');
return ['code' => -1, 'msg' => '地址不能为空', 'data' => '']; if (empty($address)) {
} return ['code' => -1, 'msg' => '地址不能为空', 'data' => ''];
//todo 获取地址 }
$address = GameUser::find()->select(['nickname'])->where(['address' => $address])->one(); //todo 获取地址
if (!empty($address)) { $address = GameUser::find()->select(['nickname'])->where(['address' => $address])->one();
return ['code' => 0, 'msg'=> 'Succeed', 'data' => $address]; if (!empty($address)) {
} return ['code' => 0, 'msg' => 'Succeed', 'data' => $address];
return ['code' => -1, 'msg' => 'Failed']; }
} return ['code' => -1, 'msg' => 'Failed'];
}
public function actionSaveGameInfo()
{
$post = Yii::$app->request->post();
$address = $post['address'] ?? '';
$nickname = $post['nickname'] ?? '';
if (empty($address) || empty($nickname)) {
return ['code' => -1, 'msg' => '地址和昵称不能为空', 'data' => null];
}
//判断重复
$count = GameUserAddress::find()->where(['address' => $address])->count();
if ($count > 0) {
return ['code' => -1, 'msg' => '地址已存在', 'data' => null];
}
// todo 保存
$models = new GameUserAddress();
$models->nickname = $nickname;
$models->address = $address;
if ($models->save()) {
return ['code' => 0, 'msg' => 'Succeed', 'data' => $models->id];
}
return ['code' => -1, 'msg' => 'Failed', 'data' => null];
}
public function actionGetNicknameByAddressArr()
{
$address = Yii::$app->request->post('address', '');
if (empty($address)) {
return ['code' => -1, 'msg' => '地址不能为空', 'data' => ''];
}
//todo 获取地址
$address = GameUserAddress::find()
->select(['nickname', 'address'])
->where(['in', 'address', $address])
->asArray()->all();
if (!empty($address)) {
return ['code' => 0, 'msg' => 'Succeed', 'data' => $address];
}
return ['code' => -1, 'msg' => 'Failed'];
}
} }
\ No newline at end of file
...@@ -159,13 +159,12 @@ class Chain33Business ...@@ -159,13 +159,12 @@ class Chain33Business
* 获取游戏状态 * 获取游戏状态
* @return array * @return array
*/ */
public static function getGameStatus() public static function getGameStatus($node_params = [])
{ {
$node_params = \Yii::$app->params['chain_parallel']['wasm'];
$service = new Chain33Service($node_params); $service = new Chain33Service($node_params);
$execer = 'wasm'; $execer = 'wasm';
$funcName = 'WasmGetContractTable'; $funcName = 'WasmGetContractTable';
$contractName = 'user.p.tschain.user.wasm.dice'; $contractName = $node_params['contractName'];
$items[] = [ $items[] = [
'tableName' => 'gamestatus', 'tableName' => 'gamestatus',
'key' => 'dice_statics' 'key' => 'dice_statics'
...@@ -179,13 +178,12 @@ class Chain33Business ...@@ -179,13 +178,12 @@ class Chain33Business
* @param integer $end * @param integer $end
* @return array * @return array
*/ */
public static function getBetStatus($start, $end, $roundArr = []) public static function getBetStatus($start, $end, $roundArr = [], $node_params = [])
{ {
$node_params = \Yii::$app->params['chain_parallel']['wasm'];
$service = new Chain33Service($node_params); $service = new Chain33Service($node_params);
$execer = 'wasm'; $execer = 'wasm';
$funcName = 'WasmGetContractTable'; $funcName = 'WasmGetContractTable';
$contractName = 'user.p.tschain.user.wasm.dice'; $contractName = $node_params['contractName'];
if (empty($roundArr)) { if (empty($roundArr)) {
for($i = $start + 1; $i <= $end; $i++){ for($i = $start + 1; $i <= $end; $i++){
$items[] = [ $items[] = [
...@@ -211,9 +209,8 @@ class Chain33Business ...@@ -211,9 +209,8 @@ class Chain33Business
* @param null * @param null
* @return array * @return array
*/ */
public static function getLastHeader() public static function getLastHeader($node_params = [])
{ {
$node_params = \Yii::$app->params['chain_parallel']['wasm'];
$service = new Chain33Service($node_params); $service = new Chain33Service($node_params);
return $service->getLastHeader(); return $service->getLastHeader();
} }
......
...@@ -63,7 +63,7 @@ class CoinGameBet extends BaseActiveRecord ...@@ -63,7 +63,7 @@ class CoinGameBet extends BaseActiveRecord
public static function loadArray(array $data) public static function loadArray(array $data)
{ {
return self::getDb()->createCommand()->batchInsert(self::tableName(), return self::getDb()->createCommand()->batchInsert(self::tableName(),
['round', 'player', 'amount', 'height', 'guess_num', 'rand_num', 'player_win'], ['round', 'player', 'amount', 'height', 'guess_num', 'rand_num', 'player_win', 'platform'],
$data)->execute(); $data)->execute();
} }
} }
<?php
/**
* Created By Sublime text 3
* User: rlgyzhcn
* Date: 18-08-12
* Time: 下午14:17
*/
namespace common\models\pwallet;
use common\core\BaseActiveRecord;
use Yii;
class GameUserAddress extends BaseActiveRecord{
public static function getDb(){
return Yii::$app->get('db_pwallet');
}
public function formName()
{
return '';
}
}
\ No newline at end of file
...@@ -16,105 +16,120 @@ class GameBetController extends Controller ...@@ -16,105 +16,120 @@ class GameBetController extends Controller
*/ */
public function actionGameStatus() public function actionGameStatus()
{ {
$service = new Chain33Business(); $nodes = \Yii::$app->params['chain_parallel']['wasm'];
$result = $service->getGameStatus(); if(empty($nodes)){
if( 0 !== $result['code']){ echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL;
echo date('Y-m-d H:i:s') . $result['msg'].PHP_EOL;
return 0; return 0;
} }
foreach ($nodes as $key => $node) {
$service = new Chain33Business();
$result = $service->getGameStatus($node);
if (0 !== $result['code']) {
echo $key.':'.date('Y-m-d H:i:s') . $result['msg'].PHP_EOL;
continue;
}
$queryResultItems = $result['result'] ?? []; $queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){ if (empty($queryResultItems)) {
echo date('Y-m-d H:i:s') . 'error'.PHP_EOL; echo $key.':'.date('Y-m-d H:i:s') . 'error'.PHP_EOL;
return 0; 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'];
$cache_current_round = Yii::$app->redis->get('chain33_game_bet_status'); $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()->max('round'); $cache_current_round = CoinGameBet::find()->where(['platform' => $key])->max('round');
Yii::$app->redis->set('chain33_game_bet_status',$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 date('Y-m-d H:i:s') . '数据已为最新'.PHP_EOL; echo $key.':'.date('Y-m-d H:i:s') . '数据已为最新' . PHP_EOL;
return 0; continue;
} }
Yii::$app->redis->set('chain33_game_bet_status',$current_round,'EX',300); Yii::$app->redis->set('chain33_game_bet_status_'.$key, $current_round, 'EX', 300);
$result = $service->getBetStatus($cache_current_round, $current_round); $result = $service->getBetStatus($cache_current_round, $current_round, '', $node);
if( 0 !== $result['code']){ if (0 !== $result['code']) {
echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
return 0; continue;
} }
$queryResultItems = $result['result'] ?? []; $queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){ if (empty($queryResultItems)) {
echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
return 0; continue;
}
$platform = $key;
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'], $platform
];
}
CoinGameBet::loadArray($datas);
echo $platform.':'.date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL;
continue;
} }
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; return 0;
} }
public function actionBetUpdate() public function actionBetUpdate()
{ {
$service = new Chain33Business(); $nodes = \Yii::$app->params['chain_parallel']['wasm'];
$result = $service->getLastHeader(); if(empty($nodes)){
$result = $result['result'] ?? []; echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL;
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; return 0;
} }
foreach ($nodes as $key => $node) {
$service = new Chain33Business();
$result = $service->getLastHeader($node);
$height = $result['result']['height'];
$models = CoinGameBet::find()->select('round')->where([
'and',
['valid' => CoinGameBet::VAILD_FALSE],
['<', 'height', $height - 12],
['platform' => $key]
])->all();
if(empty($models)){
echo $key.':'.date('Y-m-d H:i:s') . '无需更新的数据'.PHP_EOL;
continue;
}
$valid_arr = [];
foreach ($models as $model) {
$valid_arr[] = $model->round;
}
$result = $service->getBetStatus('', '', $valid_arr, $node);
if( 0 !== $result['code']){
echo $key.':'.date('Y-m-d H:i:s') . '数据错误'.PHP_EOL;
continue;
}
$queryResultItems = $result['result'] ?? []; $queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){ if(empty($queryResultItems)){
echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL; echo $key.':'.date('Y-m-d H:i:s') . '数据错误'.PHP_EOL;
return 0; continue;
} }
foreach ($queryResultItems['queryResultItems'] as $key => $val){ $platform = $key;
if (false == $val['found']) continue; foreach ($queryResultItems['queryResultItems'] as $key => $val){
$resultArr = json_decode($val['resultJSON'],true); if (false == $val['found']) continue;
CoinGameBet::updateAll([ $resultArr = json_decode($val['resultJSON'],true);
'amount' => $resultArr['amount'], CoinGameBet::updateAll([
'height' => $resultArr['height'], 'amount' => $resultArr['amount'],
'guess_num' => $resultArr['guess_num'], 'height' => $resultArr['height'],
'rand_num' => $resultArr['rand_num'], 'guess_num' => $resultArr['guess_num'],
'player_win' => $resultArr['player_win'], 'rand_num' => $resultArr['rand_num'],
'valid' => CoinGameBet::VAILD_TRUE 'player_win' => $resultArr['player_win'],
],[ 'valid' => CoinGameBet::VAILD_TRUE
'round' => $resultArr['round'] ],[
]); 'round' => $resultArr['round'],
'platform' => $platform
]);
}
echo $platform.':'.date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL;
continue;
} }
echo date('Y-m-d H:i:s') . '数据更新成功'.PHP_EOL;
return 0; return 0;
} }
} }
\ 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