Skip to content

Commit

Permalink
<jx3>[feat]item price
Browse files Browse the repository at this point in the history
  • Loading branch information
HornCopper committed Sep 14, 2024
1 parent dfc32ee commit 951f1da
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
51 changes: 42 additions & 9 deletions src/plugins/jx3/trade/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from nonebot import on_command
from nonebot.typing import T_State
from nonebot.adapters import Message
from nonebot.params import CommandArg
from nonebot.params import CommandArg, Arg
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageSegment as ms

from src.tools.utils.file import get_content_local
Expand Down Expand Up @@ -82,24 +83,56 @@ async def _(event: GroupMessageEvent, args: Message = CommandArg()):
item_v2_ = on_command("jx3_item_v2", aliases={"物价v2", "物价"}, force_whitespace=True, priority=5)

@item_v2_.handle()
async def _(event: GroupMessageEvent, args: Message = CommandArg()):
async def _(event: GroupMessageEvent, state: T_State, args: Message = CommandArg()):
"""
获取外观物价:
Example:-物价v2 山神盒子
Example:-物价v2 大橙武券
"""
if args.extract_plain_text() == "":
return
image = await getSingleItemPrice(args.extract_plain_text())
if isinstance(image, list):
await item_v2_.finish(image[0])
elif isinstance(image, str):
img = get_content_local(image)
await item_v2_.finish(ms.image(img))
data = await getSingleItemPrice(args.extract_plain_text())
if isinstance(data, list):
await item_v2_.finish(data[0])
elif isinstance(data, dict):
aliases = data["v"]
if len(aliases) > 20:
aliases = aliases[:20]
if len(aliases) == 0:
await item_v2_.finish("唔……未找到该物品!")
if len(aliases) == 1:
img = await getSingleItemPrice(aliases[0], True)
if not isinstance(img, str):
return
img_content = get_content_local(img)
await item_v2_.finish(ms.image(img_content))
state["v"] = aliases
msg = "音卡找到下面的相关物品,请回复前方序号来搜索!"
for num, name in enumerate(aliases, start=1):
msg += f"\n[{num}] {name}"
await item_v2_.send(msg)
return
elif isinstance(data, str):
img = get_content_local(data)
await item_v2_.finish(ms.image(img))

@item_v2_.got("num")
async def _(event: GroupMessageEvent, state: T_State, num: Message = Arg()):
num_ = num.extract_plain_text()
data = state["v"]
if not check_number(num_):
await item_v2_.finish("唔……输入的不是数字,取消搜索。")
if int(num_) > len(data):
await item_v2_.finish("唔……不存在该数字对应的搜索结果,请重新搜索!")
name = data[int(num_)-1]
img = await getSingleItemPrice(name, True)
if not isinstance(img, str):
return
img_content = get_content_local(img)
await item_v2_.finish(ms.image(img_content))

sl_ = on_command("jx3_sl", aliases={"无封"}, priority=5, force_whitespace=True)

@sl_.handle()
async def _(event: GroupMessageEvent, args: Message = CommandArg()):
if args.extract_plain_text() == "":
return
Expand Down
30 changes: 22 additions & 8 deletions src/plugins/jx3/trade/item_v2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, List, Literal, Union
from typing import Tuple, List, Literal, Union, Optional
from pathlib import Path

from src.tools.utils.request import get_api, post_url
Expand Down Expand Up @@ -48,16 +48,26 @@ async def queryWj(url: str, params: dict = {}):
data = await post_url(url, headers=headers, json=params)
return json.loads(data)

async def getRawName(alias_name: str) -> Union[str, Literal[False]]:
async def getRawName(alias_name: str) -> Union[str, list]:
item_aliases_data = json.loads(read(TOOLS + "/item_aliases.json", "{}"))
if alias_name in item_aliases_data:
alias_name = item_aliases_data[alias_name]
item_data = await queryWj("https://www.aijx3.cn/api/wj/basedata/getBaseGoodsList")
item_data = item_data["data"]

# 精准匹配 如果成功匹配不再模糊搜索
for each_item in item_data:
if alias_name in each_item["goodsAliasAll"]:
if alias_name in each_item["goodsAliasAll"] or alias_name == each_item["goodsName"]:
return each_item["goodsName"]
return False

# 模糊搜索 给出列表
matched = []
for each_item in item_data:
aliases = each_item["goodsAliasAll"]
for alias in aliases:
if alias_name in alias:
matched.append(each_item["goodsName"])
return matched

async def getItemHistory(standard_name: str) -> Tuple[List[int], List[str]]:
current_timestamp = get_current_time()
Expand Down Expand Up @@ -126,7 +136,7 @@ async def quertAJ3Info(item_standard_name: str):

def select_min_max(data: list, margin: float = 0.1, round_to: int = 10) -> tuple[int, int]:
if not data:
raise ValueError("数据列表为空")
return 0, 100000
data = [float(item) for item in data]
data_min = min(data)
data_max = max(data)
Expand All @@ -142,10 +152,12 @@ def round_up(value: float, round_to: int) -> float:
adjusted_max = round_up(optimal_max, round_to)
return int(adjusted_min), int(adjusted_max)

async def getSingleItemPrice(item_name: str):
async def getSingleItemPrice(item_name: str, exact: bool = False) -> Optional[Union[str, dict, list]]:
standard_name = await getRawName(item_name)
if standard_name == False:
return ["唔……未收录该物品!\n请到音卡用户群内进行反馈,我们会及时添加别名!"]
if isinstance(standard_name, list):
return {"v": standard_name}
if exact:
standard_name = item_name
basic_item_info = await getItemDetail(standard_name)
if basic_item_info == False:
return ["唔……未收录该物品!\n请到音卡用户群内进行反馈,我们会及时添加别名!"]
Expand Down Expand Up @@ -180,4 +192,6 @@ async def getSingleItemPrice(item_name: str):
final_html = CACHE + "/" + get_uuid() + ".html"
write(final_html, html)
final_path = await generate(final_html, False, "body", False)
if not isinstance(final_path, str):
return
return Path(final_path).as_uri()
2 changes: 2 additions & 0 deletions src/plugins/jx3/trade/sl.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,6 @@ async def getSingleEquipment(raw_message: str):
final_html = CACHE + "/" + get_uuid() + ".html"
write(final_html, html)
final_path = await generate(final_html, False, "table", False)
if not isinstance(final_path, str):
return
return Path(final_path).as_uri()

0 comments on commit 951f1da

Please sign in to comment.