Skip to content

Commit

Permalink
Add an excel cache
Browse files Browse the repository at this point in the history
  • Loading branch information
thesadru committed Nov 8, 2023
1 parent 3f61662 commit 888b422
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
42 changes: 35 additions & 7 deletions arkprts/assets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ class Assets(abc.ABC):
"""Default server."""
loaded: bool
"""Whether the data was loaded at any point during the code execution."""
excel_cache: dict[netn.ArknightsServer, dict[str, typing.Any]]
"""Cache of loaded excel files."""
json_loads: typing.Callable[[bytes], typing.Any]
"""Alternative self.json_load"""

def __init__(self, *, default_server: netn.ArknightsServer = "en") -> None:
def __init__(
self,
*,
default_server: netn.ArknightsServer = "en",
json_loads: typing.Callable[[bytes], typing.Any] = json.loads,
) -> None:
self.default_server = default_server
self.loaded = False
self.excel_cache = {}
self.json_loads = json_loads

@classmethod
def create(
Expand Down Expand Up @@ -73,13 +84,25 @@ async def aget_file(self, path: str, *, server: netn.ArknightsServer | None = No

def get_excel(self, name: str, *, server: netn.ArknightsServer | None = None) -> models.DDict:
"""Get a gamedata table file."""
data = self.get_file(f"gamedata/excel/{name}.json", server=server)
return models.DDict(json.loads(data))
path = f"gamedata/excel/{name}.json"
if data := self.excel_cache.setdefault(server or self.default_server, {}).get(path):
return models.DDict(data)

data = self.json_loads(self.get_file(path, server=server))
self.excel_cache[server or self.default_server][path] = data

return models.DDict(data)

async def aget_excel(self, name: str, *, server: netn.ArknightsServer | None = None) -> models.DDict:
"""Get a gamedata table file without requiring load."""
data = await self.aget_file(f"gamedata/excel/{name}.json", server=server)
return models.DDict(json.loads(data))
path = f"gamedata/excel/{name}.json"
if data := self.excel_cache.setdefault(server or self.default_server, {}).get(path):
return models.DDict(data)

data = self.json_loads(await self.aget_file(path, server=server))
self.excel_cache[server or self.default_server][path] = data

return models.DDict(data)

def __getitem__(self, name: str) -> models.DDict:
"""Get a gamedata table file."""
Expand All @@ -106,12 +129,17 @@ def get_item(self, id: str, *, server: netn.ArknightsServer | None = None) -> mo
def get_medal(self, id: str, *, server: netn.ArknightsServer | None = None) -> models.DDict:
"""Get a medal."""
data = self.get_excel("medal_table", server=server)
return data["medalList"][id]
return next(m for m in data["medalList"] if m["medalId"] == id)

def get_medal_group(self, id: str, *, server: netn.ArknightsServer | None = None) -> models.DDict:
"""Get a medal group. Way too specific probably."""
data = self.get_excel("medal_table", server=server)
return next(i for i in data["medalTypeData"]["activityMedal"]["groupData"] if i["groupId"] == id)
return next(
medal_group
for groups in data["medalTypeData"].values()
for medal_group in groups["groupData"]
if medal_group["groupId"] == id
)

def get_skill(self, id: str, *, server: netn.ArknightsServer | None = None) -> models.DDict:
"""Get a skill."""
Expand Down
4 changes: 3 additions & 1 deletion arkprts/assets/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def run_flatbuffers(
# "--no-warnings",
"--force-empty",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if code != 0:
Expand Down Expand Up @@ -314,8 +315,9 @@ def __init__(
*,
default_server: netn.ArknightsServer | None = None,
network: netn.NetworkSession | None = None,
json_loads: typing.Callable[[bytes], typing.Any] = json.loads,
) -> None:
super().__init__(default_server=default_server or "en")
super().__init__(default_server=default_server or "en", json_loads=json_loads)

temporary_directory = pathlib.Path(tempfile.gettempdir())
self.directory = pathlib.Path(directory or temporary_directory / "ArknightsResources")
Expand Down
4 changes: 3 additions & 1 deletion arkprts/assets/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import asyncio
import fnmatch
import json
import logging
import os
import os.path
Expand Down Expand Up @@ -212,8 +213,9 @@ def __init__(
resources_repository: str | None = None,
*,
default_server: netn.ArknightsServer = "en",
json_loads: typing.Callable[[bytes], typing.Any] = json.loads,
) -> None:
super().__init__(default_server=default_server)
super().__init__(default_server=default_server, json_loads=json_loads)

default_directory = pathlib.Path(tempfile.gettempdir())

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "arkprts"
requires-python = ">=3.9"
version = "0.3.4"
version = "0.3.5"

[build-system]
requires = ["setuptools"]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="arkprts",
version="0.3.4",
version="0.3.5",
description="Arknights python wrapper.",
url="https://github.com/thesadru/arkprts",
packages=find_packages(exclude=["tests", "tests.*"]),
Expand Down

0 comments on commit 888b422

Please sign in to comment.