Support multiple bets in one go with !roulette in coins.py

This commit is contained in:
jesopo 2018-08-16 11:54:51 +01:00
parent 8ef02fdbd8
commit 84a793caf2

View file

@ -190,28 +190,30 @@ class Module(object):
event["timer"].redo() event["timer"].redo()
def roulette(self, event): def roulette(self, event):
bet = event["args_split"][0].lower() bets = event["args_split"][0].lower().split(",")
if bet == "0": if "0" in bets:
event["stderr"].write("You can't bet on 0") event["stderr"].write("You can't bet on 0")
return return
bet_amount = event["args_split"][1].lower() bet_amounts = [amount.lower() for amount in event["args_split"][1:]]
if bet_amount == "all": if len(bet_amounts) == 1 and bet_amounts[0] == "all":
bet_amount = event["user"].get_setting("coins", "0.0") bet_amounts[0] = event["user"].get_setting("coins", "0.0")
if decimal.Decimal(bet_amount) <= DECIMAL_ZERO: if decimal.Decimal(bet_amounts[0]) <= DECIMAL_ZERO:
event["stderr"].write("You have no coins to bet") event["stderr"].write("You have no coins to bet")
return return
for i, bet_amount in enumerate(bet_amounts):
match = REGEX_FLOAT.match(bet_amount) match = REGEX_FLOAT.match(bet_amount)
if not match or round(decimal.Decimal(bet_amount), 2 if not match or round(decimal.Decimal(bet_amount), 2
) <= DECIMAL_ZERO: ) <= DECIMAL_ZERO:
event["stderr"].write( event["stderr"].write(
"Please provide a positive number of coins to bet") "Please provide a positive number of coins to bet")
return return
bet_amount = decimal.Decimal(match.group(0)) bet_amounts[i] = decimal.Decimal(match.group(0))
bet_amount_total = sum(bet_amounts)
user_coins = decimal.Decimal(event["user"].get_setting("coins", user_coins = decimal.Decimal(event["user"].get_setting("coins",
"0.0")) "0.0"))
if bet_amount > user_coins: if bet_amount_total > user_coins:
event["stderr"].write("You don't have enough coins to bet") event["stderr"].write("You don't have enough coins to bet")
return return
@ -219,14 +221,20 @@ class Module(object):
# 1dozen (1-12), 2dozen (13-24), 3dozen (25-36) # 1dozen (1-12), 2dozen (13-24), 3dozen (25-36)
# 1column (1,4..34), 2column (2,5..35), 3column (3,6..36) # 1column (1,4..34), 2column (2,5..35), 3column (3,6..36)
choice = random.randint(0, 36) choice = random.randint(0, 36)
odds = 0 winnings = {}
losses = {}
if choice == 0: if choice == 0:
loss = sum(bet_amounts)
event["user"].set_setting("coins", str(user_coins-loss))
event["stdout"].write("Roulette spin lands on 0, " event["stdout"].write("Roulette spin lands on 0, "
"the house wins, %s loses %s" % ( "the house wins, %s loses %s" % (
event["user"].nickname, bet_amount)) event["user"].nickname, loss))
return return
street_match = REGEX_STREET.match(bet)
failed = False
for i, bet in enumerate(bets):
street_match = REGEX_STREET.match(bet)
odds = 0
if bet == "even": if bet == "even":
odds = 1*((choice % 2) == 0) odds = 1*((choice % 2) == 0)
elif bet == "odd": elif bet == "odd":
@ -254,19 +262,36 @@ class Module(object):
elif street_match: elif street_match:
row = int(street_match.group(1)) row = int(street_match.group(1))
odds = 11*(((row*3)-2) <= choice <= (row*3)) odds = 11*(((row*3)-2) <= choice <= (row*3))
elif bet.isdigit(): elif bet.isdigit() and (1 <= int(bet) <= 36):
odds = 35*(choice == int(bet)) odds = 35*(choice == int(bet))
else: else:
event["stderr"].write("Unknown bet") event["stderr"].write("Unknown bet")
failed = True
break
if odds == 0:
losses[bet] = bet_amounts[i]
else:
winnings[bet] = [odds, bet_amounts[i]*odds]
if failed:
return return
if not odds == 0: winnings_str = ["%s for %s (%d to 1)" % (winnings[bet][1], bet,
winnings = round(bet_amount * decimal.Decimal(odds), 2) winnings[bet][0]) for bet in winnings.keys()]
event["user"].set_setting("coins", str(user_coins + winnings))
coin_winnings = sum(bet[1] for bet in winnings.values())
coin_losses = sum([loss for loss in losses.values()])
new_user_coins = (user_coins-coin_losses)+coin_winnings
event["user"].set_setting("coins", str(new_user_coins))
if not losses and winnings:
event["stdout"].write("Roulette spin lands on %d, " event["stdout"].write("Roulette spin lands on %d, "
"%s wins %s with odds of %d to 1" % (choice, "%s wins %s" % (choice, event["user"].nickname,
event["user"].nickname, winnings, odds)) ", ".join(winnings_str)))
elif losses and winnings:
event["stdout"].write("Roulette spin lands on %d, "
"%s wins %s; loses %s" % (choice, event["user"].nickname,
", ".join(winnings_str), str(coin_losses)))
else: else:
event["user"].set_setting("coins", str(user_coins-bet_amount))
event["stdout"].write("Roulette spin lands on %d, " event["stdout"].write("Roulette spin lands on %d, "
"%s loses %s" % (choice, event["user"].nickname, bet_amount)) "%s loses %s" % (choice, event["user"].nickname,
str(coin_losses)))