Shortened up a few pieces of code in the battling methods
This commit is contained in:
parent
5072bebec0
commit
af02f64f3c
1 changed files with 42 additions and 43 deletions
|
@ -9,29 +9,24 @@ import random
|
|||
def battlingOff(**kwargs):
|
||||
player1 = kwargs.get('player1')
|
||||
player2 = kwargs.get('player2')
|
||||
if player1 is not None:
|
||||
battling = config.getContent('battling')
|
||||
|
||||
if player1:
|
||||
del battling[player1]
|
||||
elif player2:
|
||||
battling = {p1:p2 p1,p2 in battling.items() if not p2 == player2}
|
||||
|
||||
config.saveContent('battling',battling)
|
||||
elif player2 is not None:
|
||||
battling = config.getContent('battling')
|
||||
for p1_id,p2_id in battling.items():
|
||||
if p2_id == player2:
|
||||
del battling[p1_id]
|
||||
config.saveContent('battling',battling)
|
||||
break
|
||||
|
||||
|
||||
def userBattling(ctx):
|
||||
battling = config.getContent('battling')
|
||||
if battling is None:
|
||||
return False
|
||||
if str(ctx.command) == 'battle':
|
||||
if ctx.message.author.id in battling:
|
||||
if ctx.message.author.id in battling or ctx.message.author.id in battling.values():
|
||||
return True
|
||||
if str(ctx.command) == 'battle':
|
||||
return ctx.message.mentions[0].id in battling.values()
|
||||
else:
|
||||
return ctx.message.author.id in battling.values()
|
||||
|
||||
return False
|
||||
|
||||
|
@ -53,9 +48,7 @@ def updateBattleRecords(winner, loser):
|
|||
battles[loser.id] = record
|
||||
else:
|
||||
battles = {winner.id: "1-0", loser.id: "0-1"}
|
||||
if config.saveContent('battle_records', battles):
|
||||
return True
|
||||
return False
|
||||
return config.saveContent('battle_records', battles)
|
||||
|
||||
|
||||
class Interaction:
|
||||
|
@ -83,12 +76,12 @@ class Interaction:
|
|||
if userBattling(ctx):
|
||||
await self.bot.say("You or the person you are trying to battle is already in a battle!")
|
||||
return
|
||||
fmt = "{0.mention} has challenged you to a battle {1.mention}\n!accept or !decline"
|
||||
battling = config.getContent('battling')
|
||||
if battling is None:
|
||||
battling = {}
|
||||
|
||||
battling = config.getContent('battling') or {}
|
||||
battling[ctx.message.author.id] = ctx.message.mentions[0].id
|
||||
config.saveContent('battling',battling)
|
||||
|
||||
fmt = "{0.mention} has challenged you to a battle {1.mention}\n!accept or !decline"
|
||||
await self.bot.say(fmt.format(ctx.message.author, player2))
|
||||
t = Timer(180, battlingOff, {'player1':ctx.message.author.id})
|
||||
t.start()
|
||||
|
@ -100,22 +93,25 @@ class Interaction:
|
|||
if not userBattling(ctx):
|
||||
await self.bot.say("You are not currently in a battle!")
|
||||
return
|
||||
battling = config.getContent('battling')
|
||||
|
||||
battling = config.getContent('battling') or {}
|
||||
p1 = [p1_id for p1_id,p2_id in battling.items() if p2_id == ctx.message.author.id]
|
||||
if len(p1) == 0:
|
||||
await self.bot.say("You are not currently being challenged to a battle!")
|
||||
return
|
||||
|
||||
battleP1 = discord.utils.find(lambda m: m.id == p1[0],ctx.message.server.members)
|
||||
battleP2 = ctx.message.author
|
||||
for p1_id,p2_id in battling.items():
|
||||
if p2_id == ctx.message.author.id:
|
||||
battleP1 = discord.utils.find(lambda m: m.id == p1_id,ctx.message.server.members)
|
||||
num = random.randint(1, 100)
|
||||
|
||||
fmt = config.battleWins[random.randint(0, len(config.battleWins) - 1)]
|
||||
if num <= 50:
|
||||
|
||||
if random.randint(1, 100) < 50:
|
||||
await self.bot.say(fmt.format(battleP1.mention, battleP2.mention))
|
||||
if not updateBattleRecords(battleP1, battleP2):
|
||||
await self.bot.say("I was unable to save this data")
|
||||
battlingOff(player2 = ctx.message.author.id)
|
||||
elif num > 50:
|
||||
updateBattleRecords(battleP1, battleP2)
|
||||
else:
|
||||
await self.bot.say(fmt.format(battleP2.mention, battleP1.mention))
|
||||
if not updateBattleRecords(battleP2, battleP1):
|
||||
await self.bot.say("I was unable to save this data")
|
||||
updateBattleRecords(battleP2, battleP1)
|
||||
|
||||
battlingOff(player2 = ctx.message.author.id)
|
||||
|
||||
@commands.command(pass_context=True, no_pm=True)
|
||||
|
@ -125,13 +121,17 @@ class Interaction:
|
|||
if not userBattling(ctx):
|
||||
await self.bot.say("You are not currently in a battle!")
|
||||
return
|
||||
battleP1 = ctx.message.author
|
||||
for p1_id,p2_id in battling.items():
|
||||
if p2_id == ctx.message.author.id:
|
||||
battleP2 = discord.utils.find(lambda m: m.id == p2_id,ctx.message.server.members)
|
||||
|
||||
battling = config.getContent('battling') or {}
|
||||
p1 = [p1_id for p1_id,p2_id in battling.items() if p2_id == ctx.message.author.id]
|
||||
if len(p1) == 0:
|
||||
await self.bot.say("You are not currently being challenged to a battle!")
|
||||
return
|
||||
battleP1 = discord.utils.find(lambda m: m.id == p1[0],ctx.message.server.members)
|
||||
battleP2 = ctx.message.author
|
||||
|
||||
await self.bot.say("{0} has chickened out! {1} wins by default!".format(battleP2.mention, battleP1.mention))
|
||||
if not updateBattleRecords(battleP1, battleP2):
|
||||
await self.bot.say("I was unable to save this data")
|
||||
updateBattleRecords(battleP1, battleP2)
|
||||
battlingOff(player2 = ctx.message.author.id)
|
||||
|
||||
@commands.command(pass_context=True, no_pm=True)
|
||||
|
@ -152,9 +152,8 @@ class Interaction:
|
|||
await self.bot.say("Why the heck are you booping me? Get away from me >:c")
|
||||
return
|
||||
|
||||
boops = config.getContent('boops')
|
||||
if boops is None:
|
||||
boops = {}
|
||||
boops = config.getContent('boops') or {}
|
||||
|
||||
amount = 1
|
||||
booper_boops = boops.get(ctx.message.author.id)
|
||||
if booper_boops is None:
|
||||
|
|
Loading…
Reference in a new issue