Browse Source

Implement donate

Kirk Trombley 3 years ago
parent
commit
085574db9a
1 changed files with 34 additions and 1 deletions
  1. 34 1
      commands/commands/rollcoin.py

+ 34 - 1
commands/commands/rollcoin.py

@@ -15,7 +15,7 @@ from rollbot.injection import Data, Sender, Config, Const, Arg, Reply, OriginAdm
 # Spend
 #   !tip - transfer rollcoins from one person to another
 #   * !gacha - insert a rollcoin (which enters the treasury), receive a random NFT
-#   * !donate - split an amount and donate it to everyone
+#   !donate - split an amount and donate it to everyone
 # Market
 #   !bet - put some amount of rollcoins into the market, some portion of which will be put in the treasury, evolves market
 #   !cash - take some amount of rollcoins from the market, up to investment + the number of coins in the treasury, evolves market
@@ -127,6 +127,39 @@ async def tip(
     return f"Done! {target_name} now has {target_wallet.balance} RollCoins!"
 
 
+@as_command
+async def donate(
+    wallets: Data(RollcoinWallet),
+    sender_id: Sender,
+    sender_wallet: SenderWallet,
+    wallet_store: Data(RollcoinWallet),
+    amount: Arg(0, convert=convert_amount, missing_msg="You must provide an amount to donate!", fail_msg="Could not parse {} as value"),
+):
+    if not isinstance(amount, float):
+        # handle special converters
+        amount = amount(sender_wallet.balance)
+
+    if sender_wallet.balance == 0:
+        return "Sorry! You don't have any rollcoins right now - try mining!"
+
+    if amount > sender_wallet.balance:
+        return f"Sorry! You only have {sender_wallet.balance} RollCoins available - try mining for more!"
+
+    if amount <= 0:
+        RollbotFailure.INVALID_ARGUMENTS.raise_exc(f"Amount must be positive, not {amount}")
+
+    sender_wallet.balance -= amount
+    await wallet_store.save(sender_id, sender_wallet)
+
+    wallets = [(wid, w) async for wid, w in wallet_store.all() if wid != sender_id]
+    to_donate = amount / len(wallets)
+    for (wallet_id, wallet) in wallets:
+        wallet.balance += to_donate
+        await wallet_store.save(wallet_id, wallet)
+
+    return f"Done! Donated {to_donate} to each other wallet, leaving you with {sender_wallet.balance} RollCoins!"
+
+
 async def evolve_market(state, transitions, multipliers, wallet_store, state_store):
     state.market_state = random.choices(range(len(transitions)), weights=transitions[state.market_state], k=1)[0]
     await state_store.save(GLOBAL_STATE_KEY, state)