-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
305 lines (264 loc) · 11.7 KB
/
main.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import base64
from enum import Flag
import io
import json
import os
import random
import traceback
from decimal import *
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
from hoshino import Service, priv, util
from hoshino.typing import CQEvent, MessageSegment
from .avatar import *
from .r_calc import calc_r
from .rrank import get_tops
from .register import register
from .best20image import *
from .bomb import BombApi
sv = Service('Dynamite', manage_priv=priv.ADMIN, enable_on_default=True, visible=True)
bomb = BombApi("http://43.142.173.63:10483/bomb/v2")
def search_files(directory, file_extension):
file_list = []
for root, sub_dirs, files in os.walk(directory):
for file_name in files:
if file_name.endswith(file_extension):
file_list.append(os.path.join(root, file_name))
return file_list
def img2b64(img: Image.Image) -> str:
bytesio = BytesIO()
img.save(bytesio, 'PNG')
image_bytes = bytesio.getvalue()
base64_str = base64.b64encode(image_bytes).decode()
return 'base64://' + base64_str
def get_account(qq_id: str):
account_path = os.path.join(os.path.dirname(__file__), 'account.json')
with open(account_path, "r", encoding='utf8') as f:
account = json.load(f)
try:
return True, account[qq_id]["uuid"]
except Exception:
return False, ""
@sv.on_prefix(('/dyreg'))
async def command_register_admin(bot, ev: CQEvent):
admin_list = ["2737723325", "3070190799", "1580885360", "520127606", "1255710289", "2539990262"]
qq_id = ev.user_id
if(str(qq_id) not in admin_list):
await bot.finish(ev, f'您不是Explode管理员,请找管理员注册账号', at_sender=True)
return
args = ev.message.extract_plain_text().strip().split()
try:
username = args[0]
password = args[1]
except:
await bot.finish(ev, f'格式为/dyreg 昵称 密码', at_sender=True)
return
flag, msg = register(username, password)
if(flag == True):
await bot.finish(ev, f'玩家{username}注册成功,uid为{msg}', at_sender=True)
else:
await bot.finish(ev, f'注册时出现问题: {msg}', at_sender=True)
@sv.on_prefix(('/dya', '/dyavatar'))
async def command_avatar_upload(bot, ev: CQEvent):
qq_id = ev.user_id
flag, user_id = get_account(str(qq_id))
if not flag:
await bot.finish(ev, f'您还未绑定,请用/dybind指令绑定', at_sender=True)
return
flag = upload_avatar(qq_id, user_id)
username = bomb.get_user(user_id)["username"]
if(flag == True):
await bot.finish(ev, f'玩家{username}已更新为Q{qq_id}的头像', at_sender=True)
else:
await bot.finish(ev, f'头像更新失败', at_sender=True)
# @sv.on_prefix(('/dyleaderboard', '/dyl'))
# async def command_dyleaderboard(bot, ev: CQEvent):
# await bot.send(ev, "正在更新排行榜")
# tops = get_tops()
# msg = "Explode Leaderboard:\n"
# for i in range(0, len(tops)):
# msg += f"#{i + 1}\n"
# msg += f"{tops[i]['username']} R:{tops[i]['R']} \n"
# await bot.finish(ev, msg)
@sv.on_prefix(('/dyR', '/dyr'))
async def command_r_calc(bot, ev: CQEvent):
args = ev.message.extract_plain_text().strip().split()
if len(args) == 2:
try:
rating = float(args[0])
acc = float(args[1]) / 100
except Exception:
await bot.finish(ev, f'请输入合法的参数', at_sender=True)
return
if acc < 0.93 or rating < 5.6:
r = int(acc * 100 / 2)
else:
chebyshev = Decimal(calc_r(rating, acc)).quantize(Decimal("1"), rounding="ROUND_HALF_UP")
r = max(chebyshev, Decimal(acc * 100 / 2))
await bot.finish(ev, f'计算后R值:{r}\n误差为±1R', at_sender=True)
elif len(args) == 4:
try:
rating = float(args[0])
perfect = int(args[1])
good = int(args[2])
miss = int(args[3])
except Exception:
await bot.finish(ev, f'请输入合法的参数', at_sender=True)
return
acc = (perfect + good / 2) / (perfect + good + miss)
if acc < 0.93 or rating < 5.6:
r = int(acc * 100 / 2)
else:
chebyshev = Decimal(calc_r(rating, acc)).quantize(Decimal("1"), rounding="ROUND_HALF_UP")
r = max(chebyshev, Decimal(acc * 100 / 2))
await bot.finish(ev, f'计算后R值:{r}\n误差为±1R', at_sender=True)
else:
await bot.finish(ev, f'目前有两种计算方式\n/dyR 定数 Acc\n/dyR 定数 perfect good miss', at_sender=True)
@sv.on_prefix(('/dy绑定', '/dybind'))
async def command_bind(bot, ev: CQEvent):
qq_id = ev.user_id
username = ev.message.extract_plain_text().strip()
username = username.strip()
if(username == ""):
await bot.finish(ev, f"绑定失败,请输入您的账号昵称")
try:
account_path = os.path.join(os.path.dirname(__file__), 'account.json')
with open(account_path, "r", encoding='utf8') as f:
account = json.load(f)
user_id = bomb.get_user_by_name(username)["id"]
try:
old_name = account[str(qq_id)]["name"]
account[str(qq_id)]["uuid"] = user_id
account[str(qq_id)]["name"] = username
await bot.send(ev, f"Q{qq_id}已成功换绑{username}\n原账号:{old_name}", at_sender=True)
except Exception:
account[str(qq_id)] = {}
account[str(qq_id)]["uuid"] = user_id
account[str(qq_id)]["name"] = username
await bot.send(ev, f"Q{qq_id}已成功绑定{username}", at_sender=True)
with open(account_path, 'w', encoding='utf8') as f:
json.dump(account, f, indent=4, ensure_ascii=False)
except Exception as e:
traceback.print_exc()
await bot.send(ev, f"绑定失败,{e}")
@sv.on_prefix(('/dyrecent', '/dyre', '/exre', '/exrecent'))
async def command_dyrecentpic(bot, ev: CQEvent):
qq_id = ev.user_id
args = ev.message.extract_plain_text().strip().split()
# if(str(qq_id) == "2307957938"):
# await bot.finish(ev, f'不能查询NyaBye130的Best20成绩', at_sender=True)
if len(args) == 1:
username = args[0]
user_id = bomb.get_user_by_name(username)["id"]
else:
flag, user_id = get_account(str(qq_id))
if not flag:
await bot.finish(ev, f'您还未绑定,请用/dybind指令绑定', at_sender=True)
return
else:
username = bomb.get_user(user_id)["username"]
await bot.send(ev, f'正在查询{username}的Recent成绩', at_sender=True)
try:
recentDict = bomb.get_user_recent_records(user_id)
record = recentDict[0]
score = record["score"]
perfect = record["perfect"]
good = record["good"]
miss = record["miss"]
chartId = record["chartId"]
try:
chart_info = bomb.get_chart(chartId)
# print(chart_info)
except Exception:
await bot.finish(ev, f"查询时出现错误:{Exception}")
return
difficultyClass = chart_info["difficultyClass"]
difficultyValue = chart_info["difficultyValue"]
difficultyText = get_difficulty_class_text(difficultyClass)
# print(chart_info)
try:
setInfo = bomb.get_set(chart_info["parentSetId"])
except Exception:
await bot.finish(ev, f"查询时出现错误:{Exception}")
return
set_id = setInfo["id"]
musicName = setInfo["musicName"]
r = Decimal(record["r"] or 0).quantize(Decimal("1"), rounding="ROUND_HALF_UP")
try:
accuracy = Decimal((perfect + good * 0.5) / (perfect + good + miss) * 100.0).quantize(Decimal("0.01"),
rounding="ROUND_HALF_UP")
except:
accuracy = "0.00"
illustration_path = os.path.join(resource_path, f"{set_id}.webp")
try:
illustration_image = get_illustration_image(illustration_path, 408, 230)
except Exception:
try:
url = f"http://43.142.173.63:5244/d/download/cover/480x270_jpg/{set_id}"
downloadimg(url, illustration_path)
illustration_image = get_illustration_image(illustration_path, 408, 230)
# print(set_id)
# f"http://43.142.173.63:5244/d/download/cover/480x270_jpg/{set_id}"
except Exception:
await bot.finish(ev, f"查询时出现错误:{Exception}")
return
img = MessageSegment.image(util.pic2b64(illustration_image))
await bot.send(ev, f"{img}\n{musicName} {difficultyText}{difficultyValue}\n{score} {accuracy}% R:{r}\n{perfect}-{good}-{miss}")
except Exception as e:
await bot.send(ev, f"查询时出现错误:{e}")
@sv.on_prefix(('/dyb20', '/b20', '/exb20'))
async def command_dyb20pic(bot, ev: CQEvent):
qq_id = ev.user_id
args = ev.message.extract_plain_text().strip().split()
# if(str(qq_id) == "2307957938"):
# await bot.finish(ev, f'不能查询NyaBye130的Best20成绩', at_sender=True)
if len(args) == 1:
username = args[0]
user_id = bomb.get_user_by_name(username)["id"]
else:
flag, user_id = get_account(str(qq_id))
if not flag:
await bot.finish(ev, f'您还未绑定,请用/dybind指令绑定', at_sender=True)
return
else:
# print(bomb.get_user(user_id))
username = bomb.get_user(user_id)["username"]
await bot.send(ev, f'正在查询{username}的Best20成绩', at_sender=True)
try:
img = await draw_best20(bomb, user_id)
base64str = img2b64(img)
msg = MessageSegment.image(base64str)
await bot.send(ev, msg)
except Exception as e:
await bot.send(ev, f"查询时出现错误:{e}")
# await bot.send(ev, f'[CQ:image,file={img}]', at_sender=True)
# await bot.finish(ev, f'{msg}', at_sender=True)
@sv.on_prefix(('/NyaBye130', '/nyabye130', '/NyaBye', '/nyabye', '/喵拜'))
async def command_nyabye(bot, ev: CQEvent):
args = ev.message.extract_plain_text().strip().split()
flag = True
try:
if args[0] == "overdose":
res_path = os.path.join(os.path.dirname(__file__), "res")
record_path = os.path.join(res_path, "NYABYE130 OVERDOSE.wav")
voice_rec = MessageSegment.record(f'file:///{os.path.abspath(record_path)}')
flag = False
await bot.finish(ev, voice_rec)
except Exception:
flag = True
if(flag == True):
res_path = os.path.join(os.path.dirname(__file__), "res")
record_path = os.path.join(res_path, "NyaBye130")
file_list = search_files(record_path, "wav")
record_path = random.choice(file_list)
voice_rec = MessageSegment.record(f'file:///{os.path.abspath(record_path)}')
await bot.send(ev, voice_rec)
@sv.on_prefix(('/aplo', '/Aplo', '/Aploplex', '/aploplex'))
async def command_aplo(bot, ev: CQEvent):
args = ev.message.extract_plain_text().strip().split()
res_path = os.path.join(os.path.dirname(__file__), "res")
record_path = os.path.join(res_path, "Aploplex")
file_list = search_files(record_path, "wav")
record_path = random.choice(file_list)
voice_rec = MessageSegment.record(f'file:///{os.path.abspath(record_path)}')
await bot.send(ev, voice_rec)