Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/maimai/__init__.py

363 lines
15 KiB
Python
Raw Normal View History

from core.builtins import Bot, Plain, Image as BImage
2023-06-12 05:51:36 +00:00
from core.builtins import command_prefix
2023-03-04 08:51:56 +00:00
from core.component import module
2022-08-04 07:52:42 +00:00
from core.logger import Logger
2023-06-05 04:10:27 +00:00
from core.utils.image import msgchain2image
2023-06-09 05:18:08 +00:00
from modules.maimai.libraries.maimai_best_50 import generate
2023-06-19 16:37:14 +00:00
from modules.maimai.libraries.maimaidx_api_data import get_alias
2023-06-20 11:20:34 +00:00
from modules.maimai.libraries.maimaidx_project import get_rank, get_player_score
2021-11-12 14:25:53 +00:00
from modules.maimai.libraries.maimaidx_music import *
2023-06-19 16:37:14 +00:00
from .regex import *
2021-10-12 15:02:36 +00:00
2022-05-13 13:54:12 +00:00
total_list = TotalList()
2023-06-02 01:48:55 +00:00
diff_label = ['Basic', 'Advanced', 'Expert', 'Master', 'Re:MASTER']
2023-06-01 17:08:10 +00:00
diff_label_abbr = ['bas', 'adv', 'exp', 'mas', 'rem']
2023-05-28 05:57:27 +00:00
diff_label_zhs = ['绿', '', '', '', '']
2023-05-28 06:20:26 +00:00
diff_label_zht = ['', '', '']
2023-05-28 05:57:27 +00:00
2022-05-13 13:54:12 +00:00
2021-10-12 15:02:36 +00:00
def song_txt(music: Music):
return [Plain(f"{music.id}\u200B. {music.title}{' (DX)' if music['type'] == 'DX' else ''}\n"),
2023-06-09 04:36:40 +00:00
BImage(f"https://www.diving-fish.com/covers/{get_cover_len5_id(music.id)}.png", ),
2023-06-05 09:34:41 +00:00
Plain(f"\n{'/'.join(str(ds) for ds in music.ds)}")]
2021-10-12 15:02:36 +00:00
2023-06-12 05:51:36 +00:00
def get_diff(diff):
2023-06-01 17:08:10 +00:00
diff = diff.lower()
2023-06-02 01:48:55 +00:00
diff_label_lower = [label.lower() for label in diff_label]
2023-06-09 10:12:08 +00:00
2023-05-28 05:57:27 +00:00
if diff in diff_label_zhs:
level = diff_label_zhs.index(diff)
elif diff in diff_label_zht:
level = diff_label_zht.index(diff)
2023-06-01 17:08:10 +00:00
elif diff in diff_label_abbr:
level = diff_label_abbr.index(diff)
2023-06-02 01:48:55 +00:00
elif diff in diff_label_lower:
level = diff_label_lower.index(diff)
2023-05-28 05:57:27 +00:00
else:
2023-05-28 06:04:14 +00:00
level = None
2023-05-28 05:57:27 +00:00
return level
2021-10-12 15:02:36 +00:00
2023-06-01 17:08:10 +00:00
mai = module('maimai', developers=['mai-bot', 'OasisAkari', 'DoroWolf'], alias='mai',
desc='{maimai.help.desc}')
2021-10-24 10:55:45 +00:00
2023-05-27 12:44:55 +00:00
@mai.handle('level <level> {{maimai.help.level}}')
2023-06-09 10:12:08 +00:00
async def _(msg: Bot.MessageSession, level: str):
2023-05-27 12:46:03 +00:00
result_set = await diff_level_q(level)
2023-05-27 12:44:55 +00:00
s = msg.locale.t("maimai.message.level", level=level) + "\n"
for elem in result_set:
s += f"{elem[0]}\u200B. {elem[1]}{' (DX)' if elem[5] == 'DX' else ''} {elem[3]} {elem[4]} ({elem[2]})\n"
2023-05-27 13:15:53 +00:00
if len(result_set) == 0:
return await msg.finish(msg.locale.t("maimai.message.music_not_found"))
2023-05-28 02:20:17 +00:00
if len(result_set) <= 10:
await msg.finish(s.strip())
2023-05-28 02:15:08 +00:00
else:
2023-06-05 04:10:27 +00:00
img = await msgchain2image([Plain(s)])
2023-05-28 02:15:08 +00:00
await msg.finish([BImage(img)])
2023-06-09 10:12:08 +00:00
2023-05-28 04:18:15 +00:00
async def diff_level_q(level):
result_set = []
music_data = (await total_list.get()).filter(level=level)
for music in sorted(music_data, key=lambda i: int(i['id'])):
for i in music.diff:
2023-06-09 10:12:08 +00:00
result_set.append(
(music['id'],
music['title'],
2023-06-10 04:23:59 +00:00
music['ds'][i],
diff_label[i],
music['level'][i],
music['type']))
2023-05-28 04:18:15 +00:00
return result_set
2023-05-27 12:44:55 +00:00
2023-06-19 16:37:14 +00:00
@mai.handle('base <rating> [<rating_max>] {{maimai.help.base}}')
2023-06-09 10:12:08 +00:00
async def _(msg: Bot.MessageSession, rating: float, rating_max: float = None):
2023-05-27 10:35:36 +00:00
if rating_max is not None:
if rating > rating_max:
return await msg.finish(msg.locale.t('error.range.invalid'))
2023-06-19 16:37:14 +00:00
result_set = await base_level_q(rating, rating_max)
s = msg.locale.t("maimai.message.base.range", rating=round(rating, 1), rating_max=round(rating_max, 1)) + "\n"
2023-05-27 10:35:36 +00:00
else:
2023-06-19 16:37:14 +00:00
result_set = await base_level_q(rating)
s = msg.locale.t("maimai.message.base", rating=round(rating, 1)) + "\n"
2021-10-12 15:02:36 +00:00
for elem in result_set:
s += f"{elem[0]}\u200B. {elem[1]}{' (DX)' if elem[5] == 'DX' else ''} {elem[3]} {elem[4]} ({elem[2]})\n"
2023-05-27 13:23:52 +00:00
if len(result_set) == 0:
return await msg.finish(msg.locale.t("maimai.message.music_not_found"))
2023-05-27 10:23:41 +00:00
if len(result_set) > 200:
2023-05-27 03:33:49 +00:00
return await msg.finish(msg.locale.t("maimai.message.too_much", length=len(result_set)))
2023-05-28 02:20:17 +00:00
if len(result_set) <= 10:
await msg.finish(s.strip())
2023-05-28 02:15:08 +00:00
else:
2023-06-05 04:10:27 +00:00
img = await msgchain2image([Plain(s)])
2023-05-28 02:15:08 +00:00
await msg.finish([BImage(img)])
2021-10-12 15:02:36 +00:00
2023-06-09 10:12:08 +00:00
2023-06-19 16:37:14 +00:00
async def base_level_q(ds1, ds2=None):
2023-05-28 04:18:15 +00:00
result_set = []
if ds2 is not None:
music_data = (await total_list.get()).filter(ds=(ds1, ds2))
else:
music_data = (await total_list.get()).filter(ds=ds1)
for music in sorted(music_data, key=lambda i: int(i['id'])):
for i in music.diff:
2023-06-09 10:12:08 +00:00
result_set.append(
(music['id'],
music['title'],
2023-06-10 04:23:59 +00:00
music['ds'][i],
diff_label[i],
music['level'][i],
music['type']))
2023-05-28 04:18:15 +00:00
return result_set
@mai.handle('search <keyword> {{maimai.help.search}}')
2023-06-09 10:12:08 +00:00
async def _(msg: Bot.MessageSession, keyword: str):
name = keyword.strip()
2023-05-28 04:51:41 +00:00
res = (await total_list.get()).filter(title_search=name)
if len(res) == 0:
2023-05-28 04:18:15 +00:00
return await msg.finish(msg.locale.t("maimai.message.music_not_found"))
2023-05-28 04:51:41 +00:00
if len(res) > 200:
2023-06-01 06:25:50 +00:00
return await msg.finish(msg.locale.t("maimai.message.too_much", length=len(res)))
2023-05-28 04:18:15 +00:00
else:
2023-05-31 14:45:50 +00:00
search_result = msg.locale.t("maimai.message.search", keyword=name) + "\n"
2023-05-28 04:51:41 +00:00
for music in sorted(res, key=lambda i: int(i['id'])):
search_result += f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"
2023-05-28 04:51:41 +00:00
if len(res) <= 10:
await msg.finish([Plain(search_result.strip())])
else:
2023-06-05 04:10:27 +00:00
img = await msgchain2image([Plain(search_result)])
2023-05-28 04:51:41 +00:00
await msg.finish([BImage(img)])
2023-05-28 04:18:15 +00:00
2021-10-12 15:02:36 +00:00
2023-06-19 16:37:14 +00:00
@mai.handle('alias <sid> {{maimai.help.alias}}')
async def _(msg: Bot.MessageSession, sid: str):
if not sid.isdigit():
await msg.finish(msg.locale.t('maimai.message.error.non_digital'))
music = (await total_list.get()).by_id(sid)
if not music:
return await msg.finish(msg.locale.t("maimai.message.music_not_found"))
title = f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}"
2023-06-19 16:37:14 +00:00
alias = await get_alias(sid)
if len(alias) == 0:
return await msg.finish(msg.locale.t("maimai.message.alias_not_found"))
else:
result = msg.locale.t("maimai.message.alias", title=title) + "\n"
result += "\n".join(alias)
await msg.finish([Plain(result.strip())])
2023-06-20 11:20:34 +00:00
@mai.handle('b50 [<username>] {{maimai.help.b50}}')
2023-06-09 10:12:08 +00:00
async def _(msg: Bot.MessageSession, username: str = None):
2023-06-08 14:55:40 +00:00
if username is None and msg.target.senderFrom == "QQ":
2023-06-09 05:18:08 +00:00
payload = {'qq': msg.session.sender, 'b50': True}
2023-06-08 14:55:40 +00:00
else:
if username is None:
await msg.finish(msg.locale.t("maimai.message.no_username"))
2023-06-09 05:18:08 +00:00
payload = {'username': username, 'b50': True}
2023-06-20 11:20:34 +00:00
img = await generate(msg, payload)
await msg.finish([BImage(img)])
2023-06-20 12:48:34 +00:00
@mai.handle('info <id_or_alias> [<username>] {{maimai.help.info}}')
2023-06-20 11:20:34 +00:00
async def _(msg: Bot.MessageSession, id_or_alias: str, username: str = None):
if id_or_alias.isdigit():
sid = id_or_alias
2023-06-08 14:55:40 +00:00
else:
2023-06-20 11:20:34 +00:00
sid_list = await get_alias(id_or_alias, get_music=True)
if len(sid_list) == 0:
await msg.finish(msg.locale.t("maimai.message.music_not_found"))
elif len(sid_list) > 1:
res = msg.locale.t("maimai.message.song.prompt") + "\n"
for sid in sorted(sid_list, key=int):
s = (await total_list.get()).by_id(sid)
res += f"{s['id']}\u200B. {s['title']}{' (DX)' if s['type'] == 'DX' else ''}\n"
2023-06-20 11:20:34 +00:00
await msg.finish(res.strip())
else:
sid = str(sid_list[0])
2023-06-08 14:55:40 +00:00
2023-06-20 11:20:34 +00:00
music = (await total_list.get()).by_id(sid)
if music is None:
await msg.finish(msg.locale.t("maimai.message.music_not_found"))
2023-06-08 14:55:40 +00:00
2023-05-19 15:00:36 +00:00
if username is None and msg.target.senderFrom == "QQ":
2023-06-20 11:20:34 +00:00
payload = {'qq': msg.session.sender}
2022-07-12 17:27:34 +00:00
else:
2023-05-19 15:00:36 +00:00
if username is None:
2023-05-21 06:41:50 +00:00
await msg.finish(msg.locale.t("maimai.message.no_username"))
2023-06-20 11:20:34 +00:00
payload = {'username': username}
2023-06-20 11:31:55 +00:00
output = await get_player_score(msg, payload, sid)
2023-06-20 11:20:34 +00:00
file = f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"
await msg.finish(
[Plain(f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"),
2023-06-20 11:20:34 +00:00
BImage(f"{file}"), Plain(output)])
2023-06-19 16:37:14 +00:00
@mai.handle('rank [<username>] {{maimai.help.rank}}')
async def _(msg: Bot.MessageSession, username: str = None):
if username is None and msg.target.senderFrom == "QQ":
payload = {'qq': msg.session.sender}
2022-07-12 17:27:34 +00:00
else:
2023-06-19 16:37:14 +00:00
if username is None:
await msg.finish(msg.locale.t("maimai.message.no_username"))
payload = {'username': username}
result = await get_rank(msg, payload)
time, total_rank, average_rating, username, rating, rank, surpassing_rate = result
formatted_average_rating = "{:.4f}".format(average_rating)
formatted_surpassing_rate = "{:.2f}".format(surpassing_rate)
await msg.finish(msg.locale.t('maimai.message.rank', time=time, total_rank=total_rank, user=username,
rating=rating, rank=rank, average_rating=formatted_average_rating,
surpassing_rate=formatted_surpassing_rate))
2023-05-21 06:41:50 +00:00
2023-05-27 03:20:04 +00:00
2023-06-12 05:11:36 +00:00
@mai.handle('random <diff+level> [<dx_type>] {{maimai.help.random.filter}}')
async def _(msg: Bot.MessageSession, dx_type: str = None):
filter = msg.parsed_msg['<diff+level>']
2023-06-12 04:35:02 +00:00
level = ''
diff = ''
2023-06-05 09:01:14 +00:00
try:
2023-06-12 05:11:36 +00:00
if dx_type in ["dx", "DX"]:
dx_type = ["DX"]
elif dx_type in ["sd", "SD", "标准", "標準"]:
dx_type = ["SD"]
2023-06-05 09:01:14 +00:00
else:
2023-06-12 05:11:36 +00:00
dx_type = ["SD", "DX"]
2023-06-05 09:01:14 +00:00
2023-06-12 04:35:02 +00:00
for char in filter:
if char.isdigit() or char == '+':
level += char
else:
diff += char
if level == "":
2023-06-05 10:47:13 +00:00
if diff == "#":
2023-06-12 05:11:36 +00:00
music_data = (await total_list.get()).filter(type=dx_type)
2023-06-05 09:01:14 +00:00
else:
2023-06-05 10:03:38 +00:00
raise ValueError
2023-06-05 10:47:13 +00:00
else:
2023-06-12 04:35:02 +00:00
if diff == "":
2023-06-12 05:11:36 +00:00
music_data = (await total_list.get()).filter(level=level, type=dx_type)
2023-06-05 10:03:38 +00:00
else:
2023-06-12 05:51:36 +00:00
music_data = (await total_list.get()).filter(level=level, diff=[get_diff(diff)], type=dx_type)
2023-06-05 09:01:14 +00:00
if len(music_data) == 0:
rand_result = msg.locale.t("maimai.message.music_not_found")
else:
rand_result = song_txt(music_data.random())
await msg.finish(rand_result)
except Exception as e:
Logger.error(e)
await msg.finish(msg.locale.t("maimai.message.random.error"))
2023-05-27 12:15:10 +00:00
@mai.handle('random {{maimai.help.random}}')
2023-05-27 03:20:04 +00:00
async def _(msg: Bot.MessageSession):
await msg.finish(song_txt((await total_list.get()).random()))
2023-05-28 05:23:05 +00:00
2023-06-19 16:37:14 +00:00
@mai.handle('song <id_or_alias> [<diff>] {{maimai.help.song}}')
async def _(msg: Bot.MessageSession, id_or_alias: str, diff: str = None):
if id_or_alias.isdigit():
sid = id_or_alias
else:
sid_list = await get_alias(id_or_alias, get_music=True)
if len(sid_list) == 0:
await msg.finish(msg.locale.t("maimai.message.music_not_found"))
elif len(sid_list) > 1:
res = msg.locale.t("maimai.message.song.prompt") + "\n"
for sid in sorted(sid_list, key=int):
s = (await total_list.get()).by_id(sid)
res += f"{s['id']}\u200B. {s['title']}{' (DX)' if s['type'] == 'DX' else ''}\n"
2023-06-19 16:37:14 +00:00
await msg.finish(res.strip())
else:
sid = str(sid_list[0])
music = (await total_list.get()).by_id(sid)
if music is None:
await msg.finish(msg.locale.t("maimai.message.music_not_found"))
2023-05-28 05:23:05 +00:00
if diff is not None:
2023-06-12 05:51:36 +00:00
diff_index = get_diff(diff)
2023-06-19 16:37:14 +00:00
if len(music['ds']) == 4 and diff_index == 4:
await msg.finish(msg.locale.t("maimai.message.chart_not_found"))
2023-06-10 04:23:59 +00:00
chart = music['charts'][diff_index]
ds = music['ds'][diff_index]
level = music['level'][diff_index]
file = f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"
if len(chart['notes']) == 4:
2023-06-19 16:37:14 +00:00
message = msg.locale.t(
2023-06-10 04:23:59 +00:00
"maimai.message.song.sd",
diff=diff_label[diff_index],
level=level,
ds=ds,
tap=chart['notes'][0],
hold=chart['notes'][1],
slide=chart['notes'][2],
2023-06-12 05:11:36 +00:00
brk=chart['notes'][3],
2023-06-10 04:23:59 +00:00
charter=chart['charter'])
else:
2023-06-19 16:37:14 +00:00
message = msg.locale.t(
2023-06-10 04:23:59 +00:00
"maimai.message.song.dx",
diff=diff_label[diff_index],
level=level,
ds=ds,
tap=chart['notes'][0],
hold=chart['notes'][1],
slide=chart['notes'][2],
touch=chart['notes'][3],
2023-06-12 05:11:36 +00:00
brk=chart['notes'][4],
2023-06-10 04:23:59 +00:00
charter=chart['charter'])
2023-06-19 16:37:14 +00:00
await msg.finish(
[Plain(f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"),
2023-06-19 16:37:14 +00:00
BImage(f"{file}"), Plain(message)])
2023-05-28 05:23:05 +00:00
else:
2023-06-10 04:23:59 +00:00
file = f"https://www.diving-fish.com/covers/{get_cover_len5_id(music['id'])}.png"
2023-06-19 16:37:14 +00:00
await msg.finish(
[Plain(f"{music['id']}\u200B. {music['title']}{' (DX)' if music['type'] == 'DX' else ''}\n"),
2023-06-10 04:23:59 +00:00
BImage(f"{file}"),
2023-06-19 16:37:14 +00:00
Plain(msg.locale.t("maimai.message.song",
2023-06-10 04:23:59 +00:00
artist=music['basic_info']['artist'], genre=music['basic_info']['genre'],
bpm=music['basic_info']['bpm'], version=music['basic_info']['from'],
2023-06-19 16:37:14 +00:00
level='/'.join((str(ds) for ds in music['ds']))))])
2023-05-21 06:41:50 +00:00
2023-06-17 04:39:47 +00:00
2023-06-12 05:51:36 +00:00
@mai.handle('scoreline <sid> <diff> <scoreline> {{maimai.help.scoreline}}')
async def _(msg: Bot.MessageSession, diff: str, sid: str, scoreline: float):
2023-06-12 05:11:36 +00:00
try:
2023-06-12 05:51:36 +00:00
diff_index = get_diff(diff)
music = (await total_list.get()).by_id(sid)
2023-06-12 05:11:36 +00:00
chart: Dict[Any] = music['charts'][diff_index]
tap = int(chart['notes'][0])
slide = int(chart['notes'][2])
hold = int(chart['notes'][1])
touch = int(chart['notes'][3]) if len(chart['notes']) == 5 else 0
brk = int(chart['notes'][-1])
total_score = 500 * tap + slide * 1500 + hold * 1000 + touch * 500 + brk * 2500
break_bonus = 0.01 / brk
break_50_reduce = total_score * break_bonus / 4
2023-06-12 05:51:36 +00:00
reduce = 101 - scoreline
2023-06-12 05:11:36 +00:00
if reduce <= 0 or reduce >= 101:
raise ValueError
2023-06-12 05:51:36 +00:00
tap_great = "{:.2f}".format(total_score * reduce / 10000)
tap_great_prop = "{:.4f}".format(10000 / total_score)
b2t_great = "{:.3f}".format(break_50_reduce / 100)
b2t_great_prop = "{:.4f}".format(break_50_reduce / total_score * 100)
2023-06-12 10:16:02 +00:00
await msg.finish(f'''{music['title']}{' (DX)' if music['type'] == 'DX' else ''} {diff_label[diff_index]}
2023-06-17 04:39:47 +00:00
{msg.locale.t('maimai.message.scoreline',
scoreline=scoreline,
tap_great=tap_great,
tap_great_prop=tap_great_prop,
brk=brk,
b2t_great=b2t_great,
2023-06-12 07:49:18 +00:00
b2t_great_prop=b2t_great_prop)}''')
2023-06-12 05:11:36 +00:00
except Exception:
2023-06-12 06:03:20 +00:00
await msg.finish(msg.locale.t('maimai.message.scoreline.error', prefix=command_prefix[0]))