forked from kcn3388/fgogacha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fgo_lucky.py
272 lines (238 loc) · 9.83 KB
/
fgo_lucky.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import json.encoder
import os.path
from typing import List
from aiocqhttp import ActionFailed
from hoshino import HoshinoBot
from hoshino.util import DailyNumberLimiter
from .get.get_lucky_bag import get_all_lucky_bag, send_lucky_bag, get_lucky_gacha
from .path_and_json import *
from hoshino.typing import CQEvent
lucky_limit = DailyNumberLimiter(1)
LUCKY_EXCEED_NOTICE = f"您今天已经抽过{lucky_limit.max}次福袋了,欢迎明早5点后再来!"
@sv_lucky.on_fullmatch(("帮助fgo福袋", "帮助FGO福袋", "帮助bgo福袋", "帮助BGO福袋"))
@sv_lucky.on_rex(re.compile(r"^[fb]go[福f][袋d][帮b][助z]$", re.IGNORECASE))
async def bangzhu(bot: HoshinoBot, ev: CQEvent):
if not priv.check_priv(ev, priv.ADMIN):
await bot.finish(ev, '此命令仅群管可用~')
helps = gen_node(sv_lucky_help)
await bot.send_group_forward_msg(group_id=ev['group_id'], messages=helps)
@sv_lucky.on_rex(re.compile(r"^[更g][新x][fb]go[福f][袋d]$", re.IGNORECASE))
async def update_lucky_bag(bot: HoshinoBot, ev: CQEvent):
crt_file = False
group_config = load_config(ev, True)
if not group_config["crt_path"] == "False":
crt_file = os.path.join(crt_folder_path, group_config["crt_path"])
lucky_bag = await get_all_lucky_bag(crt_file)
if isinstance(lucky_bag, dict):
with open(lucky_path, "w", encoding="utf-8") as f:
f.write(json.dumps(lucky_bag, indent=2, ensure_ascii=False))
await bot.finish(ev, "已更新福袋信息")
else:
await bot.finish(ev, "福袋信息获取错误")
@sv_lucky.on_rex(re.compile(
r"^[查c][询x][fb]go[福f][袋d](\s("
r"jp(\s.+)?|日(服)?(\s.+)?|"
r"cn(\s.+)?|国(服)?(\s.+)?|"
r"abstract|概况|"
r"next|未来"
r"))?$",
re.IGNORECASE
))
async def check_lucky_bag(bot: HoshinoBot, ev: CQEvent):
msg = ev.message.extract_plain_text().split()
if len(msg) < 2:
await bot.finish(ev, "食用指南:查询fgo福袋 + 国服/日服/概况/未来/更新")
crt_file = False
group_config = load_config(ev, True)
if not group_config["crt_path"] == "False":
crt_file = os.path.join(crt_folder_path, group_config["crt_path"])
if not os.path.exists(lucky_path):
sv_lucky.logger.info("初始化数据json...")
open(lucky_path, 'w')
lucky_bag = {
"abstract": "",
"cn": [],
"jp": []
}
else:
try:
lucky_bag = json.load(open(lucky_path, encoding="utf-8"))
except json.decoder.JSONDecodeError:
lucky_bag = {
"abstract": "",
"cn": [],
"jp": []
}
if re.match(r"jp|日(服)?|cn|国(服)?", msg[1]):
if len(lucky_bag['jp']) == 0:
await bot.finish(ev, "请先获取福袋:[更新fgo福袋]")
if len(msg) < 3:
await bot.finish(
ev,
f"食用指南:查询fgo福袋 + 国服/日服 + 编号/全部\n国服已结束的福袋召唤:{len(lucky_bag['cn'])}个;\n"
f"日服已结束的福袋召唤:{len(lucky_bag['jp'])}个;\n"
f"国服千里眼福袋:{len(lucky_bag['jp']) - len(lucky_bag['cn'])}个"
)
lucky_id: str = msg[2]
if re.match(r"jp|日(服)?", msg[1]):
try:
if lucky_id.isdigit():
select_lucky: Dict = lucky_bag['jp'][int(lucky_id) - 1]
else:
if re.match(r"all|全部", lucky_id):
select_lucky: List = lucky_bag['jp']
else:
await bot.send(ev, "编号错误")
return
except IndexError:
await bot.send(ev, "编号错误")
return
else:
try:
if lucky_id.isdigit():
select_lucky: Dict = lucky_bag['cn'][int(lucky_id) - 1]
else:
if re.match(r"all|全部", lucky_id):
select_lucky: List = lucky_bag['cn']
else:
await bot.send(ev, "编号错误")
return
except IndexError:
await bot.send(ev, "编号错误")
return
lucky_nodes = await send_lucky_bag(select_lucky, crt_file)
try:
await bot.send_group_forward_msg(group_id=ev['group_id'], messages=lucky_nodes)
return
except ActionFailed:
await bot.finish(ev, "合并转发失败,请尝试获取单独福袋信息")
if re.match(r"abstract|概况", msg[1]):
if not lucky_bag["abstract"]:
await bot.finish(ev, "请先获取福袋:[更新fgo福袋]")
abstract_msg = gen_node(lucky_bag["abstract"].strip())
try:
await bot.send_group_forward_msg(group_id=ev['group_id'], messages=abstract_msg)
return
except ActionFailed:
await bot.finish(ev, lucky_bag["abstract"].strip())
if re.match(r"next|未来", msg[1]):
if len(lucky_bag['jp']) == 0:
await bot.finish(ev, "请先获取福袋:[更新fgo福袋]")
select_lucky: List = lucky_bag['jp'][-2:]
lucky_nodes = await send_lucky_bag(select_lucky, crt_file, is_next=True)
try:
await bot.send_group_forward_msg(group_id=ev['group_id'], messages=lucky_nodes)
return
except ActionFailed:
await bot.finish(ev, "合并转发失败,请尝试获取单独福袋信息")
@sv_lucky.on_rex(re.compile(
r"^[抽c][fb]go[福f][袋d](\s("
r"jp((\s\d+)+)?|日(服)?((\s\d+)+)?|"
r"cn((\s\d+)+)?|国(服)?((\s\d+)+)?"
r"))?$", re.IGNORECASE
))
async def gacha_lucky_bag(bot: HoshinoBot, ev: CQEvent):
if not lucky_limit.check(f"{ev.user_id}@{ev.group_id}"):
await bot.finish(ev, LUCKY_EXCEED_NOTICE, at_sender=True)
lucky_limit.increase(f"{ev.user_id}@{ev.group_id}", 1)
msg = ev.message.extract_plain_text().split()
if len(msg) < 3:
await bot.finish(ev, "食用指南:抽fgo福袋 + 国服/日服 + 福袋编号 + 子池子编号(默认为1)")
try:
lucky_bag = json.load(open(lucky_path, encoding="utf-8"))
except Exception as e:
sv_lucky.logger.error(f"获取福袋数据出错:{e}")
await bot.send(ev, "获取福袋数据出错,请先获取福袋:[更新fgo福袋]")
return
lucky_id = msg[2]
sub_id = msg[3] if len(msg) > 3 else 1
server = "国服"
if re.match(r"jp|日(服)?", msg[1]):
server = "日服"
try:
select_lucky: Dict = lucky_bag['jp'][int(lucky_id) - 1]
except IndexError:
await bot.send(ev, "编号错误")
return
else:
try:
select_lucky: Dict = lucky_bag['cn'][int(lucky_id) - 1]
except IndexError:
await bot.send(ev, "编号错误")
return
if "detail" not in select_lucky:
await bot.finish(ev, "该卡池不支持模拟抽卡")
try:
select_lucky_pool = select_lucky["detail"][int(sub_id) - 1]
except IndexError:
await bot.send(ev, "编号错误")
return
gacha_result = await get_lucky_gacha(select_lucky_pool)
img_path = []
get_pup5_id = []
get_pup4_id = []
get_5_id = []
get_4_id = []
for each in select_lucky_pool["servants"]:
if each["type"] == "svt_5":
for each_id in each["ids"]:
get_5_id.append([0, int(each_id)])
if each["type"] == "svt_4":
for each_id in each["ids"]:
get_4_id.append([0, int(each_id)])
for each in gacha_result:
if each[0] == "svt":
svt = int(each[2])
for each_card in get_pup5_id:
if each_card[1] == svt:
each_card[0] += 1
for each_card in get_pup4_id:
if each_card[1] == svt:
each_card[0] += 1
for each_card in get_5_id:
if each_card[1] == svt:
each_card[0] += 1
for each_card in get_4_id:
if each_card[1] == svt:
each_card[0] += 1
img_path.append(os.path.join(svt_path, f"Servant{str(svt).zfill(3)}.jpg"))
if each[0] == "cft":
cft = int(each[2])
img_path.append(os.path.join(cft_path, f"礼装{str(cft).zfill(3)}.jpg"))
group_config = load_config(ev, True)
style = group_config["style"]
# 文字图标版,更快
if not style == "图片":
cards = []
for each in img_path:
cards.append(Image.open(each).resize((66, 72)))
rows = 3
cols = 4
base_img = Image.open(frame_path).resize(((66 * cols) + 40, (72 * rows) + 40))
r_counter = 0
c_counter = 0
for each in cards:
base_img.paste(each, ((66 * c_counter) + 20, (72 * r_counter) + 20))
c_counter += 1
if c_counter >= cols:
r_counter += 1
if r_counter >= rows:
break
else:
c_counter = 0
else:
# 图片版,较慢
if server == "国服":
base_img = Image.open(back_cn_path).convert("RGBA")
else:
base_img = Image.open(back_path).convert("RGBA")
masker = Image.open(mask_path).resize((width, height))
for i, pic_path in enumerate(img_path):
tmp_img = Image.open(pic_path).resize((width, height))
tmp_img = tmp_img.convert('RGBA')
base_img.paste(tmp_img, box_list[i], mask=masker)
msg = f"\n抽取的福袋:{select_lucky['name']}\n" \
f"抽取的子福袋:{select_lucky_pool['sub_title']}\n" \
f"抽卡结果:\n" \
f"{gen_ms_img(base_img)}\n"
await bot.send(ev, msg.strip(), at_sender=True)