Browse Source

Rewrite blockchain to do queries during dep injection

Kirk Trombley 4 years ago
parent
commit
45abce240e
1 changed files with 14 additions and 9 deletions
  1. 14 9
      src/plugins/rollcoin.py

+ 14 - 9
src/plugins/rollcoin.py

@@ -6,7 +6,7 @@ from sudoku_py import SudokuGenerator, Sudoku
 
 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, require_admin
-from rollbot.plugins.injection import Database, Config, Arg, Singleton, Message, Lazy, Query
+from rollbot.plugins.injection import Config, Arg, Singleton, Message, Lazy, Query
 
 @as_sender_singleton
 class RollcoinWallet:
@@ -322,21 +322,26 @@ def gamble(amount: Arg(0, parse_amount, "Amount should be a number or ALL or FRA
 
 @with_help("View the whole blockchain (i.e., who has how many rollcoins)")
 @as_plugin
-def blockchain(db: Database, wallets: Config("rollcoin.wallets")):
+def blockchain(
+    names: Config("rollcoin.wallets"),
+    wallets: Query(RollcoinWallet),
+    holdings: Query(RollcoinGamblingWallet)):
+    wallets = {w.sender_id: w.balance for w in wallets}
+    holdings = {w.sender_id: w.balance for w in holdings}
     results = []
-    for name, wallet_id in wallets.items():
-        wallet = db.query(RollcoinWallet).get(wallet_id)
-        market = db.query(RollcoinGamblingWallet).get(wallet_id)
+    for name, wallet_id in names.items():
+        wallet = wallets.get(wallet_id, None)
+        market = holdings.get(wallet_id, None)
         status = name.title() + "\n"
         total = 0
         if wallet is not None:
-            status += f"\t\tWallet: {wallet.balance}\n"
-            total += wallet.balance
+            status += f"\t\tWallet: {wallet}\n"
+            total += wallet
         else:
             status += "\t\tHas not used their rollcoin wallet\n"
         if market is not None:
-            status += f"\t\tMarket: {market.balance}\n"
-            total += market.balance
+            status += f"\t\tMarket: {market}\n"
+            total += market
         else:
             status += "\t\tHas not invested their rollcoins\n"
         if total != 0: