Commit e3cc6424 authored by ZhuChunYang's avatar ZhuChunYang

lottery init

parent d0598d10
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/10/8
* Time: 11:36
*/
namespace common\business;
use common\models\psources\Coin;
use common\service\coin\CoinFactory;
class LotteryBusiness
{
/**
* 参与/构造交易
*/
public static function trade()
{
}
/**
* 开奖
*/
public static function lottery()
{
}
}
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/10/8
* Time: 11:24
*/
namespace common\models\psources;
use Yii;
class CoinLottery extends BaseActiveRecord
{
public static function tableName()
{
return 'coin_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,$end_height,$stage)
{
$coin_lottery = new self();
$coin_lottery->stage = $stage;
$coin_lottery->start_height = $start_height;
$coin_lottery->end_height = $end_height;
$coin_lottery->date = date('Y-m-d');
$coin_lottery->save();
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/10/8
* Time: 11:00
*/
namespace console\controllers;
use common\business\Chain33Business;
use common\models\psources\CoinLottery;
use yii\console\Controller;
use Yii;
class LotteryController extends Controller
{
/**
* 交易并开奖
*/
public function actionTradeAndLottery()
{
echo date('Y-m-d H:i:s').': '.'每天10点彩票活动准时和大家见面'.PHP_EOL;
$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();
}
}
}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);
}
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);
}
sleep(2);
}
}
exit;
}
}
\ 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