Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

86 lines
4.1KB

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