瀏覽代碼

Add ALL and FRAC options to rollcoin

Kirk Trombley 4 年之前
父節點
當前提交
cd091bce05
共有 1 個文件被更改,包括 35 次插入1 次删除
  1. 35 1
      src/plugins/rollcoin.py

+ 35 - 1
src/plugins/rollcoin.py

@@ -70,12 +70,17 @@ def balance(wallet: Singleton(RollcoinWallet), holdings: Singleton(RollcoinGambl
     return f"You have {wallet.balance} Rollcoins in your wallet and {holdings.balance} Rollcoins in the market"
 
 
+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 - not {raw_amount}").raise_exc()
+        RollbotFailure.INVALID_ARGUMENTS.with_reason(f"Amount should be a number or ALL or FRAC - not {raw_amount}").raise_exc()
     return amount, rest
 
 
@@ -84,6 +89,10 @@ def assert_positive(amount):
         RollbotFailure.INVALID_ARGUMENTS.with_reason(f"Amount should be positive").raise_exc()
 
 
+def fractional_part(number):
+    return float("0." + str(float(number)).split(".")[1])
+
+
 @with_help("Tip someone else some Rollcoins: !tip target amount")
 @as_plugin
 def tip(args: ArgList, 
@@ -95,6 +104,13 @@ def tip(args: ArgList,
 
     target, *rest = args
     amount, _ = pop_amount_arg(rest)
+    if amount in SPECIAL_AMOUNTS:
+        if amount == "ALL":
+            amount = sender_wallet.balance
+        elif amount == "FRAC":
+            amount = fractional_part(sender_wallet.balance)
+        else:
+            amount = -1
     assert_positive(amount)
 
     target_id = wallets.get(target.lower(), None)
@@ -126,6 +142,13 @@ def donate(args: ArgList,
         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
+        elif amount == "FRAC":
+            amount = fractional_part(sender_wallet.balance)
+        else:
+            amount = -1
     assert_positive(amount)
 
     if sender_wallet.balance < amount:
@@ -249,6 +272,17 @@ def gamble(db: Database,
         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
+        elif amount == "FRAC":
+            amount = fractional_part(sender_wallet.balance)
+        elif amount == "-ALL":
+            amount = -sender_holdings.balance
+        elif amount == "-FRAC":
+            amount = -fractional_part(sender_holdings.balance)
+        else:
+            raise ValueError(amount)
 
     if amount == 0:
         RollbotFailure.INVALID_ARGUMENTS.with_reason(f"Amount should be nonzero").raise_exc()