add parser for items

This commit is contained in:
Karako 2023-05-01 21:31:48 +08:00
parent fb77ca0569
commit 8c1361cb43
No known key found for this signature in database
GPG Key ID: 5920831B0095D4A0
3 changed files with 70 additions and 10 deletions

View File

@ -1,7 +1,5 @@
from model.enums import ItemType, MaterialType from model.enums import FoodQuality, ItemType, MaterialType
from utils.model import BaseModel
from model.enums import FoodQuality
from model._base import Model as BaseModel
class Item(BaseModel): class Item(BaseModel):

View File

@ -1,19 +1,81 @@
from model.enums import ItemType import ujson as json
from aiofiles import open as async_open
from model.enums import FoodQuality, ItemType, MaterialType
from model.item import Food, Item, Material, Namecard
from utils.const import PROJECT_ROOT
from utils.manager import ResourceManager from utils.manager import ResourceManager
from utils.typedefs import Lang from utils.typedefs import Lang
OUT_DIR = PROJECT_ROOT.joinpath("out")
# noinspection PyShadowingBuiltins # noinspection PyShadowingBuiltins
async def parse_item_data(lang: Lang): async def parse_item_data(lang: Lang):
out_path = OUT_DIR.joinpath(f"{lang}")
out_path.mkdir(exist_ok=True, parents=True)
manager = ResourceManager(lang=lang) manager = ResourceManager(lang=lang)
json_data = manager.fetch("MaterialExcelConfigData") json_data = manager.fetch("MaterialExcelConfigData")
for data in json_data:
item_list = []
for data in filter(lambda x: "rankLevel" in x, json_data):
id = data["id"] id = data["id"]
name = manager.get_text(data["nameTextMapHash"]) if name := manager.get_text(data["nameTextMapHash"]) is None:
continue
type = manager.get_text(data["typeDescTextMapHash"]) type = manager.get_text(data["typeDescTextMapHash"])
icon = data["icon"] icon = data["icon"]
rarity = data["rankLevel"] rarity = data["rankLevel"]
description = manager.get_text(data["descTextMapHash"]) description = manager.get_text(data["descTextMapHash"]) or ""
special_description = manager.get_text(data["specialDescTextMapHash"]) special_description = manager.get_text(data["specialDescTextMapHash"])
item_type = ItemType(data["itemType"].removeprefix('ITEM_')) item_type = ItemType(data["itemType"].removeprefix("ITEM_"))
kwargs = {
"id": id,
"name": name,
"type": type,
"icon": icon,
"rarity": rarity,
"description": description,
"special_description": special_description,
"item_type": item_type,
}
try:
if "picPath" in data and data["picPath"]:
pictures = data["picPath"]
item = Namecard(pictures=pictures, **kwargs)
elif "materialType" in data:
material_type = MaterialType(
data["materialType"].removeprefix("MATERIAL_")
)
item = Material(material_type=material_type, **kwargs)
elif "foodQuality" in data:
quality = FoodQuality(data["foodQuality"])
effect = manager.get_text(data["effectDescTextMapHash"])
effect_icon = data["effectIcon"]
effect_name = data["effectName"]
item = Food(
quality=quality,
effect=effect,
effect_icon=effect_icon,
effect_name=effect_name,
**kwargs,
)
else:
item = Item(**kwargs)
except Exception as e:
breakpoint()
raise e
item_list.append(item)
async with async_open(out_path / "item.json", encoding="utf-8", mode="w") as file:
await file.write(
json.dumps(
[i.dict() for i in item_list],
ensure_ascii=False,
encode_html_chars=False,
),
)
breakpoint() breakpoint()

View File

@ -7,7 +7,7 @@ from orjson import loads as json_loads
from orjson import JSONDecodeError as _JSONDecodeError from orjson import JSONDecodeError as _JSONDecodeError
from orjson import JSONEncodeError as _JSONEncodeError from orjson import JSONEncodeError as _JSONEncodeError
__all__ = ("JSONEncodeError", "JSONDecodeError", "json_dumps", "json_loads") __all__ = ("JSONEncodeError", "JSONDecodeError", "dumps", "loads")
JSONEncodeError = _JSONEncodeError JSONEncodeError = _JSONEncodeError
JSONDecodeError = _JSONDecodeError JSONDecodeError = _JSONDecodeError