Commit 69907aaa authored by ZhuChunYang's avatar ZhuChunYang

update

parent 8808f176
......@@ -44,6 +44,35 @@ class LotteryBusiness
}
/**
* 参与/构造交易(新)
*/
public static function newtrade($amount, $number, $way, $fee)
{
$config = Yii::$app->params['lottery'];
$key = $config['key'];
$service = new LotteryService();
$trade_result = $service->newtrade($amount, $number, $way, $fee);
if ($trade_result['code'] == 0) { //交易成功
$trade_txhex = $trade_result['result'];
$sign_result = $service->sign($key,$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 = $send_transaction_result['msg'];
}
}else{
$error = $sign_result['msg'];
}
}else{
$error = $trade_result['msg'];
}
return ['code' => -1, 'msg' => $error];
}
/**
* 开奖
*/
public static function lottery($fee = 0)
......@@ -74,6 +103,36 @@ class LotteryBusiness
}
/**
* 开奖(新)
*/
public static function newlottery($fee = 0)
{
$config = Yii::$app->params['lottery'];
$key = $config['createor_key'];
$error = '';
$service = new LotteryService();
$lottery_result = $service->newlottery($fee);
if ($lottery_result['code'] == 0) { //交易成功
$lottery_txhex = $lottery_result['result'];
$sign_result = $service->sign($key,$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 = $send_transaction_result['msg'];
}
}else{
$error = $sign_result['msg'];
}
}else{
$error = $lottery_result['msg'];
}
return ['code' => -1, 'msg' => $error];
}
/**
* @return array
* 获取当前彩票信息
*/
......@@ -91,6 +150,23 @@ class LotteryBusiness
}
/**
* @return array
* 获取当前彩票信息(新)
*/
public static function getNewLotteryCurrentInfo()
{
$service = new LotteryService();
$lottery_current_info = $service->getNewLotteryCurrentInfo();
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
* 获取指定期数中奖号码
*/
......
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/12/14
* Time: 15:49
*/
namespace common\models\psources;
use Yii;
class CoinNewLottery extends BaseActiveRecord
{
public static function tableName()
{
return 'coin_new_lottery';
}
public function beforeSave($instert)
{
if (parent::beforeSave($instert)) {
if ($this->isNewRecord) {
$this->create_time = Yii::$app->formatter->asTimestamp('now');
} else {
$this->update_time = Yii::$app->formatter->asTimestamp('now');
}
return true;
} else {
return false;
}
}
/**
* 获取指定日期最后的一条记录(stage max)
*/
public static function getMaxStage($date = '')
{
if(!$date){
$date = date('Y-m-d');
}
return self::find()->where(['date' => $date])->orderBy(['stage' => SORT_DESC])->one();
}
public static function addOneRecord($start_height,$last_lottery_height,$stage,$round,$start_date)
{
$coin_lottery = new self();
$coin_lottery->stage = $stage;
$coin_lottery->start_height = $start_height;
$coin_lottery->last_lottery_height = $last_lottery_height;
$coin_lottery->status = 2;
$coin_lottery->round = $round;
$coin_lottery->date = $start_date;
$coin_lottery->save();
}
/**
* @return array|null|\yii\db\ActiveRecord
* 获取最新记录
*/
public static function getLastRecord()
{
return self::find()->orderBy(['id' => SORT_DESC])->asArray()->one();
}
}
\ No newline at end of file
......@@ -96,6 +96,33 @@ class LotteryService
}
/**
* 参与交易(未签名)
* @param $amount
* @param $number
* @param $way
* @param $fee
* @return array|mixed
*/
public function newtrade($amount, $number, $way, $fee )
{
$config = Yii::$app->params['lottery'];
$lotterId = $config['new_lotteryId'];
$params = [
"execer" => "lottery",
"actionName" => "LotteryBuy",
"payload" => [
"lotteryId" => $lotterId,
"amount" => $amount,
"number" => $number,
"way" => $way,
"fee" => $fee,
]
];
return $this->send($params, 'Chain33.CreateTransaction');
}
/**
* 开奖
*/
public function lottery($fee)
......@@ -114,6 +141,24 @@ class LotteryService
}
/**
* 开奖
*/
public function newlottery($fee)
{
$config = Yii::$app->params['lottery'];
$lotterId = $config['new_lotteryId'];
$params = [
"execer" => "lottery",
"actionName" => "LotteryDraw",
"payload" => [
"lotteryId" => $lotterId,
"fee" => $fee
]
];
return $this->send($params, 'Chain33.CreateTransaction');
}
/**
* 获取lottery当前信息
*/
public function getLotteryCurrentInfo()
......@@ -129,6 +174,21 @@ class LotteryService
}
/**
* 获取lottery当前信息
*/
public function getNewLotteryCurrentInfo()
{
$config = Yii::$app->params['lottery'];
$lotterId = $config['new_lotteryId'];
$params = [
"execer" => 'lottery',
"funcName" => "GetLotteryCurrentInfo",
"payload" => ['lotteryId' => $lotterId]
];
return $this->send($params, 'Chain33.Query');
}
/**
* @param $round
* @return array|mixed
* 获取中奖号码
......
......@@ -10,6 +10,7 @@ namespace console\controllers;
use common\business\Chain33Business;
use common\business\LotteryBusiness;
use common\models\psources\CoinLottery;
use common\models\psources\CoinNewLottery;
use yii\console\Controller;
use Yii;
......@@ -129,6 +130,119 @@ class LotteryController extends Controller
exit;
}
/**
* 交易并开奖(新)
*/
public function actionNewTradeAndLottery()
{
echo date('Y-m-d H:i:s').': '.'每天10点彩票活动准时和大家见面'.PHP_EOL;
$lottery_config = Yii::$app->params['lottery'];
$start_date = date('Y-m-d');
$lotter_status = Yii::$app->redis_app->get('new_lottery_status');
if($lotter_status == 'running'){
exit;
}
Yii::$app->redis_app->set('new_lottery_status','running');
while(true){
try{
//获取最近lottery状态
$lottery_current_info = LotteryBusiness::getNewLotteryCurrentInfo();
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 = LotteryBusiness::getLastHeader();
if ($last_header['code'] != 0) {
echo '获取区块高度失败: ' . $last_header['msg'].PHP_EOL;
sleep(2);
continue;
}
$last_header = $last_header['result'];
//获取最新竞猜期数以及区块高度
$last = CoinNewLottery::getMaxStage($start_date);
if($last && $last->stage == $lottery_config['period_num'] && $last->status == 3){ //达到80期并且开完奖
echo date('Y-m-d H:i:s').': '.'今天彩票期数已达到80期,下期明天10点不见不散'.PHP_EOL;
Yii::$app->redis_app->set('new_lottery_status','ending');
exit;
}
$start_height = $lottery_current_result['lastTransToPurStateOnMain'];
$last_lottery_height = $lottery_current_result['lastTransToDrawStateOnMain'];
$round = $lottery_current_result['round'];
if ($last) {
if($lottery_current_result['status'] == 2){ //交易状态,等待开奖
if($last_header['height'] >= $start_height + $lottery_config['period_total_step']){ //超过起始高度40个区块可以开奖
$lottery_result = LotteryBusiness::newlottery();
if($lottery_result['code'] == 0){ //如果开奖操作成功
echo date('Y-m-d H:i:s').': '.'开奖成功 '.PHP_EOL;
}else{
echo date('Y-m-d H:i:s').': '.'开奖异常: '.$lottery_result['msg'].PHP_EOL;
}
sleep(10);
}else{
if($last->start_height != $start_height){
CoinNewLottery::addOneRecord($start_height,$last_lottery_height,$last->stage+1,$round,$start_date);
echo date('Y-m-d H:i:s').': '.'总期数:'.$round." 交易数据添加成功!".PHP_EOL;
}
sleep(10);
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;
}
if($last_header['height'] > $last_lottery_height){ //当前区块高度大于上期开奖高度,可以进行交易进入下一期
$trade_result = LotteryBusiness::newtrade(1,12345,5,0);
if($trade_result['code'] == 0){
echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL;
//CoinLottery::addOneRecord($start_height,$last_lottery_height,$last->stage+1,$round,$start_date);
}else{
echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL;
}
sleep(60);
}
}
} else {
//今天第一期
$stage = 1;
if($lottery_current_result['status'] == 2){ //交易状态,等待开奖
if($last_header['height'] >= $start_height + $lottery_config['period_total_step']){ //超过起始高度40个区块可以开奖
CoinNewLottery::addOneRecord($start_height,$last_lottery_height,$stage,$round,$start_date);
$lottery_result = LotteryBusiness::newlottery();
if($lottery_result['code'] == 0){ //如果开奖操作成功
}else{
echo date('Y-m-d H:i:s').': '.'开奖异常: '.$lottery_result['msg'].PHP_EOL;
}
sleep(10);
}
}else if($lottery_current_result['status'] == 3){ //开完奖状态,等待交易进入下一期
if($last_header['height'] > $last_lottery_height){ //当前区块高度大于上期开奖高度,可以进行交易进入下一期
$trade_result = LotteryBusiness::newtrade(1,12345,5,0);
if($trade_result['code'] == 0){
echo date('Y-m-d H:i:s').': '.'交易成功 '.PHP_EOL;
}else{
echo date('Y-m-d H:i:s').': '.'参与交易异常: '.$trade_result['msg'].PHP_EOL;
}
sleep(60);
}
}
}
}catch (\Exception $e){
echo date('Y-m-d H:i:s').': '.'异常: '.$e->getMessage().PHP_EOL;
sleep(10);
}
}
Yii::$app->redis_app->set('new_lottery_status','ending');
exit;
}
public function actionTrade()
{
$res = LotteryBusiness::trade(1,12345,5,0);
......
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