Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wallet
token
Commits
e3cc6424
Commit
e3cc6424
authored
Oct 09, 2018
by
ZhuChunYang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lottery init
parent
d0598d10
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
172 additions
and
0 deletions
+172
-0
LotteryBusiness.php
common/business/LotteryBusiness.php
+31
-0
CoinLottery.php
common/models/psources/CoinLottery.php
+54
-0
LotteryController.php
console/controllers/LotteryController.php
+87
-0
No files found.
common/business/LotteryBusiness.php
0 → 100644
View file @
e3cc6424
<?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
()
{
}
}
common/models/psources/CoinLottery.php
0 → 100644
View file @
e3cc6424
<?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
console/controllers/LotteryController.php
0 → 100644
View file @
e3cc6424
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment