Commit 920ee7b7 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/bet' into 'master'

Feature/bet See merge request !31
parents fe28e0a0 299b2b0c
......@@ -8,6 +8,8 @@
namespace api\base;
use Yii;
use yii\web\Response;
use yii\web\Controller;
class BaseController extends Controller
......@@ -44,4 +46,91 @@ class BaseController extends Controller
$this->end = microtime(true);
return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
}
/**
* 返回成功结果,附加成功code
* @param type $data
* @return array
*/
public static function formatSuccessResult($data = null)
{
return self::formatResult(0, 'ok', $data);
}
/**
* 返回结果,附加部分信息
* @param int $errcode
* @param string $errmsg
* @param array $data
*/
public static function formatResult($errcode, $errmsg, $data = null)
{
$callback = Yii::$app->request->get('callback');
$result = [
'errcode' => $errcode,
'errmsg' => $errmsg,
];
if($data !== null) {
$result['data'] = Yii::createObject('yii\rest\Serializer')->serialize($data);
}
$response = Yii::$app->response;
$response->format = Response::FORMAT_JSON;
//jsonp数据格式
if(!is_null($callback)) {
Yii::$app->getResponse()->format = Response::FORMAT_JSONP;
$result = [
'data' => $result,
'callback' => $callback,
];
}
return $result;
}
protected function addPaginationHeaders($pagination)
{
$links = [];
foreach ($pagination->getLinks(true) as $rel => $url) {
$links[] = "<$url>; rel=$rel";
}
Yii::$app->getResponse()->getHeaders()
->set('X-Pagination-Total-Count', $pagination->totalCount)
->set('X-Pagination-Page-Count', $pagination->getPageCount())
->set('X-Pagination-Current-Page', $pagination->getPage() + 1)
->set('X-Pagination-Per-Page', $pagination->pageSize)
->set('Link', implode(', ', $links));
}
protected function getRequestedFields()
{
$fields = Yii::$app->request->get('fields');
$expand = Yii::$app->request->get('expand');
return [
preg_split('/\s*,\s*/', $fields, -1, PREG_SPLIT_NO_EMPTY),
preg_split('/\s*,\s*/', $expand, -1, PREG_SPLIT_NO_EMPTY),
];
}
protected function serializeModels(array $models)
{
list ($fields, $expand) = $this->getRequestedFields();
foreach ($models as $i => $model) {
if ($model instanceof Arrayable) {
$models[$i] = $model->toArray($fields, $expand);
} elseif (is_array($model)) {
$models[$i] = ArrayHelper::toArray($model);
}
}
return $models;
}
}
\ No newline at end of file
<?php
namespace api\controllers;
use common\business\Chain33Business;
use common\models\psources\CoinGameBet;
use Yii;
use api\base\BaseController;
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()
{
$player = Yii::$app->request->get('player', '');
$page = Yii::$app->request->get('page', 1);
if(empty($player)){
$msg = '请求参数错误';
$code = -1;
$data = null;
goto doEnd;
}
$query = CoinGameBet::find()
->select('round, player, amount, height, guess_num, guess_num, rand_num, player_win')
->where('player= :player',[':player' => $player])
->orderBy('update_time desc');
$count = $query->count();
if( 0 == $count) {
$msg = '数据不存在';
$code = -1;
$data = null;
goto doEnd;
}
$data = $query->offset(($page - 1) * 20)->limit(20)->asArray()->all();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '20']);
$data = [
'list' => $data,
'page' => [
'pageCount' => $pages->pageCount,
'pageSize' => 20,
'currentPage' => $page,
]
];
$code = 1;
$msg = 'success';
doEnd :
return ['code' => $code, 'data' => $data, 'msg' => $msg];
}
}
\ No newline at end of file
......@@ -9,10 +9,10 @@
namespace api\controllers;
use api\base\BaseController;
use common\business\Chain33Business;
use common\models\psources\CoinDailyStatistics;
use common\models\psources\CoinDailyNumbers;
use common\models\psources\CoinDailyTimes;
use common\service\chain33\Chain33Service;
use Yii;
class VisitStatisticsController extends BaseController
......@@ -120,7 +120,7 @@ class VisitStatisticsController extends BaseController
return ['code' => -1,'data' => [], 'msg' => '缺少必要的参数'];
}
$service = new Chain33Service();
$service = new Chain33Business();
$result = $service->getDappTradeResult($coins_address);
if( 0 !== $result['code']){
......
......@@ -142,6 +142,64 @@ class Chain33Business
}
/**
* 获取coin地址下DAPP交易信息
* @param string $address
* @return array
*/
public static function getDappTradeResult($address)
{
$service = new Chain33Service();
$execer = 'wasm';
$funcName = 'WasmGetContractTable';
$contractName = 'user.p.tschain.user.wasm.dice';
$items[] = [
'tableName' => 'addrinfo',
'key' => 'addrinfo-' . $address
];
return $service->chain33Query($execer, $funcName, $contractName, $items);
}
/*
* 获取游戏状态
* @return array
*/
public static function getGameStatus()
{
$node_params = \Yii::$app->params['chain_parallel']['wasm'];
$service = new Chain33Service($node_params);
$execer = 'wasm';
$funcName = 'WasmGetContractTable';
$contractName = 'user.p.para.user.wasm.dice';
$items[] = [
'tableName' => 'gamestatus',
'key' => 'dice_statics'
];
return $service->chain33Query($execer, $funcName, $contractName, $items);
}
/*
* 获取投注状态
* @param integer $start
* @param integer $end
* @return array
*/
public static function getBetStatus($start, $end)
{
$node_params = \Yii::$app->params['chain_parallel']['wasm'];
$service = new Chain33Service($node_params);
$execer = 'wasm';
$funcName = 'WasmGetContractTable';
$contractName = 'user.p.para.user.wasm.dice';
for($i = $start + 1; $i <= $end; $i++){
$items[] = [
'tableName' => 'roundinfo',
'key' => 'round:' . $i
];
}
return $service->chain33Query($execer, $funcName, $contractName, $items);
}
/**
* 获取地址下的所有交易记录
*
* @param $addr
......
<?php
namespace common\models\psources;
use common\core\BaseActiveRecord;
use Yii;
/**
* CoinReleaseMember
*
* @property int $id
* @property int $status
* @property string $address
* @property string $amount
*/
class CoinGameBet extends BaseActiveRecord
{
const SCENARIOS_ADD = 'add';
const SCENARIOS_UPDATE = 'update';
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_game_bet}}';
}
public function attributeLabels()
{
return [
'id' => 'ID',
'round' => '地址',
'player' => '数量',
'amount' => '交易hash',
'height' => '状态',
'rand_num' => '状态',
'player_win' => '状态',
];
}
public function rules()
{
return [
[['round', 'amount', 'height', 'guess_num', 'rand_num', 'player_win'], 'int'],
[['player'], 'string']
];
}
public function sercians()
{
return [
self::SCENARIOS_ADD => ['round', 'amount', 'height', 'guess_num', 'rand_num', 'player_win', 'player'],
self::SCENARIOS_UPDATE => ['round', 'amount', 'height', 'guess_num', 'rand_num', 'player_win', 'player'],
];
}
public static function loadArray(array $data)
{
return self::getDb()->createCommand()->batchInsert(self::tableName(),
['round', 'player', 'amount', 'height', 'guess_num', 'rand_num', 'player_win'],
$data)->execute();
}
}
......@@ -94,7 +94,28 @@ class Chain33Service
]
]
];
return $this->send($params);
}
/**
* Chain33服务查询
* @param string $execer
* @param string $funcName
* @param string $contractName
* @param array $items
* @return array
*/
public function chain33Query($execer = 'wasm', $funcName = 'WasmGetContractTable', $contractName = 'user.p.para.user.wasm.dice', $items = [])
{
$params = [
'execer' => $execer,
'funcName' => $funcName,
'payload' => [
'contractName' => $contractName,
'items' =>
$items
]
];
return $this->send($params);
}
......
<?php
namespace console\controllers;
use common\business\Chain33Business;
use common\models\psources\CoinGameBet;
use yii\console\Controller;
use Yii;
class GameBetController extends Controller
{
/**
* 获取游戏状态
*
* @return array
*/
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;
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_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){
echo date('Y-m-d H:i:s') . '数据已为最新'.PHP_EOL;
return 0;
}
Yii::$app->redis->set('chain33_game_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;
}
}
\ 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