diff --git a/CHANGELOG.md b/CHANGELOG.md index 628437f..f7cdb1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,19 @@ # Release Notes # 本项目Log +## v0.0.1.190723 alpha (2019-07-23) + +### Added +- pk lottery + +### Changed +- + +### Fixed +- + + + ## v0.0.1.190713 alpha (2019-07-13) ### Added diff --git a/DOC.md b/DOC.md index 725c5fa..3e5eab3 100644 --- a/DOC.md +++ b/DOC.md @@ -29,9 +29,10 @@ B 站直播实用脚本 |GroupSignIn |19.07.13 |应援团签到 | |Storm |19.07.13 |节奏风暴 | |Notice |19.07.13 |Server酱 | -|RaffleHandler |19.07.13 |统一活动抽奖 | +|UnifyRaffle |19.07.23 |统一活动抽奖 | |MasterSite |19.07.13 |主站(观看、分享、投币)| -|Guard |19.07.13 |舰长上船亲密度 | +|Guard |19.07.23 |舰长上船亲密度 | +|PkRaffle |19.07.23 |大乱斗 | ## 打赏赞助 diff --git a/README.md b/README.md index c8b5bcf..20b14b7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ B 站直播实用脚本 ## 公告 -Currently for Personal Edition **0.0.1.190713 alpha** +Currently for Personal Edition **0.0.1.190723 alpha** ## 文档 @@ -11,8 +11,8 @@ Currently for Personal Edition **0.0.1.190713 alpha** * [更新日志 / CHANGELOG.md](./CHANGELOG.md) ## 交流 - Group: [55308141](https://jq.qq.com/?_wv=1027&k=5AIDaJg) +Group: [590512629](https://jq.qq.com/?_wv=1027&k=5S5PwTE) ## 打赏 diff --git a/conf/user.conf.example b/conf/user.conf.example index e8b14c8..d1ff7c4 100644 --- a/conf/user.conf.example +++ b/conf/user.conf.example @@ -12,32 +12,15 @@ REFRESH_TOKEN= COOKIE= ####################### -# 功能设置 # +# 固定设置 # ####################### # 推送服务器 USE_SERVER=true -SERVER_ADDR= -SERVER_KEY= +SERVER_ADDR=tcp://raffle.biem.club:10010 +SERVER_KEY=,*(?PVl]nIbo35sB -# 舰长总督 -USE_GUARD=true - -# 主站助手 -USE_MASTER_SITE=true - -# 活跃弹幕|弹幕房间(为空则随机)|弹幕内容(为空随机,但是URL不能为空) -USE_DANMU=true -DANMU_ROOMID=9522051 -DANMU_CONTENT= -CONTENT_FETCH="https://v1.hitokoto.cn/?encode=text" - -# 视频投币|投币稿件数(每日任务最大5)|自定义稿件ID,空则随机(AV_NUM=1生效) -USE_ADD_COIN=false -ADD_COIN_AV_NUM=1 -ADD_COIN_AV=33492180 - -# SERVER酱,用于推送消息 +# SERVER酱, 用于推送消息 USE_SCKEY= # 切换HTTPS,为真则使用https协议 @@ -54,19 +37,43 @@ ROOM_ID=9522051 # 弹幕监控房间(为空则随机) SOCKET_ROOM_ID=9522051 +####################### +# 功能设置 # +####################### + +# 主站助手 +USE_MASTER_SITE=true + +# 舰长总督 +USE_GUARD=true + +# 统一活动 +USE_ACTIVE=true + +# 大乱斗 +USE_PK=true + +# 实物抽奖 +USE_MO=true + +# 银瓜子兑换硬币 +USE_SILVER2COIN=true + # 节奏风暴|丢弃率(0-100)|尝试数(范围值) USE_STORM=true STORM_DROPRATE=0 STORM_ATTEMPT=30,50 -# 统一活动 -USE_ACTIVE=true +# 活跃弹幕|弹幕房间(为空则随机)|弹幕内容(为空随机,但是URL不能为空) +USE_DANMU=true +DANMU_ROOMID=9522051 +DANMU_CONTENT= +CONTENT_FETCH="https://v1.hitokoto.cn/?encode=text" -# 银瓜子兑换硬币 -USE_SILVER2COIN=true - -# 实物抽奖 -USE_MO=true +# 视频投币|投币稿件数(每日任务最大5)|自定义稿件ID,空则随机(AV_NUM=1生效) +USE_ADD_COIN=false +ADD_COIN_AV_NUM=1 +ADD_COIN_AV=33492180 ####################### # 日志设置 # @@ -102,13 +109,3 @@ APP_CALLBACK="http://www.example.com/api.send?text={account}[{level}]: {message} # ERROR 400 # APP_CALLBACK_LEVEL=400 - -####################### -# 固定设置 # -####################### - -# 用于打包请求 -ACTIONENTRY=7 - -# 用于发送心跳 -ACTIONHEARTBEAT=2 \ No newline at end of file diff --git a/index.php b/index.php index 2a9b680..d4f2682 100644 --- a/index.php +++ b/index.php @@ -49,9 +49,10 @@ class Index GiftHeart::run(); Winning::run(); MaterialObject::run(); - TcpClinet::run(); + TcpClient::run(); Storm::run(); - RaffleHandler::run(); + PkRaffle::run(); + UnifyRaffle::run(); Guard::run(); Statistics::run(); usleep(0.1 * 1000000); diff --git a/src/BaseRaffle.php b/src/BaseRaffle.php new file mode 100644 index 0000000..c085a9d --- /dev/null +++ b/src/BaseRaffle.php @@ -0,0 +1,116 @@ + time()) { + return; + } + static::startLottery(); + } + + /** + * 抽奖逻辑 + * @return bool + */ + protected static function startLottery(): bool + { + $max_num = mt_rand(5, 10); + while ($max_num) { + $raffle = array_shift(static::$wait_list); + if (is_null($raffle)) { + break; + } + Live::goToRoom($raffle['room_id']); + Statistics::addJoinList(static::ACTIVE_TITLE); + static::lottery($raffle); + $max_num--; + } + return true; + } + + + /** + * 检查抽奖列表 + * @param $rid + * @return bool + */ + abstract protected static function check($rid): bool; + + + /** + * @use 请求抽奖 + * @param array $data + * @return bool + */ + abstract protected static function lottery(array $data): bool; + + + /** + * 重复检测 + * @param int $lid + * @return bool + */ + protected static function toRepeatLid(int $lid): bool + { + if (in_array($lid, static::$all_list)) { + return true; + } + if (count(static::$all_list) > 2000) { + static::$all_list = []; + } + array_push(static::$all_list, $lid); + + return false; + } + + /** + * 数据推入队列 + * @param array $data + * @return bool + */ + public static function pushToQueue(array $data): bool + { + if (getenv(static::ACTIVE_SWITCH) == 'false') { + return false; + } + + if (Live::fishingDetection($data['rid'])) { + return false; + } + static::check($data['rid']); + $wait_num = count(static::$wait_list); + if ($wait_num > 2) { + Log::info("当前队列中共有 {$wait_num} 个" . static::ACTIVE_TITLE . "待抽奖"); + } + return true; + } + +} + + diff --git a/src/DataTreating.php b/src/DataTreating.php index f8d2430..ac35b1a 100644 --- a/src/DataTreating.php +++ b/src/DataTreating.php @@ -32,15 +32,19 @@ class DataTreating break; case "raffle": // 礼物 - RaffleHandler::pushToQueue($info); + UnifyRaffle::pushToQueue($info); break; case "guard": // 舰长 Guard::pushToQueue($info); break; case "small_tv": - // 小电视 - RaffleHandler::pushToQueue($info); + // 电视 + UnifyRaffle::pushToQueue($info); + break; + case 'pk': + // 乱斗 + PkRaffle::pushToQueue($info); break; default: break; diff --git a/src/Guard.php b/src/Guard.php index 9beabdf..1e8ce2e 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -12,19 +12,17 @@ namespace lkeme\BiliHelper; class Guard { - const KEY = '总督舰长'; - const SWITCH = 'USE_GUARD'; + const ACTIVE_TITLE = '总督舰长'; + const ACTIVE_SWITCH = 'USE_GUARD'; public static $lock = 0; - private static $wait_list = []; - private static $finsh_list = []; - private static $all_list = []; - + protected static $wait_list = []; + protected static $all_list = []; public static function run() { - if (getenv(self::SWITCH) == 'false') { + if (getenv(self::ACTIVE_SWITCH) == 'false') { return; } if (self::$lock > time()) { @@ -48,15 +46,15 @@ class Guard $guard_lid = $guard['lid']; $guard_rid = $guard['rid']; Live::goToRoom($guard_rid); - Statistics::addJoinList(self::KEY); + Statistics::addJoinList(self::ACTIVE_TITLE); $data = self::lottery($guard_rid, $guard_lid); if ($data['code'] == 0) { - Statistics::addSuccessList(self::KEY); - Log::notice("房间 {$guard_rid} 编号 {$guard_lid} " . self::KEY . ": {$data['data']['message']}"); + Statistics::addSuccessList(self::ACTIVE_TITLE); + Log::notice("房间 {$guard_rid} 编号 {$guard_lid} " . self::ACTIVE_TITLE . ": {$data['data']['message']}"); } elseif ($data['code'] == 400 && $data['msg'] == '你已经领取过啦') { - Log::info("房间 {$guard_rid} 编号 {$guard_lid} " . self::KEY . ": {$data['msg']}"); + Log::info("房间 {$guard_rid} 编号 {$guard_lid} " . self::ACTIVE_TITLE . ": {$data['msg']}"); } else { - Log::warning("房间 {$guard_rid} 编号 {$guard_lid} " . self::KEY . ": {$data['msg']}"); + Log::warning("房间 {$guard_rid} 编号 {$guard_lid} " . self::ACTIVE_TITLE . ": {$data['msg']}"); } $max_num--; } @@ -109,17 +107,17 @@ class Guard */ public static function pushToQueue(array $data): bool { - if (getenv(self::SWITCH) == 'false') { + if (getenv(self::ACTIVE_SWITCH) == 'false') { return false; } if (self::toRepeatLid($data['lid'])) { return false; } - Statistics::addPushList(self::KEY); + Statistics::addPushList(self::ACTIVE_TITLE); self::$wait_list = array_merge(self::$wait_list, [['rid' => $data['rid'], 'lid' => $data['lid']]]); $wait_num = count(self::$wait_list); if ($wait_num > 2) { - Log::info("当前队列中共有 {$wait_num} 个" . self::KEY . "待抽奖"); + Log::info("当前队列中共有 {$wait_num} 个" . self::ACTIVE_TITLE . "待抽奖"); } return true; } diff --git a/src/Live.php b/src/Live.php index 5a47bef..3a14e74 100644 --- a/src/Live.php +++ b/src/Live.php @@ -146,12 +146,8 @@ class Live Silver::$lock = $second; MaterialObject::$lock = $second; - Websocket::$lock = $second; GiftHeart::$lock = $second; - Guard::$lock = $second; - RaffleHandler::$lock = $second; - RaffleHandler::$rw_lock = $second; - + TcpClient::$lock = $second; return; } diff --git a/src/PkRaffle.php b/src/PkRaffle.php new file mode 100644 index 0000000..a04c49d --- /dev/null +++ b/src/PkRaffle.php @@ -0,0 +1,92 @@ + $rid + ]; + $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v1/pk/check'; + $raw = Curl::get($url, Sign::api($payload)); + $de_raw = json_decode($raw, true); + + // 计数 && 跳出 + $total = count($de_raw['data']); + if (!$total) { + return false; + } + + for ($i = 0; $i < $total; $i++) { + $data = [ + 'raffle_id' => $de_raw['data'][$i]['pk_id'], + 'title' => $de_raw['data'][$i]['title'], + 'room_id' => $de_raw['data'][$i]['room_id'] + ]; + if (static::toRepeatLid($data['raffle_id'])) { + continue; + } + Statistics::addPushList(static::ACTIVE_TITLE); + array_push(static::$wait_list, $data); + } + return true; + } + + + /** + * @use 请求抽奖 + * @param array $data + * @return bool + */ + protected static function lottery(array $data): bool + { + $user_info = User::parseCookies(); + $payload = [ + 'id' => $data['raffle_id'], + 'roomid' => $data['room_id'], + 'csrf_token' => $user_info['token'], + "csrf" => $user_info['token'], + ]; + $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v1/pk/join'; + $raw = Curl::post($url, Sign::api($payload)); + $de_raw = json_decode($raw, true); + /* + * {'code': 0, 'message': '0', 'ttl': 1, 'data': {'id': 343560, 'gift_type': 0, 'award_id': '1', 'award_text': '辣条X1', 'award_image': 'https://i0.hdslb.com/bfs/live/da6656add2b14a93ed9eb55de55d0fd19f0fc7f6.png', 'award_num': 0, 'title': '大乱斗获胜抽奖'}} + * {'code': -1, 'message': '抽奖已结束', 'ttl': 1} + * {'code': -2, 'message': '您已参加过抽奖', 'ttl': 1} + * {"code":-403,"data":null,"message":"访问被拒绝","msg":"访问被拒绝"} + */ + if (isset($de_raw['code']) && $de_raw['code'] == 0) { + Statistics::addSuccessList(static::ACTIVE_TITLE); + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . static::ACTIVE_TITLE . ": {$de_raw['data']['award_text']}"); + } else { + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . static::ACTIVE_TITLE . ": {$de_raw['message']}"); + } + return true; + } +} diff --git a/src/Storm.php b/src/Storm.php index 0caec65..f076def 100644 --- a/src/Storm.php +++ b/src/Storm.php @@ -12,21 +12,23 @@ namespace lkeme\BiliHelper; class Storm { - const KEY = '节奏风暴'; - const SWITCH = 'USE_STORM'; + const ACTIVE_TITLE = '节奏风暴'; + const ACTIVE_SWITCH = 'USE_STORM'; public static $lock = 0; + + protected static $wait_list = []; + protected static $all_list = []; + private static $drop_rate = null; private static $attempt = null; - private static $wait_list = []; - private static $all_list = []; /** * @throws \Exception */ public static function run() { - if (getenv(self::SWITCH) == 'false') { + if (getenv(self::ACTIVE_SWITCH) == 'false') { return; } if (self::$lock > time()) { @@ -65,7 +67,7 @@ class Storm $storm_lid = $storm['lid']; $storm_rid = $storm['rid']; Live::goToRoom($storm_rid); - Statistics::addJoinList(self::KEY); + Statistics::addJoinList(self::ACTIVE_TITLE); $num = random_int((int)self::$attempt[0], (int)self::$attempt[1]); for ($i = 1; $i < $num; $i++) { if (!self::lottery($storm_rid, $storm_lid, $i)) { @@ -119,7 +121,7 @@ class Storm return false; } if ($de_raw['code'] == 0) { - Statistics::addSuccessList(self::KEY); + Statistics::addSuccessList(self::ACTIVE_TITLE); Log::notice(self::formatInfo($lid, $num, $de_raw['data']['mobile_content'])); return false; } @@ -182,17 +184,17 @@ class Storm */ public static function pushToQueue(array $data): bool { - if (getenv(self::SWITCH) == 'false') { + if (getenv(self::ACTIVE_SWITCH) == 'false') { return false; } if (self::toRepeatLid($data['lid'])) { return false; } - Statistics::addPushList(self::KEY); + Statistics::addPushList(self::ACTIVE_TITLE); self::$wait_list = array_merge(self::$wait_list, [['rid' => $data['rid'], 'lid' => $data['lid']]]); $wait_num = count(self::$wait_list); if ($wait_num > 2) { - Log::info("当前队列中共有 {$wait_num} 个" . self::KEY . "待抽奖"); + Log::info("当前队列中共有 {$wait_num} 个" . self::ACTIVE_TITLE . "待抽奖"); } return true; } diff --git a/src/TcpClinet.php b/src/TcpClient.php similarity index 97% rename from src/TcpClinet.php rename to src/TcpClient.php index c62b767..0ff3977 100644 --- a/src/TcpClinet.php +++ b/src/TcpClient.php @@ -13,7 +13,7 @@ namespace lkeme\BiliHelper; use Exception; use Socket\Raw\Factory; -class TcpClinet +class TcpClient { public static $lock = 0; private static $heart_lock = 0; @@ -216,8 +216,8 @@ class TcpClinet break; case 'error': // 致命错误 - Log::warning($body); - exit("推送服务器发生致命错误 {$raw_data['data']['msg']}"); + Log::error("推送服务器发生致命错误 {$raw_data['data']['msg']}"); + exit(); break; case 'heartbeat': // 服务端心跳推送 diff --git a/src/RaffleHandler.php b/src/UnifyRaffle.php similarity index 54% rename from src/RaffleHandler.php rename to src/UnifyRaffle.php index 26e81c5..0a00318 100644 --- a/src/RaffleHandler.php +++ b/src/UnifyRaffle.php @@ -10,56 +10,24 @@ namespace lkeme\BiliHelper; -class RaffleHandler +class UnifyRaffle extends BaseRaffle { - const KEY = '统一活动'; - const SWITCH = 'USE_ACTIVE'; + const ACTIVE_TITLE = '统一活动'; + const ACTIVE_SWITCH = 'USE_ACTIVE'; public static $lock = 0; public static $rw_lock = 0; - private static $wait_list = []; - private static $finsh_list = []; - private static $all_list = []; - - public static function run() - { - if (getenv(self::SWITCH) == 'false') { - return; - } - if (self::$lock > time()) { - return; - } - self::startLottery(); - } - - /** - * 抽奖逻辑 - * @return bool - */ - protected static function startLottery(): bool - { - $max_num = mt_rand(5, 10); - while ($max_num) { - $raffle = array_shift(self::$wait_list); - if (is_null($raffle)) { - break; - } - Live::goToRoom($raffle['room_id']); - Statistics::addJoinList(self::KEY); - self::lottery($raffle); - $max_num--; - } - return true; - } - + protected static $wait_list = []; + protected static $finish_list = []; + protected static $all_list = []; /** * 检查抽奖列表 * @param $rid * @return bool */ - private static function checkWeb($rid): bool + protected static function check($rid): bool { $payload = [ 'roomid' => $rid @@ -86,11 +54,11 @@ class RaffleHandler 'type' => $de_raw['data']['list'][$i]['type'], 'room_id' => $rid ]; - if (self::toRepeatLid($data['raffle_id'])) { + if (static::toRepeatLid($data['raffle_id'])) { continue; } - Statistics::addPushList(self::KEY); - array_push(self::$wait_list, $data); + Statistics::addPushList(static::ACTIVE_TITLE); + array_push(static::$wait_list, $data); } return true; } @@ -102,17 +70,17 @@ class RaffleHandler public static function resultWeb() { // 时间锁 - if (self::$rw_lock > time()) { + if (static::$rw_lock > time()) { return; } // 如果待查询为空 && 去重 - if (!count(self::$finsh_list)) { - self::$rw_lock = time() + 40; + if (!count(static::$finish_list)) { + static::$rw_lock = time() + 40; return; } // 查询,每次查询10个 $flag = 0; - foreach (self::$finsh_list as $winning_web) { + foreach (static::$finish_list as $winning_web) { $flag++; if ($flag > 40) { break; @@ -132,7 +100,7 @@ class RaffleHandler case 3: break; case 2: - Statistics::addSuccessList(self::KEY); + Statistics::addSuccessList(static::ACTIVE_TITLE); // 提示信息 $info = "房间 {$winning_web['room_id']} 编号 {$winning_web['raffle_id']} {$winning_web['title']}: 获得"; $info .= "{$de_raw['data']['gift_name']}X{$de_raw['data']['gift_num']}"; @@ -142,14 +110,14 @@ class RaffleHandler Notice::run('raffle', $info); } // 删除查询完成ID - unset(self::$finsh_list[$flag - 1]); - self::$finsh_list = array_values(self::$finsh_list); + unset(static::$finish_list[$flag - 1]); + static::$finish_list = array_values(static::$finish_list); break; default: break; } } - self::$rw_lock = time() + 40; + static::$rw_lock = time() + 40; return; } @@ -157,8 +125,9 @@ class RaffleHandler /** * @use 请求抽奖 * @param array $data + * @return bool */ - private static function lottery(array $data) + protected static function lottery(array $data): bool { $payload = [ 'raffleId' => $data['raffle_id'], @@ -168,52 +137,11 @@ class RaffleHandler $raw = Curl::get($url, Sign::api($payload)); $de_raw = json_decode($raw, true); if (isset($de_raw['code']) && $de_raw['code']) { - Log::notice("房间 {$data['raffle_id']} 编号 {$data['room_id']} " . self::KEY . ": {$de_raw['message']}"); + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . static::ACTIVE_TITLE . ": {$de_raw['message']}"); } else { - Log::notice("房间 {$data['raffle_id']} 编号 {$data['room_id']} " . self::KEY . ": {$de_raw['msg']}"); - array_push(self::$finsh_list, $data); - } - return; - } - - /** - * 重复检测 - * @param int $lid - * @return bool - */ - private static function toRepeatLid(int $lid): bool - { - if (in_array($lid, self::$all_list)) { - return true; - } - if (count(self::$all_list) > 2000) { - self::$all_list = []; - } - array_push(self::$all_list, $lid); - - return false; - } - - /** - * 数据推入队列 - * @param array $data - * @return bool - */ - public static function pushToQueue(array $data): bool - { - if (getenv(self::SWITCH) == 'false') { - return false; - } - - if (Live::fishingDetection($data['rid'])) { - return false; - } - self::checkWeb($data['rid']); - $wait_num = count(self::$wait_list); - if ($wait_num > 2) { - Log::info("当前队列中共有 {$wait_num} 个" . self::KEY . "待抽奖"); + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . static::ACTIVE_TITLE . ": {$de_raw['msg']}"); + array_push(static::$finish_list, $data); } return true; } - } diff --git a/src/Winning.php b/src/Winning.php index dd13788..5467b2d 100644 --- a/src/Winning.php +++ b/src/Winning.php @@ -18,7 +18,7 @@ class Winning public static function run() { // 活动统一 - RaffleHandler::resultWeb(); + UnifyRaffle::resultWeb(); // 实物 self::winningRecords();