Commit 55c1f4df authored by ZhuChunYang's avatar ZhuChunYang

add robot lottery

parent f990e95f
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
namespace common\business; namespace common\business;
use common\models\psources\Coin; use common\models\psources\Coin;
use common\service\chain33\LotteryService;
use common\service\coin\CoinFactory; use common\service\coin\CoinFactory;
class LotteryBusiness class LotteryBusiness
...@@ -16,16 +17,52 @@ class LotteryBusiness ...@@ -16,16 +17,52 @@ class LotteryBusiness
/** /**
* 参与/构造交易 * 参与/构造交易
*/ */
public static function trade() public static function trade($amount, $number, $way, $fee)
{ {
$error = '';
$service = new LotteryService();
$trade_result = $service->trade($amount, $number, $way, $fee);
if ($trade_result['code'] == 0) { //交易成功
$trade_txhex = $trade_result['result'];
$sign_result = $service->sign($trade_txhex);
if($sign_result['code'] == 0){ //签名成功
$data = $sign_result['result'];
$send_transaction_result = $service->sendTransaction($data);
if($send_transaction_result['code'] == 0){ //发送交易成功
return ['code' => 0,'result' => $send_transaction_result['result']];
}
}else{
$error = $sign_result['msg'];
}
}else{
$error = $trade_result['msg'];
}
return ['code' => -1, 'msg' => $error];
} }
/** /**
* 开奖 * 开奖
*/ */
public static function lottery() public static function lottery($fee = 0)
{ {
$error = '';
$service = new LotteryService();
$lottery_result = $service->lottery($fee);
if ($lottery_result['code'] == 0) { //交易成功
$lottery_txhex = $lottery_result['result'];
$sign_result = $service->sign($lottery_txhex);
if($sign_result['code'] == 0){ //签名成功
$data = $sign_result['result'];
$send_transaction_result = $service->sendTransaction($data);
if($send_transaction_result['code'] == 0){ //发送交易成功
return ['code' => 0,'result' => $send_transaction_result['result']];
}
}else{
$error = $sign_result['msg'];
}
}else{
$error = $lottery_result['msg'];
}
return ['code' => -1, 'msg' => $error];
} }
} }
...@@ -51,7 +51,7 @@ class Chain33Service ...@@ -51,7 +51,7 @@ class Chain33Service
$ch->setRawPostData($jsonrpc); $ch->setRawPostData($jsonrpc);
$result = $ch->post(self::urlBuild(), false); $result = $ch->post(self::urlBuild(), false);
if (!$result) { if (!$result) {
return ['code' => -1, 'msg' => '请求失败']; return ['code' => -1, 'msg' => $ch->errorText];
} }
if (0 == $result['id'] && empty($result['error'])) { if (0 == $result['id'] && empty($result['error'])) {
$result['code'] = $result['id']; $result['code'] = $result['id'];
......
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/10/12
* Time: 10:08
*/
namespace common\service\chain33;
use Yii;
use common\helpers\Curl;
/**
* Lottery平行链 区块链接口
*/
class LotteryService
{
/**
* @return string
* 平行链上操作彩票
*/
public static function urlLotteryBuild()
{
$config = Yii::$app->params['lottery'];
$service = $config['service'];
$scheme = $service['scheme'] ?? 'http';
$host = $service['host'] ?? '127.0.0.1';
$port = (string)$service['port'] ?? '';
if ($port) {
return $scheme . '://' . $host . ':' . $port;
} else {
return $scheme . '://' . $host;
}
}
public static function jsonRpcBuild($params = [], $method = 'Chain33.Query')
{
$data = [
'jsonrpc' => '2.0',
'id' => 0,
'method' => $method,
'params' => [],
];
if (!empty($params)) {
$data['params'][] = $params;
}
return json_encode($data);
}
public function send($params = [], $method = 'Chain33.Query')
{
$ch = new Curl();
$jsonrpc = self::jsonRpcBuild($params, $method);
if($method == 'Chain33.SignRawTx'){
//var_dump($jsonrpc);die;
}
$ch->setHeader('Content-Type', 'application/json');
$ch->setRawPostData($jsonrpc);
$result = $ch->post(self::urlLotteryBuild(), false);
if (!$result) {
return ['code' => -1, 'msg' => $ch->errorText];
}
if (0 == $result['id'] && empty($result['error'])) {
$result['code'] = $result['id'];
unset($result['id']);
return $result;
} else {
return ['code' => -1, 'msg' => $result['error']];
}
}
/**
* 参与交易(未签名)
* @param $amount
* @param $number
* @param $way
* @param $fee
* @return array|mixed
*/
public function trade($amount, $number, $way, $fee )
{
$config = Yii::$app->params['lottery'];
$lotterId = $config['lotteryId'];
$params = [
"lotteryId" => $lotterId,
"amount" => $amount,
"number" => $number,
"way" => $way,
"fee" => $fee,
];
return $this->send($params, 'Chain33.CreateRawLotteryBuyTx');
}
/**
* 开奖
*/
public function lottery($fee)
{
$config = Yii::$app->params['lottery'];
$lotterId = $config['lotteryId'];
$params = [
"lotteryId" => $lotterId,
"fee" => $fee
];
return $this->send($params, 'Chain33.CreateRawLotteryDrawTx');
}
/**
* @param $txhex
* @return array|mixed
* 签名
*/
public function sign($txhex)
{
$config = Yii::$app->params['lottery'];
$addr = $config['addr'];
$key = $config['key'];
$params = [
//'addr' => $addr,
'privkey' => $key,
'txhex' => $txhex,
'expire' => '3600s',
'index' => 0
];
return $this->send($params, 'Chain33.SignRawTx');
}
public function sendTransaction($data)
{
$params = [
'data' => $data,
];
return $this->send($params, 'Chain33.SendTransaction');
}
}
\ No newline at end of file
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace console\controllers; namespace console\controllers;
use common\business\Chain33Business; use common\business\Chain33Business;
use common\business\LotteryBusiness;
use common\models\psources\CoinLottery; use common\models\psources\CoinLottery;
use yii\console\Controller; use yii\console\Controller;
use Yii; use Yii;
...@@ -24,63 +25,93 @@ class LotteryController extends Controller ...@@ -24,63 +25,93 @@ class LotteryController extends Controller
$lottery_config = Yii::$app->params['lottery']; $lottery_config = Yii::$app->params['lottery'];
//获取最新的区块高度 //获取最新的区块高度
while(true){ while(true){
$last_header = Chain33Business::getLastHeader(); try{
if ($last_header['code'] != 0) { $last_header = Chain33Business::getLastHeader();
echo '获取区块高度失败: ' . $last_header['msg'].PHP_EOL; if ($last_header['code'] != 0) {
sleep(2); echo '获取区块高度失败: ' . $last_header['msg'].PHP_EOL;
continue; sleep(2);
} continue;
$last_header = $last_header['result']; }
//获取最新竞猜期数以及区块高度 $last_header = $last_header['result'];
$last = CoinLottery::getMaxStage(); //获取最新竞猜期数以及区块高度
if($last && $last->stage == $lottery_config['period_num'] && $last->status == 1){ //达到40期并且开完奖 $last = CoinLottery::getMaxStage();
echo date('Y-m-d H:i:s').': '.'今天彩票期数已达到40期,下期明天10点不见不散'.PHP_EOL; if($last && $last->stage == $lottery_config['period_num'] && $last->status == 1){ //达到40期并且开完奖
exit; echo date('Y-m-d H:i:s').': '.'今天彩票期数已达到40期,下期明天10点不见不散'.PHP_EOL;
} exit;
if ($last) { }
$start_height = $last->start_height; if ($last) {
$end_height = $start_height + $lottery_config['period_total_step']; //某一期结束区块高度 $start_height = $last->start_height;
if($last->status == 0){ //未开奖 $end_height = $start_height + $lottery_config['period_total_step']; //某一期结束区块高度
if($last_header['height'] < $end_height){ //未到开奖区块高度 if($last->status == 0){ //未开奖
sleep(2); if($last_header['height'] < $end_height){ //未到开奖区块高度
continue; sleep(2);
} continue;
if($last_header['height'] >= $end_height){
/*****************************开奖************************************/
echo date('Y-m-d H:i:s').': '.'开奖高度: '.$last_header['height'].PHP_EOL;
if(true){ //如果开奖成功
$last->status = 1;
$last->lottery_height = $last_header['height'];
$last->result = rand(10000,99999);
$last->save();
} }
} if($last_header['height'] >= $end_height){
}else{ //已开奖 /*****************************开奖************************************/
if($last_header['height'] > $end_height){ //到下期参与交易高度 echo date('Y-m-d H:i:s').': '.'开奖高度: '.$last_header['height'].PHP_EOL;
/*****************************开始下期交易****************************/ $lottery_result = LotteryBusiness::lottery();
if(true){ echo date('Y-m-d H:i:s').': '.'开奖成功'.PHP_EOL;
$start_height = $last_header['height']; if($lottery_result['code'] == 0){ //如果开奖成功
$end_height = $start_height + $lottery_config['period_total_step']; $last->status = 1;
$stage = $last->stage + 1; $last->lottery_height = $last_header['height'];
CoinLottery::addOneRecord($start_height,$end_height,$stage); $last->result = rand(10000,99999);
$last->save();
}else{
echo date('Y-m-d H:i:s').': '.'开奖异常: '.$lottery_result['msg'].PHP_EOL;
}
}
}else{ //已开奖
if($last_header['height'] > $end_height){ //到下期参与交易高度
/*****************************开始下期交易****************************/
echo date('Y-m-d H:i:s').': '.'区块交易高度: '.$last_header['height'].PHP_EOL;
$trade_result = LotteryBusiness::trade(1,12345,5,0);
echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL;
if($trade_result['code'] == 0){
$start_height = $last_header['height'];
$end_height = $start_height + $lottery_config['period_total_step'];
$stage = $last->stage + 1;
CoinLottery::addOneRecord($start_height,$end_height,$stage);
}else{
echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL;
}
sleep(2);
} }
sleep(2);
} }
} else {
//第一期参与交易
$stage = 1;
$start_height = $last_header['height'];
$end_height = $last_header['height'] + $lottery_config['period_total_step'];
/***************************参与交易**********************************/
echo date('Y-m-d H:i:s').': '.'区块交易高度: '.$last_header['height'].PHP_EOL;
$trade_result = LotteryBusiness::trade(1,12345,5,0);
echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL;
if($trade_result['code'] ==0){ //如果交易成功
CoinLottery::addOneRecord($start_height,$end_height,$stage);
}else{
echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL;
}
sleep(2);
} }
} else { }catch (\Exception $e){
//第一期参与交易 echo date('Y-m-d H:i:s').': '.'异常: '.$e->getMessage().PHP_EOL;
$stage = 1;
$start_height = $last_header['height'];
$end_height = $last_header['height'] + $lottery_config['period_total_step'];
/***************************参与交易**********************************/
if(true){ //如果交易成功
CoinLottery::addOneRecord($start_height,$end_height,$stage);
}
sleep(2); sleep(2);
} }
} }
exit; exit;
} }
public function actionTrade()
{
$res = LotteryBusiness::trade(1,12345,5,0);
}
public function actionLottery()
{
$res = LotteryBusiness::lottery();
var_dump($res);
}
} }
\ 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