Explorar o código

Some refactoring to hangguy using the new rollbot features

Kirk Trombley %!s(int64=5) %!d(string=hai) anos
pai
achega
934c0ee44c
Modificáronse 1 ficheiros con 40 adicións e 91 borrados
  1. 40 91
      src/plugins/hangguy.py

+ 40 - 91
src/plugins/hangguy.py

@@ -4,7 +4,7 @@ import base64
 
 from sqlalchemy import Column, Integer, String, DateTime
 
-from rollbot import as_plugin, RollbotResponse, RollbotFailure, ModelBase, pop_arg
+from rollbot import as_plugin, RollbotResponse, RollbotFailure, ModelBase, pop_arg, as_group_singleton
 
 from config import get_secret
 
@@ -138,20 +138,17 @@ GUY_STAGES = [
 ,
 ]
 
+@as_group_singleton
+class HangguyGameState:
+    target: str = None
+    state: str = None
+    bad_guesses: str = ""
 
-class GameState(ModelBase):
-    __tablename__ = "hangguy_game_state"
-    group_id = Column(String, primary_key=True)
-    target = Column(String)
-    state = Column(String)
-    bad_guesses = Column(String)
 
-
-class GuyState(ModelBase):
-    __tablename__ = "hangguy_guy_state"
-    group_id = Column(String, primary_key=True)
-    state = Column(Integer)
-    games = Column(Integer)
+@as_group_singleton
+class HangguyGuyState:
+    state: int = 0
+    games: int = 0
 
 
 class DeadGuy(ModelBase):
@@ -176,31 +173,6 @@ class DeadGuy(ModelBase):
         )
 
 
-def get_game(db, group_id):
-    game_state = db.query(GameState).get(group_id)
-    if game_state is None:
-        game_state = GameState(
-            group_id=group_id,
-            target=None,
-            state=None,
-            bad_guesses=""
-        )
-        db.add(game_state)
-    return game_state
-
-
-def get_guy(db, group_id):
-    guy_state = db.query(GuyState).get(group_id)
-    if guy_state is None:
-        guy_state = GuyState(
-            group_id=group_id,
-            state=0,
-            games=0
-        )
-        db.add(guy_state)
-    return guy_state
-
-
 def init_game(game_state, phrase):
     if phrase is None:
         new_phrase = "TEST PHRASE" # TODO this needs to actually pull from something (and sanitize it) eventually
@@ -273,24 +245,7 @@ def render_game(game_state, guy_state):
         "Bad Guesses: " + ", ".join(game_state.bad_guesses)
 
 
-def handle_in_game(bot, db, msg, game_state, guy_state):
-    if msg.raw_args.startswith("!"):
-        subc = msg.raw_args[1:].strip().lower()
-        if subc == "cancel":
-            end_game(game_state)
-            return "The game has been cancelled. The guy has not been reset."
-        elif subc == "alert":
-            return subc_alert(bot, msg)
-        else:
-            return RollbotResponse(
-                msg,
-                failure=RollbotFailure.INVALID_SUBCOMMAND,
-                debugging=dict(
-                    explain="The only in-game subcommands are !cancel to end the game and !alert to alert the main chat."
-                )
-            )
-
-    guess = msg.raw_args.upper()
+def handle_guess(db, guess, game_state, guy_state):
     prefix = ""
     if len(guess) == 1:
         if guess.isalpha():
@@ -338,48 +293,42 @@ def subc_retire(db, guy_state):
 
 def subc_alert(bot, msg):
     bot.manually_post_message(f"{msg.name} wants you to check out the hangguy chat!", get_secret("hangguy.alert_chat"))
-    return RollbotResponse(msg, respond=False)
+    return None # do not respond in the current chat
 
 
 @as_plugin
-def hangguy(bot, db, msg):
-    game_state = get_game(db, msg.group_id)
-    guy_state = get_guy(db, msg.group_id)
-
+def hangguy(bot, db, msg, subc, game_data: HangguyGameState, guy_data: HangguyGuyState):
     if msg.raw_args is None:
-        return RollbotResponse(msg, failure=RollbotFailure.MISSING_SUBCOMMAND)
+        return RollbotFailure.MISSING_SUBCOMMAND.with_reason("Must provide subcommand or guess")
 
-    if game_is_active(game_state):
-        return handle_in_game(bot, db, msg, game_state, guy_state)
-
-    if not msg.raw_args.startswith("!"):
-        return RollbotResponse(
-            msg,
-            failure=RollbotFailure.MISSING_SUBCOMMAND,
-            debugging=dict(
-                explain="You are not currently in a game, and so you must use one of the subcommands: !view, !retire, !start, !alert"
+    if game_is_active(game_data):
+        if subc is None:
+            return handle_guess(db, msg.raw_args.upper(), game_data, guy_data)   
+        elif subc.command == "cancel":
+            end_game(game_data)
+            return "The game has been cancelled. The guy has not been reset."
+        elif subc.command == "alert":
+            return subc_alert(bot, msg)
+        else:
+            return RollbotFailure.INVALID_SUBCOMMAND.with_reason(
+                "The only in-game subcommands are !cancel to end the game and !alert to alert the main chat."
             )
-        )
-
-    subc, rest = pop_arg(msg.raw_args[1:])
 
-    subc = subc.lower()
-
-    if subc == "view":
-        return f"The current guy has {survival_msg(guy_state)}\n" \
-            + render_guy(guy_state)
-    elif subc == "retire":
-        return subc_retire(db, guy_state)
-    elif subc == "start":
-        init_game(game_state, rest)
-        return render_game(game_state, guy_state)
-    elif subc == "alert":
+    if subc is None:
+        return RollbotFailure.MISSING_SUBCOMMAND.with_reason(
+            "You are not currently in a game, and so you must use one of the subcommands: !view, !retire, !start, !alert"
+        )
+    elif subc.command == "view":
+        return f"The current guy has {survival_msg(guy_data)}\n" \
+            + render_guy(guy_data)
+    elif subc.command == "retire":
+        return subc_retire(db, guy_data)
+    elif subc.command == "start":
+        init_game(game_data, subc.raw_args)
+        return render_game(game_data, guy_data)
+    elif subc.command == "alert":
         return subc_alert(bot, msg)
     else:
-        return RollbotResponse(
-            msg,
-            failure=RollbotFailure.INVALID_SUBCOMMAND,
-            debugging=dict(
-                explain="You must use one of the subcommands: !view, !retire, !start, !alert"
-            )
+        return RollbotFailure.INVALID_SUBCOMMAND.with_reason(
+            "You must use one of the subcommands: !view, !retire, !start, !alert"
         )