2018-08-09 12:18:05 +00:00
|
|
|
import math, random, time
|
2018-08-09 11:24:09 +00:00
|
|
|
import Utils
|
|
|
|
|
|
|
|
SIDES = {"heads": 0, "tails": 1}
|
|
|
|
DEFAULT_REDEEM_DELAY = 600 # 600 seconds, 10 minutes
|
2018-08-09 11:34:12 +00:00
|
|
|
DEFAULT_REDEEM_AMOUNT = 100
|
2018-08-09 11:24:09 +00:00
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot):
|
|
|
|
bot.events.on("received.command.coins").hook(self.coins,
|
|
|
|
help="Show how many coins you have")
|
|
|
|
bot.events.on("received.command.redeemcoins").hook(
|
|
|
|
self.redeem_coins, help="Redeem free coins")
|
|
|
|
bot.events.on("received.command.flip").hook(self.flip,
|
|
|
|
help="Bet coins on a coin flip", usage=
|
|
|
|
"heads|tails <coin amount>", min_args=2)
|
2018-08-09 12:18:05 +00:00
|
|
|
bot.events.on("received.command.sendcoins").hook(
|
|
|
|
self.send, min_args=2, help="Send coins to a user",
|
|
|
|
usage="<nickname> <amount>")
|
2018-08-09 11:24:09 +00:00
|
|
|
|
|
|
|
def coins(self, event):
|
|
|
|
coins = event["user"].get_setting("coins", 0)
|
|
|
|
event["stdout"].write("%s has %d coin%s" % (
|
|
|
|
event["user"].nickname, coins,
|
|
|
|
"" if coins == 1 else "s"))
|
|
|
|
|
|
|
|
def redeem_coins(self, event):
|
2018-08-09 11:34:12 +00:00
|
|
|
user_coins = event["user"].get_setting("coins", 0)
|
|
|
|
if user_coins == 0:
|
|
|
|
last_redeem = event["user"].get_setting("last-redeem", None)
|
|
|
|
redeem_delay = event["server"].get_setting("redeem-delay",
|
|
|
|
DEFAULT_REDEEM_DELAY)
|
2018-08-09 11:24:09 +00:00
|
|
|
|
2018-08-09 11:34:12 +00:00
|
|
|
if last_redeem == None or (time.time()-last_redeem
|
|
|
|
) >= redeem_delay:
|
|
|
|
user_coins = event["user"].get_setting("coins", 0)
|
|
|
|
redeem_amount = event["server"].get_setting(
|
|
|
|
"redeem-amount", DEFAULT_REDEEM_AMOUNT)
|
|
|
|
event["user"].set_setting("coins", user_coins+redeem_amount)
|
|
|
|
event["stdout"].write("Redeemed %d coins" % redeem_amount)
|
|
|
|
event["user"].set_setting("last-redeem", time.time())
|
|
|
|
else:
|
|
|
|
time_left = (last_redeem+redeem_delay)-time.time()
|
|
|
|
event["stdout"].write("Please wait %s before redeeming" %
|
2018-08-09 12:18:05 +00:00
|
|
|
Utils.to_pretty_time(math.ceil(time_left)))
|
2018-08-09 11:24:09 +00:00
|
|
|
else:
|
2018-08-09 11:34:12 +00:00
|
|
|
event["stderr"].write(
|
|
|
|
"You can only redeem coins when you have none")
|
2018-08-09 11:24:09 +00:00
|
|
|
|
|
|
|
def flip(self, event):
|
|
|
|
side_name = event["args_split"][0].lower()
|
|
|
|
coin_bet = event["args_split"][1]
|
|
|
|
|
|
|
|
if not coin_bet.isdigit():
|
|
|
|
event["stderr"].write("Please provide a number of coins to bet")
|
|
|
|
return
|
|
|
|
coin_bet = int(coin_bet)
|
|
|
|
if not side_name in SIDES:
|
|
|
|
event["stderr"].write("Please provide 'heads' or 'tails'")
|
|
|
|
return
|
|
|
|
|
|
|
|
user_coins = event["user"].get_setting("coins", 0)
|
|
|
|
if coin_bet > user_coins:
|
|
|
|
event["stderr"].write("You don't have enough coins to bet")
|
|
|
|
return
|
|
|
|
|
|
|
|
chosen_side = random.choice(list(SIDES.keys()))
|
|
|
|
win = side_name == chosen_side
|
|
|
|
|
|
|
|
if win:
|
|
|
|
event["user"].set_setting("coins", user_coins+coin_bet)
|
2018-08-09 11:29:51 +00:00
|
|
|
event["stdout"].write("%s flips %s and wins %d!" % (
|
2018-08-09 11:24:09 +00:00
|
|
|
event["user"].nickname, side_name, coin_bet))
|
|
|
|
else:
|
|
|
|
event["user"].set_setting("coins", user_coins-coin_bet)
|
2018-08-09 12:35:31 +00:00
|
|
|
event["stdout"].write("%s flips %s and loses %d!" % (
|
2018-08-09 11:24:09 +00:00
|
|
|
event["user"].nickname, side_name, coin_bet))
|
2018-08-09 12:18:05 +00:00
|
|
|
|
|
|
|
def send(self, event):
|
|
|
|
send_amount = event["args_split"][1]
|
|
|
|
if not send_amount.isdigit() or int(send_amount) <= 0:
|
|
|
|
event["stderr"].write(
|
|
|
|
"Please provide a positive number of coins to send")
|
|
|
|
return
|
|
|
|
send_amount = int(send_amount)
|
|
|
|
|
|
|
|
user_coins = event["user"].get_setting("coins")
|
|
|
|
redeem_amount = event["server"].get_setting(
|
|
|
|
"redeem-amount", DEFAULT_REDEEM_AMOUNT)
|
|
|
|
new_user_coins = user_coins - send_amount
|
|
|
|
|
|
|
|
if new_user_coins == 0:
|
|
|
|
event["stderr"].write("You have no coins")
|
|
|
|
return
|
|
|
|
elif new_user_coins < redeem_amount:
|
|
|
|
event["stderr"].write(
|
|
|
|
"You cannot send an amount of money that puts"
|
|
|
|
" you below %d coins" % redeem_amount)
|
|
|
|
return
|
|
|
|
target_user = event["server"].get_user(event["args_split"][0])
|
|
|
|
target_user_coins = target_user.get_setting("coins", 0)
|
|
|
|
event["user"].set_setting("coins", new_user_coins)
|
|
|
|
target_user.set_setting("coins", target_user_coins+send_amount)
|
|
|
|
|
|
|
|
event["stdout"].write("%s sent %d coins to %s" % (
|
|
|
|
event["user"].nickname, send_amount, target_user.nickname))
|