Skip to content

Commit

Permalink
Merge pull request #18 from BA-OST-Index/dev
Browse files Browse the repository at this point in the history
2024/03/17 storyinfo auto & i18 reduction
  • Loading branch information
sctop authored Mar 17, 2024
2 parents c4b4d19 + f9937a5 commit 7268775
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 27 deletions.
2 changes: 1 addition & 1 deletion data
Submodule data updated 114 files
146 changes: 146 additions & 0 deletions data_model/actual_data/_story/story_part_auto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from collections import OrderedDict

from data_model.actual_data.track import TrackInfo
from data_model.actual_data.character import CharacterInfo
from data_model.actual_data.background import BackgroundInfo
from data_model.tool.to_json import IToJson
from data_model.tool.tool import KeyToMultiValueDict


class StoryPartAutoDataAll(IToJson):
def __init__(self, data, story):
self.data = data
self.story = story

self.background = OrderedDict()
self.character = OrderedDict()
self.track = OrderedDict()

def load(self):
# bg
for i in self.data["background"]:
temp = BackgroundInfo.get_instance(i)
temp.register(self.story)
self.background[i] = temp
# track
for i in self.data["track"]:
temp = TrackInfo.get_instance(i)
temp.register(self.story)
self.track[i] = temp
# character
for i in self.data["character"]:
temp = CharacterInfo.get_instance(i)
temp.register(self.story)
self.character[i] = temp

def to_json(self):
return {
"background": {key: value.to_json_basic() for (key, value) in self.background.items()},
"character": {key: value.to_json_basic() for (key, value) in self.character.items()},
"track": {key: value.to_json_basic() for (key, value) in self.track.items()}
}

def to_json_basic(self):
return self.to_json()


class StoryPartAutoData(IToJson):
def __init__(self, data, story):
self.data = data
self.story = story

self.data_all = StoryPartAutoDataAll(data["all"], story)
self._bg_to_track = KeyToMultiValueDict()
self._char_to_track = KeyToMultiValueDict()
self._bg_to_char = KeyToMultiValueDict()

self.data_special = {
"flag": self.data["special"]["flag"],
"track": None if self.data["special"]["track"] == "OST_0" else TrackInfo.get_instance(
self.data["special"]["track"])
}

def load(self):
self.data_all.load()

# pre-process
del self.data["track_to_bg"]["OST_0"]

# track to bg
for (track_id, bgs) in self.data["track_to_bg"].items():
if track_id == "OST_0":
continue

track = self.data_all.track[track_id]
for bg_id in bgs:
if bg_id in ["BG_Red.jpg", "BG_Black.jpg", "BG_White.jpg"]:
continue

bg = self.data_all.background[bg_id]

track.register(bg)
bg.register(track)

self._bg_to_track[bg_id] = track_id

# track to char
for (track_id, chars) in self.data["track_to_char"].items():
if track_id == "OST_0":
continue

track = self.data_all.track[track_id]
for char_id in chars:
char = self.data_all.character[char_id]

track.register(char)
char.register(track)

self._char_to_track[char_id] = track_id

# char to bg
for (char_id, bgs) in self.data["char_to_bg"].items():
char = self.data_all.character[char_id]
for bg_id in bgs:
if bg_id in ["BG_Red.jpg", "BG_Black.jpg", "BG_White.jpg"]:
continue

bg = self.data_all.background[bg_id]

char.register(bg)
bg.register(char)

self._bg_to_char[bg_id] = char_id

def to_json(self):
return {
"all": self.data_all.to_json(),
"track_to_bg": self.data["track_to_bg"],
"track_to_char": self.data["track_to_char"],
"bg_to_char": self._bg_to_char.to_json(),
"bg_to_track": self._bg_to_track.to_json(),
"char_to_track": self._char_to_track.to_json(),
"char_to_bg": self.data["char_to_bg"],
"special": {
"flag": self.data_special["flag"],
"track": None if self.data_special["track"] is None else self.data_special["track"].to_json_basic()
}
}

