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

合并 Dev n变更 #87

Merged
merged 2 commits into from
Dec 16, 2024
Merged

合并 Dev n变更 #87

merged 2 commits into from
Dec 16, 2024

Conversation

PackageInstaller
Copy link
Member

@PackageInstaller PackageInstaller commented Dec 16, 2024

Summary by Sourcery

通过引入新类来封装相关属性,重构玩家和Boss增益处理,并通过重新格式化和对齐来提高代码可读性。

增强功能:

  • 通过引入 BossBuffUserRandomBuff 类来封装相关属性,重构玩家和Boss增益处理。
  • 通过重新格式化和对齐代码块,以及在运算符和注释周围添加空格来提高代码可读性。
Original summary in English

Summary by Sourcery

Refactor the player and boss buff handling by introducing new classes to encapsulate related attributes, and improve code readability through reformatting and alignment.

Enhancements:

  • Refactor player and boss buff handling by introducing BossBuff and UserRandomBuff classes to encapsulate related attributes.
  • Improve code readability by reformatting and aligning code blocks, and adding spaces around operators and comments.

AnLanyoru and others added 2 commits December 16, 2024 08:51
修改了战斗临时buff存储方式到对象存储
Copy link

sourcery-ai bot commented Dec 16, 2024

审核指南由 Sourcery 提供

此拉取请求重构了玩家战斗和 Boss 战斗代码,通过引入适当的类结构来管理增益状态并清理代码格式,以提高代码的组织性和可维护性。

增益管理的类图

classDiagram
    class BossBuff {
        +int boss_zs
        +int boss_hx
        +int boss_bs
        +int boss_xx
        +int boss_jg
        +int boss_jh
        +int boss_jb
        +int boss_xl
    }

    class UserRandomBuff {
        +int random_break
        +int random_xx
        +int random_hx
        +int random_def
    }

    class UserBattleBuffDate {
        +String user_id
    }

    note for BossBuff "此类封装了 Boss 增益状态。"
    note for UserRandomBuff "此类封装了用户随机增益状态。"
    note for UserBattleBuffDate "此类管理用户战斗增益数据。"
Loading

文件级别的更改

更改 详情 文件
引入新的类结构来管理增益状态
  • 创建了 BossBuff 类以封装 Boss 增益属性
  • 创建了 UserRandomBuff 类以封装用户随机增益属性
  • 用类实例属性替换了全局变量
nonebot_plugin_xiuxian_2/xiuxian/xiuxian_utils/player_fight.py
重构增益处理函数以使用新的类结构
  • 更新了 get_turnatk() 以接受 BossBuff 和 UserRandomBuff 参数
  • 更新了 get_turnatk_boss() 以接受 BossBuff 参数
  • 修改了 after_atk_sub_buff_handle() 以使用新的增益类
nonebot_plugin_xiuxian_2/xiuxian/xiuxian_utils/player_fight.py
改进了代码格式和组织
  • 修复了整个文件的缩进和间距问题
  • 添加了适当的换行以提高可读性
  • 清理了注释和文档
nonebot_plugin_xiuxian_2/xiuxian/xiuxian_utils/player_fight.py

提示和命令

与 Sourcery 互动

  • 触发新审核: 在拉取请求中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审核评论。
  • 从审核评论生成 GitHub 问题: 通过回复审核评论请求 Sourcery 创建一个问题。
  • 生成拉取请求标题: 在拉取请求标题的任何地方写 @sourcery-ai 以随时生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任何地方写 @sourcery-ai summary 以随时生成 PR 摘要。您也可以使用此命令指定摘要应插入的位置。

自定义您的体验

访问您的仪表板以:

  • 启用或禁用审核功能,例如 Sourcery 生成的拉取请求摘要、审核指南等。
  • 更改审核语言。
  • 添加、删除或编辑自定义审核说明。
  • 调整其他审核设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request refactors the player fight and boss fight code to improve code organization and maintainability by introducing proper class structures for managing buff states and cleaning up the code formatting.

Class diagram for Buff Management

classDiagram
    class BossBuff {
        +int boss_zs
        +int boss_hx
        +int boss_bs
        +int boss_xx
        +int boss_jg
        +int boss_jh
        +int boss_jb
        +int boss_xl
    }

    class UserRandomBuff {
        +int random_break
        +int random_xx
        +int random_hx
        +int random_def
    }

    class UserBattleBuffDate {
        +String user_id
    }

    note for BossBuff "This class encapsulates the boss buff states."
    note for UserRandomBuff "This class encapsulates the user random buff states."
    note for UserBattleBuffDate "This class manages user battle buff data."
