Nextcord作战指挥机构及建议

Nextcord Fight Command Organization and Suggestions

我在 Nextcord 中创建了一个非常基本的战斗命令。命令是这样的:

/fight [your character] [your weapon] [your enemy] [enemy's weapon]

每个角色和武器都分配有自己的力量、速度和防御统计数据。并且统计数据全部加起来以找到他们的最终统计数据。然后它对敌人做同样的事情并计算结果。

我想做的是让我的代码更有条理,命令更复杂。例如,让总体统计数据较低的人由于速度更快而有机会获胜。目前我觉得很多输入都是手动的,所以制作新角色非常繁琐。有没有人有什么建议? 此外,我计划为某些角色和武器赋予某些统计数据负值。比如给武士刀负防御和spork负力量。

我当前的战斗齿轮代码:

class fight(commands.Cog):
    def __init__(self, client):
        self.client = client

    testServerID = 948362430256410685

    @nextcord.slash_command(name = "fight", description = "does fight stuff idk", guild_ids=[testServerID])
    async def fight(self, interaction: Interaction, char1, weapon1, char2, weapon2):
    # Speed            
        sDre = 3
        sHenry = 4
        sSonic = 10
        sNub = 3
        sGod = 5
        sPewdiepie = 6
    # Strength
        stDre = 3
        stHenry = 6
        stSonic = 5
        stNub = 4
        stGod = 5
        sPewdiepie = 6
    # Defense
        dDre = 6
        dHenry = 7
        dSonic = 5
        dNub = 5
        dGod = 5
        dPewdiepie = 6

    # WeaponStats
    # Speed
        sKatana = 6
        sKnife = 4
        sSpork = 1
        sNokia = 0
        sDiamondSword = 6
    # Strength
        stKatana = 3
        stKnife = 7
        stSpork = 6
        stNokia = 3
        stDiamondSword = 4
    # Defense
        dKatana = 1
        dKnife = 2
        dSpork = 2
        dNokia = 9
        dDiamondSword = 2

    # Char1
        if char1 == 'Dre':
            fighterstat = sDre+stDre+dDre
        if char1 == 'Henry':
            fighterstat = sHenry+stHenry+dHenry
        if char1 == 'Sonic':
            fighterstat = sSonic+stSonic+dSonic
        if char1 == 'Nub':
            fighterstat = sNub+stNub+dNub
        if char1 == 'God':
            fighterstat = sGod+stGod+dGod
        if char1 == 'Pewdiepie':
            fighterstat = sDre+stDre+dDre

    # Weapon1
        if weapon1 == 'Katana':
            weaponstat = sKatana+stKatana+dKatana
        if weapon1 == 'Knife':
            weaponstat = sKnife+stKnife+dKnife
        if weapon1 == 'Spork':
            weaponstat = sSpork+stSpork+dSpork
        if weapon1 == 'Nokia':
            weaponstat = sNokia+stNokia+dNokia
        if weapon1 == 'DiamondSword':
            weaponstat = sDiamondSword+stDiamondSword+dDiamondSword

    # Weapon2
        if weapon2 == 'Katana':
            eweaponstat = sKatana+stKatana+dKatana
        if weapon2 == 'Knife':
            eweaponstat = sKnife+stKnife+dKnife
        if weapon2 == 'Spork':
            eweaponstat = sSpork+stSpork+dSpork
        if weapon2 == 'Nokia':
            eweaponstat = sNokia+stNokia+dNokia
        if weapon2 == 'DiamondSword':
            eweaponstat = sDiamondSword+stDiamondSword+dDiamondSword

    # Char1
        if char2 == 'Dre':
            efighterstat = sDre+stDre+dDre
        if char2 == 'Henry':
            efighterstat = sHenry+stHenry+dHenry
        if char2 == 'Sonic':
            efighterstat = sSonic+stSonic+dSonic
        if char2 == 'Nub':
            efighterstat = sNub+stNub+dNub
        if char2 == 'God':
            efighterstat = sGod+stGod+dGod
        if char2 == 'Pewdiepie':
            efighterstat = sDre+stDre+dDre

        fFighter = fighterstat+weaponstat
        fEnemy = efighterstat+eweaponstat

        win = 'Your character has won the duel! Excellent choice'
        lose = 'Your character has lost, try again'
        tie = 'You both are equals, try different weapons'

        if fFighter < fEnemy:
            await interaction.response.send_message(lose)
        if fFighter > fEnemy:
            await interaction.response.send_message(win)
        if fFighter == fEnemy:
            await interaction.response.send_message(tie)

        print(fFighter)
        print(fFighter)

def setup(client):
    client.add_cog(fight(client))

如果您愿意将数据保存在字典中

class Fight(commands.Cog):
    
    testServerID = 948362430256410685

    char_speed = {           
        'Dre': 3,
        'Henry': 4,
        'Sonic': 10,
        'Nub': 3,
        'God': 5,
        'Pewdiepie': 6,
    }        

    char_strength = {
        'Dre': 3,
        'Henry': 6,
        'Sonic': 5,
        'Nub': 4,
        'God': 5,
        'Pewdiepie': 6,
    }

    char_defense = {
        'Dre': 6,
        'Henry': 7,
        'Sonic': 5,
        'Nub': 5,
        'God': 5,
        'Pewdiepie': 6,
    }

    # ... other ...

那么你可以写成

@nextcord.slash_command(name="fight", description="does fight stuff idk", guild_ids=[testServerID])
async def fight(self, interaction: Interaction, char1, weapon1, char2, weapon2):

    if (char1 in self.char_speed) and (char1 in self.char_strength) and (char1 in self.char_defense): 
        fighterstat = self.char_speed[char1] + self.char_strange[char1] + self.char_defense[char1]
    else:
        print('Unknow char1:', char1)

    # ... other ...

如果您需要更多字符,那么您只需向字典添加值 - 无需更改其余代码。

您甚至可以从文件中读取数据。或者您可以添加添加字符的 discord 命令。


最后你可以按角色和武器分组

class Fight(commands.Cog):
    
    testServerID = 948362430256410685
    
    character = {
        'Dre':   {'speed': 3,  'strength': 3, 'defense': 6},
        'Henry': {'speed': 4,  'strength': 6, 'defense': 7},
        'Sonic': {'speed': 10, 'strength': 5, 'defense': 5},
        'Nub':   {'speed': 3,  'strength': 4, 'defense': 5},
        'God':   {'speed': 5,  'strength': 5, 'defense': 5},
        'Pewdiepie': {'speed': 6, 'strength': 6, 'defense': 6},
    }        

    weapon = {
        'Katana': {'speed': 6,  'strength': 3, 'defense': 1},
        # ... code ...
    }

然后可以更简单

@nextcord.slash_command(name="fight", description="does fight stuff idk", guild_ids=[testServerID])
async def fight(self, interaction: Interaction, char1, weapon1, char2, weapon2):

    if char1 in self.character:
        data = self.character[char1]
        fighterstat = data['speed'] + data['strange'] + data['defense']
    else:
        print('Unknow char1:', char1)


    # ... other ...