mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-18 15:33:03 +08:00
fix(LibTimeSelector) style(LibReserve): 修复时间转换方法 _timeToMins 并重命名为 _timeStrToMins
- 之前的实现未严格限制传入参数为整形,导致在转换时间字符串时可能出现类型错误。 - 重命名为 _timeStrToMins 以明确表示该方法仅用于时间字符串转换。并更新相关调用。 - 重命名 __selectSeatTime 中的冗长局部变量,便于理解和维护。 - 删除多余的时间格式转换嗲用
This commit is contained in:
+37
-32
@@ -190,10 +190,10 @@ class LibReserve(LibTimeSelector):
|
||||
if reserve_info.get("end_time") is None:
|
||||
reserve_info["end_time"] = {}
|
||||
if "time" not in reserve_info["end_time"]:
|
||||
end_mins = self._timeToMins(reserve_info["begin_time"]["time"])
|
||||
end_mins = self._timeStrToMins(reserve_info["begin_time"]["time"])
|
||||
end_mins = end_mins + int(reserve_info["expect_duration"]*60)
|
||||
reserve_info["end_time"] = {
|
||||
"time": self._minsToTime(end_mins),
|
||||
"time": self._minsToTimeStr(end_mins),
|
||||
"max_diff": 30,
|
||||
"prefer_early": False
|
||||
}
|
||||
@@ -215,8 +215,8 @@ class LibReserve(LibTimeSelector):
|
||||
):
|
||||
|
||||
begin_time, end_time = reserve_info["begin_time"], reserve_info["end_time"]
|
||||
begin_mins = self._timeToMins(begin_time["time"])
|
||||
end_mins = self._timeToMins(end_time["time"])
|
||||
begin_mins = self._timeStrToMins(begin_time["time"])
|
||||
end_mins = self._timeStrToMins(end_time["time"])
|
||||
# if end time is earlier than begin_time, exchange them
|
||||
if end_mins < begin_mins:
|
||||
self._showTrace(
|
||||
@@ -225,15 +225,15 @@ class LibReserve(LibTimeSelector):
|
||||
reserve_info["end_time"] = begin_time
|
||||
reserve_info["begin_time"] = end_time
|
||||
begin_time, end_time = reserve_info["begin_time"], reserve_info["end_time"]
|
||||
begin_mins = self._timeToMins(begin_time["time"])
|
||||
end_mins = self._timeToMins(end_time["time"])
|
||||
begin_mins = self._timeStrToMins(begin_time["time"])
|
||||
end_mins = self._timeStrToMins(end_time["time"])
|
||||
# ensure the end time is not later than 23:30
|
||||
if end_mins > self._timeToMins("23:30"):
|
||||
if end_mins > self._timeStrToMins("23:30"):
|
||||
self._showTrace(
|
||||
f"结束时间 {end_time['time']} 晚于 23:30, 自动设置为 23:30"
|
||||
)
|
||||
reserve_info["end_time"]["time"] = "23:30"
|
||||
end_mins = self._timeToMins("23:30")
|
||||
end_mins = self._timeStrToMins("23:30")
|
||||
# ensure the duration is not longer than 8 hours
|
||||
if reserve_info["satisfy_duration"]:
|
||||
if reserve_info["expect_duration"] > 8:
|
||||
@@ -250,7 +250,7 @@ class LibReserve(LibTimeSelector):
|
||||
f"{float((end_mins - begin_mins)/60)} 小时 "
|
||||
f"超出最大时长 8 小时, 自动设置为 8 小时"
|
||||
)
|
||||
reserve_info["end_time"]["time"] = self._minsToTime(begin_mins + 8*60)
|
||||
reserve_info["end_time"]["time"] = self._minsToTimeStr(begin_mins + 8*60)
|
||||
return True
|
||||
|
||||
|
||||
@@ -481,6 +481,9 @@ class LibReserve(LibTimeSelector):
|
||||
|
||||
"""
|
||||
Select the nearest available time option.
|
||||
|
||||
Returns:
|
||||
int: The actual selected time value in minutes.
|
||||
"""
|
||||
# Wait for time options to load
|
||||
try:
|
||||
@@ -515,7 +518,7 @@ class LibReserve(LibTimeSelector):
|
||||
)
|
||||
return target_time
|
||||
self._showTrace(
|
||||
f"无法选择最近的 {time_type} {self._minsToTime(target_time)}, "
|
||||
f"无法选择最近的 {time_type} {self._minsToTimeStr(target_time)}, "
|
||||
f"所有可选时间与目标时间相差都超过 {max_time_diff} 分钟"
|
||||
)
|
||||
self._showTrace(f"当前可供预约的 {time_type} 有: {free_times}")
|
||||
@@ -530,47 +533,49 @@ class LibReserve(LibTimeSelector):
|
||||
satisfy_duration: bool = True
|
||||
) -> bool:
|
||||
|
||||
"""Select seat begin and end time."""
|
||||
expect_begin_time = actual_begin_time = begin_time["time"]
|
||||
expect_end_time = actual_end_time = end_time["time"]
|
||||
expect_begin_mins = self._timeToMins(expect_begin_time)
|
||||
actual_begin_mins = expect_begin_mins
|
||||
expect_end_mins = self._timeToMins(expect_end_time)
|
||||
"""
|
||||
Select seat begin and end time.
|
||||
"""
|
||||
exp_beg_tm_str = act_beg_tm_str = begin_time["time"]
|
||||
exp_end_tm_str = act_end_tm_str = end_time["time"]
|
||||
exp_beg_mins = self._timeStrToMins(exp_beg_tm_str)
|
||||
act_beg_mins = exp_beg_mins
|
||||
exp_end_mins = self._timeStrToMins(exp_end_tm_str)
|
||||
act_end_mins = exp_end_mins
|
||||
|
||||
# Select begin time
|
||||
if self.__selectNearestTime(
|
||||
if act_beg_mins := self.__selectNearestTime(
|
||||
time_id="startTime",
|
||||
time_type="开始时间",
|
||||
target_time=expect_begin_mins,
|
||||
target_time=exp_beg_mins,
|
||||
max_time_diff=begin_time["max_diff"],
|
||||
prefer_earlier=begin_time["prefer_early"]
|
||||
) == -1:
|
||||
) and act_beg_mins == -1:
|
||||
return False
|
||||
actual_begin_time = self._minsToTime(expect_begin_mins)
|
||||
actual_begin_mins = self._timeToMins(actual_begin_time)
|
||||
act_beg_tm_str = self._minsToTimeStr(act_beg_mins)
|
||||
|
||||
# If 'satisfy_duration' is True, select end time based on actual begin time
|
||||
if satisfy_duration:
|
||||
expect_end_mins = self.validateAndAdjustEndTime(actual_begin_mins, expct_duration)
|
||||
expect_end_time = self._minsToTime(expect_end_mins)
|
||||
exp_end_mins = int(self.validateAndAdjustEndTime(act_beg_mins, expct_duration))
|
||||
exp_end_tm_str = self._minsToTimeStr(exp_end_mins)
|
||||
self._showTrace(
|
||||
f"需要满足期望预约持续时间: {expct_duration} 小时, "
|
||||
f"根据开始时间 {actual_begin_time} 计算结束时间: {expect_end_time}"
|
||||
f"根据开始时间 {act_beg_tm_str} 计算结束时间: {exp_end_tm_str}"
|
||||
)
|
||||
|
||||
# Select end time
|
||||
if self.__selectNearestTime(
|
||||
if act_end_mins := self.__selectNearestTime(
|
||||
time_id="endTime",
|
||||
time_type="结束时间",
|
||||
target_time=expect_end_mins,
|
||||
target_time=exp_end_mins,
|
||||
max_time_diff=end_time["max_diff"],
|
||||
prefer_earlier=end_time["prefer_early"]
|
||||
) == -1:
|
||||
) and act_end_mins == -1:
|
||||
return False
|
||||
actual_end_time = self._minsToTime(expect_end_mins)
|
||||
act_end_tm_str = self._minsToTimeStr(act_end_mins)
|
||||
self._showTrace(
|
||||
f"期望预约时间段: {expect_begin_time} - {expect_end_time}, "
|
||||
f"实际预约时间段: {actual_begin_time} - {actual_end_time}"
|
||||
f"期望预约时间段: {exp_beg_tm_str} - {exp_end_tm_str}, "
|
||||
f"实际预约时间段: {act_beg_tm_str} - {act_end_tm_str}"
|
||||
)
|
||||
return True
|
||||
|
||||
@@ -584,8 +589,8 @@ class LibReserve(LibTimeSelector):
|
||||
"""
|
||||
Validate and adjust reserve end time to library closing time if needed.
|
||||
"""
|
||||
LIBRARY_CLOSE_TIME = self._timeToMins("23:30")
|
||||
expect_end_mins = begin_mins + duration * 60
|
||||
LIBRARY_CLOSE_TIME = self._timeStrToMins("23:30")
|
||||
expect_end_mins = int(begin_mins + duration*60)
|
||||
if expect_end_mins > LIBRARY_CLOSE_TIME:
|
||||
expect_end_mins = LIBRARY_CLOSE_TIME
|
||||
self._showTrace(
|
||||
|
||||
Reference in New Issue
Block a user