Commit 7e766367 authored by ZhuChunYang's avatar ZhuChunYang

彩票功能重构

parent 15ec0f4e
...@@ -8,9 +8,8 @@ ...@@ -8,9 +8,8 @@
namespace common\business; namespace common\business;
use common\models\psources\Coin;
use common\service\chain33\LotteryService; use common\service\chain33\LotteryService;
use common\service\coin\CoinFactory; use Yii;
class LotteryBusiness class LotteryBusiness
{ {
...@@ -19,12 +18,14 @@ class LotteryBusiness ...@@ -19,12 +18,14 @@ class LotteryBusiness
*/ */
public static function trade($amount, $number, $way, $fee) public static function trade($amount, $number, $way, $fee)
{ {
$config = Yii::$app->params['lottery'];
$key = $config['key'];
$error = ''; $error = '';
$service = new LotteryService(); $service = new LotteryService();
$trade_result = $service->trade($amount, $number, $way, $fee); $trade_result = $service->trade($amount, $number, $way, $fee);
if ($trade_result['code'] == 0) { //交易成功 if ($trade_result['code'] == 0) { //交易成功
$trade_txhex = $trade_result['result']; $trade_txhex = $trade_result['result'];
$sign_result = $service->sign($trade_txhex); $sign_result = $service->sign($key,$trade_txhex);
if($sign_result['code'] == 0){ //签名成功 if($sign_result['code'] == 0){ //签名成功
$data = $sign_result['result']; $data = $sign_result['result'];
$send_transaction_result = $service->sendTransaction($data); $send_transaction_result = $service->sendTransaction($data);
...@@ -45,12 +46,14 @@ class LotteryBusiness ...@@ -45,12 +46,14 @@ class LotteryBusiness
*/ */
public static function lottery($fee = 0) public static function lottery($fee = 0)
{ {
$config = Yii::$app->params['lottery'];
$key = $config['createor_key'];
$error = ''; $error = '';
$service = new LotteryService(); $service = new LotteryService();
$lottery_result = $service->lottery($fee); $lottery_result = $service->lottery($fee);
if ($lottery_result['code'] == 0) { //交易成功 if ($lottery_result['code'] == 0) { //交易成功
$lottery_txhex = $lottery_result['result']; $lottery_txhex = $lottery_result['result'];
$sign_result = $service->sign($lottery_txhex); $sign_result = $service->sign($key,$lottery_txhex);
if($sign_result['code'] == 0){ //签名成功 if($sign_result['code'] == 0){ //签名成功
$data = $sign_result['result']; $data = $sign_result['result'];
$send_transaction_result = $service->sendTransaction($data); $send_transaction_result = $service->sendTransaction($data);
...@@ -65,4 +68,38 @@ class LotteryBusiness ...@@ -65,4 +68,38 @@ class LotteryBusiness
} }
return ['code' => -1, 'msg' => $error]; return ['code' => -1, 'msg' => $error];
} }
/**
* @return array
* 获取当前彩票信息
*/
public static function getLotteryCurrentInfo()
{
$service = new LotteryService();
$lottery_current_info = $service->getLotteryCurrentInfo();
if ($lottery_current_info['code'] == 0) { //交易成功
$result = $lottery_current_info['result'];
return ['code' => 0,'result' => $result];
}else{
$error = $lottery_current_info['msg'];
}
return ['code' => -1, 'msg' => $error];
}
/**
* @param $round
* 获取指定期数中奖号码
*/
public static function getLuckyNumber($round)
{
$service = new LotteryService();
$lottery_current_info = $service->getLuckyNumber($round);
if ($lottery_current_info['code'] == 0) { //交易成功
$result = $lottery_current_info['result'];
return ['code' => 0,'result' => $result];
}else{
$error = $lottery_current_info['msg'];
}
return ['code' => -1, 'msg' => $error];
}
} }
...@@ -40,13 +40,15 @@ class CoinLottery extends BaseActiveRecord ...@@ -40,13 +40,15 @@ class CoinLottery extends BaseActiveRecord
return self::find()->where(['date' => $date])->orderBy(['stage' => SORT_DESC])->one(); return self::find()->where(['date' => $date])->orderBy(['stage' => SORT_DESC])->one();
} }
public static function addOneRecord($start_height,$end_height,$stage) public static function addOneRecord($start_height,$last_lottery_height,$stage,$round,$start_date)
{ {
$coin_lottery = new self(); $coin_lottery = new self();
$coin_lottery->stage = $stage; $coin_lottery->stage = $stage;
$coin_lottery->start_height = $start_height; $coin_lottery->start_height = $start_height;
$coin_lottery->end_height = $end_height; $coin_lottery->last_lottery_height = $last_lottery_height;
$coin_lottery->date = date('Y-m-d'); $coin_lottery->status = 2;
$coin_lottery->round = $round;
$coin_lottery->date = $start_date;
$coin_lottery->save(); $coin_lottery->save();
} }
......
...@@ -104,17 +104,47 @@ class LotteryService ...@@ -104,17 +104,47 @@ class LotteryService
return $this->send($params, 'Chain33.CreateRawLotteryDrawTx'); return $this->send($params, 'Chain33.CreateRawLotteryDrawTx');
} }
/**
* 获取lottery当前信息
*/
public function getLotteryCurrentInfo()
{
$config = Yii::$app->params['lottery'];
$lotterId = $config['lotteryId'];
$params = [
"execer" => 'lottery',
"funcName" => "GetLotteryCurrentInfo",
"payload" => ['lotteryId' => $lotterId]
];
return $this->send($params, 'Chain33.Query');
}
/**
* @param $round
* @return array|mixed
* 获取中奖号码
*/
public function getLuckyNumber($round)
{
$config = Yii::$app->params['lottery'];
$lotterId = $config['lotteryId'];
$params = [
"execer" => 'lottery',
"funcName" => "GetLotteryRoundLuckyNumber",
"payload" => ['lotteryId' => $lotterId,'round' => $round]
];
return $this->send($params, 'Chain33.Query');
}
/** /**
* @param $txhex * @param $txhex
* @return array|mixed * @return array|mixed
* 签名 * 签名
*/ */
public function sign($txhex) public function sign($key,$txhex)
{ {
$config = Yii::$app->params['lottery'];
$addr = $config['addr'];
$key = $config['key'];
$params = [ $params = [
//'addr' => $addr, //'addr' => $addr,
'privkey' => $key, 'privkey' => $key,
......
...@@ -23,9 +23,23 @@ class LotteryController extends Controller ...@@ -23,9 +23,23 @@ class LotteryController extends Controller
{ {
echo date('Y-m-d H:i:s').': '.'每天10点彩票活动准时和大家见面'.PHP_EOL; echo date('Y-m-d H:i:s').': '.'每天10点彩票活动准时和大家见面'.PHP_EOL;
$lottery_config = Yii::$app->params['lottery']; $lottery_config = Yii::$app->params['lottery'];
//获取最新的区块高度 $start_date = date('Y-m-d');
$lotter_status = Yii::$app->redis_app->get('lottery_status');
if($lotter_status == 'running'){
exit;
}
Yii::$app->redis_app->set('lottery_status','running');
while(true){ while(true){
try{ try{
//获取最近lottery状态
$lottery_current_info = LotteryBusiness::getLotteryCurrentInfo();
if($lottery_current_info['code'] != 0) {
echo '获取lottery_current_info失败: ' . $lottery_current_info['msg'].PHP_EOL;
sleep(2);
continue;
}
$lottery_current_result = $lottery_current_info['result'];
//获取最新的区块高度
$last_header = Chain33Business::getLastHeader(); $last_header = Chain33Business::getLastHeader();
if ($last_header['code'] != 0) { if ($last_header['code'] != 0) {
echo '获取区块高度失败: ' . $last_header['msg'].PHP_EOL; echo '获取区块高度失败: ' . $last_header['msg'].PHP_EOL;
...@@ -35,71 +49,83 @@ class LotteryController extends Controller ...@@ -35,71 +49,83 @@ class LotteryController extends Controller
$last_header = $last_header['result']; $last_header = $last_header['result'];
//获取最新竞猜期数以及区块高度 //获取最新竞猜期数以及区块高度
$last = CoinLottery::getMaxStage(); $last = CoinLottery::getMaxStage();
if($last && $last->stage == $lottery_config['period_num'] && $last->status == 1){ //达到40期并且开完奖 if($last && $last->stage == $lottery_config['period_num'] && $last->status == 3){ //达到80期并且开完奖
echo date('Y-m-d H:i:s').': '.'今天彩票期数已达到40期,下期明天10点不见不散'.PHP_EOL; echo date('Y-m-d H:i:s').': '.'今天彩票期数已达到80期,下期明天10点不见不散'.PHP_EOL;
Yii::$app->redis_app->set('lottery_status','ending');
exit; exit;
} }
$start_height = $lottery_current_result['lastTransToPurStateOnMain'];
$last_lottery_height = $lottery_current_result['lastTransToDrawStateOnMain'];
$round = $lottery_current_result['round'];
if ($last) { if ($last) {
$start_height = $last->start_height; if($lottery_current_result['status'] == 2){ //交易状态,等待开奖
$end_height = $start_height + $lottery_config['period_total_step']; //某一期结束区块高度 if($last_header['height'] >= $start_height + $lottery_config['period_total_step']){ //超过起始高度40个区块可以开奖
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;
$lottery_result = LotteryBusiness::lottery(); $lottery_result = LotteryBusiness::lottery();
echo date('Y-m-d H:i:s').': '.'开奖成功'.PHP_EOL; if($lottery_result['code'] == 0){ //如果开奖操作成功
if($lottery_result['code'] == 0){ //如果开奖成功
$last->status = 1;
$last->lottery_height = $last_header['height'];
$last->result = rand(10000,99999);
$last->save();
}else{ }else{
echo date('Y-m-d H:i:s').': '.'开奖异常: '.$lottery_result['msg'].PHP_EOL; echo date('Y-m-d H:i:s').': '.'开奖异常: '.$lottery_result['msg'].PHP_EOL;
} }
}else{
if($last->start_height != $start_height){
$last->start_height = $start_height;
$last->round = $round;
$last->save();
echo date('Y-m-d H:i:s').': '.'总期数:'.$round." 交易数据更新成功!".PHP_EOL;
}
sleep(2);
continue;
}
}else if($lottery_current_result['status'] == 3){ //已开奖
if($last->status != 3 ){ //开奖成功后数据库记录还没更新
$last->status = 3;
$last->lottery_height = $last_lottery_height;
$last->result = $lottery_current_result['luckyNumber'];
$last->save();
echo date('Y-m-d H:i:s').': '.'总期数:'.$round." 开奖数据更新成功!".PHP_EOL;
} }
}else{ //已开奖 if($last_header['height'] > $last_lottery_height){ //当前区块高度大于上期开奖高度,可以进行交易进入下一期
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); $trade_result = LotteryBusiness::trade(1,12345,5,0);
echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL;
if($trade_result['code'] == 0){ if($trade_result['code'] == 0){
$start_height = $last_header['height']; echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL;
$end_height = $start_height + $lottery_config['period_total_step']; CoinLottery::addOneRecord($start_height,$last_lottery_height,$last->stage+1,$round,$start_date);
$stage = $last->stage + 1;
CoinLottery::addOneRecord($start_height,$end_height,$stage);
}else{ }else{
echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL; echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL;
} }
sleep(2); sleep(60);
} }
} }
} else { } else {
//第一期参与交易 //今天第一期
$stage = 1; $stage = 1;
$start_height = $last_header['height']; if($lottery_current_result['status'] == 2){ //交易状态,等待开奖
$end_height = $last_header['height'] + $lottery_config['period_total_step']; if($last_header['height'] >= $start_height + $lottery_config['period_total_step']){ //超过起始高度40个区块可以开奖
/***************************参与交易**********************************/ CoinLottery::addOneRecord($start_height,$last_lottery_height,$stage,$round,$start_date);
echo date('Y-m-d H:i:s').': '.'区块交易高度: '.$last_header['height'].PHP_EOL; $lottery_result = LotteryBusiness::lottery();
if($lottery_result['code'] == 0){ //如果开奖操作成功
}else{
echo date('Y-m-d H:i:s').': '.'开奖异常: '.$lottery_result['msg'].PHP_EOL;
}
}
}else if($lottery_current_result['status'] == 3){ //开完奖状态,等待交易进入下一期
if($last_header['height'] > $last_lottery_height){ //当前区块高度大于上期开奖高度,可以进行交易进入下一期
$trade_result = LotteryBusiness::trade(1,12345,5,0); $trade_result = LotteryBusiness::trade(1,12345,5,0);
if($trade_result['code'] == 0){
echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL; echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL;
if($trade_result['code'] ==0){ //如果交易成功
CoinLottery::addOneRecord($start_height,$end_height,$stage);
}else{ }else{
echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL; echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL;
} }
sleep(2); sleep(60);
}
}
} }
}catch (\Exception $e){ }catch (\Exception $e){
echo date('Y-m-d H:i:s').': '.'异常: '.$e->getMessage().PHP_EOL; echo date('Y-m-d H:i:s').': '.'异常: '.$e->getMessage().PHP_EOL;
sleep(2); sleep(2);
} }
} }
Yii::$app->redis_app->set('lottery_status','ending');
exit; exit;
} }
...@@ -114,4 +140,16 @@ class LotteryController extends Controller ...@@ -114,4 +140,16 @@ class LotteryController extends Controller
var_dump($res); var_dump($res);
} }
public function actionLotteryCurrentInfo()
{
$res = LotteryBusiness::getLotteryCurrentInfo();
var_dump($res);
}
public function actionLotteryLuckyNumber()
{
$res = LotteryBusiness::getLuckyNumber((int)206);
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