@property
def bgm_special(self):
# see this traceback:
# --------------------------
# File "F:\GitFile\BA_OST_Index_Parser\data_model\actual_data\track.py", line 96, in register
# for i in file_loader.part.bgm_special:
# --------------------------
# and now you know.
if self.data_special["flag"]:
return [self.data_special["track"]]
else:
return []

def to_json_basic_tracks(self):
# traceback:
# File "F:\GitFile\BA_OST_Index_Parser\data_model\actual_data\story.py", line 355, in to_json
# t["bgm_special"] = self.part.to_json_basic_tracks()
return [self.data_special["track"].to_json_basic()]
4 changes: 3 additions & 1 deletion data_model/actual_data/_story/story_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def load(self, data):
class StoryPosBond(StoryPos):
student = String('student')
no = Integer('no')
_components = ["student", "no"]
favor_rank = Integer('favor_rank')
_components = ["student", "no", "favor_rank"]

def load(self, data):
super().load(data)
self.student = data["student"]
self.no = data["segment"]
self.favor_rank = data["favor_rank"]


def storyPosAuto(data: dict, key_name: str = "pos"):
Expand Down
2 changes: 1 addition & 1 deletion data_model/actual_data/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def to_json_basic(self):
}

@classmethod
def get_instance(cls, instance_id):
def get_instance(cls, instance_id) -> "BackgroundInfo":
return super().get_instance(instance_id)

def _get_instance_offset(self, offset: int):
Expand Down
20 changes: 17 additions & 3 deletions data_model/actual_data/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class CharacterInfo(FileLoader, InterpageMixin, UsedByRegisterMixin, RelatedToRe
_instance = {}

@classmethod
def get_instance(cls, instance_id):
def get_instance(cls, instance_id) -> "CharacterInfo":
def return_instance(instance):
if is_comm:
return CharacterInfoProxyComm(instance)
Expand Down Expand Up @@ -315,6 +315,7 @@ def __init__(self, **kwargs):
self._armor_type = schale_db_manager.query_constant("students", self.char_name, "ArmorType")

self._bond_track = None
self._bond_rank = -1

self.used_by = CharacterUsedBy()
if "related_to" not in kwargs["data"].keys():
Expand All @@ -334,6 +335,17 @@ def bond_track(self, value):
self._bond_track = value
value.bond_chars.append(self)

@property
def bond_rank(self):
return self._bond_rank

@bond_rank.setter
def bond_rank(self, value):
if self._bond_rank != -1:
raise RuntimeError("Cannot set bond track after the bond track has been set!")

self._bond_rank = value

@staticmethod
def _get_instance_id(data: dict):
return "STU_" + data["name"].upper()
Expand Down Expand Up @@ -392,7 +404,8 @@ def to_json(self):
},
"related_to": self.related_to.to_json_basic(),

"bond_track": self.bond_track.to_json_basic() if self.bond_track else None
"bond_track": self.bond_track.to_json_basic() if self.bond_track else None,
"bond_rank": self.bond_rank
}

def to_json_basic(self):
Expand Down Expand Up @@ -437,7 +450,8 @@ def to_json_basic(self):
"lang": self.armor_type.to_json()
}
},
"bond_track": self.bond_track.to_json_basic() if self.bond_track else None
"bond_track": self.bond_track.to_json_basic() if self.bond_track else None,
"bond_rank": self.bond_rank
}

