You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.4KB

  1. # encoding:utf-8
  2. from bridge.bridge import Bridge
  3. from bridge.context import ContextType
  4. from bridge.reply import Reply, ReplyType
  5. import plugins
  6. from plugins import *
  7. from common.log import logger
  8. # https://github.com/bupticybee/ChineseAiDungeonChatGPT
  9. class StoryTeller():
  10. def __init__(self, bot, sessionid, story):
  11. self.bot = bot
  12. self.sessionid = sessionid
  13. bot.sessions.clear_session(sessionid)
  14. self.first_interact = True
  15. self.story = story
  16. def reset(self):
  17. self.bot.sessions.clear_session(self.sessionid)
  18. self.first_interact = True
  19. def action(self, user_action):
  20. if user_action[-1] != "。":
  21. user_action = user_action + "。"
  22. if self.first_interact:
  23. prompt = """现在来充当一个冒险文字游戏,描述时候注意节奏,不要太快,仔细描述各个人物的心情和周边环境。一次只需写四到六句话。
  24. 开头是,""" + self.story + " " + user_action
  25. self.first_interact = False
  26. else:
  27. prompt = """继续,一次只需要续写四到六句话,总共就只讲5分钟内发生的事情。""" + user_action
  28. return prompt
  29. @plugins.register(name="Dungeon", desc="A plugin to play dungeon game", version="1.0", author="lanvent", desire_priority= 0)
  30. class Dungeon(Plugin):
  31. def __init__(self):
  32. super().__init__()
  33. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  34. logger.info("[Dungeon] inited")
  35. self.games = {}
  36. def on_handle_context(self, e_context: EventContext):
  37. if e_context['context'].type != ContextType.TEXT:
  38. return
  39. bottype = Bridge().get_bot_type("chat")
  40. if bottype != "chatGPT":
  41. return
  42. bot = Bridge().get_bot("chat")
  43. content = e_context['context'].content[:]
  44. clist = e_context['context'].content.split(maxsplit=1)
  45. sessionid = e_context['context']['session_id']
  46. logger.debug("[Dungeon] on_handle_context. content: %s" % clist)
  47. if clist[0] == "$停止冒险":
  48. if sessionid in self.games:
  49. self.games[sessionid].reset()
  50. del self.games[sessionid]
  51. reply = Reply(ReplyType.INFO, "冒险结束!")
  52. e_context['reply'] = reply
  53. e_context.action = EventAction.BREAK_PASS
  54. elif clist[0] == "$开始冒险" or sessionid in self.games:
  55. if sessionid not in self.games or clist[0] == "$开始冒险":
  56. if len(clist)>1 :
  57. story = clist[1]
  58. else:
  59. story = "你在树林里冒险,指不定会从哪里蹦出来一些奇怪的东西,你握紧手上的手枪,希望这次冒险能够找到一些值钱的东西,你往树林深处走去。"
  60. self.games[sessionid] = StoryTeller(bot, sessionid, story)
  61. reply = Reply(ReplyType.INFO, "冒险开始,你可以输入任意内容,让故事继续下去。故事背景是:" + story)
  62. e_context['reply'] = reply
  63. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  64. else:
  65. prompt = self.games[sessionid].action(content)
  66. e_context['context'].type = ContextType.TEXT
  67. e_context['context'].content = prompt
  68. e_context.action = EventAction.CONTINUE