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

View File

@ -1,10 +1,52 @@
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):
name = MapStringField()
name = MapTextField()
"""Name of the item."""
description = MapStringField()
description = MapTextField()
"""Description of the item."""
special_description = MapTextField()
"""Special description of the item."""