Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 判断胖次活动是否开启, 没开启时不识别胖次掉落 #160

Merged
merged 3 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions autowsgr/game/game_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ def click_result(timer: Timer, max_times=1):
timer.wait_images(IMG.fight_image[14])
looted = False
while timer.wait_image(IMG.fight_image[14], timeout=0.5):
if not looted:
if timer.can_get_loot and not looted:
if timer.image_exist(IMG.fight_result['LOOT'], need_screen_shot=True):
timer.got_loot_num += 1
timer.logger.info(f'捞到胖次! 当前胖次数:{timer.got_loot_num}')
looted = True
else:
timer.logger.debug('没有识别到胖次')
else:
timer.logger.debug('已经识别过胖次')
timer.click(915, 515, delay=0.25, times=1)


Expand Down
7 changes: 6 additions & 1 deletion autowsgr/game/get_game_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,17 @@ def get_loot_and_ship(timer: Timer):
try:
timer.got_loot_num = ret.get('loot')
if timer.got_loot_num is None:
timer.can_get_loot = False
timer.got_loot_num = 0
except:
timer.logger.warning('赋值给got_loot_num失败')
timer.can_get_loot = False
timer.got_loot_num = 0
timer.logger.info(f'已掉落胖次:{timer.got_loot_num}')
timer.logger.info(f'已掉落舰船:{timer.got_ship_num}')
if timer.can_get_loot:
timer.logger.info(f'已掉落胖次:{timer.got_loot_num}')
else:
timer.logger.info('胖次活动未开启, 无法捞胖次')
return ret


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 代码改进的必要性:该修改添加了对can_get_loot属性的处理,用于判断是否可以进行捞胖次活动。这是一个合理的改进,因为它增加了对活动状态的判断,避免了在不合适的情况下记录无意义的日志信息。

  2. 逻辑错误或潜在运行时错误:没有明显的逻辑错误或运行时错误。can_get_loot属性在异常情况下也被正确设置为False,确保了代码的健壮性。

  3. 兼容性问题:该修改不会破坏现有逻辑。can_get_loot属性的引入不会影响其他部分的代码,且在没有该属性的旧代码中也不会引发错误。

  4. 代码结构优化:代码结构已经比较简洁,但可以考虑将can_get_loot的初始值在Timer类的构造函数中明确初始化,以避免潜在的未定义行为。

  5. 潜在性能问题:没有明显的性能问题。日志记录操作的开销很小,不会对性能产生显著影响。

  6. 安全性问题:没有涉及敏感信息泄露或运行外部代码的风险。

  7. 改进建议

    • 建议在Timer类的构造函数中初始化can_get_loot属性,以确保它在所有情况下都有一个明确的初始值。
    • 可以考虑将can_get_loot的默认值设置为True,以便在没有明确设置的情况下,代码仍然可以正常工作。

综上所述,代码修改是合理的,但可以进一步优化以确保代码的健壮性。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 该修改有必要,添加了对can_get_loot属性的处理,使得代码能够更好地处理“胖次活动未开启”的情况,提升了代码的健壮性和用户体验。

  2. 没有明显的逻辑错误或潜在的运行时错误。修改后的代码在try块中增加了对can_get_loot的处理,并在except块中也进行了相应的处理,确保了异常情况下的逻辑一致性。

  3. 没有与其他代码部分的兼容性问题。修改后的代码逻辑清晰,不会破坏与修改无关的现有逻辑。

  4. 代码结构可以进一步优化。例如,timer.got_loot_num = 0的赋值在try块和except块中重复出现,可以考虑将其提取到finally块中,以减少代码重复。

  5. 没有潜在的性能问题。修改后的代码只是增加了简单的条件判断和日志输出,不会对性能产生显著影响。

  6. 没有敏感信息泄露的风险。代码中没有涉及硬编码的密钥、密码等敏感信息。

  7. 没有运行来路不明的外部代码的风险。代码中所有操作都是基于传入的参数和对象属性,没有涉及外部代码的执行。

