From 90c5479a158ebf2cfde79fb9d8b427919f670ebd Mon Sep 17 00:00:00 2001
From: Lkeme <19500576+lkeme@users.noreply.github.com>
Date: Fri, 14 Feb 2020 18:23:59 +0800
Subject: [PATCH] [update] MaterialObject
---
CHANGELOG.md | 14 ++
DOC.md | 48 +++----
README.md | 2 +-
src/core/Curl.php | 2 +-
src/plugin/AnchorRaffle.php | 2 +-
src/plugin/BaseRaffle.php | 2 +-
src/plugin/GuardRaffle.php | 3 +-
src/plugin/Live.php | 2 +-
src/plugin/MasterSite.php | 2 +-
src/plugin/MaterialObject.php | 255 +++++++++++++++++++---------------
src/plugin/PkRaffle.php | 2 +-
src/plugin/StormRaffle.php | 2 +-
src/plugin/ZoneTcpClient.php | 2 +-
13 files changed, 194 insertions(+), 144 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 60a6b09..5fc3453 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,20 @@
# Release Notes
# 本项目Log
+## v0.2.0.200214 alpha (2020-02-14)
+
+### Added
+-
+
+### Changed
+- 优化实物抽奖流程
+- 优化延迟礼物抽奖
+
+### Fixed
+- 修正部分函数名称
+- 尝试减少静态占用
+-
+
## v0.2.0.200208 alpha (2020-02-08)
### Added
diff --git a/DOC.md b/DOC.md
index 50869d7..95b455d 100644
--- a/DOC.md
+++ b/DOC.md
@@ -2,7 +2,7 @@