Loading

File-Level Changes

Change Details Files
Introduced new class structures to manage buff states
  • Created BossBuff class to encapsulate boss buff attributes
  • Created UserRandomBuff class to encapsulate user random buff attributes
  • Replaced global variables with class instance attributes
nonebot_plugin_xiuxian_2/xiuxian/xiuxian_utils/player_fight.py
Refactored buff handling functions to use the new class structures
  • Updated get_turnatk() to accept BossBuff and UserRandomBuff parameters
  • Updated get_turnatk_boss() to accept BossBuff parameter
  • Modified after_atk_sub_buff_handle() to use the new buff classes
nonebot_plugin_xiuxian_2/xiuxian/xiuxian_utils/player_fight.py
Improved code formatting and organization
  • Fixed indentation and spacing issues throughout the file
  • Added proper line breaks for improved readability
  • Cleaned up comments and documentation
nonebot_plugin_xiuxian_2/xiuxian/xiuxian_utils/player_fight.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@PackageInstaller PackageInstaller merged commit 4d61bc4 into master Dec 16, 2024
2 checks passed
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

@PackageInstaller - 我已经审查了你的更改 - 这里有一些反馈:

总体评论

  • 考虑在未来的PR中分解一些较长的函数,以进一步提高可维护性
这是我在审查期间查看的内容
  • 🟡 一般问题:发现5个问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我变得更有用!请点击每条评论上的👍或👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @PackageInstaller - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider breaking down some of the longer functions in future PRs to improve maintainability further
Here's what I looked at during the review
  • 🟡 General issues: 5 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

self.random_def = 0


empty_boss_buff = BossBuff()
Copy link

Choose a reason for hiding this comment

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

建议: 考虑将空buff转换为适当的单例常量

这些空的buff对象应该被定义为使用@Property装饰器的适当常量,或者使它们不可变以防止意外修改。考虑重命名为EMPTY_BOSS_BUFF以遵循Python常量命名约定。

建议实现:

from dataclasses import dataclass
from functools import cached_property


@dataclass(frozen=True)
class UserRandomBuff:
    """用户随机状态的不可变buff类"""
    @cached_property
    def random_break(self) -> int:
        return 0

    @cached_property
    def random_xx(self) -> int:
        return 0

    @cached_property
    def random_hx(self) -> int:
        return 0

    @cached_property
    def random_def(self) -> int:
        return 0


# 单例实例
EMPTY_USER_RANDOM_BUFF = UserRandomBuff()

你还需要:

  1. 将相同的模式应用于BossBuff类(我在提供的代码中看不到)
  2. 更新所有对empty_boss_buff的引用以使用EMPTY_BOSS_BUFF
  3. 更新所有对empty_ussr_random_buff的引用以使用EMPTY_USER_RANDOM_BUFF
  4. 向BossBuff类添加类似的数据类和属性装饰器
Original comment in English

suggestion: Consider making empty buffs into proper singleton constants

These empty buff objects should be defined as proper constants using @Property decorators or making them immutable to prevent accidental modification. Consider renaming to EMPTY_BOSS_BUFF to follow Python naming conventions for constants.

Suggested implementation:

from dataclasses import dataclass
from functools import cached_property


@dataclass(frozen=True)
class UserRandomBuff:
    """Immutable buff class for user random stats"""
    @cached_property
    def random_break(self) -> int:
        return 0

    @cached_property
    def random_xx(self) -> int:
        return 0

    @cached_property
    def random_hx(self) -> int:
        return 0

    @cached_property
    def random_def(self) -> int:
        return 0


# Singleton instances
EMPTY_USER_RANDOM_BUFF = UserRandomBuff()

