Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dungeon.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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", desire_priority=0, namecn="文字冒险", desc="A plugin to play dungeon game", version="1.0", author="lanvent")
  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. trigger_prefix = conf().get('plugin_trigger_prefix', "$")
  55. if clist[0] == f"{trigger_prefix}停止冒险":
  56. if sessionid in self.games:
  57. self.games[sessionid].reset()
  58. del self.games[sessionid]
  59. reply = Reply(ReplyType.INFO, "冒险结束!")
  60. e_context['reply'] = reply
  61. e_context.action = EventAction.BREAK_PASS
  62. elif clist[0] == f"{trigger_prefix}开始冒险" or sessionid in self.games:
  63. if sessionid not in self.games or clist[0] == f"{trigger_prefix}开始冒险":
  64. if len(clist)>1 :
  65. story = clist[1]
  66. else:
  67. story = "你在树林里冒险,指不定会从哪里蹦出来一些奇怪的东西,你握紧手上的手枪,希望这次冒险能够找到一些值钱的东西,你往树林深处走去。"
  68. self.games[sessionid] = StoryTeller(bot, sessionid, story)
  69. reply = Reply(ReplyType.INFO, "冒险开始,你可以输入任意内容,让故事继续下去。故事背景是:" + story)
  70. e_context['reply'] = reply
  71. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  72. else:
  73. prompt = self.games[sessionid].action(content)
  74. e_context['context'].type = ContextType.TEXT
  75. e_context['context'].content = prompt
  76. e_context.action = EventAction.BREAK # 事件结束,不跳过处理context的默认逻辑
  77. def get_help_text(self, **kwargs):
  78. help_text = "可以和机器人一起玩文字冒险游戏。\n"
  79. if kwargs.get('verbose') != True:
  80. return help_text
  81. trigger_prefix = conf().get('plugin_trigger_prefix', "$")
  82. help_text = f"{trigger_prefix}开始冒险 "+"背景故事: 开始一个基于{背景故事}的文字冒险,之后你的所有消息会协助完善这个故事。\n"+f"{trigger_prefix}停止冒险: 结束游戏。\n"
  83. if kwargs.get('verbose') == True:
  84. help_text += f"\n命令例子: '{trigger_prefix}开始冒险 你在树林里冒险,指不定会从哪里蹦出来一些奇怪的东西,你握紧手上的手枪,希望这次冒险能够找到一些值钱的东西,你往树林深处走去。'"
  85. return help_text