diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ae1fb5..669ac6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,18 @@ # Release Notes # 本项目Log +## v0.1.0.200101 alpha (2020-01-01) + +### Added +- 任务逻辑引入协程 + +### Changed +- + +### Fixed +- 修复礼物赠送异常 +- + ## v0.1.0.191227 alpha (2019-12-27) ### Added diff --git a/DOC.md b/DOC.md index 7b218da..2c02486 100644 --- a/DOC.md +++ b/DOC.md @@ -216,7 +216,9 @@ http://live.bilibili.com/9522051 https://api.live.bilibili.com/room/v1/Room/room_init?id=3 ``` -所有直播间号码小于 1000 的直播间为短号,该脚本在每次启动会自动修正部分功能,特殊标注的请留意, +所有直播间号码小于 1000 的直播间为短号,部分4位直播间也为短号, + +该脚本在每次启动会自动修正部分功能,特殊标注的请留意。 ## 相关 diff --git a/README.md b/README.md index 95357f1..f84a1e6 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.1.0.191227 alpha** +Currently for Personal Edition **0.1.0.200101 alpha** ## 文档 diff --git a/composer.json b/composer.json index ded0d15..5faa992 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "monolog/monolog": "^1.23", "bramus/monolog-colored-line-formatter": "^2.0", "clue/socket-raw": "^1.4.1", - "vlucas/phpdotenv": "^4.1" + "vlucas/phpdotenv": "^4.1", + "amphp/amp": "^2.4" }, "license": "MIT", "authors": [ diff --git a/composer.lock b/composer.lock index 4093152..31d1e93 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,91 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6034c5a467eb270c2c7d1ce12d7277fd", + "content-hash": "bc4f20d52292405305a8aa3bd6dcf4bd", "packages": [ + { + "name": "amphp/amp", + "version": "v2.4.0", + "source": { + "type": "git", + "url": "https://github.com/amphp/amp.git", + "reference": "13930a582947831bb66ff1aeac28672fd91c38ea" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/amphp/amp/zipball/13930a582947831bb66ff1aeac28672fd91c38ea", + "reference": "13930a582947831bb66ff1aeac28672fd91c38ea", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=7" + }, + "require-dev": { + "amphp/php-cs-fixer-config": "dev-master", + "amphp/phpunit-util": "^1", + "ext-json": "*", + "phpstan/phpstan": "^0.8.5", + "phpunit/phpunit": "^6.0.9 | ^7", + "react/promise": "^2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Amp\\": "lib" + }, + "files": [ + "lib/functions.php", + "lib/Internal/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Daniel Lowrey", + "email": "rdlowrey@php.net" + }, + { + "name": "Aaron Piotrowski", + "email": "aaron@trowski.com" + }, + { + "name": "Bob Weinand", + "email": "bobwei9@hotmail.com" + }, + { + "name": "Niklas Keller", + "email": "me@kelunik.com" + } + ], + "description": "A non-blocking concurrency framework for PHP applications.", + "homepage": "http://amphp.org/amp", + "keywords": [ + "async", + "asynchronous", + "awaitable", + "concurrency", + "event", + "event-loop", + "future", + "non-blocking", + "promise" + ], + "time": "2019-11-11T19:32:05+00:00" + }, { "name": "bramus/ansi-php", "version": "3.0.3", diff --git a/conf/user.conf.example b/conf/user.conf.example index 20ff895..9ae8832 100644 --- a/conf/user.conf.example +++ b/conf/user.conf.example @@ -60,12 +60,12 @@ SLEEP_SECTION=2,3,4,5,6 # 基础设置 # ####################### -# 独立推送服务(主) +# 独立推送服务(主)|主备冲突 USE_ALONE_SERVER=true ALONE_SERVER_ADDR=tcp://47.102.120.84:10010 ALONE_SERVER_KEY=,*(?PVl]nIbo35sB -# 分区推送服务(备) +# 分区推送服务(备)|主备冲突 USE_ZONE_SERVER=false ZONE_SERVER_ADDR=tcp://broadcastlv.chat.bilibili.com:2243/sub @@ -87,8 +87,9 @@ USE_SCKEY= # 直播间ID,用于礼物赠送 ROOM_ID=9522051 -# 勋章亲密度,测试功能,使用长位房间号,左侧优先,仅支持辣条和亿元 -# 是否填满(按时间投喂正常礼物,否则过期礼物)|直播间ID列表|逗号分隔 +# 勋章亲密度,测试功能,有短位使用短位反之长位,左侧优先,仅支持辣条和亿元 +# 当列表中所有ID都已达到每日上限,剩余礼物赠送ROOM_ID指定的直播间 +# 是否填满(按时间投喂正常礼物,否则过期礼物)|直播间ID列表|逗号分隔| FEED_FILL=false ROOM_LIST=9522051 diff --git a/src/core/App.php b/src/core/App.php index 0603064..372c8f1 100644 --- a/src/core/App.php +++ b/src/core/App.php @@ -10,7 +10,9 @@ namespace BiliHelper\Core; -use BiliHelper\Plugin; +use Amp\Delayed; +use Amp\Loop; +use function Amp\asyncCall; class App { @@ -38,35 +40,54 @@ class App return $this; } + /** + * @use 新任务 + * @param string $classname + */ + public function newTask(string $classname) + { + asyncCall(function () use ($classname) { + while (true) { + call_user_func(array('BiliHelper\Plugin\\' . $classname, 'run'), []); + yield new Delayed(1000); + } + }); + + } + + /** * @use 核心运行 */ public function start() { - while (true) { - Plugin\Login::run(); - Plugin\Sleep::run(); - Plugin\MasterSite::run(); - Plugin\Daily::run(); - Plugin\Heart::run(); - Plugin\Task::run(); - Plugin\Silver::run(); - Plugin\Barrage::run(); - Plugin\Silver2Coin::run(); - Plugin\GiftSend::run(); - Plugin\GroupSignIn::run(); - Plugin\GiftHeart::run(); - Plugin\MaterialObject::run(); - Plugin\AloneTcpClient::run(); - Plugin\ZoneTcpClient::run(); - Plugin\StormRaffle::run(); - Plugin\GiftRaffle::run(); - Plugin\PkRaffle::run(); - Plugin\GuardRaffle::run(); - Plugin\AnchorRaffle::run(); - Plugin\AwardRecord::run(); - Plugin\Statistics::run(); - usleep(0.1 * 1000000); + $plugins = [ + 'Login', + 'Sleep', + 'MasterSite', + 'Daily', + 'Heart', + 'Task', + 'Silver', + 'Barrage', + 'Silver2Coin', + 'GiftSend', + 'GroupSignIn', + 'GiftHeart', + 'MaterialObject', + 'AloneTcpClient', + 'ZoneTcpClient', + 'StormRaffle', + 'GiftRaffle', + 'PkRaffle', + 'GuardRaffle', + 'AnchorRaffle', + 'AwardRecord', + 'Statistics', + ]; + foreach ($plugins as $plugin) { + $this->newTask($plugin); } + Loop::run(); } } \ No newline at end of file diff --git a/src/plugin/GiftSend.php b/src/plugin/GiftSend.php index ee1aeb1..bf0cbde 100644 --- a/src/plugin/GiftSend.php +++ b/src/plugin/GiftSend.php @@ -21,6 +21,7 @@ class GiftSend protected static $tid = 0; protected static $r_uid = 0; protected static $room_id = 0; + protected static $short_id = 0; protected static $room_list = []; protected static $medal_list = []; @@ -53,28 +54,30 @@ class GiftSend return false; } self::getMedalList(); - foreach (self::$medal_list as $key => $val) { + foreach (self::$medal_list as $room_id => $total_intimacy) { $bag_list = self::fetchBagList(); if (getenv('FEED_FILL') == 'false') { $bag_list = self::checkExpireGift($bag_list); } if (count($bag_list)) { - self::$tid = $key; + self::$tid = $room_id; self::getRoomInfo(); - array_multisort(array_column($bag_list, "expire_at"), SORT_ASC, $bag_list); + // array_multisort(array_column($bag_list, "expire_at"), SORT_ASC, $bag_list); } else { break; } + $current_intimacy = 0; foreach ($bag_list as $gift) { // 是辣条、亿元 && 不是过期礼物 - if (!in_array($gift['gift_id'], [1, 6]) && getenv('FEED_FILL') != 'false') { + if (!in_array($gift['gift_id'], [1, 6])) { continue; } - $amt = self::calcAmt($gift); + Log::notice("直播间 {$room_id} 需赠送亲密度 {$total_intimacy} 剩余亲密度 " . ($total_intimacy - $current_intimacy)); + $amt = self::calcAmt($gift, $total_intimacy - $current_intimacy); self::sendGift($gift, $amt); - $val -= $amt; - if (!$val) { - Log::notice("直播间 {$key} 亲密度 {$val} 送满啦~送满啦~"); + $current_intimacy += ($gift['gift_id'] == 6) ? ($amt * 10) : $amt; + if (!($current_intimacy - $total_intimacy)) { + Log::notice("直播间 {$room_id} 亲密度 {$total_intimacy} 送满啦~送满啦~"); break; } } @@ -227,24 +230,26 @@ class GiftSend Log::info('直播间信息生成完毕!'); self::$r_uid = (string)$data['data']['uid']; self::$room_id = (string)$data['data']['room_id']; + self::$short_id = $data['data']['short_id'] ? (string)$data['data']['short_id'] : self::$room_id; } /** * @use 计算赠送数量 * @param array $gift + * @param int $surplus_num * @return int */ - protected static function calcAmt(array $gift): int + protected static function calcAmt(array $gift, int $surplus_num): int { $amt = $gift['gift_num']; if ($gift['gift_id'] == 1) { - $amt = (self::$medal_list[self::$room_id] > $gift['gift_num']) ? $gift['gift_num'] : self::$medal_list[self::$room_id]; + $amt = ($surplus_num > $gift['gift_num']) ? $gift['gift_num'] : floor($surplus_num); } if ($gift['gift_id'] == 6) { - $amt = (floor(self::$medal_list[self::$room_id] / 10) > $gift['gift_num']) ? $gift['gift_num'] : floor(self::$medal_list[self::$room_id] / 10); + $amt = (floor($surplus_num / 10) > $gift['gift_num']) ? $gift['gift_num'] : floor($surplus_num / 10); } - return $amt; + return ($amt < 1) ? 1 : $amt; } diff --git a/src/plugin/Sleep.php b/src/plugin/Sleep.php index 9bc6203..17be221 100644 --- a/src/plugin/Sleep.php +++ b/src/plugin/Sleep.php @@ -21,8 +21,8 @@ class Sleep // TODO 黑白名单|考虑添加到每个插件内部自动添加|优化RUN逻辑代码 private static $unlock_hour = 24; private static $unlock_time = 0; - private static $fillable = ['Login', 'Sleep', 'Daily', 'MasterSite', 'GiftSend', 'Task', 'Silver2Coin', 'GroupSignIn', 'GiftHeart', 'AwardRecord', 'Statistics']; - private static $guarded = ['Barrage', 'Heart', 'Silver', 'MaterialObject', 'AloneTcpClient', 'ZoneTcpClient', 'StormRaffle', 'GuardRaffle', 'PkRaffle', 'GiftRaffle', 'AnchorRaffle']; + private static $fillable = ['Login', 'Sleep', 'Daily', 'MasterSite', 'GiftSend', 'Task', 'Silver2Coin', 'GroupSignIn', 'AwardRecord', 'Statistics']; + private static $guarded = ['Barrage', 'GiftHeart', 'Heart', 'Silver', 'MaterialObject', 'AloneTcpClient', 'ZoneTcpClient', 'StormRaffle', 'GuardRaffle', 'PkRaffle', 'GiftRaffle', 'AnchorRaffle']; private static $sleep_section = [];