Commit 55c1f4df authored by ZhuChunYang's avatar ZhuChunYang

add robot lottery

parent f990e95f
......@@ -9,6 +9,7 @@
namespace common\business;
use common\models\psources\Coin;
use common\service\chain33\LotteryService;
use common\service\coin\CoinFactory;
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
$ch->setRawPostData($jsonrpc);
$result = $ch->post(self::urlBuild(), false);
if (!$result) {
return ['code' => -1, 'msg' => '请求失败'];
return ['code' => -1, 'msg' => $ch->errorText];
}
if (0 == $result['id'] && empty($result['error'])) {
$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 @@
namespace console\controllers;
use common\business\Chain33Business;
use common\business\LotteryBusiness;
use common\models\psources\CoinLottery;
use yii\console\Controller;
use Yii;
......@@ -24,63 +25,93 @@ class LotteryController extends Controller
$lottery_config = Yii::$app->params['lottery'];
//获取最新的区块高度
while(true){
$last_header = Chain33Business::getLastHeader();
if ($last_header['code'] != 0) {
echo '获取区块高度失败: ' . $last_header['msg'].PHP_EOL;
sleep(2);
continue;
}
$last_header = $last_header['result'];
//获取最新竞猜期数以及区块高度
$last = CoinLottery::getMaxStage();
if($last && $last->stage == $lottery_config['period_num'] && $last->status == 1){ //达到40期并且开完奖
echo date('Y-m-d H:i:s').': '.'今天彩票期数已达到40期,下期明天10点不见不散'.PHP_EOL;
exit;
}
if ($last) {
$start_height = $last->start_height;
$end_height = $start_height + $lottery_config['period_total_step']; //某一期结束区块高度
if($last->status == 0){ //未开奖
if($last_header['height'] < $end_height){ //未到开奖区块高度
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();
try{
$last_header = Chain33Business::getLastHeader();
if ($last_header['code'] != 0) {
echo '获取区块高度失败: ' . $last_header['msg'].PHP_EOL;
sleep(2);
continue;
}
$last_header = $last_header['result'];
//获取最新竞猜期数以及区块高度
$last = CoinLottery::getMaxStage();
if($last && $last->stage == $lottery_config['period_num'] && $last->status == 1){ //达到40期并且开完奖
echo date('Y-m-d H:i:s').': '.'今天彩票期数已达到40期,下期明天10点不见不散'.PHP_EOL;
exit;
}
if ($last) {
$start_height = $last->start_height;
$end_height = $start_height + $lottery_config['period_total_step']; //某一期结束区块高度
if($last->status == 0){ //未开奖
if($last_header['height'] < $end_height){ //未到开奖区块高度
sleep(2);
continue;
}
}
}else{ //已开奖
if($last_header['height'] > $end_height){ //到下期参与交易高度
/*****************************开始下期交易****************************/
if(true){
$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);
if($last_header['height'] >= $end_height){
/*****************************开奖************************************/
echo date('Y-m-d H:i:s').': '.'开奖高度: '.$last_header['height'].PHP_EOL;
$lottery_result = LotteryBusiness::lottery();
echo date('Y-m-d H:i:s').': '.'开奖成功'.PHP_EOL;
if($lottery_result['code'] == 0){ //如果开奖成功
$last->status = 1;
$last->lottery_height = $last_header['height'];
$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 {
//第一期参与交易
$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);
}
}catch (\Exception $e){
echo date('Y-m-d H:i:s').': '.'异常: '.$e->getMessage().PHP_EOL;
sleep(2);
}
}
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