You'll also need to:

  1. Apply the same pattern to the BossBuff class (which I can't see in the provided code)
  2. Update all references to empty_boss_buff to use EMPTY_BOSS_BUFF
  3. Update all references to empty_ussr_random_buff to use EMPTY_USER_RANDOM_BUFF
  4. Add similar dataclass and property decorators to the BossBuff class


# boss["减伤"] = random.randint(40,90)/100 # boss减伤率
boss["减伤"] = 0.05 # boss减伤率
boss_st1 = random.randint(0, 100) # boss神通1
Copy link

Choose a reason for hiding this comment

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

建议: 将魔法数字提取为命名常量

概率范围(0-25, 26-50等)和buff值应该提取为模块级别的命名常量。这将使逻辑更易于维护和调整。

建议实现:

# Boss buff 概率范围
BOSS_ATTACK_PROB_MIN = 0
BOSS_ATTACK_PROB_MAX = 25
BOSS_CRIT_PROB_MIN = 26
BOSS_CRIT_PROB_MAX = 50
BOSS_THIRD_PROB_MIN = 51
BOSS_THIRD_PROB_MAX = 75

# Boss buff 值
BOSS_DAMAGE_REDUCTION = 0.05
BOSS_ATTACK_BUFF = 1.0
BOSS_CRIT_BUFF = 0.7
BOSS_NO_BUFF = 0.0

        boss["减伤"] = BOSS_DAMAGE_REDUCTION  # boss减伤率
        boss_st1 = random.randint(0, 100)  # boss神通1
        if BOSS_ATTACK_PROB_MIN <= boss_st1 <= BOSS_ATTACK_PROB_MAX:
            boss_buff.boss_zs = BOSS_ATTACK_BUFF  # boss攻击
            boss_buff.boss_hx = BOSS_NO_BUFF
            boss_buff.boss_bs = BOSS_NO_BUFF
            boss_buff.boss_xx = BOSS_NO_BUFF
        elif BOSS_CRIT_PROB_MIN <= boss_st1 <= BOSS_CRIT_PROB_MAX:
            boss_buff.boss_zs = BOSS_NO_BUFF
            boss_buff.boss_hx = BOSS_CRIT_BUFF  # boss会心
            boss_buff.boss_bs = BOSS_NO_BUFF
            boss_buff.boss_xx = BOSS_NO_BUFF
        elif BOSS_THIRD_PROB_MIN <= boss_st1 <= BOSS_THIRD_PROB_MAX:

常量应该添加在模块级别(文件顶部)。确保没有任何现有的常量与这些名称相同。你可能需要调整常量名称以更好地匹配你的项目命名约定。

Original comment in English

suggestion: Extract magic numbers into named constants

The probability ranges (0-25, 26-50 etc) and buff values should be extracted into named constants at the module level. This would make the logic more maintainable and easier to adjust.

Suggested implementation:

# Boss buff probability ranges
BOSS_ATTACK_PROB_MIN = 0
BOSS_ATTACK_PROB_MAX = 25
BOSS_CRIT_PROB_MIN = 26
BOSS_CRIT_PROB_MAX = 50
BOSS_THIRD_PROB_MIN = 51
BOSS_THIRD_PROB_MAX = 75

# Boss buff values
BOSS_DAMAGE_REDUCTION = 0.05
BOSS_ATTACK_BUFF = 1.0
BOSS_CRIT_BUFF = 0.7
BOSS_NO_BUFF = 0.0

        boss["减伤"] = BOSS_DAMAGE_REDUCTION  # boss减伤率
        boss_st1 = random.randint(0, 100)  # boss神通1
        if BOSS_ATTACK_PROB_MIN <= boss_st1 <= BOSS_ATTACK_PROB_MAX:
            boss_buff.boss_zs = BOSS_ATTACK_BUFF  # boss攻击
            boss_buff.boss_hx = BOSS_NO_BUFF
            boss_buff.boss_bs = BOSS_NO_BUFF
            boss_buff.boss_xx = BOSS_NO_BUFF
        elif BOSS_CRIT_PROB_MIN <= boss_st1 <= BOSS_CRIT_PROB_MAX:
            boss_buff.boss_zs = BOSS_NO_BUFF
            boss_buff.boss_hx = BOSS_CRIT_BUFF  # boss会心
            boss_buff.boss_bs = BOSS_NO_BUFF
            boss_buff.boss_xx = BOSS_NO_BUFF
        elif BOSS_THIRD_PROB_MIN <= boss_st1 <= BOSS_THIRD_PROB_MAX:

The constants should be added at the module level (top of the file). Make sure there aren't any existing constants with the same names. You may want to adjust the constant names to better match your project's naming conventions.

boss_buff.boss_jb = 0
boss_buff.boss_xl = 1 # boss禁蓝

if convert_rank('遁一境初期')[0] < convert_rank((boss["jj"] + '中期'))[0] < convert_rank('江湖好手')[0]: # 遁一以下
Copy link

Choose a reason for hiding this comment

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

建议: 分解复杂的boss buff分配逻辑

boss buff分配逻辑嵌套很深且难以理解。考虑将每个等级的buff逻辑提取到单独的函数中,如apply_boss_buffs_for_rank(),以提高可读性和可维护性。

建议实现:

            apply_first_tier_buffs(boss_buff)
        elif 76 <= boss_st2 <= 100:
            apply_second_tier_buffs(boss_buff)
        elif 51 <= boss_st1 <= 75:
            apply_third_tier_buffs(boss_buff)
        elif 75 <= boss_st1 <= 100:
            apply_fourth_tier_buffs(boss_buff)

def apply_first_tier_buffs(boss_buff):
    """为第一层boss应用buff"""
    boss_buff.boss_jg = 0
    boss_buff.boss_jh = 0
    boss_buff.boss_jb = 1.5  # boss降暴
    boss_buff.boss_xl = 0

def apply_second_tier_buffs(boss_buff):
    """为第二层boss应用buff"""
    boss_buff.boss_jg = 0
    boss_buff.boss_jh = 0
    boss_buff.boss_jb = 0
    boss_buff.boss_xl = 1  # boss禁蓝
    boss_buff.boss_hx = 0.7  # boss会心
    boss_buff.boss_bs = 0
    boss_buff.boss_xx = 0

def apply_third_tier_buffs(boss_buff):
    """为第三层boss应用buff"""
    boss_buff.boss_zs = 0
    boss_buff.boss_hx = 0
    boss_buff.boss_bs = 2  # boss暴伤
    boss_buff.boss_xx = 0

def apply_fourth_tier_buffs(boss_buff):
    """为第四层boss应用buff"""
    boss_buff.boss_zs = 0

请注意,此重构假设:

  1. boss_buff对象在代码的其他地方正确定义
  2. boss_st1和boss_st2变量仍然在作用域内
  3. 你可能想要根据每个等级在游戏逻辑中代表的内容添加更具描述性的函数名称
  4. 你可能想要在这些函数中添加错误处理或验证
Original comment in English

suggestion: Break down complex boss buff assignment logic

The boss buff assignment logic is deeply nested and hard to follow. Consider extracting each rank's buff logic into separate functions like apply_boss_buffs_for_rank() to improve readability and maintainability.

Suggested implementation:

            apply_first_tier_buffs(boss_buff)
        elif 76 <= boss_st2 <= 100:
            apply_second_tier_buffs(boss_buff)
        elif 51 <= boss_st1 <= 75:
            apply_third_tier_buffs(boss_buff)
        elif 75 <= boss_st1 <= 100:
            apply_fourth_tier_buffs(boss_buff)

def apply_first_tier_buffs(boss_buff):
    """Apply buffs for first tier boss"""
    boss_buff.boss_jg = 0
    boss_buff.boss_jh = 0
    boss_buff.boss_jb = 1.5  # boss降暴
    boss_buff.boss_xl = 0

def apply_second_tier_buffs(boss_buff):
    """Apply buffs for second tier boss"""
    boss_buff.boss_jg = 0
    boss_buff.boss_jh = 0
    boss_buff.boss_jb = 0
    boss_buff.boss_xl = 1  # boss禁蓝
    boss_buff.boss_hx = 0.7  # boss会心
    boss_buff.boss_bs = 0
    boss_buff.boss_xx = 0

def apply_third_tier_buffs(boss_buff):
    """Apply buffs for third tier boss"""
    boss_buff.boss_zs = 0
    boss_buff.boss_hx = 0
    boss_buff.boss_bs = 2  # boss暴伤
    boss_buff.boss_xx = 0

def apply_fourth_tier_buffs(boss_buff):
    """Apply buffs for fourth tier boss"""
    boss_buff.boss_zs = 0

Note that this refactoring assumes:

  1. The boss_buff object is properly defined elsewhere in the code
  2. The boss_st1 and boss_st2 variables are still available in the scope
  3. You may want to add more descriptive function names based on what each tier represents in your game logic
  4. You might want to add error handling or validation inside these functions


def get_turnatk(player, buff=0, user_battle_buff_date={}): #辅修功法14
def get_turnatk(player, buff=0, user_battle_buff_date={},
Copy link

Choose a reason for hiding this comment

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

问题 (bug_risk): 避免使用可变的默认参数

使用空字典作为默认参数是危险的,因为它将在函数调用之间共享。使用None作为默认值,并在需要时在函数内部创建一个新的字典。

Original comment in English

issue (bug_risk): Avoid using mutable default arguments

Using an empty dict as a default argument is dangerous as it will be shared between function calls. Use None as the default and create a new dict inside the function if needed.

@@ -1653,7 +1703,9 @@


# 处理攻击后辅修功法效果
def after_atk_sub_buff_handle(player1_sub_open, player1, user1_main_buff_data, subbuffdata1, damage1, player2):
def after_atk_sub_buff_handle(player1_sub_open, player1, user1_main_buff_data, subbuffdata1, damage1, player2,
Copy link

Choose a reason for hiding this comment

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

建议: 考虑将相关参数分组到一个上下文对象中

此函数有许多密切相关的参数。考虑创建一个BattleContext类来保存状态,并传递它以减少参数数量并提高可维护性。

建议实现:

class BattleContext:
    def __init__(self, 
                 attacker_sub_open: bool,
                 attacker: dict,
                 attacker_main_buff_data: dict,
                 attacker_sub_buff_data: dict,
                 damage: int,
                 defender: dict,
                 boss_buff: BossBuff = empty_boss_buff,
                 random_buff: UserRandomBuff = empty_boss_buff):
        self.attacker_sub_open = attacker_sub_open
        self.attacker = attacker
        self.attacker_main_buff_data = attacker_main_buff_data
        self.attacker_sub_buff_data = attacker_sub_buff_data
        self.damage = damage
        self.defender = defender
        self.boss_buff = boss_buff
        self.random_buff = random_buff

# 处理攻击后辅修功法效果
def after_atk_sub_buff_handle(battle_ctx: BattleContext):
    msg = ""

    if not battle_ctx.attacker_sub_open:

    if 'max_hp' not in battle_ctx.attacker:
        exp = int(battle_ctx.attacker['exp'])
        battle_ctx.attacker['max_hp'] = int(exp / 2) * (
            1 + battle_ctx.attacker_main_buff_data.get('hpbuff', 0) + impart_hp_per_1 if battle_ctx.attacker_main_buff_data is not None else 0)

你需要:

  1. 更新所有对after_atk_sub_buff_handle的调用以创建并传递一个BattleContext对象
  2. 更新函数体的其余部分以使用battle_ctx而不是单个参数
  3. 如果对这些参数执行了常见操作,考虑向BattleContext类添加辅助方法
  4. 导入类型提示中使用的任何必要类型(如BossBuff和UserRandomBuff)
  5. 更新任何文档或注释以反映新的参数结构
Original comment in English

suggestion: Consider grouping related parameters into a context object

This function has many parameters that are closely related. Consider creating a BattleContext class to hold the state and passing that instead to reduce parameter count and improve maintainability.

Suggested implementation:

class BattleContext:
    def __init__(self, 
                 attacker_sub_open: bool,
                 attacker: dict,
                 attacker_main_buff_data: dict,
                 attacker_sub_buff_data: dict,
                 damage: int,
                 defender: dict,
                 boss_buff: BossBuff = empty_boss_buff,
                 random_buff: UserRandomBuff = empty_boss_buff):
        self.attacker_sub_open = attacker_sub_open
        self.attacker = attacker
        self.attacker_main_buff_data = attacker_main_buff_data
        self.attacker_sub_buff_data = attacker_sub_buff_data
        self.damage = damage
        self.defender = defender
        self.boss_buff = boss_buff
        self.random_buff = random_buff

# 处理攻击后辅修功法效果
def after_atk_sub_buff_handle(battle_ctx: BattleContext):
    msg = ""

    if not battle_ctx.attacker_sub_open:

    if 'max_hp' not in battle_ctx.attacker:
        exp = int(battle_ctx.attacker['exp'])
        battle_ctx.attacker['max_hp'] = int(exp / 2) * (
            1 + battle_ctx.attacker_main_buff_data.get('hpbuff', 0) + impart_hp_per_1 if battle_ctx.attacker_main_buff_data is not None else 0)

You'll need to:

  1. Update all calls to after_atk_sub_buff_handle to create and pass a BattleContext object
  2. Update the rest of the function body to use battle_ctx instead of individual parameters
  3. Consider adding helper methods to BattleContext class if there are common operations performed on these parameters
  4. Import any necessary types used in type hints (like BossBuff and UserRandomBuff)
  5. Update any documentation or comments to reflect the new parameter structure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants