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;
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()
{
$platform = Yii::$app->request->get('platform', 'ts_wallet');
$player = Yii::$app->request->get('player', '');
$page = Yii::$app->request->get('page', 1);
if(empty($player)){
if(empty($player) || empty($platform)){
$msg = '请求参数错误';
$code = -1;
$data = null;
......@@ -72,8 +23,9 @@ class GameBetController extends BaseController
}
$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])
->andWhere(['platform' => $platform])
#->andWhere(['valid' => CoinGameBet::VAILD_TRUE])
->orderBy('update_time desc');
......
......@@ -11,46 +11,90 @@ namespace api\controllers;
use Yii;
use api\base\BaseController;
use common\models\pwallet\GameUser;
use common\models\pwallet\GameUserAddress;
class GameController extends BaseController {
/**
* 保存地址与昵称
*/
public function actionSaveAddressAndNickname(){
$post = Yii::$app->request->post();
$address = $post['address']??'';
$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' => '昵称重复'];
}
// todo 保存
$models = new GameUser();
$models->nickname = $nickname;
$models->address = $address;
if ($models->save()) {
return ['code' => 0, 'msg' => 'Succeed', 'data' => $models->id];
}
return ['code' => -1, 'msg' => 'Failed'];
}
class GameController extends BaseController
{
/**
* 保存地址与昵称
*/
public function actionSaveAddressAndNickname()
{
$post = Yii::$app->request->post();
$address = $post['address'] ?? '';
$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' => '昵称重复'];
}
// todo 保存
$models = new GameUser();
$models->nickname = $nickname;
$models->address = $address;
if ($models->save()) {
return ['code' => 0, 'msg' => 'Succeed', 'data' => $models->id];
}
return ['code' => -1, 'msg' => 'Failed'];
}
/**
* 根据地址获取昵称
*/
public function actionGetNicknameByAddress(){
$address = Yii::$app->request->post('address', '');
if (empty($address)) {
return ['code' => -1, 'msg' => '地址不能为空', 'data' => ''];
}
//todo 获取地址
$address = GameUser::find()->select(['nickname'])->where(['address' => $address])->one();
if (!empty($address)) {
return ['code' => 0, 'msg'=> 'Succeed', 'data' => $address];
}
return ['code' => -1, 'msg' => 'Failed'];
}
/**
* 根据地址获取昵称
*/
public function actionGetNicknameByAddress()
{
$address = Yii::$app->request->post('address', '');
if (empty($address)) {
return ['code' => -1, 'msg' => '地址不能为空', 'data' => ''];
}
//todo 获取地址
$address = GameUser::find()->select(['nickname'])->where(['address' => $address])->one();
if (!empty($address)) {
return ['code' => 0, 'msg' => 'Succeed', 'data' => $address];
}
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
* 获取游戏状态
* @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);
$execer = 'wasm';
$funcName = 'WasmGetContractTable';
$contractName = 'user.p.tschain.user.wasm.dice';
$contractName = $node_params['contractName'];
$items[] = [
'tableName' => 'gamestatus',
'key' => 'dice_statics'
......@@ -179,13 +178,12 @@ class Chain33Business
* @param integer $end
* @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);
$execer = 'wasm';
$funcName = 'WasmGetContractTable';
$contractName = 'user.p.tschain.user.wasm.dice';
$contractName = $node_params['contractName'];
if (empty($roundArr)) {
for($i = $start + 1; $i <= $end; $i++){
$items[] = [
......@@ -211,9 +209,8 @@ class Chain33Business
* @param null
* @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);
return $service->getLastHeader();
}
......
......@@ -63,7 +63,7 @@ class CoinGameBet extends BaseActiveRecord
public static function loadArray(array $data)
{
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();
}
}
<?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
*/
public function actionGameStatus()
{
$service = new Chain33Business();
$result = $service->getGameStatus();
if( 0 !== $result['code']){
echo date('Y-m-d H:i:s') . $result['msg'].PHP_EOL;
$nodes = \Yii::$app->params['chain_parallel']['wasm'];
if(empty($nodes)){
echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL;
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'] ?? [];
if(empty($queryResultItems)){
echo date('Y-m-d H:i:s') . 'error'.PHP_EOL;
return 0;
}
$queryResultItems = $result['result'] ?? [];
if (empty($queryResultItems)) {
echo $key.':'.date('Y-m-d H:i:s') . 'error'.PHP_EOL;
continue;
}
$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);
$resultJSON = json_decode($queryResultItems['queryResultItems'][0]['resultJSON'], true);
$current_round = $resultJSON['current_round'];
$cache_current_round = Yii::$app->redis->get('chain33_game_bet_status_'.$key);
if (empty($cache_current_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);
}
$cache_current_round = (false == $cache_current_round ? 0 : $cache_current_round);
if ($cache_current_round >= $current_round) {
echo $key.':'.date('Y-m-d H:i:s') . '数据已为最新' . PHP_EOL;
continue;
}
Yii::$app->redis->set('chain33_game_bet_status_'.$key, $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;
}
$result = $service->getBetStatus($cache_current_round, $current_round, '', $node);
if (0 !== $result['code']) {
echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
continue;
}
$queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){
echo date('Y-m-d H:i:s') . '数据错误'.PHP_EOL;
return 0;
$queryResultItems = $result['result'] ?? [];
if (empty($queryResultItems)) {
echo $key.':'.date('Y-m-d H:i:s') . '数据错误' . PHP_EOL;
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;
}
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;
$nodes = \Yii::$app->params['chain_parallel']['wasm'];
if(empty($nodes)){
echo date('Y-m-d H:i:s') . '无节点'.PHP_EOL;
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'] ?? [];
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']
]);
$queryResultItems = $result['result'] ?? [];
if(empty($queryResultItems)){
echo $key.':'.date('Y-m-d H:i:s') . '数据错误'.PHP_EOL;
continue;
}
$platform = $key;
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'],
'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;
}
}
\ 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