🚧 build

This commit is contained in:
Karako 2023-08-30 01:08:24 +08:00
parent baeee55de5
commit 14e4ac029f
No known key found for this signature in database
2 changed files with 77 additions and 31 deletions

View File

@ -1,7 +1,7 @@
from pathlib import Path from pathlib import Path
import peewee import peewee
from peewee import IntegerField, SqliteDatabase from peewee import CharField, IntegerField, SqliteDatabase
from genshin.wiki.config import get_wiki_lang from genshin.wiki.config import get_wiki_lang
from genshin.wiki.tools.const import DATA_DIR from genshin.wiki.tools.const import DATA_DIR
@ -11,15 +11,16 @@ from genshin.wiki.utils import LimitedSizeDict
__all__ = ( __all__ = (
"Model", "Model",
"ModelMeta", "ModelMeta",
"MapString", "MappingTextModel",
"MapStringField", "MapText",
"MapTextField",
) )
_database = SqliteDatabase(Path(__file__).joinpath('../sqlite.db')) _database = SqliteDatabase(Path(__file__).joinpath('../sqlite.db'))
_database.connect() _database.connect()
_lang_database_map: LimitedSizeDict[Lang, SqliteDatabase] = LimitedSizeDict(size_limit=256) _text_database = SqliteDatabase(DATA_DIR.joinpath('text.db').resolve())
class ModelMeta: class ModelMeta:
database: SqliteDatabase = _database database: SqliteDatabase = _database
@ -29,41 +30,44 @@ class Model(peewee.Model):
class Meta(ModelMeta): class Meta(ModelMeta):
abstract = True abstract = True
_map_string_cache: dict[int, "MapString"] = {}
class MapString(str): class MappingTextModel(peewee.Model):
class Meta:
database= _text_database
id = IntegerField(primary_key=True)
text_id = IntegerField()
context = CharField()
lang = CharField()
_map_string_cache: dict[int, "MapText"] = LimitedSizeDict(size_limit=256)
class MapText(str):
__slots__ = ("_text_id", "_lang") __slots__ = ("_text_id", "_lang")
def __new__(cls, target: int | str) -> "MapString": def __new__(cls, target: int | str) -> "MapText":
lang = get_wiki_lang() lang = get_wiki_lang()
_map_string_cache_key = hash((str(lang), target,)) _map_string_cache_key = hash((str(lang), target))
result = _map_string_cache.get(_map_string_cache_key, None) result = _map_string_cache.get(_map_string_cache_key, None)
if result is not None: if result is not None:
return result return result
if lang not in _lang_database_map:
database = SqliteDatabase(DATA_DIR.joinpath(lang + '.db').resolve())
database.connect()
_lang_database_map[lang] = database
else:
database = _lang_database_map[lang]
if isinstance(target, int): if isinstance(target, int):
text_id = target text_id = target
text = database.execute_sql( model = MappingTextModel.get_or_none(text_id=text_id, lang=lang)
"SELECT context FROM mapping_text WHERE id = ?", (target,) text = model.context if model is not None else ""
).fetchone()[0]
else: else:
text = target text = target
text_id = database.execute_sql( model = MappingTextModel.get_or_none(context=text, lang=lang)
"SELECT id FROM mapping_text WHERE context = ?", (target,) text_id = model.text_id if model is not None else 0
).fetchone()[0]
result = super().__new__(cls, text) result = super().__new__(cls, text)
result._text_id = text_id result._text_id = text_id
result._lang = lang result._lang = lang
_map_string_cache[_map_string_cache_key] = result _map_string_cache[_map_string_cache_key] = result
return result return result
@property @property
@ -74,10 +78,10 @@ class MapString(str):
def text_id(self) -> int: def text_id(self) -> int:
return self._text_id return self._text_id
class MapStringField(IntegerField): class MapTextField(IntegerField):
def db_value(self, value: str | int | MapString) -> int: def db_value(self, value: str | int | MapText) -> int:
return MapString(value).text_id return MapText(value).text_id
def python_value(self, value: int) -> MapString: def python_value(self, value: int) -> MapText:
return MapString(value) return MapText(value)

View File

@ -1,10 +1,52 @@
from peewee import IntegerField, CharField from peewee import IntegerField, CharField
from genshin.wiki._database._mode import Model, MapStringField from genshin.wiki._database._mode import Model, MapTextField
# cdGroup
# cdTime
# closeBagAfterUsed
# descTextMapHash
# destroyReturnMaterial
# destroyReturnMaterialCount
# destroyRule
# effectDescTextMapHash
# effectGadgetID
# effectIcon
# effectName
# foodQuality
# gadgetId
# globalItemLimit
# icon
# id
# interactionTitleTextMapHash
# isForceGetHint
# isHidden
# isSplitDrop
# itemType
# itemUse
# materialType
# maxUseCount
# nameTextMapHash
# noFirstGetHint
# picPath
# playGainEffect
# rank
# rankLevel
# satiationParams
# setID
# specialDescTextMapHash
# stackLimit
# typeDescTextMapHash
# useLevel
# useOnGain
# useTarget
# weight
class Item(Model): class Item(Model):
name = MapStringField() name = MapTextField()
"""Name of the item.""" """Name of the item."""
description = MapStringField() description = MapTextField()
"""Description of the item.""" """Description of the item."""
special_description = MapTextField()
"""Special description of the item."""