改进建议:

  • timer.got_loot_num = 0的赋值提取到finally块中,以减少代码重复。
  • 考虑在try块中增加对ret是否为None的检查,以避免潜在的NoneType错误。
try:
    timer.got_loot_num = ret.get('loot')
    if timer.got_loot_num is None:
        timer.can_get_loot = False
except:
    timer.logger.warning('赋值给got_loot_num失败')
    timer.can_get_loot = False
finally:
    timer.got_loot_num = 0

timer.logger.info(f'已掉落舰船:{timer.got_ship_num}')
if timer.can_get_loot:
    timer.logger.info(f'已掉落胖次:{timer.got_loot_num}')
else:
    timer.logger.info('胖次活动未开启, 无法捞胖次')
return ret

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 代码改进的必要性
    该修改是有必要的,新增了timer.can_get_loot的逻辑,用于判断是否可以获取“胖次”掉落。相比旧代码,新增了对活动是否开启的判断,并相应地调整了日志输出,提升了代码的健壮性和可读性。

  2. 逻辑错误或潜在运行时错误
    没有明显的逻辑错误或运行时错误。timer.can_get_loot的引入是合理的,且在异常处理中也正确地设置了timer.can_get_loot = False

  3. 兼容性问题
    没有发现与其他代码部分的兼容性问题。修改后的代码不会破坏现有逻辑,且新增的逻辑与原有功能是兼容的。

  4. 代码结构优化
    代码结构清晰,逻辑合理。不过可以考虑将timer.can_get_loot的初始化放在函数开头,以确保在异常情况下也能正确设置默认值。例如:

    timer.can_get_loot = True  # 默认设置为True
    try:
        timer.got_loot_num = ret.get('loot')
        if timer.got_loot_num is None:
            timer.can_get_loot = False
            timer.got_loot_num = 0
    except:
        timer.logger.warning('赋值给got_loot_num失败')
        timer.can_get_loot = False
        timer.got_loot_num = 0
  5. 潜在性能问题
    没有明显的性能问题。新增的逻辑对性能影响可以忽略不计。

  6. 敏感信息泄露风险
    没有发现敏感信息泄露的风险。

  7. 运行来路不明的外部代码
    没有运行来路不明的外部代码。

改进建议

  • 建议在函数开头初始化timer.can_get_loot = True,以确保在异常情况下也能正确设置默认值。
  • 可以考虑将timer.can_get_loot的判断逻辑提取到一个单独的函数中,以提高代码的可读性和复用性。

综上,代码修改是合理的,但可以通过上述建议进一步优化。

Expand Down
5 changes: 3 additions & 2 deletions autowsgr/scripts/daily_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ def _get_unfinished_plan_description(self, i: int) -> str:
plan_str += f'正在执行的PLAN: {self.fight_complete_times[i][2]}, '
plan_str += f'已出击次数/目标次数: {self.fight_complete_times[i][0]}/{self.fight_complete_times[i][1]}, '
plan_str += f'消耗快修数量: {self.timer.quick_repaired_cost}, '
plan_str += f'已掉落船数量: {self.timer.got_ship_num}, '
plan_str += f'已掉落胖次数量: {self.timer.got_loot_num}'
plan_str += f'已掉落船数量: {self.timer.got_ship_num}'
if self.timer.can_get_loot:
plan_str += f', 已掉落胖次数量: {self.timer.got_loot_num}'
return plan_str

def _get_unfinished(self) -> int:
Expand Down
1 change: 1 addition & 0 deletions autowsgr/timer/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, config: UserConfig, logger: Logger) -> None:
self.got_ship_num = 0 # 当天已掉落的船
self.got_loot_num = 0 # 当天已掉落的胖次
self.quick_repaired_cost = 0 # 消耗快修数量
self.can_get_loot = True # 是否可以捞胖次
"""
以上时能用到的
以下是暂时用不到的
Expand Down
Loading