From 8c1361cb43541592168190b267d9e4e721eeb134 Mon Sep 17 00:00:00 2001 From: Karako Date: Mon, 1 May 2023 21:31:48 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=20add=20parser=20for=20items?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/item.py | 6 ++--- scripts/item.py | 72 +++++++++++++++++++++++++++++++++++++++++++++---- utils/json.py | 2 +- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/model/item.py b/model/item.py index 0eb545b..f1c0286 100644 --- a/model/item.py +++ b/model/item.py @@ -1,7 +1,5 @@ -from model.enums import ItemType, MaterialType - -from model.enums import FoodQuality -from model._base import Model as BaseModel +from model.enums import FoodQuality, ItemType, MaterialType +from utils.model import BaseModel class Item(BaseModel): diff --git a/scripts/item.py b/scripts/item.py index c9f6bf8..1bf8105 100644 --- a/scripts/item.py +++ b/scripts/item.py @@ -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.typedefs import Lang +OUT_DIR = PROJECT_ROOT.joinpath("out") + # noinspection PyShadowingBuiltins 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) 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"] - name = manager.get_text(data["nameTextMapHash"]) + if name := manager.get_text(data["nameTextMapHash"]) is None: + continue type = manager.get_text(data["typeDescTextMapHash"]) icon = data["icon"] rarity = data["rankLevel"] - description = manager.get_text(data["descTextMapHash"]) + description = manager.get_text(data["descTextMapHash"]) or "" 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() diff --git a/utils/json.py b/utils/json.py index d8f2251..bca94e8 100644 --- a/utils/json.py +++ b/utils/json.py @@ -7,7 +7,7 @@ from orjson import loads as json_loads from orjson import JSONDecodeError as _JSONDecodeError from orjson import JSONEncodeError as _JSONEncodeError -__all__ = ("JSONEncodeError", "JSONDecodeError", "json_dumps", "json_loads") +__all__ = ("JSONEncodeError", "JSONDecodeError", "dumps", "loads") JSONEncodeError = _JSONEncodeError JSONDecodeError = _JSONDecodeError