2018-10-13 00:14:07 +00:00
|
|
|
import datetime, decimal, functools, math, random, re, secrets, time
|
2018-10-12 17:07:23 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-08-09 11:24:09 +00:00
|
|
|
|
|
|
|
SIDES = {"heads": 0, "tails": 1}
|
|
|
|
DEFAULT_REDEEM_DELAY = 600 # 600 seconds, 10 minutes
|
2018-08-10 11:22:41 +00:00
|
|
|
DEFAULT_REDEEM_AMOUNT = "100.0"
|
|
|
|
DEFAULT_INTEREST_RATE = "0.01"
|
2018-08-10 09:27:34 +00:00
|
|
|
INTEREST_INTERVAL = 60*60 # 1 hour
|
2018-08-10 11:22:41 +00:00
|
|
|
DECIMAL_ZERO = decimal.Decimal("0")
|
2018-10-10 11:10:53 +00:00
|
|
|
REGEX_FLOAT = re.compile("(?:\d+(?:\.\d{1,2}|$)|\.\d{1,2})")
|
2018-10-12 14:55:32 +00:00
|
|
|
DEFAULT_MARKET_CAP = str(1_000_000_000)
|
2018-08-09 11:24:09 +00:00
|
|
|
|
2018-10-12 17:31:28 +00:00
|
|
|
HOUR_SECONDS = (1*60)*60
|
2018-10-12 16:40:37 +00:00
|
|
|
LOTTERY_INTERVAL = (60*60)*6 # 6 hours
|
|
|
|
LOTTERY_BUYIN = "100.00"
|
|
|
|
|
2018-08-15 16:55:05 +00:00
|
|
|
RED = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
|
|
|
|
BLACK = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
|
|
|
|
|
2018-08-15 17:33:14 +00:00
|
|
|
SMALL = range(1, 19)
|
|
|
|
BIG = range(19, 37)
|
|
|
|
|
2018-08-15 16:55:05 +00:00
|
|
|
FIRST_DOZEN = list(range(1, 13))
|
|
|
|
SECOND_DOZEN = list(range(13, 25))
|
|
|
|
THIRD_DOZEN = list(range(25, 37))
|
|
|
|
|
|
|
|
FIRST_COLUMN = list(range(1, 37))[0::3]
|
|
|
|
SECOND_COLUMN = list(range(1, 37))[1::3]
|
|
|
|
THIRD_COLUMN = list(range(1, 37))[2::3]
|
|
|
|
|
2018-08-18 17:26:47 +00:00
|
|
|
REGEX_STREET = re.compile("street([1-9]|1[0-2])$")
|
2018-08-15 16:55:05 +00:00
|
|
|
|
2018-10-21 10:52:12 +00:00
|
|
|
WALLET_DEFAULT_NAME = "default"
|
|
|
|
WALLET_DEFAULTS = {"in": WALLET_DEFAULT_NAME, "out": WALLET_DEFAULT_NAME}
|
2018-10-16 12:26:29 +00:00
|
|
|
class CoinParseException(Exception):
|
|
|
|
pass
|
|
|
|
|
2018-10-12 17:07:23 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def on_load(self):
|
|
|
|
self.timers.add("coin-interest", INTEREST_INTERVAL,
|
2018-10-12 16:40:37 +00:00
|
|
|
time.time()+self._until_next_hour())
|
2018-10-12 17:07:23 +00:00
|
|
|
self.timers.add("coin-lottery", LOTTERY_INTERVAL,
|
2018-10-12 16:40:37 +00:00
|
|
|
time.time()+self._until_next_6_hour())
|
2018-09-05 12:51:42 +00:00
|
|
|
|
2018-10-12 16:40:37 +00:00
|
|
|
def _until_next_hour(self, now=None):
|
2018-10-12 16:53:57 +00:00
|
|
|
now = now or datetime.datetime.utcnow()
|
2018-08-09 13:22:25 +00:00
|
|
|
until_next_hour = 60-now.second
|
2018-10-12 16:40:37 +00:00
|
|
|
return until_next_hour+((60-(now.minute+1))*60)
|
|
|
|
def _until_next_6_hour(self):
|
2018-10-12 16:53:57 +00:00
|
|
|
now = datetime.datetime.utcnow()
|
2018-10-12 16:40:37 +00:00
|
|
|
until_next_hour = self._until_next_hour(now)
|
2018-10-12 17:31:28 +00:00
|
|
|
until_next_6_hour = (6-(now.hour%6))-1
|
|
|
|
until_next_6_hour = until_next_6_hour*HOUR_SECONDS
|
2018-10-12 16:40:37 +00:00
|
|
|
return until_next_hour+until_next_6_hour
|
2018-08-09 13:22:25 +00:00
|
|
|
|
2018-10-12 14:55:32 +00:00
|
|
|
def _get_pool(self, server):
|
|
|
|
return decimal.Decimal(server.get_setting("coins", DEFAULT_MARKET_CAP))
|
|
|
|
def _set_pool(self, server, amount):
|
2018-10-16 12:46:25 +00:00
|
|
|
server.set_setting("coins", self._coin_str(amount))
|
2018-10-12 14:55:32 +00:00
|
|
|
def _take_from_pool(self, server, amount):
|
|
|
|
coins = self._get_pool(server)
|
|
|
|
self._set_pool(server, coins-amount)
|
|
|
|
def _give_to_pool(self, server, amount):
|
|
|
|
coins = self._get_pool(server)
|
|
|
|
self._set_pool(server, coins+amount)
|
|
|
|
|
2018-10-18 12:19:16 +00:00
|
|
|
def _get_user_wallets(self, user):
|
2018-10-19 10:50:11 +00:00
|
|
|
return user.get_setting("wallets", {WALLET_DEFAULT: "0.0"})
|
2018-10-18 12:19:16 +00:00
|
|
|
def _set_user_wallets(self, user, wallets):
|
|
|
|
user.set_setting("wallets", wallets)
|
|
|
|
def _reset_user_wallets(self, user):
|
|
|
|
user.del_setting("wallets")
|
2018-10-18 14:24:33 +00:00
|
|
|
def _user_has_wallet(self, user, wallet):
|
|
|
|
return wallet.lower() in self._get_user_wallets(user)
|
2018-10-18 12:19:16 +00:00
|
|
|
|
2018-10-18 14:18:16 +00:00
|
|
|
def _get_user_coins(self, user, wallet=WALLET_DEFAULT):
|
2018-10-18 12:19:16 +00:00
|
|
|
wallets = self._get_user_wallets(user)
|
|
|
|
return decimal.Decimal(wallets.get(wallet.lower(), "0.0"))
|
|
|
|
def _get_all_user_coins(self, user):
|
|
|
|
wallets = self._get_user_wallets(user)
|
2018-10-19 10:48:21 +00:00
|
|
|
return sum(decimal.Decimal(amount) for amount in wallets.values())
|
2018-10-18 14:18:16 +00:00
|
|
|
def _set_user_coins(self, user, coins, wallet=WALLET_DEFAULT):
|
2018-10-18 12:19:16 +00:00
|
|
|
wallets = self._get_user_wallets(user)
|
|
|
|
wallets[wallet.lower()] = self._coin_str(coins)
|
|
|
|
self._set_user_wallets(user, wallets)
|
2018-10-19 09:58:36 +00:00
|
|
|
def _add_user_wallet(self, user, wallet):
|
|
|
|
wallets = self._get_user_wallets(user)
|
|
|
|
wallets[wallet.lower()] = "0.0"
|
|
|
|
self._set_user_wallets(user, wallets)
|
|
|
|
def _remove_user_wallet(self, user, wallet):
|
|
|
|
wallets = self._get_user_wallets(user)
|
|
|
|
del wallets[wallet.lower()]
|
2018-10-19 10:38:23 +00:00
|
|
|
self._set_user_wallets(user, wallets)
|
2018-10-16 12:05:55 +00:00
|
|
|
|
2018-10-16 12:58:02 +00:00
|
|
|
def _all_coins(self, server):
|
2018-10-18 12:38:54 +00:00
|
|
|
coins = server.get_all_user_settings("wallets", [])
|
|
|
|
|
2018-10-18 12:40:36 +00:00
|
|
|
for i, (nickname, wallet) in enumerate(coins):
|
2018-10-19 10:48:21 +00:00
|
|
|
user_coins = sum(decimal.Decimal(v) for v in wallet.values())
|
2018-10-18 12:45:38 +00:00
|
|
|
coins[i] = (nickname, user_coins)
|
2018-10-18 12:38:54 +00:00
|
|
|
|
2018-10-18 12:49:32 +00:00
|
|
|
return dict(filter(lambda coin: coin[1], coins))
|
2018-10-16 12:58:02 +00:00
|
|
|
|
2018-10-16 13:06:38 +00:00
|
|
|
def _redeem_amount(self, server):
|
|
|
|
return decimal.Decimal(server.get_setting("redeem-amount",
|
|
|
|
DEFAULT_REDEEM_AMOUNT))
|
|
|
|
def _redeem_delay(self, server):
|
|
|
|
return server.get_setting("redeem-delay", DEFAULT_REDEEM_DELAY)
|
|
|
|
|
2018-10-18 14:18:16 +00:00
|
|
|
def _give(self, server, user, amount, wallet=WALLET_DEFAULT):
|
2018-10-19 10:24:44 +00:00
|
|
|
user_coins = self._get_user_coins(user, wallet)
|
2018-10-16 12:39:45 +00:00
|
|
|
self._take_from_pool(server, amount)
|
2018-10-18 14:18:16 +00:00
|
|
|
self._set_user_coins(user, user_coins+amount, wallet)
|
2018-10-19 10:34:18 +00:00
|
|
|
return user_coins+amount
|
2018-10-18 14:18:16 +00:00
|
|
|
def _take(self, server, user, amount, wallet=WALLET_DEFAULT):
|
2018-10-19 10:24:44 +00:00
|
|
|
user_coins = self._get_user_coins(user, wallet)
|
2018-10-16 12:41:02 +00:00
|
|
|
self._give_to_pool(server, amount)
|
2018-10-18 14:18:16 +00:00
|
|
|
self._set_user_coins(user, user_coins-amount, wallet)
|
2018-10-19 10:34:18 +00:00
|
|
|
return user_coins-amount
|
2018-10-18 14:18:16 +00:00
|
|
|
def _move(self, user1, user2, amount, from_wallet=WALLET_DEFAULT,
|
|
|
|
to_wallet=WALLET_DEFAULT):
|
2018-10-19 10:24:44 +00:00
|
|
|
user1_coins = self._get_user_coins(user1, from_wallet)
|
2018-10-18 14:18:16 +00:00
|
|
|
self._set_user_coins(user1, user1_coins-amount, from_wallet)
|
2018-10-16 12:39:45 +00:00
|
|
|
|
2018-10-19 10:24:44 +00:00
|
|
|
user2_coins = self._get_user_coins(user2, to_wallet)
|
2018-10-18 14:18:16 +00:00
|
|
|
self._set_user_coins(user2, user2_coins+amount, to_wallet)
|
2018-10-16 12:39:45 +00:00
|
|
|
|
2018-10-16 12:05:55 +00:00
|
|
|
def _coin_str(self, coins):
|
|
|
|
return "{0:.2f}".format(coins)
|
2018-10-16 12:26:29 +00:00
|
|
|
def _parse_coins(self, s, minimum=None):
|
|
|
|
try:
|
|
|
|
s = utils.parse_number(s)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
match = REGEX_FLOAT.match(s)
|
|
|
|
if match:
|
|
|
|
coins = decimal.Decimal(match.group(0))
|
|
|
|
if minimum == None or coins >= minimum:
|
|
|
|
return coins
|
|
|
|
else:
|
|
|
|
raise CoinParseException(
|
|
|
|
"Coin amount provided is lower than %s" % minimum)
|
|
|
|
else:
|
|
|
|
raise CoinParseException(
|
|
|
|
"Please provide a valid positive coin amount")
|
2018-10-13 21:20:40 +00:00
|
|
|
|
2018-10-21 10:57:06 +00:00
|
|
|
def _get_default_wallets(self, user):
|
|
|
|
return user.get_setting("default-wallets", DEFAULT_WALLETS)
|
2018-10-21 10:52:12 +00:00
|
|
|
def _set_default_wallet(self, user, type, wallet):
|
2018-10-21 10:57:06 +00:00
|
|
|
default_wallets = self._get_default_wallets(user)
|
2018-10-21 10:52:12 +00:00
|
|
|
default_wallets[type.lower()] = wallet.lower()
|
|
|
|
user.set_setting("default-wallets", default_wallets)
|
|
|
|
def _default_wallet(self, user, type):
|
2018-10-21 10:57:06 +00:00
|
|
|
default_wallets = self._get_default_wallets(user)
|
2018-10-21 10:52:12 +00:00
|
|
|
return default_wallets.get(type.lower(), None)
|
2018-10-19 09:58:36 +00:00
|
|
|
def _default_wallets(self, user):
|
2018-10-21 10:52:12 +00:00
|
|
|
default_wallet_in = self._default_wallet(user, "in")
|
|
|
|
default_wallet_out = self._default_wallet(user, "out")
|
|
|
|
return default_wallet_in, default_wallet_out
|
2018-10-21 10:57:06 +00:00
|
|
|
def _default_wallet_for(self, user, wallet):
|
|
|
|
default_wallets = self._get_default_wallets(user)
|
|
|
|
for key, value in default_wallets:
|
|
|
|
if value.lower() == wallet.lower():
|
|
|
|
return key
|
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
def _parse_wallets(self, user, s):
|
|
|
|
if not s:
|
|
|
|
return self._default_wallets()
|
|
|
|
if not ":" in s:
|
|
|
|
return s, s
|
2018-10-21 10:52:12 +00:00
|
|
|
wallet_in_default, wallet_out_default = self._default_wallets(user)
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet_1, _, wallet_2 = s.partition(":")
|
2018-10-19 10:35:25 +00:00
|
|
|
wallet_1 = wallet_1.lower() or WALLET_DEFAULT
|
2018-10-19 10:34:18 +00:00
|
|
|
wallet_2 = wallet_2.lower() or WALLET_DEFAULT
|
|
|
|
|
|
|
|
wallets = self._get_user_wallets(user)
|
|
|
|
if not wallet_1 in wallets or not wallet_2 in wallets:
|
|
|
|
raise utils.EventError("Unknown wallet")
|
|
|
|
|
|
|
|
return wallet_1, wallet_2
|
2018-10-19 09:58:36 +00:00
|
|
|
|
2018-10-12 14:55:32 +00:00
|
|
|
@utils.hook("received.command.bank")
|
|
|
|
def bank(self, event):
|
2018-10-16 12:53:32 +00:00
|
|
|
"""
|
|
|
|
:help: Show how many coins The Bank currently has
|
|
|
|
"""
|
2018-10-12 15:08:22 +00:00
|
|
|
event["stdout"].write("The Bank has %s coins" %
|
2018-10-16 12:05:55 +00:00
|
|
|
self._coin_str(self._get_pool(event["server"])))
|
2018-10-12 14:55:32 +00:00
|
|
|
|
2018-10-13 21:20:40 +00:00
|
|
|
def _total_coins(self, server):
|
2018-10-18 12:49:32 +00:00
|
|
|
all_coins = sum(self._all_coins(server).values())
|
2018-10-14 07:38:58 +00:00
|
|
|
return self._get_pool(server)+all_coins
|
2018-10-13 15:47:46 +00:00
|
|
|
|
2018-10-13 15:57:18 +00:00
|
|
|
@utils.hook("received.command.totalcoins")
|
|
|
|
def total_coins(self, event):
|
2018-10-16 12:53:32 +00:00
|
|
|
"""
|
|
|
|
:help: Show how many coins are currently in circulation
|
|
|
|
"""
|
2018-10-16 12:05:55 +00:00
|
|
|
event["stdout"].write("Total coins: %s" % self._coin_str(
|
2018-10-13 15:57:18 +00:00
|
|
|
self._total_coins(event["server"])))
|
2018-10-13 15:47:46 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.coins")
|
2018-08-09 11:24:09 +00:00
|
|
|
def coins(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Show how many coins you have
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-08-09 15:37:55 +00:00
|
|
|
if event["args_split"]:
|
|
|
|
target = event["server"].get_user(event["args_split"][0])
|
|
|
|
else:
|
|
|
|
target = event["user"]
|
2018-10-18 12:19:16 +00:00
|
|
|
coins = self._get_all_user_coins(target)
|
2018-08-10 11:22:41 +00:00
|
|
|
event["stdout"].write("%s has %s coin%s" % (target.nickname,
|
2018-10-16 12:05:55 +00:00
|
|
|
self._coin_str(coins), "" if coins == 1 else "s"))
|
2018-08-09 11:24:09 +00:00
|
|
|
|
2018-10-18 14:20:05 +00:00
|
|
|
@utils.hook("received.command.wallet")
|
2018-10-18 14:18:16 +00:00
|
|
|
def wallet(self, event):
|
|
|
|
"""
|
|
|
|
:help: Show your wallets and their balances
|
|
|
|
:usage: [wallet]
|
|
|
|
"""
|
2018-10-18 14:24:33 +00:00
|
|
|
if not event["args_split"]:
|
2018-10-18 14:18:16 +00:00
|
|
|
wallets = self._get_user_wallets(event["user"]).keys()
|
|
|
|
event["stdout"].write("%s: your available wallets are: %s" %
|
|
|
|
(event["user"].nickname, ", ".join(wallets)))
|
2018-10-18 14:24:33 +00:00
|
|
|
else:
|
|
|
|
wallet = event["args"]
|
2018-10-18 14:25:42 +00:00
|
|
|
if not self._user_has_wallet(event["user"], wallet):
|
2018-10-18 14:24:33 +00:00
|
|
|
raise utils.EventError("%s: you don't have a '%s' wallet" %
|
|
|
|
(event["user"].nickname, wallet))
|
2018-10-18 14:26:28 +00:00
|
|
|
coins = self._get_user_coins(event["user"], wallet)
|
2018-10-18 14:24:33 +00:00
|
|
|
event["stdout"].write("%s: you have %s coins in your '%s' wallet" %
|
|
|
|
(event["user"].nickname, self._coin_str(coins), wallet))
|
2018-10-18 14:18:16 +00:00
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
@utils.hook("received.command.addwallet", authenticated=True, min_args=1)
|
|
|
|
def add_wallet(self, event):
|
2018-10-20 13:00:28 +00:00
|
|
|
"""
|
|
|
|
:help: Add a wallet to your account
|
|
|
|
:usage: <wallet name>
|
|
|
|
"""
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet = event["args_split"][0]
|
2018-10-19 10:21:37 +00:00
|
|
|
if self._user_has_wallet(event["user"], wallet):
|
2018-10-19 09:58:36 +00:00
|
|
|
raise utils.EventError("%s: you already have a '%s' wallet" %
|
|
|
|
(event["user"].nickname, wallet))
|
|
|
|
self._add_user_wallet(event["user"], wallet)
|
|
|
|
event["stdout"].write("%s: added a '%s' wallet" % (
|
|
|
|
event["user"].nickname, wallet))
|
|
|
|
@utils.hook("received.command.removewallet", authenticated=True, min_args=1)
|
|
|
|
def remove_wallet(self, event):
|
2018-10-20 13:00:28 +00:00
|
|
|
"""
|
|
|
|
:help: Remove a wallet from your account
|
|
|
|
:usage: <wallet name>
|
|
|
|
"""
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet = event["args_split"][0]
|
2018-10-19 10:21:37 +00:00
|
|
|
if not self._user_has_wallet(event["user"], wallet):
|
2018-10-19 09:58:36 +00:00
|
|
|
raise utils.EventError("%s: you don't have a '%s' wallet" %
|
|
|
|
(event["user"].nickname, wallet))
|
2018-10-21 10:57:06 +00:00
|
|
|
default_type = self._default_wallet_for(event["user"], wallet)
|
|
|
|
if default_type:
|
|
|
|
raise utils.EventError("%s: you cannot delete a default wallet "
|
|
|
|
"('%s' is the default wallet for '%s')" %
|
|
|
|
(event["user"].nickname, wallet, default_type))
|
2018-10-19 09:58:36 +00:00
|
|
|
|
|
|
|
coins = self._get_user_coins(event["user"], wallet)
|
|
|
|
self._give(event["server"], event["user"], coins, WALLET_DEFAULT)
|
|
|
|
self._remove_user_wallet(event["user"], wallet)
|
|
|
|
event["stdout"].write("%s: removed wallet '%s' and shifted any funds "
|
|
|
|
"to your default wallet" % (event["user"].nickname, wallet))
|
|
|
|
|
2018-10-21 10:52:12 +00:00
|
|
|
@utils.hook("received.command.defaultwallet", authenticated=True,
|
|
|
|
min_args=2)
|
|
|
|
def default_wallet(self, event):
|
|
|
|
"""
|
|
|
|
:help: Set a default wallet for a given wallet type
|
|
|
|
:usage: <type> <wallet>
|
|
|
|
"""
|
|
|
|
type = event["args_split"][0]
|
|
|
|
wallet = event["args_split"][1]
|
|
|
|
if not self._user_has_wallet(event["user"], wallet):
|
|
|
|
raise utils.EventError("%s: Unknown wallet" %
|
|
|
|
event["user"].nickname)
|
|
|
|
|
|
|
|
self._set_default_wallet(event["user"], type, wallet)
|
|
|
|
event["stdout"].write("%s: Set default wallet for '%s' to '%s'" %
|
|
|
|
(event["user"].nickname, type, wallet))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.resetcoins", min_args=1)
|
2018-08-28 11:39:16 +00:00
|
|
|
def reset_coins(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Reset a user's coins to 0
|
|
|
|
:usage: <target>
|
|
|
|
:permission: resetcoins
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-08-28 11:39:16 +00:00
|
|
|
target = event["server"].get_user(event["args_split"][0])
|
2018-10-18 12:19:16 +00:00
|
|
|
coins = self._get_all_user_coins(target)
|
2018-10-16 12:43:45 +00:00
|
|
|
self._take(event["server"], target, coins)
|
2018-10-18 12:19:16 +00:00
|
|
|
self._reset_user_wallets(target)
|
2018-10-16 12:43:45 +00:00
|
|
|
event["stdout"].write("Reset coins for %s" % target.nickname)
|
2018-08-28 11:39:16 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.givecoins", min_args=1)
|
2018-09-05 12:51:42 +00:00
|
|
|
def give_coins(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Give coins to a user
|
2018-10-20 13:00:28 +00:00
|
|
|
:usage: <nickname> <coins> [wallet]
|
2018-09-30 16:29:09 +00:00
|
|
|
:permission: givecoins
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-19 09:58:36 +00:00
|
|
|
_, wallet_out = self._default_wallets(event["user"])
|
2018-10-20 12:55:47 +00:00
|
|
|
if len(event["args_split"]) > 2:
|
2018-10-19 09:58:36 +00:00
|
|
|
_, wallet_out = self._parse_wallets(event["user"],
|
2018-10-20 12:55:47 +00:00
|
|
|
event["args_split"][2])
|
2018-10-19 09:58:36 +00:00
|
|
|
|
2018-09-05 12:51:42 +00:00
|
|
|
target = event["server"].get_user(event["args_split"][0])
|
2018-10-16 12:26:29 +00:00
|
|
|
try:
|
2018-10-17 18:39:43 +00:00
|
|
|
coins = self._parse_coins(event["args_split"][1], DECIMAL_ZERO)
|
2018-10-16 12:26:29 +00:00
|
|
|
except CoinParseException as e:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: %s" % (event["user"].nickname, str(e)))
|
2018-10-12 14:55:32 +00:00
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
self._give(event["server"], target, coins, wallet_out)
|
2018-09-05 12:51:42 +00:00
|
|
|
event["stdout"].write("Gave '%s' %s coins" % (target.nickname,
|
2018-10-16 12:46:25 +00:00
|
|
|
self._coin_str(coins)))
|
2018-08-28 11:39:16 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.richest")
|
2018-08-09 12:49:44 +00:00
|
|
|
def richest(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Show the top 10 richest users
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-16 12:58:02 +00:00
|
|
|
top_10 = utils.top_10(self._all_coins(event["server"]),
|
2018-10-04 04:33:15 +00:00
|
|
|
convert_key=lambda nickname: utils.prevent_highlight(
|
|
|
|
event["server"].get_user(nickname).nickname),
|
2018-10-16 12:05:55 +00:00
|
|
|
value_format=lambda value: self._coin_str(value))
|
2018-10-04 04:33:15 +00:00
|
|
|
event["stdout"].write("Richest users: %s" % ", ".join(top_10))
|
2018-08-09 12:49:44 +00:00
|
|
|
|
2018-09-29 11:53:39 +00:00
|
|
|
def _redeem_cache(self, server, user):
|
|
|
|
return "redeem|%s|%s@%s" % (server.id, user.username, user.hostname)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.redeemcoins")
|
2018-08-09 11:24:09 +00:00
|
|
|
def redeem_coins(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Redeem your free coins
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-18 12:19:16 +00:00
|
|
|
user_coins = self._get_all_user_coins(event["user"])
|
2018-08-10 11:22:41 +00:00
|
|
|
if user_coins == DECIMAL_ZERO:
|
2018-09-29 11:53:39 +00:00
|
|
|
cache = self._redeem_cache(event["server"], event["user"])
|
|
|
|
if not self.bot.cache.has_item(cache):
|
2018-10-19 09:58:36 +00:00
|
|
|
_, wallet_out = self._default_wallets(event["user"])
|
|
|
|
if len(event["args_split"]) > 0:
|
|
|
|
_, wallet_out = self._parse_wallets(event["user"],
|
|
|
|
event["args_split"][0])
|
|
|
|
|
2018-10-16 13:06:38 +00:00
|
|
|
redeem_amount = self._redeem_amount(event["server"])
|
2018-10-19 09:58:36 +00:00
|
|
|
self._give(event["server"], event["user"], redeem_amount,
|
|
|
|
wallet_out)
|
2018-10-12 14:55:32 +00:00
|
|
|
|
2018-10-16 12:05:55 +00:00
|
|
|
event["stdout"].write("Redeemed %s coins" % self._coin_str(
|
2018-08-10 11:22:41 +00:00
|
|
|
redeem_amount))
|
2018-09-29 11:53:39 +00:00
|
|
|
|
2018-10-16 13:06:38 +00:00
|
|
|
redeem_delay = self._redeem_delay(event["server"])
|
2018-09-29 11:53:39 +00:00
|
|
|
self.bot.cache.temporary_cache(cache, redeem_delay)
|
2018-08-09 11:34:12 +00:00
|
|
|
else:
|
2018-09-29 11:53:39 +00:00
|
|
|
time_left = self.bot.cache.until_expiration(cache)
|
2018-10-12 16:40:37 +00:00
|
|
|
event["stderr"].write("%s: Please wait %s before redeeming" % (
|
|
|
|
event["user"].nickname,
|
|
|
|
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(
|
2018-10-12 16:40:37 +00:00
|
|
|
"%s: You can only redeem coins when you have none" %
|
|
|
|
event["user"].nickname)
|
2018-08-09 11:24:09 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.flip", min_args=2, authenticated=True)
|
2018-08-09 11:24:09 +00:00
|
|
|
def flip(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Bet on a coin flip
|
2018-10-20 13:00:28 +00:00
|
|
|
:usage: heads|tails <coin amount> [wallet_in:wallet_out]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet_in, wallet_out = self._default_wallets(event["user"])
|
2018-10-19 10:15:41 +00:00
|
|
|
if len(event["args_split"]) > 2:
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet_in, wallet_out = self._parse_wallets(event["user"],
|
2018-10-19 10:00:02 +00:00
|
|
|
event["args_split"][2])
|
2018-10-19 09:58:36 +00:00
|
|
|
|
2018-08-09 11:24:09 +00:00
|
|
|
side_name = event["args_split"][0].lower()
|
2018-08-13 15:01:06 +00:00
|
|
|
coin_bet = event["args_split"][1].lower()
|
|
|
|
if coin_bet == "all":
|
2018-10-19 09:58:36 +00:00
|
|
|
coin_bet = self._get_user_coins(event["user"], wallet_in)
|
2018-10-16 12:05:55 +00:00
|
|
|
if coin_bet <= DECIMAL_ZERO:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You have no coins to bet" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-10-16 12:05:55 +00:00
|
|
|
else:
|
|
|
|
try:
|
2018-10-16 12:26:29 +00:00
|
|
|
coin_bet = self._parse_coins(coin_bet, DECIMAL_ZERO)
|
|
|
|
except CoinParseException as e:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: %s" % (event["user"].nickname,
|
2018-10-16 12:26:29 +00:00
|
|
|
str(e)))
|
2018-08-13 15:01:06 +00:00
|
|
|
|
2018-08-09 11:24:09 +00:00
|
|
|
if not side_name in SIDES:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: Please provide 'heads' or 'tails'" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-08-09 11:24:09 +00:00
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
user_coins = self._get_user_coins(event["user"], wallet_in)
|
2018-08-09 11:24:09 +00:00
|
|
|
if coin_bet > user_coins:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You don't have enough coins to bet" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-08-09 11:24:09 +00:00
|
|
|
|
2018-10-10 11:02:41 +00:00
|
|
|
chosen_side = secrets.choice(list(SIDES.keys()))
|
2018-08-09 11:24:09 +00:00
|
|
|
win = side_name == chosen_side
|
|
|
|
|
2018-10-16 12:26:29 +00:00
|
|
|
coin_bet_str = self._coin_str(coin_bet)
|
2018-08-09 11:24:09 +00:00
|
|
|
if win:
|
2018-10-19 10:34:18 +00:00
|
|
|
new_total = self._give(event["server"], event["user"], coin_bet,
|
|
|
|
wallet_out)
|
2018-10-10 23:26:16 +00:00
|
|
|
event["stdout"].write(
|
|
|
|
"%s flips %s and wins %s coin%s! (new total: %s)" % (
|
|
|
|
event["user"].nickname, side_name, coin_bet_str,
|
2018-10-19 10:34:18 +00:00
|
|
|
"" if coin_bet == 1 else "s", self._coin_str(new_total)
|
2018-10-10 23:26:16 +00:00
|
|
|
)
|
|
|
|
)
|
2018-08-09 11:24:09 +00:00
|
|
|
else:
|
2018-10-19 09:58:36 +00:00
|
|
|
self._take(event["server"], event["user"], coin_bet, wallet_in)
|
2018-10-10 23:26:16 +00:00
|
|
|
event["stdout"].write(
|
2018-10-11 09:35:41 +00:00
|
|
|
"%s flips %s and loses %s coin%s! (new total: %s)" % (
|
2018-10-10 23:26:16 +00:00
|
|
|
event["user"].nickname, side_name, coin_bet_str,
|
2018-10-16 12:39:45 +00:00
|
|
|
"" if coin_bet == 1 else "s",
|
|
|
|
self._coin_str(user_coins-coin_bet)
|
2018-10-10 23:26:16 +00:00
|
|
|
)
|
|
|
|
)
|
2018-08-09 12:18:05 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.sendcoins", min_args=2, authenticated=True)
|
2018-08-09 12:18:05 +00:00
|
|
|
def send(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Send coins to another user
|
2018-10-20 13:00:28 +00:00
|
|
|
:usage: <nickname> <amount> [wallet_in:wallet_out]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-19 12:03:21 +00:00
|
|
|
target_user = event["server"].get_user(event["args_split"][0])
|
|
|
|
|
|
|
|
wallet_in, _ = self._default_wallets(event["user"])
|
|
|
|
_, wallet_out = self._default_wallets(target_user)
|
2018-10-19 09:58:36 +00:00
|
|
|
if len(event["args_split"]) > 2:
|
2018-10-19 12:03:21 +00:00
|
|
|
wallet_in, _ = self._parse_wallets(event["user"],
|
|
|
|
event["args_split"][2])
|
|
|
|
_, wallet_out = self._parse_wallets(target_user,
|
2018-10-19 10:00:57 +00:00
|
|
|
event["args_split"][2])
|
2018-10-19 09:58:36 +00:00
|
|
|
|
2018-10-19 12:03:21 +00:00
|
|
|
if event["user"].get_id() == target_user.get_id():
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You can't send coins to yourself" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-10-16 12:05:55 +00:00
|
|
|
|
2018-08-09 12:18:05 +00:00
|
|
|
send_amount = event["args_split"][1]
|
2018-10-16 12:26:29 +00:00
|
|
|
try:
|
|
|
|
send_amount = self._parse_coins(send_amount, DECIMAL_ZERO)
|
|
|
|
except CoinParseException as e:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: %s" % (event["user"].nickname, str(e)))
|
2018-08-09 12:18:05 +00:00
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
user_coins = self._get_user_coins(event["user"], wallet_in)
|
2018-10-16 13:06:38 +00:00
|
|
|
redeem_amount = self._redeem_amount(event["server"])
|
2018-10-19 10:47:34 +00:00
|
|
|
new_total_coins = self._get_all_user_coins(event["user"])-send_amount
|
2018-08-09 12:18:05 +00:00
|
|
|
|
2018-10-12 12:53:15 +00:00
|
|
|
if user_coins == DECIMAL_ZERO:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You have no coins" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-10-19 10:47:34 +00:00
|
|
|
elif new_total_coins < redeem_amount:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError(
|
2018-10-12 16:40:37 +00:00
|
|
|
"%s: You cannot send an amount of money that puts"
|
|
|
|
" you below %s coins" % (
|
|
|
|
event["user"].nickname,
|
2018-10-16 12:05:55 +00:00
|
|
|
self._coin_str(redeem_amount)))
|
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
target_user_coins = self._get_user_coins(target_user, wallet_out)
|
2018-08-28 11:45:42 +00:00
|
|
|
if target_user_coins == None:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You can only send coins to users that "
|
2018-10-12 16:40:37 +00:00
|
|
|
"have had coins before" % event["user"].nickname)
|
2018-08-28 11:45:42 +00:00
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
self._move(event["user"], target_user, send_amount, wallet_in,
|
|
|
|
wallet_out)
|
2018-08-09 12:18:05 +00:00
|
|
|
|
2018-08-10 09:28:43 +00:00
|
|
|
event["stdout"].write("%s sent %s coins to %s" % (
|
2018-10-16 12:05:55 +00:00
|
|
|
event["user"].nickname, self._coin_str(send_amount),
|
2018-08-10 09:28:43 +00:00
|
|
|
target_user.nickname))
|
2018-08-09 13:22:25 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.roulette", min_args=2, authenticated=True)
|
2018-08-15 16:55:05 +00:00
|
|
|
def roulette(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Spin a roulette wheel
|
2018-10-20 13:00:28 +00:00
|
|
|
:usage: <type> <amount> [wallet_in:wallet_out]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-21 07:21:58 +00:00
|
|
|
expected_args = 1
|
|
|
|
expected_args += len(event["args_split"][0].split(","))
|
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet_in, wallet_out = self._default_wallets(event["user"])
|
2018-10-21 07:21:58 +00:00
|
|
|
if len(event["args_split"]) > expected_args:
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet_in, wallet_out = self._parse_wallets(event["user"],
|
2018-10-21 07:21:58 +00:00
|
|
|
event["args_split"][expected_args])
|
2018-10-19 09:58:36 +00:00
|
|
|
|
2018-08-16 10:54:51 +00:00
|
|
|
bets = event["args_split"][0].lower().split(",")
|
|
|
|
if "0" in bets:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You can't bet on 0" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-08-16 10:54:51 +00:00
|
|
|
bet_amounts = [amount.lower() for amount in event["args_split"][1:]]
|
2018-08-18 17:26:47 +00:00
|
|
|
if len(bet_amounts) < len(bets):
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: Please provide an amount for each bet" %
|
2018-10-20 18:08:46 +00:00
|
|
|
event["user"].nickname)
|
2018-08-16 10:54:51 +00:00
|
|
|
if len(bet_amounts) == 1 and bet_amounts[0] == "all":
|
2018-10-19 09:58:36 +00:00
|
|
|
bet_amounts[0] = self._get_user_coins(event["user"], wallet_in)
|
2018-10-16 12:05:55 +00:00
|
|
|
if bet_amounts[0] <= DECIMAL_ZERO:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You have no coins to bet" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-10-16 12:05:55 +00:00
|
|
|
bet_amounts[0] = self._coin_str(bet_amounts[0])
|
2018-08-15 16:55:05 +00:00
|
|
|
|
2018-08-16 10:54:51 +00:00
|
|
|
for i, bet_amount in enumerate(bet_amounts):
|
2018-10-14 14:27:49 +00:00
|
|
|
try:
|
2018-10-17 07:40:23 +00:00
|
|
|
bet_amounts[i] = self._parse_coins(bet_amount, DECIMAL_ZERO)
|
2018-10-16 12:26:29 +00:00
|
|
|
except CoinParseException as e:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: %s" % (event["user"].nickname,
|
2018-10-16 12:26:29 +00:00
|
|
|
str(e)))
|
|
|
|
|
2018-08-16 10:54:51 +00:00
|
|
|
bet_amount_total = sum(bet_amounts)
|
2018-08-15 16:55:05 +00:00
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
user_coins = self._get_user_coins(event["user"], wallet_in)
|
2018-08-16 10:54:51 +00:00
|
|
|
if bet_amount_total > user_coins:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You don't have enough coins to bet" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-08-15 16:55:05 +00:00
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
payin = sum(bet_amounts)
|
2018-10-20 13:07:24 +00:00
|
|
|
self._take(event["server"], event["user"], payin, wallet_in)
|
2018-10-19 09:58:36 +00:00
|
|
|
self._give_to_pool(event["server"], payin)
|
2018-10-12 23:44:04 +00:00
|
|
|
# black, red, odds, evens, low (1-18), high (19-36)
|
2018-08-15 16:55:05 +00:00
|
|
|
# 1dozen (1-12), 2dozen (13-24), 3dozen (25-36)
|
|
|
|
# 1column (1,4..34), 2column (2,5..35), 3column (3,6..36)
|
2018-10-10 17:12:54 +00:00
|
|
|
choice = secrets.randbelow(37)
|
2018-08-16 10:54:51 +00:00
|
|
|
winnings = {}
|
|
|
|
losses = {}
|
2018-08-15 16:55:05 +00:00
|
|
|
if choice == 0:
|
|
|
|
event["stdout"].write("Roulette spin lands on 0, "
|
|
|
|
"the house wins, %s loses %s" % (
|
2018-10-19 09:58:36 +00:00
|
|
|
event["user"].nickname, payin))
|
2018-08-15 16:55:05 +00:00
|
|
|
return
|
2018-08-16 10:54:51 +00:00
|
|
|
|
2018-08-18 17:26:47 +00:00
|
|
|
colour = "red" if choice in RED else "black"
|
2018-08-16 10:54:51 +00:00
|
|
|
for i, bet in enumerate(bets):
|
|
|
|
street_match = REGEX_STREET.match(bet)
|
|
|
|
odds = 0
|
|
|
|
if bet == "even":
|
|
|
|
odds = 1*((choice % 2) == 0)
|
|
|
|
elif bet == "odd":
|
|
|
|
odds = 1*((choice % 2) == 1)
|
|
|
|
elif bet == "red":
|
|
|
|
odds = 1*(choice in RED)
|
|
|
|
elif bet == "black":
|
|
|
|
odds = 1*(choice in BLACK)
|
2018-08-18 17:26:47 +00:00
|
|
|
elif bet == "small" or bet == "low":
|
2018-08-16 10:54:51 +00:00
|
|
|
odds = 1*(choice in SMALL)
|
2018-08-18 17:26:47 +00:00
|
|
|
elif bet == "big" or bet == "high":
|
2018-08-16 10:54:51 +00:00
|
|
|
odds = 1*(choice in BIG)
|
|
|
|
elif bet == "dozen1":
|
|
|
|
odds = 2*(choice in FIRST_DOZEN)
|
|
|
|
elif bet == "dozen2":
|
|
|
|
odds = 2*(choice in SECOND_DOZEN)
|
|
|
|
elif bet == "dozen3":
|
|
|
|
odds = 2*(choice in THIRD_DOZEN)
|
|
|
|
elif bet == "column1":
|
|
|
|
odds = 2*(choice in FIRST_COLUMN)
|
|
|
|
elif bet == "column2":
|
|
|
|
odds = 2*(choice in SECOND_COLUMN)
|
|
|
|
elif bet == "column3":
|
|
|
|
odds = 2*(choice in THIRD_COLUMN)
|
|
|
|
elif street_match:
|
|
|
|
row = int(street_match.group(1))
|
|
|
|
odds = 11*(((row*3)-2) <= choice <= (row*3))
|
|
|
|
elif bet.isdigit() and (1 <= int(bet) <= 36):
|
|
|
|
odds = 35*(choice == int(bet))
|
|
|
|
else:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: Unknown bet" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
2018-10-16 13:47:01 +00:00
|
|
|
|
2018-08-16 10:54:51 +00:00
|
|
|
if odds == 0:
|
|
|
|
losses[bet] = bet_amounts[i]
|
|
|
|
else:
|
|
|
|
winnings[bet] = [odds, bet_amounts[i]*odds]
|
2018-08-15 16:55:05 +00:00
|
|
|
|
2018-08-16 10:54:51 +00:00
|
|
|
winnings_str = ["%s for %s (%d to 1)" % (winnings[bet][1], bet,
|
|
|
|
winnings[bet][0]) for bet in winnings.keys()]
|
|
|
|
|
|
|
|
coin_winnings = sum(bet[1] for bet in winnings.values())
|
2018-10-19 10:48:21 +00:00
|
|
|
coin_losses = sum(loss for loss in losses.values())
|
2018-10-12 14:55:32 +00:00
|
|
|
|
|
|
|
if coin_winnings:
|
2018-10-19 09:58:36 +00:00
|
|
|
self._give(event["server"], event["user"], coin_winnings,
|
|
|
|
wallet_out)
|
2018-10-12 14:55:32 +00:00
|
|
|
|
2018-08-18 17:26:47 +00:00
|
|
|
total_winnings_str = " (%s total)" % coin_winnings if len(
|
|
|
|
winnings.keys()) > 1 else ""
|
2018-08-16 10:54:51 +00:00
|
|
|
|
2018-08-18 17:26:47 +00:00
|
|
|
choice = "%d %s" % (choice, colour)
|
2018-08-16 10:54:51 +00:00
|
|
|
if not losses and winnings:
|
2018-08-18 17:26:47 +00:00
|
|
|
event["stdout"].write("Roulette spin lands on %s, "
|
|
|
|
"%s wins %s%s" % (choice, event["user"].nickname,
|
2018-10-16 12:05:55 +00:00
|
|
|
", ".join(winnings_str), total_winnings_str))
|
2018-08-16 10:54:51 +00:00
|
|
|
elif losses and winnings:
|
2018-08-18 17:26:47 +00:00
|
|
|
event["stdout"].write("Roulette spin lands on %s, "
|
|
|
|
"%s wins %s%s; loses %s" % (choice,
|
|
|
|
event["user"].nickname, ", ".join(winnings_str),
|
|
|
|
str(total_winnings_str), str(coin_losses)))
|
2018-08-15 16:55:05 +00:00
|
|
|
else:
|
2018-08-18 17:26:47 +00:00
|
|
|
event["stdout"].write("Roulette spin lands on %s, "
|
2018-08-16 10:54:51 +00:00
|
|
|
"%s loses %s" % (choice, event["user"].nickname,
|
|
|
|
str(coin_losses)))
|
2018-10-12 16:40:37 +00:00
|
|
|
|
|
|
|
@utils.hook("timer.coin-interest")
|
|
|
|
def interest(self, event):
|
|
|
|
for server in self.bot.servers.values():
|
2018-10-16 12:58:02 +00:00
|
|
|
all_coins = self._all_coins(server)
|
|
|
|
|
2018-10-12 16:40:37 +00:00
|
|
|
interest_rate = decimal.Decimal(server.get_setting(
|
|
|
|
"interest-rate", DEFAULT_INTEREST_RATE))
|
2018-10-16 13:06:38 +00:00
|
|
|
redeem_amount = self._redeem_amount(server)
|
2018-10-16 12:58:02 +00:00
|
|
|
|
2018-10-16 14:10:21 +00:00
|
|
|
for nickname, coins in all_coins.items():
|
2018-10-12 16:40:37 +00:00
|
|
|
if coins > redeem_amount:
|
|
|
|
interest = round(coins*interest_rate, 2)
|
|
|
|
self._take_from_pool(server, interest)
|
2018-10-18 13:28:48 +00:00
|
|
|
|
|
|
|
wallets = server.get_user_setting(nickname, "wallets", {})
|
2018-10-18 14:18:16 +00:00
|
|
|
default_coins = wallets.get(WALLET_DEFAULT, "0.0")
|
2018-10-18 13:28:48 +00:00
|
|
|
default_coins = decimal.Decimal(default_coins)
|
2018-10-18 14:18:16 +00:00
|
|
|
wallets[WALLET_DEFAULT] = self._coin_str(
|
|
|
|
default_coins+interest)
|
2018-10-18 13:28:48 +00:00
|
|
|
server.set_user_setting(nickname, "wallets", wallets)
|
2018-10-12 16:40:37 +00:00
|
|
|
event["timer"].redo()
|
|
|
|
|
2018-10-13 00:16:18 +00:00
|
|
|
@utils.hook("received.command.lotterybuy", authenticated=True)
|
2018-10-12 16:40:37 +00:00
|
|
|
def lottery_buy(self, event):
|
|
|
|
"""
|
|
|
|
:help: By ticket(s) for the lottery
|
2018-10-20 13:00:28 +00:00
|
|
|
:usage: [amount] [wallet]
|
2018-10-12 16:40:37 +00:00
|
|
|
"""
|
2018-10-19 09:58:36 +00:00
|
|
|
wallet_in, _ = self._default_wallets(event["user"])
|
|
|
|
if len(event["args_split"]) > 0:
|
|
|
|
wallet_in, _ = self._parse_wallets(event["user"],
|
|
|
|
event["args_split"][0])
|
|
|
|
|
2018-10-12 19:20:32 +00:00
|
|
|
amount = 1
|
|
|
|
if event["args_split"]:
|
|
|
|
amount = event["args_split"][0]
|
2018-10-12 16:40:37 +00:00
|
|
|
if not amount.isdigit():
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: Please provide a positive number "
|
2018-10-12 16:40:37 +00:00
|
|
|
"of tickets to buy" % event["user"].nickname)
|
|
|
|
amount = int(amount)
|
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
user_coins = self._get_user_coins(event["user"], wallet_in)
|
2018-10-12 16:40:37 +00:00
|
|
|
coin_amount = decimal.Decimal(LOTTERY_BUYIN)*amount
|
|
|
|
if coin_amount > user_coins:
|
2018-10-16 13:47:01 +00:00
|
|
|
raise utils.EventError("%s: You don't have enough coins" %
|
2018-10-12 16:40:37 +00:00
|
|
|
event["user"].nickname)
|
|
|
|
|
2018-10-19 09:58:36 +00:00
|
|
|
self._take(event["server"], event["user"], coin_amount, wallet_in)
|
2018-10-12 16:40:37 +00:00
|
|
|
|
|
|
|
lottery = event["server"].get_setting("lottery", {})
|
|
|
|
nickname = event["user"].nickname_lower
|
|
|
|
if not nickname in lottery:
|
|
|
|
lottery[nickname] = 0
|
|
|
|
lottery[nickname] += amount
|
|
|
|
event["server"].set_setting("lottery", lottery)
|
|
|
|
|
|
|
|
event["stdout"].write("%s: You bought %d lottery ticket%s for %s" % (
|
|
|
|
event["user"].nickname, amount, "" if amount == 1 else "s",
|
2018-10-16 12:05:55 +00:00
|
|
|
self._coin_str(coin_amount)))
|
2018-10-12 16:40:37 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.mylottery")
|
|
|
|
def my_lottery(self, event):
|
2018-10-16 12:53:32 +00:00
|
|
|
"""
|
|
|
|
:help: Show how many lottery tickets you currently have
|
|
|
|
"""
|
2018-10-12 16:40:37 +00:00
|
|
|
lottery = event["server"].get_setting("lottery", {})
|
|
|
|
count = lottery.get(event["user"].nickname_lower, 0)
|
|
|
|
event["stdout"].write("%s: You have %d lottery ticket%s" % (
|
|
|
|
event["user"].nickname, count, "" if count == 1 else "s"))
|
|
|
|
|
|
|
|
@utils.hook("received.command.jackpot")
|
|
|
|
def jackpot(self, event):
|
2018-10-16 12:53:32 +00:00
|
|
|
"""
|
|
|
|
:help: Show the current lottery jackpot
|
|
|
|
"""
|
2018-10-12 16:40:37 +00:00
|
|
|
lottery = event["server"].get_setting("lottery", {})
|
2018-10-19 10:48:21 +00:00
|
|
|
count = sum(value for nickname, value in lottery.items())
|
2018-10-12 16:40:37 +00:00
|
|
|
event["stdout"].write("%s: The current jackpot is %s" % (
|
|
|
|
event["user"].nickname, decimal.Decimal(LOTTERY_BUYIN)*count))
|
|
|
|
|
2018-10-12 17:33:34 +00:00
|
|
|
@utils.hook("received.command.nextlottery")
|
|
|
|
def next_lottery(self, event):
|
2018-10-16 12:53:32 +00:00
|
|
|
"""
|
|
|
|
:help: Show time until the next lottery draw
|
|
|
|
"""
|
2018-10-12 17:33:34 +00:00
|
|
|
until = self._until_next_6_hour()
|
|
|
|
event["stdout"].write("Next lottery is in: %s" %
|
|
|
|
utils.to_pretty_time(until))
|
|
|
|
|
2018-10-14 12:51:32 +00:00
|
|
|
@utils.hook("received.command.lotterywinner")
|
|
|
|
def lottery_winner(self, event):
|
|
|
|
"""
|
|
|
|
:help: Show who last won the lottery
|
|
|
|
"""
|
|
|
|
winner = event["server"].get_setting("lottery-winner", None)
|
|
|
|
if winner:
|
|
|
|
event["stdout"].write("Last lottery winner: %s" % winner)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("There have been no lottery winners!")
|
|
|
|
|
2018-10-12 16:40:37 +00:00
|
|
|
@utils.hook("timer.coin-lottery")
|
|
|
|
def lottery(self, event):
|
|
|
|
for server in self.bot.servers.values():
|
|
|
|
lottery = server.get_setting("lottery", {})
|
|
|
|
if lottery:
|
|
|
|
server.del_setting("lottery")
|
2018-10-13 00:14:07 +00:00
|
|
|
else:
|
|
|
|
continue
|
2018-10-12 16:40:37 +00:00
|
|
|
|
|
|
|
users = [(nickname,)*value for nickname, value in lottery.items()]
|
2018-10-13 00:14:07 +00:00
|
|
|
users = functools.reduce(lambda x, y: x+y, users)
|
2018-10-12 16:40:37 +00:00
|
|
|
winner = random.choice(users)
|
|
|
|
|
|
|
|
user = server.get_user(winner)
|
2018-10-16 12:05:55 +00:00
|
|
|
coins = self._get_user_coins(user)
|
2018-10-12 16:40:37 +00:00
|
|
|
winnings = decimal.Decimal(LOTTERY_BUYIN)*len(users)
|
2018-10-17 13:18:14 +00:00
|
|
|
new_coins = coins+winnings
|
2018-10-12 16:40:37 +00:00
|
|
|
|
2018-10-16 12:39:45 +00:00
|
|
|
self._give(server, user, winnings)
|
2018-10-14 18:43:46 +00:00
|
|
|
server.set_setting("lottery-winner", user.nickname)
|
2018-10-12 16:40:37 +00:00
|
|
|
user.send_notice("You won %s in the lottery! you now have %s coins"
|
2018-10-16 12:05:55 +00:00
|
|
|
% (self._coin_str(winnings), self._coin_str(new_coins)))
|