|
@@ -4,8 +4,9 @@ import pickle
|
|
|
|
|
|
from sudoku_py import SudokuGenerator, Sudoku
|
|
|
|
|
|
-from rollbot import as_plugin, with_help, with_startup, as_sender_singleton, as_group_singleton, RollbotFailure
|
|
|
-from rollbot.plugins.decorators.arg_wiring import Database, Config, ArgList, Singleton, Message, Lazy, Query
|
|
|
+from rollbot import as_plugin, with_help, as_sender_singleton, as_group_singleton, RollbotFailure
|
|
|
+from rollbot.plugins.decorators import with_startup, require_min_args, require_args
|
|
|
+from rollbot.plugins.decorators.arg_wiring import Database, Config, Arg, Singleton, Message, Lazy, Query
|
|
|
|
|
|
@as_sender_singleton
|
|
|
class RollcoinWallet:
|
|
@@ -73,15 +74,10 @@ def balance(wallet: Singleton(RollcoinWallet), holdings: Singleton(RollcoinGambl
|
|
|
SPECIAL_AMOUNTS = ("ALL", "FRAC", "-ALL", "-FRAC")
|
|
|
|
|
|
|
|
|
-def pop_amount_arg(args):
|
|
|
- raw_amount, *rest = args
|
|
|
- if (up_amount := raw_amount.upper()) in SPECIAL_AMOUNTS:
|
|
|
- return up_amount, rest
|
|
|
- try:
|
|
|
- amount = float(raw_amount)
|
|
|
- except ValueError:
|
|
|
- RollbotFailure.INVALID_ARGUMENTS.with_reason(f"Amount should be a number or ALL or FRAC - not {raw_amount}").raise_exc()
|
|
|
- return amount, rest
|
|
|
+def parse_amount(amount):
|
|
|
+ if (up_amount := amount.upper()) in SPECIAL_AMOUNTS:
|
|
|
+ return up_amount
|
|
|
+ return float(amount)
|
|
|
|
|
|
|
|
|
def assert_positive(amount):
|
|
@@ -93,8 +89,8 @@ def fractional_part(number):
|
|
|
return float("0." + str(float(number)).split(".")[1])
|
|
|
|
|
|
|
|
|
-def lookup_target(args: ArgList, wallets: Config("rollcoin.wallets")):
|
|
|
- target_id = wallets.get(args[0].lower(), None)
|
|
|
+def lookup_target(target_name: Arg(0), wallets: Config("rollcoin.wallets")):
|
|
|
+ target_id = wallets.get(target_name.lower(), None)
|
|
|
if target_id is None:
|
|
|
RollbotFailure.INVALID_ARGUMENTS.with_reason(f"Could not find wallet-holder {target}").raise_exc()
|
|
|
|
|
@@ -102,14 +98,12 @@ def lookup_target(args: ArgList, wallets: Config("rollcoin.wallets")):
|
|
|
|
|
|
|
|
|
@with_help("Tip someone else some Rollcoins: !tip target amount")
|
|
|
+@require_min_args(2, "Tip requires 2 arguments - target and amount")
|
|
|
@as_plugin
|
|
|
-def tip(args: ArgList,
|
|
|
+def tip(target_name: Arg(0),
|
|
|
+ amount: Arg(1, parse_amount, "Amount should be a number or ALL or FRAC - not {}"),
|
|
|
sender_wallet: Singleton(RollcoinWallet),
|
|
|
target_wallet: Lazy(Singleton(RollcoinWallet).by(lookup_target))):
|
|
|
- if len(args) < 2:
|
|
|
- RollbotFailure.INVALID_ARGUMENTS.with_reason("Tip requires 2 arguments - target and amount").raise_exc()
|
|
|
-
|
|
|
- amount, _ = pop_amount_arg(args[1:])
|
|
|
if amount in SPECIAL_AMOUNTS:
|
|
|
if amount == "ALL":
|
|
|
amount = sender_wallet.balance
|
|
@@ -128,7 +122,7 @@ def tip(args: ArgList,
|
|
|
|
|
|
sender_wallet.balance = sender_wallet.balance - amount
|
|
|
target_wallet.balance = target_wallet.balance + amount
|
|
|
- return f"Done! {args[0]} now has {target_wallet.balance} Rollcoins"
|
|
|
+ return f"Done! {target_name} now has {target_wallet.balance} Rollcoins"
|
|
|
|
|
|
|
|
|
def get_non_sender_ids(wallets: Config("rollcoin.wallets"), msg: Message):
|
|
@@ -136,14 +130,11 @@ def get_non_sender_ids(wallets: Config("rollcoin.wallets"), msg: Message):
|
|
|
|
|
|
|
|
|
@with_help("Donate money to be distributed evenly among all wallets")
|
|
|
+@require_min_args(1, "Need an amount to donate")
|
|
|
@as_plugin
|
|
|
-def donate(args: ArgList,
|
|
|
+def donate(amount: Arg(0, parse_amount, "Amount should be a number or ALL or FRAC - not {}"),
|
|
|
sender_wallet: Singleton(RollcoinWallet),
|
|
|
other_wallets: Lazy(Singleton(RollcoinWallet).by_all(get_non_sender_ids))):
|
|
|
- if len(args) < 1:
|
|
|
- return RollbotFailure.INVALID_ARGUMENTS.with_reason("Need an amount to donate")
|
|
|
-
|
|
|
- amount, _ = pop_amount_arg(args)
|
|
|
if amount in SPECIAL_AMOUNTS:
|
|
|
if amount == "ALL":
|
|
|
amount = sender_wallet.balance
|
|
@@ -256,8 +247,9 @@ def not_sender_gambling_wallet(msg: Message):
|
|
|
|
|
|
|
|
|
@with_help("Gamble some Rollcoins")
|
|
|
+@require_args(1, "Gambling requires exactly one argument: the amount to transfer (can be positive or negative)")
|
|
|
@as_plugin
|
|
|
-def gamble(args: ArgList,
|
|
|
+def gamble(amount: Arg(0, parse_amount, "Amount should be a number or ALL or FRAC - not {}"),
|
|
|
sender_wallet: Singleton(RollcoinWallet),
|
|
|
sender_holdings: Singleton(RollcoinGamblingWallet),
|
|
|
market: Singleton(RollcoinMarket),
|
|
@@ -270,10 +262,6 @@ def gamble(args: ArgList,
|
|
|
# - current market state
|
|
|
# - rng
|
|
|
# market enters new state and affects all gambling wallets, multiplying their balances
|
|
|
- if len(args) != 1:
|
|
|
- RollbotFailure.INVALID_ARGUMENTS.with_reason("Gambling requires exactly one argument: the amount to transfer (can be positive or negative)").raise_exc()
|
|
|
-
|
|
|
- amount, _ = pop_amount_arg(args)
|
|
|
if amount in SPECIAL_AMOUNTS:
|
|
|
if amount == "ALL":
|
|
|
amount = sender_wallet.balance
|