Explorar o código

Implement appraisal

Kirk Trombley %!s(int64=3) %!d(string=hai) anos
pai
achega
0677a9ce67
Modificáronse 1 ficheiros con 46 adicións e 3 borrados
  1. 46 3
      commands/commands/rollcoin.py

+ 46 - 3
commands/commands/rollcoin.py

@@ -1,11 +1,13 @@
 from dataclasses import dataclass, field
 from typing import Optional
+from logging import Logger
 import random
+import json
 
 from sudoku_py import SudokuGenerator, Sudoku
 
 from rollbot import as_command, initialize_data, RollbotFailure
-from rollbot.injection import Data, Sender, Config, Const, Arg, Reply, OriginAdmin, Args
+from rollbot.injection import Data, Sender, Config, Const, Arg, Reply, OriginAdmin, Args, Attachments, Origin
 
 # * = TODO
 # View
@@ -13,7 +15,7 @@ from rollbot.injection import Data, Sender, Config, Const, Arg, Reply, OriginAdm
 #   !blockchain - shows the contents of all wallets and the rollcoins in the treasury
 # Generate
 #   !mine - provides a sudoku that can be solved for rollcoins, which also adds to the treasury
-#   * !appraise - rollbot will purchase a post, based indirectly on number of likes received
+#   !appraise - rollbot will purchase a post, based indirectly on number of likes received
 # Spend
 #   !tip - transfer rollcoins from one person to another
 #   * !gacha - insert a rollcoin (which enters the treasury), receive a random NFT
@@ -33,8 +35,9 @@ from rollbot.injection import Data, Sender, Config, Const, Arg, Reply, OriginAdm
 @dataclass
 class RollcoinState:
     treasury: float
-    mining_puzzle: Optional[list[list[Optional[int]]]]
+    mining_puzzle: Optional[list[list[int]]]
     market_state: int
+    appraised: list[str] = field(default_factory=list)
 
 
 @initialize_data
@@ -354,6 +357,46 @@ async def mine(
     yield f"The current mining challenge is\n{Sudoku(board=state.mining_puzzle)}"
 
 
+@as_command
+async def appraise(
+    logger: Logger,
+    origin: Origin,
+    attachments: Attachments,
+    sender_id: Sender,
+    sender_wallet: SenderWallet,
+    wallet_store: Data(RollcoinWallet),
+    state: State,
+    state_store: Data(RollcoinState),
+    reply: Reply,
+):
+    if origin == "GROUPME":
+        try:
+            reply_attachment = next(a for a in attachments if a.name == "reply")
+            target_msg = json.loads(await reply_attachment.body())
+            appraisal_id = f"GROUPME-{target_msg['group_id']}-{target_msg['id']}"
+            score_base = len(target_msg["favorited_by"])
+        except:
+            logger.exception("Failed appraisal, logging for debugging")
+            RollbotFailure.INVALID_ARGUMENTS.raise_exc("Reply to a message to have it appraised, I could not read that one")
+    # other origins an be handled here
+    else:
+        RollbotFailure.INVALID_COMMAND.raise_exc(f"Message appraisal is not implemented in this platform (origin was {origin})")
+
+    if appraisal_id in state.appraised:
+        return "Sorry, I've already purchased that message!", reply
+
+    if score_base <= 2:
+        return "Sorry, I don't think that message is worth very much...", reply
+
+    value = round(abs(random.gauss(score_base * 50, 15)), 2)
+    sender_wallet.balance += value
+    await wallet_store.save(sender_id, sender_wallet)
+    state.appraised.append(appraisal_id)
+    await state_store.save(GLOBAL_STATE_KEY, state)
+
+    return f"That post is very interesting! I will purchase it for {value} RollCoins! That brings your balance to {sender_wallet.balance}", reply
+
+
 # ADMIN COMMANDS
 
 @as_command