I created an extremely basic fight command in Nextcord. The command is like so:
/fight [your character] [your weapon] [your enemy] [enemy's weapon]
Each character and weapon is assigned their own strength, speed, and defense stats. And the stats are all added up to find their final stat. Then it does the same for the enemy and it calculates the outcome.
What I would like to do is make my code more organized, and the command more intricate. For example giving someone who has overall lower statistics a chance to win due to being faster. Currently I feel like a lot of the entering is manual so making new characters is very tedious. Does anyone have any suggestions? Also, I am planning on giving certain characters and weapons negative values for certain stats. Such as giving the katana negative defense and spork negative strength.
My current code for the fight cog:
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))
Advertisement
Answer
If you would keep data in dictionaries
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 ...
then you could write it as
@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 ...
and if you would need more characters then you would have to only add values to dictionares – without changing rest of code.
You could even read data from file. Or you could add discord command which adds character.
EVentually you could group by character and weapon
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 ... }
and then it could be simpler
@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 ...