@classmethod
Expand Down
96 changes: 95 additions & 1 deletion data_model/actual_data/story.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from data_model.types.metatype.base_type import *
from data_model.types.metatype.complex import *
from data_model.actual_data.track import TrackInfo
from data_model.actual_data._story.story_pos import *
from data_model.types.url import UrlModel
from data_model.loader import i18n_translator
from data_model.types.lang_string import LangStringModelList
from data_model.loader import FileLoader
from data_model.constant.file_type import FILE_STORY_MAIN, FILE_STORY_SIDE, FILE_STORY_SHORT, FILE_STORY_EVENT, \
FILE_STORY_OTHER
FILE_STORY_OTHER, FILE_STORY_BOND
from ._story.story_part import StoryInfoPartListManager
from ._story.story_part_auto import StoryPartAutoData
from ._story.story_source_all import StoryInfoSource
from ..tool.parent_data import IParentData
from ..tool.interpage import InterpageMixin
Expand Down Expand Up @@ -158,6 +160,7 @@ def to_json(self):
t = {
"uuid": self.uuid,
"filetype": self.filetype,
"filetype_sub": 0,
"namespace": self.namespace,

"name": self.name.to_json_basic(),
Expand Down Expand Up @@ -311,6 +314,7 @@ def after_instantiate(self):
# 如果当前为回忆大厅剧情,则绑定学生的bond_track
if self.is_memory:
self.stu.bond_track = self.part.bgm_special[0]
self.stu.bond_rank = self.pos.favor_rank

self.extra_register()

Expand All @@ -333,6 +337,7 @@ def to_json(self):
t = {
"uuid": self.uuid,
"filetype": self.filetype,
"filetype_sub": 0,
"namespace": self.namespace,

"name": self.name.to_json_basic(),
Expand Down Expand Up @@ -383,3 +388,92 @@ def get_mixed_interpage_data(self, prev, next):
"pos": next.pos.to_json_basic() if next else "[NO_NEXT]"
}
}


class StoryInfoAutoMixin:
@staticmethod
def to_json(data):
temp = data
temp["filetype_sub"] = 1
return temp

@staticmethod
def to_json_basic(data):
temp = data
temp["filetype_sub"] = 1
return temp


class StoryInfoAuto(StoryInfo, StoryInfoAutoMixin):
def __init__(self, **kwargs):
super().__init__(data=kwargs["data"], namespace=kwargs["namespace"], parent_data=kwargs["parent_data"])
data = kwargs["data"]
self.is_battle = data["is_battle"]

self.name = i18n_translator[data["name"]]
self.pos = storyPosAuto(data["pos"])
self.image = UrlModel()
self.source = StoryInfoSource(data.get("source", {}))
self.image.load(self.data["image"])

self.desc = LangStringModelList('desc')
if isinstance(data["desc"], str):
self.desc.append(i18n_translator[data["desc"]])
else:
for i in data["desc"]:
self.desc.append(i18n_translator[i])

self.extra_register()

def extra_register(self):
self.part = StoryPartAutoData(self.data["data"], self)

# 手动加载,防止bug
self.part.load()

def to_json(self):
return StoryInfoAutoMixin.to_json(StoryInfo.to_json(self))

def to_json_basic(self):
return StoryInfoAutoMixin.to_json_basic(StoryInfo.to_json_basic(self))


class StoryInfoBondAuto(StoryInfoBond, StoryInfoAutoMixin):
def __init__(self, **kwargs):
super().__init__(**kwargs)

def extra_register(self):
self.part = StoryPartAutoData(self.data["data"], self)
self.part.load()

def to_json(self):
return StoryInfoAutoMixin.to_json(StoryInfoBond.to_json(self))

def to_json_basic(self):
return StoryInfoAutoMixin.to_json_basic(StoryInfoBond.to_json_basic(self))

def after_instantiate(self):
self.extra_register()

# 如果当前为回忆大厅剧情,则绑定学生的bond_track
if self.data["data"]["special"]["flag"]:
self.stu.bond_track = TrackInfo.get_instance(self.data["data"]["special"]["track"])
self.stu.bond_rank = self.pos.favor_rank


def storyinfo_dispatcher(**kwargs):
try:
filetype_sub = kwargs["data"]["filetype_sub"]
except KeyError:
filetype_sub = 0

if kwargs["data"]["filetype"] == FILE_STORY_BOND:
if filetype_sub == 0:
return StoryInfoBond(**kwargs)
else:
return StoryInfoBondAuto(**kwargs)
else:
if filetype_sub == 0:
return StoryInfo(**kwargs)
else:
return StoryInfoAuto(**kwargs)
2 changes: 1 addition & 1 deletion data_model/actual_data/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def to_json_basic(self):
return t

@classmethod
def get_instance(cls, instance_id):
def get_instance(cls, instance_id) -> "TrackInfo":
return super().get_instance(instance_id)

def _get_instance_offset(self, offset: int):
Expand Down
Loading

0 comments on commit 7268775

Please sign in to comment.