diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index 6350398..9b94f3d 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -541,87 +541,24 @@ def process_reservation_attend_window( pass return False - # 进入锁定期:在开奖附近一段时间内不再切换直播间 - focus_until_new = ft + max(0.0, poll_before) - if reservation_focus_until_ts_ref: - reservation_focus_until_ts_ref[0] = focus_until_new - else: - reservation_focus_until_ts_ref.append(focus_until_new) + # 进入锁定期 + 写完成日志 + from .attend_window_helpers import setup_lock_period_and_log_success - if reservation_after_action_pending_ref: - reservation_after_action_pending_ref[0] = True - else: - reservation_after_action_pending_ref.append(True) - - try: - from datetime import datetime - - ft_str = ( - datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S") - if ft > 0 - else "无效" - ) - - # 运行日志 - analyser._log_kv( - "预约", - f"[预约] 已回退到目标直播间,等待详情识别与参与 - 直播间:'{target_room_title[:30] if target_room_title else ''}', 开奖时间:{ft_str}, 价值:{target_value}元", - future_ts=int(ft), - future_str=ft_str, - focus_until=int(focus_until_new), - lock_period_seconds=int(focus_until_new - _now_ts()), - remain_seconds_to_open=int(ft - _now_ts()), - target_room_title=target_room_title, - target_room_title_raw=target_room_title_raw, - target_content=target_content, - target_value_yuan=target_value, - ) - - # 行为日志 - analyser._log_kv( - "行为", - "回退到预约直播间成功", - action="回退到预约直播间", - result="成功", - detail={ - "目标直播间": ( - target_room_title - if target_room_title - else target_room_title_raw - ), - "目标直播间原始": target_room_title_raw, - "福袋内容": target_content, - "福袋价值": target_value, - "预计开奖时间": ft_str, - "预计开奖时间戳": int(ft), - "剩余秒数": int(ft - _now_ts()) if ft > _now_ts() else 0, - }, - ) - except Exception: - pass - - # 不在这里弹出队列,等待真正"参与完毕"后由上层统一处理 - try: - write_state_callback() - except Exception: - pass - - # 添加函数完成日志 - try: - from datetime import datetime - - elapsed = time.time() - start_time - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window 返回结果", - result="已触发回退", - future_ts=int(ft), - now_ts=int(now_ts), - remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0, - elapsed_seconds=round(elapsed, 3), - ) - except Exception: - pass + setup_lock_period_and_log_success( + analyser, + target, + ft, + now_ts, + poll_before, + target_room_title, + target_room_title_raw, + target_content, + target_value, + reservation_focus_until_ts_ref, + reservation_after_action_pending_ref, + start_time, + write_state_callback, + ) return True diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index d36fd63..bdb6bf6 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -105,6 +105,111 @@ def log_attend_window_detail_check( pass +def setup_lock_period_and_log_success( + analyser: Any, + target: ReservationItem, + ft: float, + now_ts: float, + poll_before: float, + target_room_title: str, + target_room_title_raw: str, + target_content: str, + target_value: float, + reservation_focus_until_ts_ref: List[float], + reservation_after_action_pending_ref: List[bool], + start_time: float, + write_state_callback: Callable[[], None], +) -> None: + """成功回退到目标直播间后:设置锁定期 + 写日志 + write_state + 写完成日志。 + + 流程: + 1. focus_until_ts = ft + poll_before(设置锁定期) + 2. reservation_after_action_pending = True + 3. 写"已回退到目标直播间"运行日志和"回退成功"行为日志 + 4. write_state_callback + 5. 写"已触发回退"完成日志 + """ + import time as _time + + # 进入锁定期:在开奖附近一段时间内不再切换直播间 + focus_until_new = ft + max(0.0, poll_before) + if reservation_focus_until_ts_ref: + reservation_focus_until_ts_ref[0] = focus_until_new + else: + reservation_focus_until_ts_ref.append(focus_until_new) + + if reservation_after_action_pending_ref: + reservation_after_action_pending_ref[0] = True + else: + reservation_after_action_pending_ref.append(True) + + try: + from datetime import datetime + + ft_str = ( + datetime.fromtimestamp(ft).strftime("%Y-%m-%d %H:%M:%S") + if ft > 0 + else "无效" + ) + + # 运行日志 + analyser._log_kv( + "预约", + f"[预约] 已回退到目标直播间,等待详情识别与参与 - 直播间:'{target_room_title[:30] if target_room_title else ''}', 开奖时间:{ft_str}, 价值:{target_value}元", + future_ts=int(ft), + future_str=ft_str, + focus_until=int(focus_until_new), + lock_period_seconds=int(focus_until_new - _time.time()), + remain_seconds_to_open=int(ft - _time.time()), + target_room_title=target_room_title, + target_room_title_raw=target_room_title_raw, + target_content=target_content, + target_value_yuan=target_value, + ) + + # 行为日志 + analyser._log_kv( + "行为", + "回退到预约直播间成功", + action="回退到预约直播间", + result="成功", + detail={ + "目标直播间": ( + target_room_title if target_room_title else target_room_title_raw + ), + "目标直播间原始": target_room_title_raw, + "福袋内容": target_content, + "福袋价值": target_value, + "预计开奖时间": ft_str, + "预计开奖时间戳": int(ft), + "剩余秒数": int(ft - _time.time()) if ft > _time.time() else 0, + }, + ) + except Exception: + pass + + # 不在这里弹出队列,等待真正"参与完毕"后由上层统一处理 + try: + write_state_callback() + except Exception: + pass + + # 添加函数完成日志 + try: + elapsed = _time.time() - start_time + analyser._log_kv( + "预约", + "[预约] process_reservation_attend_window 返回结果", + result="已触发回退", + future_ts=int(ft), + now_ts=int(now_ts), + remain_seconds_to_open=int(ft - now_ts) if ft > now_ts else 0, + elapsed_seconds=round(elapsed, 3), + ) + except Exception: + pass + + def wait_after_seek_and_verify(analyser: Any) -> float: """回退操作成功后:等待直播间加载完成(写日志 + delay),返回实际等待秒数。