lintonxue00 commited on
Commit
4547a0c
1 Parent(s): ea97d2d

Upload __init__.py

Browse files
Files changed (1) hide show
  1. 不知道/回收站/3/__init__.py +176 -0
不知道/回收站/3/__init__.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="#人生重开",
26
+ extra={
27
+ "unique_name": "remake",
28
+ "example": "#人生重开",
29
+ "author": "meetwq <meetwq@gmail.com>",
30
+ "version": "0.2.7",
31
+ },
32
+ )
33
+
34
+ remake = on_command(
35
+ "#人生重开",
36
+ aliases={"liferestart", "人生重开", "人生重来"},
37
+ block=True,
38
+ priority=12,
39
+ )
40
+
41
+
42
+
43
+ @remake.handle()
44
+ async def _(state: T_State):
45
+ life_ = Life()
46
+ life_.load()
47
+ talents = life_.rand_talents(10)
48
+ state["life"] = life_
49
+ state["talents"] = talents
50
+ msg = "请发送编号选择4个天赋,如“1 2 3 4”,或发送“随机”随机选择"
51
+ des = "\n".join([f"{i}.{t}" for i, t in enumerate(talents)])
52
+ await remake.send(f"{msg}\n\n{des}")
53
+
54
+
55
+ @remake.got("nums")
56
+ async def _(state: T_State, reply: str = ArgPlainText("nums")):
57
+ def conflict_talents(talents: List[Talent]) -> Optional[Tuple[Talent, Talent]]:
58
+ for (t1, t2) in itertools.combinations(talents, 2):
59
+ if t1.exclusive_with(t2):
60
+ return t1, t2
61
+ return None
62
+
63
+ life_: Life = state["life"]
64
+ talents: List[Talent] = state["talents"]
65
+
66
+ match = re.fullmatch(r"\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*", reply)
67
+ if match:
68
+ nums = list(match.groups())
69
+ nums = [int(n) for n in nums]
70
+ nums.sort()
71
+ if nums[-1] >= 16:
72
+ await remake.reject("请发送正确的编号")
73
+
74
+ talents_selected = [talents[n] for n in nums]
75
+ ts = conflict_talents(talents_selected)
76
+ if ts:
77
+ await remake.reject(f"你选择的天赋“{ts[0].name}”和“{ts[1].name}”不能同时拥有,请重新选择")
78
+ elif reply == "随机":
79
+ while True:
80
+ nums = random.sample(range(16), 4)
81
+ nums.sort()
82
+ talents_selected = [talents[n] for n in nums]
83
+ if not conflict_talents(talents_selected):
84
+ break
85
+ elif re.fullmatch(r"[\d\s]+", reply):
86
+ await remake.reject("请发送正确的编号,如“1 2 3 4”")
87
+ else:
88
+ await remake.finish("人生重开已取消")
89
+
90
+ life_.set_talents(talents_selected)
91
+ state["talents_selected"] = talents_selected
92
+
93
+
94
+ msg = (
95
+ "请发送4个数字分配“颜值、智力、体质、家境”4个属性,如“10 10 5 5”,或发送“随机”随机选择;"
96
+ f"可用属性点为{life_.total_property()},每个属性不能超过"20"
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() * 1.5
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) > 20:
119
+ await remake.reject("每个属性不能超过20,请重新发送")
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("请发送正确的数字,如“10 10 5 5”")
129
+ else:
130
+ await remake.finish("人生重开已取消")
131
+
132
+
133
+ prop = {"CHR": nums[0], "INT": nums[1], "STR": nums[2], "MNY": nums[3]}
134
+ life_.apply_property(prop)
135
+
136
+ await remake.send("你的人生正在重开...")
137
+
138
+ msgs = [
139
+ "已选择以下天赋:\n" + "\n".join([str(t) for t in talents]),
140
+ "已设置如下属性:\n" + f"颜值{nums[0]} 智力{nums[1]} 体质{nums[2]} 家境{nums[3]}",
141
+ ]
142
+ try:
143
+ life_msgs = []
144
+ for s in life_.run():
145
+ life_msgs.append("\n".join(s))
146
+ n = 5
147
+ life_msgs = [
148
+ "\n\n".join(life_msgs[i : i + n]) for i in range(0, len(life_msgs), n)
149
+ ]
150
+ msgs.extend(life_msgs)
151
+ msgs.append(life_.gen_summary())
152
+ await send_forward_msg(bot, event, "人生重开模拟器", bot.self_id, msgs)
153
+ except:
154
+ logger.warning(traceback.format_exc())
155
+ await remake.finish("你的人生重开失败(")
156
+
157
+
158
+ async def send_forward_msg(
159
+ bot: Bot,
160
+ event: MessageEvent,
161
+ name: str,
162
+ uin: str,
163
+ msgs: List[str],
164
+ ):
165
+ def to_json(msg):
166
+ return {"type": "node", "data": {"name": name, "uin": uin, "content": msg}}
167
+
168
+ messages = [to_json(msg) for msg in msgs]
169
+ if isinstance(event, GroupMessageEvent):
170
+ await bot.call_api(
171
+ "send_group_forward_msg", group_id=event.group_id, messages=messages
172
+ )
173
+ else:
174
+ await bot.call_api(
175
+ "send_private_forward_msg", user_id=event.user_id, messages=messages
176
+ )