Skip to content

gamedata

gamedata

The game's own tables, read off the emulator once per game version and kept under ~/.ai_coc/game_data/.

A version's folder holds the decoded CSVs as the game ships them (cnt.csv, texts.csv, logic/<table>.csv) and entities.json (GameEntities) built from them. Only files are read, over ADB, and nothing touches the screen.

Functions:

Name Description
installed_version

The game version the emulator has installed, as dumpsys package names it.

newest_kept

The newest version folder whose tables this code can read, without asking the emulator.

game_data

The installed version's tables: kept ones when the version was read before, else read now.

logger

logger = logging.getLogger(__name__)

ASSET_PACK

ASSET_PACK = 'split_install_time_asset_pack.apk'

LOCALIZATION

LOCALIZATION = {'cnt': 'assets/localization/cnt.csv', 'texts': 'assets/localization/texts.csv'}

ENTITIES

ENTITIES = 'entities.json'

MEMBER_WAIT

MEMBER_WAIT = 30

installed_version

installed_version(adb: AdbController) -> str

The game version the emulator has installed, as dumpsys package names it.

Source code in src/ai_coc/adapters/gamedata.py
def installed_version(adb: AdbController) -> str:
    """The game version the emulator has installed, as `dumpsys package` names it."""
    found = re.search(r"versionName=(\S+)", adb.shell(["dumpsys", "package", COC_PACKAGE]))
    # Held to what a version looks like, since it becomes a folder name.
    if found is None or not re.fullmatch(r"\d[\w.-]*", found.group(1)):
        raise GameDataError(f"讀不到 {COC_PACKAGE} 的版本")
    return found.group(1)

newest_kept

newest_kept() -> tuple[Path, GameEntities] | None

The newest version folder whose tables this code can read, without asking the emulator.

Source code in src/ai_coc/adapters/gamedata.py
def newest_kept() -> tuple[Path, GameEntities] | None:
    """The newest version folder whose tables this code can read, without asking the emulator."""
    for folder in sorted(GAME_DATA_DIR.glob("*/"), key=_version_key, reverse=True):
        if (kept := _kept(folder)) is not None:
            return folder, kept
    return None

game_data

game_data(connect: Callable[[], AdbController], refresh: bool = False) -> GameDataReport

The installed version's tables: kept ones when the version was read before, else read now.

refresh reads them again whatever is kept. connect is called inside, so an emulator that is down falls back like any other failure: to the newest version kept on disk, with a warning. With nothing kept the failure is raised.

Source code in src/ai_coc/adapters/gamedata.py
def game_data(connect: Callable[[], AdbController], refresh: bool = False) -> GameDataReport:
    """The installed version's tables: kept ones when the version was read before, else read now.

    `refresh` reads them again whatever is kept. `connect` is called inside, so
    an emulator that is down falls back like any other failure: to the newest
    version kept on disk, with a warning. With nothing kept the failure is
    raised.
    """
    installed = None
    try:
        adb = connect()
        installed = installed_version(adb)
        folder = GAME_DATA_DIR / installed
        kept = None if refresh else _kept(folder)
        if kept is not None and kept.reading != READING:
            logger.info(
                "The tables kept for %s were read by older code; reading them again", installed
            )
        elif kept is not None:
            logger.info("Using the tables kept for %s in %s", installed, folder)
            return GameDataReport(
                outcome="cached", installed=installed, directory=folder, tables=kept
            )
        tables = _extract(adb, installed)
        logger.info(
            "Read %d entities of %s off the emulator into %s",
            len(tables.entities),
            installed,
            folder,
        )
        return GameDataReport(
            outcome="extracted", installed=installed, directory=folder, tables=tables
        )
    # RuntimeError covers ADB failing and the emulator being down; ValueError
    # covers tables this code cannot read, and a file that does not decode.
    except (RuntimeError, OSError, ValueError):
        if (newest := newest_kept()) is None:
            raise
        folder, kept = newest
        logger.warning(
            "Could not read the game's tables off the emulator; using %s's",
            folder.name,
            exc_info=True,
        )
        return GameDataReport(
            outcome="fallback", installed=installed, directory=folder, tables=kept
        )