-
+
@@ -15,29 +15,29 @@ B 站直播实用脚本
|plugin |version |description |
|--------------------|--------------------|--------------------|
-|Login |20.02.08 |账号登录 |
-|Sleep |20.02.08 |休眠控制 |
-|MasterSite |20.02.08 |主站助手 |
-|Daily |20.02.08 |每日礼包 |
-|Heart |20.02.08 |双端心跳 |
-|Task |20.02.08 |每日任务 |
-|Silver |20.02.08 |银瓜子宝箱 |
-|Barrage |20.02.08 |活跃弹幕 |
-|Silver2Coin |20.02.08 |银瓜子换硬币 |
-|GiftSend |20.02.08 |礼物赠送 |
-|GroupSignIn |20.02.08 |友爱社签到 |
-|ManGa |20.02.08 |漫画签到分享 |
-|GiftHeart |20.02.08 |心跳礼物 |
-|MaterialObject |20.02.08 |实物抽奖 |
-|AloneTcpClient |20.02.08 |独立监控 |
-|ZoneTcpClient |20.02.08 |分区监控 |
-|StormRaffle |20.02.08 |节奏风暴 |
-|GiftRaffle |20.02.08 |活动礼物 |
-|PkRaffle |20.02.08 |大乱斗 |
-|GuardRaffle |20.02.08 |舰长总督 |
-|AnchorRaffle |20.02.08 |天选时刻 |
-|AwardRecord |20.02.08 |获奖通知 |
-|Statistics |20.02.08 |数据统计 |
+|Login |20.02.14 |账号登录 |
+|Sleep |20.02.14 |休眠控制 |
+|MasterSite |20.02.14 |主站助手 |
+|Daily |20.02.14 |每日礼包 |
+|Heart |20.02.14 |双端心跳 |
+|Task |20.02.14 |每日任务 |
+|Silver |20.02.14 |银瓜子宝箱 |
+|Barrage |20.02.14 |活跃弹幕 |
+|Silver2Coin |20.02.14 |银瓜子换硬币 |
+|GiftSend |20.02.14 |礼物赠送 |
+|GroupSignIn |20.02.14 |友爱社签到 |
+|ManGa |20.02.14 |漫画签到分享 |
+|GiftHeart |20.02.14 |心跳礼物 |
+|MaterialObject |20.02.14 |实物抽奖 |
+|AloneTcpClient |20.02.14 |独立监控 |
+|ZoneTcpClient |20.02.14 |分区监控 |
+|StormRaffle |20.02.14 |节奏风暴 |
+|GiftRaffle |20.02.14 |活动礼物 |
+|PkRaffle |20.02.14 |大乱斗 |
+|GuardRaffle |20.02.14 |舰长总督 |
+|AnchorRaffle |20.02.14 |天选时刻 |
+|AwardRecord |20.02.14 |获奖通知 |
+|Statistics |20.02.14 |数据统计 |
## 打赏赞助
diff --git a/README.md b/README.md
index 77bc74b..2b21a50 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@ Group: [55308141](https://jq.qq.com/?_wv=1027&k=5AIDaJg)
## 公告
-Currently for Personal Edition **0.2.0.200208 alpha**
+Currently for Personal Edition **0.2.0.200214 alpha**
## 文档
diff --git a/src/core/Curl.php b/src/core/Curl.php
index 32775f4..b2d4249 100644
--- a/src/core/Curl.php
+++ b/src/core/Curl.php
@@ -92,7 +92,7 @@ class Curl
// 网络断开判断 延时方便连接网络
if ($ret_count < $waring) {
Log::warning("正常等待网络连接状态恢复正常...");
- sleep(mt_rand(5, 10));
+ sleep(random_int(5, 10));
}
try {
$curl = curl_init();
diff --git a/src/plugin/AnchorRaffle.php b/src/plugin/AnchorRaffle.php
index 439073f..f90f286 100644
--- a/src/plugin/AnchorRaffle.php
+++ b/src/plugin/AnchorRaffle.php
@@ -93,7 +93,7 @@ class AnchorRaffle extends BaseRaffle
'room_id' => $room_id,
'raffle_id' => $de_raw['id'],
'raffle_name' => $de_raw['award_name'],
- 'wait' => time()
+ 'wait' => time() + random_int(5, 25)
];
Statistics::addPushList(self::ACTIVE_TITLE);
array_push(self::$wait_list, $data);
diff --git a/src/plugin/BaseRaffle.php b/src/plugin/BaseRaffle.php
index e910243..069ddbf 100644
--- a/src/plugin/BaseRaffle.php
+++ b/src/plugin/BaseRaffle.php
@@ -154,7 +154,7 @@ abstract class BaseRaffle
if (in_array($lid, static::$all_list)) {
return true;
}
- if (count(static::$all_list) > 2000) {
+ if (count(static::$all_list) > 1000) {
static::$all_list = [];
}
if ($filter) {
diff --git a/src/plugin/GuardRaffle.php b/src/plugin/GuardRaffle.php
index 36dc42c..e01b03c 100644
--- a/src/plugin/GuardRaffle.php
+++ b/src/plugin/GuardRaffle.php
@@ -31,6 +31,7 @@ class GuardRaffle extends BaseRaffle
* @param int $room_id
* @param array $data
* @return bool
+ * @throws \Exception
*/
protected static function parseLotteryInfo(int $room_id, array $data): bool
{
@@ -72,7 +73,7 @@ class GuardRaffle extends BaseRaffle
'room_id' => $room_id,
'raffle_id' => $guard['id'],
'raffle_name' => $raffle_name,
- 'wait' => time()
+ 'wait' => time() + random_int(5, 25)
];
Statistics::addPushList(self::ACTIVE_TITLE);
array_push(self::$wait_list, $data);
diff --git a/src/plugin/Live.php b/src/plugin/Live.php
index b720b11..bdf1e98 100644
--- a/src/plugin/Live.php
+++ b/src/plugin/Live.php
@@ -139,7 +139,7 @@ class Live
*/
public static function randDelay($min = 0, $max = 3): bool
{
- $rand = $min + mt_rand() / mt_getrandmax() * ($max - $min);
+ $rand = $min + random_int() / mt_getrandmax() * ($max - $min);
sleep($rand);
return true;
}
diff --git a/src/plugin/MasterSite.php b/src/plugin/MasterSite.php
index 10f629f..61896d6 100644
--- a/src/plugin/MasterSite.php
+++ b/src/plugin/MasterSite.php
@@ -149,7 +149,7 @@ class MasterSite
private static function getRandomAid(): string
{
do {
- $page = mt_rand(1, 1000);
+ $page = random_int(1, 1000);
$payload = [];
$url = "https://api.bilibili.com/x/web-interface/newlist?&pn={$page}&ps=1";
$raw = Curl::get($url, Sign::api($payload));
diff --git a/src/plugin/MaterialObject.php b/src/plugin/MaterialObject.php
index d14554b..9918bce 100644
--- a/src/plugin/MaterialObject.php
+++ b/src/plugin/MaterialObject.php
@@ -18,7 +18,7 @@ class MaterialObject
{
use TimeLock;
- private static $discard_aid_list = [];
+ private static $invalid_aids = [];
private static $start_aid = 0;
private static $end_aid = 0;
@@ -30,100 +30,158 @@ class MaterialObject
if (self::getLock() > time()) {
return;
}
- // 计算AID TODO 待优化
- self::calculateAid(150, 550);
- self::drawLottery();
-
+ // TODO 优化计算AID算法
+ self::calcAid(470, 770);
+ $lottery_list = self::fetchLottery();
+ self::drawLottery($lottery_list);
self::setLock(random_int(5, 10) * 60);
}
+
/**
- * @use 实物抽奖
+ * @use 过滤抽奖Title
+ * @param string $title
* @return bool
*/
- protected static function drawLottery(): bool
+ private static function filterTitleWords(string $title): bool
{
- $block_key_list = [
- '测试', '加密', 'test', 'TEST', '钓鱼', '炸鱼', '调试', "123", "1111", "测试", "測試","Test",
- "测一测", "ce-shi", "test", "T-E-S-T", "lala", "我是抽奖标题", "压测", "測一測", "t-e-s-t"
+ $sensitive_words = [
+ '测试', '加密', 'test', 'TEST', '钓鱼', '炸鱼', '调试', "123", "1111", "测试", "測試", "Test",
+ "测一测", "ce-shi", "test", "T-E-S-T", "lala", "我是抽奖标题", "压测", "測一測", "t-e-s-t"
];
- $flag = 5;
-
- for ($i = self::$start_aid; $i < self::$end_aid; $i++) {
- if (!$flag) {
- break;
+ foreach ($sensitive_words as $word) {
+ if (strpos($title, $word) !== false) {
+ return true;
}
- // 在丢弃列表里 跳过
- if (in_array($i, self::$discard_aid_list)) {
+ }
+ return false;
+ }
+
+
+ /**
+ * @use 抽奖盒子状态
+ * @param int $aid
+ * @param string $reply
+ * @return array|bool|mixed
+ */
+ private static function boxStatus(int $aid, $reply = 'bool')
+ {
+ $payload = [
+ 'aid' => $aid,
+ ];
+ $raw = Curl::get('https://api.live.bilibili.com/lottery/v1/box/getStatus', Sign::api($payload));
+ $de_raw = json_decode($raw, true);
+ switch ($reply) {
+ // 等于0是有抽奖返回false
+ case 'bool':
+ if ($de_raw['code'] == 0) {
+ return false;
+ }
+ return true;
+ case 'array':
+ if ($de_raw['code'] == 0) {
+ return $de_raw;
+ }
+ return [];
+ default:
+ return $de_raw;
+ }
+ }
+
+
+ /**
+ * @use 获取抽奖
+ * @return array
+ */
+ private static function fetchLottery(): array
+ {
+ $lottery_list = [];
+ $max_probe = 5;
+ $probes = range(self::$start_aid, self::$end_aid);
+ foreach ($probes as $probe_aid) {
+ // 最大试探
+ if ($max_probe == 0) break;
+ // 无效列表
+ if (in_array($probe_aid, self::$invalid_aids)) {
continue;
}
+ // 试探
+ $response = self::boxStatus($probe_aid, 'array');
+ if (empty($response)) {
+ $max_probe--;
+ continue;
+ }
+ $rounds = $response['data']['typeB'];
+ $last_round = end($rounds);
+ // 最后抽奖轮次无效
+ if ($last_round['join_end_time'] < time()) {
+ array_push(self::$invalid_aids, $probe_aid);
+ continue;
+ }
+ // 过滤敏感词
+ $title = $response['data']['title'];
+ if (self::filterTitleWords($title)) {
+ continue;
+ }
+ // 过滤抽奖轮次
+ $round_num = self::filterRound($rounds);
+ if ($round_num == 0) {
+ continue;
+ }
+ array_push($lottery_list, [
+ 'aid' => $probe_aid,
+ 'num' => $round_num,
+ ]);
+ }
+ return $lottery_list;
+ }
+
+ /**
+ * @use 过滤轮次
+ * @param array $rounds
+ * @return int
+ */
+ private static function filterRound(array $rounds): int
+ {
+ foreach ($rounds as $round) {
+ $join_start_time = $round['join_start_time'];
+ $join_end_time = $round['join_end_time'];
+ if ($join_end_time > time() && time() > $join_start_time) {
+ $status = $round['status'];
+ /*
+ * 3 结束 1 抽过 -1 未开启 0 可参与
+ */
+ if ($status == 0) {
+ return $round['round_num'];
+ }
+ }
+ }
+ return 0;
+ }
+
+
+ /**
+ * @use 抽奖
+ * @param array $lottery_list
+ * @return bool
+ */
+ private static function drawLottery(array $lottery_list): bool
+ {
+ foreach ($lottery_list as $lottery) {
+ $aid = $lottery['aid'];
+ $num = $lottery['num'];
+ Log::notice("实物抽奖 {$aid} 轮次 {$num} 可参与抽奖~");
$payload = [
- 'aid' => $i,
+ 'aid' => $aid,
+ 'number' => $num,
];
- $url = 'https://api.live.bilibili.com/lottery/v1/box/getStatus';
- // 请求 && 解码
- $raw = Curl::get($url, Sign::api($payload));
+ $raw = Curl::get('https://api.live.bilibili.com/lottery/v1/Box/draw', Sign::api($payload));
$de_raw = json_decode($raw, true);
- // -403 没有抽奖
- if ($de_raw['code'] != '0') {
- $flag--;
- continue;
- }
- // 如果最后一个结束时间已过 加入丢弃
- $lotterys = $de_raw['data']['typeB'];
- $total = count($lotterys);
- if ($lotterys[$total - 1]['join_end_time'] < time()) {
- array_push(self::$discard_aid_list, $i);
- continue;
- }
-
- // 如果存在敏感词 加入丢弃
- $title = $de_raw['data']['title'];
- foreach ($block_key_list as $block_key) {
- if (strpos($title, $block_key) !== false) {
- array_push(self::$discard_aid_list, $i);
- continue;
- }
- }
-
- $num = 1;
- foreach ($lotterys as $lottery) {
- $join_end_time = $lottery['join_end_time'];
- $join_start_time = $lottery['join_start_time'];
-
- if ($join_end_time > time() && time() > $join_start_time) {
- switch ($lottery['status']) {
- case 3:
- Log::info("实物[{$i}]抽奖: 当前轮次已经结束!");
- break;
- case 1:
- Log::info("实物[{$i}]抽奖: 当前轮次已经抽过了!");
- break;
- case -1:
- Log::info("实物[{$i}]抽奖: 当前轮次暂未开启!");
- break;
- case 0:
- Log::info("实物[{$i}]抽奖: 当前轮次正在抽奖中!");
-
- $payload = [
- 'aid' => $i,
- 'number' => $num,
- ];
- $raw = Curl::get('https://api.live.bilibili.com/lottery/v1/box/draw', Sign::api($payload));
- $de_raw = json_decode($raw, true);
-
- if ($de_raw['code'] == 0) {
- Log::notice("实物[{$i}]抽奖: 成功!");
- }
- $num++;
- break;
-
- default:
- Log::info("实物[{$i}]抽奖: 当前轮次状态码[{$lottery['status'] }]未知!");
- break;
- }
- }
+ if ($de_raw['code'] == 0) {
+ Log::notice("实物抽奖 {$aid} 轮次 {$num} 参与抽奖成功~");
+ } else {
+ Log::notice("实物抽奖 {$aid} 轮次 {$num} {$de_raw['msg']}~");
}
}
return true;
@@ -131,22 +189,21 @@ class MaterialObject
/**
- * @use 计算开始结束的AID
+ * @use 计算Aid
* @param $min
* @param $max
* @return bool
* @throws \Exception
*/
- private static function calculateAid($min, $max): bool
+ private static function calcAid($min, $max): bool
{
if (self::$end_aid != 0 && self::$start_aid != 0) {
return false;
}
-
- while (1) {
+ while (true) {
$middle = round(($min + $max) / 2);
- if (self::aidPost($middle)) {
- if (self::aidPost($middle + random_int(0, 3))) {
+ if (self::boxStatus($middle)) {
+ if (self::boxStatus($middle + random_int(0, 3))) {
$max = $middle;
} else {
$min = $middle;
@@ -158,31 +215,9 @@ class MaterialObject
break;
}
}
-
- self::$start_aid = $min - random_int(30, 40);
- self::$end_aid = $min + random_int(30, 40);
+ self::$start_aid = $min - random_int(15, 30);
+ self::$end_aid = $min + random_int(15, 30);
Log::info("实物抽奖起始值[" . self::$start_aid . "],结束值[" . self::$end_aid . "]");
return true;
}
-
- /**
- * @use Aid 请求
- * @param $aid
- * @return bool
- */
- private static function aidPost($aid): bool
- {
- $payload = [
- 'aid' => $aid,
- ];
- $raw = Curl::get('https://api.live.bilibili.com/lottery/v1/box/getStatus', Sign::api($payload));
- $de_raw = json_decode($raw, true);
-
- // 等于0是有抽奖返回false
- if ($de_raw['code'] == 0) {
- return false;
- }
- // 没有抽奖
- return true;
- }
}
\ No newline at end of file
diff --git a/src/plugin/PkRaffle.php b/src/plugin/PkRaffle.php
index 578eb93..8fadf43 100644
--- a/src/plugin/PkRaffle.php
+++ b/src/plugin/PkRaffle.php
@@ -58,7 +58,7 @@ class PkRaffle extends BaseRaffle
'room_id' => $room_id,
'raffle_id' => $pk['id'],
'raffle_name' => '大乱斗',
- 'wait' => time()
+ 'wait' => time() + random_int(5, 25)
];
Statistics::addPushList(self::ACTIVE_TITLE);
array_push(self::$wait_list, $data);
diff --git a/src/plugin/StormRaffle.php b/src/plugin/StormRaffle.php
index 979c6c1..49eaa9a 100644
--- a/src/plugin/StormRaffle.php
+++ b/src/plugin/StormRaffle.php
@@ -51,7 +51,7 @@ class StormRaffle extends BaseRaffle
}
// 过滤抽奖范围
self::$drop_rate = getenv('STORM_DROPRATE') !== "" ? (int)getenv('STORM_DROPRATE') : 0;
- if (mt_rand(1, 100) <= (int)self::$drop_rate) {
+ if (random_int(1, 100) <= (int)self::$drop_rate) {
return false;
}
// 去重
diff --git a/src/plugin/ZoneTcpClient.php b/src/plugin/ZoneTcpClient.php
index b285aa9..affff54 100644
--- a/src/plugin/ZoneTcpClient.php
+++ b/src/plugin/ZoneTcpClient.php
@@ -309,7 +309,7 @@ class ZoneTcpClient
private static function genHandshakePkg($room_id): string
{
return self::packMsg(json_encode([
- 'uid' => mt_rand(1000000, 2999999),
+ 'uid' => random_int(1000000, 2999999),
'roomid' => intval($room_id),
]), 0x0007);
}