lintonxue00 commited on
Commit
9263509
1 Parent(s): 56c9fb4

Upload __init__.py

Browse files
Files changed (1) hide show
  1. 不知道/回收站/__init__.py +175 -0
不知道/回收站/__init__.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import random
3
+ import itertools
4
+ import traceback
5
+ from typing import List, Tuple, Optional
6
+
7
+ from nonebot import on_command
8
+ from nonebot.rule import to_me
9
+ from nonebot.typing import T_State
10
+ from nonebot.plugin import PluginMetadata
11
+ from nonebot.params import ArgPlainText
12
+ from nonebot.adapters.onebot.v11 import (
13
+ Bot,
14
+ MessageEvent,
15
+ GroupMessageEvent,
16
+ )
17
+ from nonebot.log import logger
18
+
19
+ from .life import Life
20
+ from .talent import Talent
21
+
22
+ __plugin_meta__ = PluginMetadata(
23
+ name="人生重开",
24
+ description="人生重开模拟器",
25
+ usage="@我 remake/liferestart/人生重开",
26
+ extra={
27
+ "unique_name": "remake",
28
+ "example": "@小Q remake",
29
+ "author": "meetwq <[email protected]>",
30
+ "version": "0.2.7",
31
+ },
32
+ )
33
+
34
+
35
+ remake = on_command(
36
+ "remake",
37
+ aliases={"liferestart", "人生重开", "人生重来"},
38
+ block=True,
39
+ rule=to_me(),
40
+ priority=12,
41
+ )
42
+
43
+
44
+ @remake.handle()
45
+ async def _(state: T_State):
46
+ life_ = Life()
47
+ life_.load()
48
+ talents = life_.rand_talents(10)
49
+ state["life"] = life_
50
+ state["talents"] = talents
51
+ msg = "请发送编号选择3个天赋,如“0 1 2”,或发送“随机”随机选择"
52
+ des = "\n".join([f"{i}.{t}" for i, t in enumerate(talents)])
53
+ await remake.send(f"{msg}\n\n{des}")
54
+
55
+
56
+ @remake.got("nums")
57
+ async def _(state: T_State, reply: str = ArgPlainText("nums")):
58
+ def conflict_talents(talents: List[Talent]) -> Optional[Tuple[Talent, Talent]]:
59
+ for (t1, t2) in itertools.combinations(talents, 2):
60
+ if t1.exclusive_with(t2):
61
+ return t1, t2
62
+ return None
63
+
64
+ life_: Life = state["life"]
65
+ talents: List[Talent] = state["talents"]
66
+
67
+ match = re.fullmatch(r"\s*(\d)\s*(\d)\s*(\d)\s*", reply)
68
+ if match:
69
+ nums = list(match.groups())
70
+ nums = [int(n) for n in nums]
71
+ nums.sort()
72
+ if nums[-1] >= 10:
73
+ await remake.reject("请发送正确的编号")
74
+
75
+ talents_selected = [talents[n] for n in nums]
76
+ ts = conflict_talents(talents_selected)
77
+ if ts:
78
+ await remake.reject(f"你选择的天赋“{ts[0].name}”和“{ts[1].name}”不能同时拥有,请重新选择")
79
+ elif reply == "随机":
80
+ while True:
81
+ nums = random.sample(range(10), 3)
82
+ nums.sort()
83
+ talents_selected = [talents[n] for n in nums]
84
+ if not conflict_talents(talents_selected):
85
+ break
86
+ elif re.fullmatch(r"[\d\s]+", reply):
87
+ await remake.reject("请发送正确的编号,如“0 1 2”")
88
+ else:
89
+ await remake.finish("人生重开已取消")
90
+
91
+ life_.set_talents(talents_selected)
92
+ state["talents_selected"] = talents_selected
93
+
94
+ msg = (
95
+ "请发送4个数字分配“颜值、智力、体质、家境”4个属性,如“5 5 5 5”,或发送“随机”随机选择;"
96
+ f"可用属性点为{life_.total_property()},每个属性不能超过10"
97
+ )
98
+ await remake.send(msg)
99
+
100
+
101
+ @remake.got("prop")
102
+ async def _(
103
+ bot: Bot,
104
+ event: MessageEvent,
105
+ state: T_State,
106
+ reply: str = ArgPlainText("prop"),
107
+ ):
108
+ life_: Life = state["life"]
109
+ talents: List[Talent] = state["talents_selected"]
110
+ total_prop = life_.total_property()
111
+
112
+ match = re.fullmatch(r"\s*(\d{1,2})\s+(\d{1,2})\s+(\d{1,2})\s+(\d{1,2})\s*", reply)
113
+ if match:
114
+ nums = list(match.groups())
115
+ nums = [int(n) for n in nums]
116
+ if sum(nums) != total_prop:
117
+ await remake.reject(f"属性之和需为{total_prop},请重新发送")
118
+ elif max(nums) > 10:
119
+ await remake.reject("每个属性不能超过10,请重新发送")
120
+ elif reply == "随机":
121
+ half_prop1 = int(total_prop / 2)
122
+ half_prop2 = total_prop - half_prop1
123
+ num1 = random.randint(0, half_prop1)
124
+ num2 = random.randint(0, half_prop2)
125
+ nums = [num1, num2, half_prop1 - num1, half_prop2 - num2]
126
+ random.shuffle(nums)
127
+ elif re.fullmatch(r"[\d\s]+", reply):
128
+ await remake.reject("请发送正确的数字,如“5 5 5 5”")
129
+ else:
130
+ await remake.finish("人生重开已取消")
131
+
132
+ prop = {"CHR": nums[0], "INT": nums[1], "STR": nums[2], "MNY": nums[3]}
133
+ life_.apply_property(prop)
134
+
135
+ await remake.send("你的人生正在重开...")
136
+
137
+ msgs = [
138
+ "已选择以下天赋:\n" + "\n".join([str(t) for t in talents]),
139
+ "已设置如下属性:\n" + f"颜值{nums[0]} 智力{nums[1]} 体质{nums[2]} 家境{nums[3]}",
140
+ ]
141
+ try:
142
+ life_msgs = []
143
+ for s in life_.run():
144
+ life_msgs.append("\n".join(s))
145
+ n = 5
146
+ life_msgs = [
147
+ "\n\n".join(life_msgs[i : i + n]) for i in range(0, len(life_msgs), n)
148
+ ]
149
+ msgs.extend(life_msgs)
150
+ msgs.append(life_.gen_summary())
151
+ await send_forward_msg(bot, event, "人生重开模拟器", bot.self_id, msgs)
152
+ except:
153
+ logger.warning(traceback.format_exc())
154
+ await remake.finish("你的人生重开失败(")
155
+
156
+
157
+ async def send_forward_msg(
158
+ bot: Bot,
159
+ event: MessageEvent,
160
+ name: str,
161
+ uin: str,
162
+ msgs: List[str],
163
+ ):
164
+ def to_json(msg):
165
+ return {"type": "node", "data": {"name": name, "uin": uin, "content": msg}}
166
+
167
+ messages = [to_json(msg) for msg in msgs]
168
+ if isinstance(event, GroupMessageEvent):
169
+ await bot.call_api(
170
+ "send_group_forward_msg", group_id=event.group_id, messages=messages
171
+ )
172
+ else:
173
+ await bot.call_api(
174
+ "send_private_forward_msg", user_id=event.user_id, messages=messages
175
+ )