Files
donnyandClaude Opus 5.5 ff8eb61bc2 test: the library through BotHost and 100 Linux arena duels on a fixture game root
tests/fixture/game is built by make-fixture.py from the Unity repo's Data/ at dev 19c6158 (AI_OldSchool, its 17
cards and scripts, bot.conf) plus a character that owns the deck. The arena tests run the published Linux exe:
100 Smart-vs-Smart duels with 0 errors, and --seed 7 giving the same report at --parallel 4 and 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:19:14 -07:00

107 lines
4.6 KiB
Python

"""Builds tests/fixture/game: the smallest game root the tests can duel on.
python3 -I tests/fixture/make-fixture.py <source root> <expansions script dir> <out dir>
<source root> holds the game's Data/locales/en-US/{cards.cdb,bot.conf}, Data/script.zip and
Data/Windbot/Decks/AI_OldSchool.ydk (the Unity repo's Data/ at the commit named in tests/fixture/README.md).
<expansions script dir> is a game copy's Expansions/script/ (its non-card files override script.zip's).
It also writes Data/Characters/fixture-oldschool, a character that owns the deck.
The cards are the deck's, plus every card id their scripts name that the database has (tokens, alias targets),
to a fixed point. The scripts are those cards' c<id>.lua and the system files (constant, utility, procedure).
"""
import os
import re
import shutil
import sqlite3
import sys
import zipfile
DECK = "AI_OldSchool"
# A character that owns the deck, so a seat on it gets that character's name and the Smart brain (the game's
# path: deck -> owning character -> brain), not Dovphin.
CHARACTER = """{
"_comment": "Test fixture: owns AI_OldSchool so its seats play the Smart brain.",
"id": "fixture-oldschool",
"name": "Old School",
"decks": [
{ "path": "AI_OldSchool", "brain": { "executor": "Smart", "difficulty": "hard", "knobs": { "aggression": 0.5 } } }
],
"brain": { "executor": "Lucky", "difficulty": "normal", "knobs": {} }
}
"""
def deck_ids(path):
ids = []
for line in open(path, encoding="utf-8-sig"):
line = line.strip()
if line.isdigit():
ids.append(int(line))
return ids
def main(src, exp_script, out):
cdb = os.path.join(src, "Data/locales/en-US/cards.cdb")
scripts = zipfile.ZipFile(os.path.join(src, "Data/script.zip"))
names = set(scripts.namelist())
db = sqlite3.connect("file:" + cdb + "?mode=ro", uri=True)
known = {row[0] for row in db.execute("SELECT id FROM datas")}
def script_of(code):
name = "script/c%d.lua" % code
return scripts.read(name).decode("utf-8", "replace") if name in names else ""
want = set(deck_ids(os.path.join(src, "Data/Windbot/Decks", DECK + ".ydk")))
todo = list(want)
while todo:
code = todo.pop()
refs = {int(n) for n in re.findall(r"\b\d{5,9}\b", script_of(code))}
alias = db.execute("SELECT alias FROM datas WHERE id=?", (code,)).fetchone()
if alias and alias[0]:
refs.add(alias[0])
for ref in refs & known - want:
want.add(ref)
todo.append(ref)
if os.path.exists(out):
shutil.rmtree(out)
os.makedirs(os.path.join(out, "Data/locales/en-US"))
os.makedirs(os.path.join(out, "Data/Windbot/Decks"))
os.makedirs(os.path.join(out, "Expansions/script"))
os.makedirs(os.path.join(out, "Data/Characters/fixture-oldschool"))
with open(os.path.join(out, "Data/Characters/fixture-oldschool/character.json"), "w", newline="\n") as f:
f.write(CHARACTER)
sub = sqlite3.connect(os.path.join(out, "Data/locales/en-US/cards.cdb"))
for (sql,) in db.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name IN ('datas','texts')"):
sub.execute(sql)
for table in ("datas", "texts"):
cols = len(db.execute("SELECT * FROM %s LIMIT 1" % table).description)
rows = db.execute("SELECT * FROM %s WHERE id IN (%s) ORDER BY id" % (table, ",".join(map(str, sorted(want))))).fetchall()
sub.executemany("INSERT INTO %s VALUES (%s)" % (table, ",".join("?" * cols)), rows)
sub.commit()
sub.execute("VACUUM")
sub.close()
keep = sorted(n for n in names if n.startswith("script/") and not re.match(r"script/c\d+\.lua$", n) and not n.endswith("/"))
keep += sorted("script/c%d.lua" % c for c in want if "script/c%d.lua" % c in names)
with zipfile.ZipFile(os.path.join(out, "Data/script.zip"), "w", zipfile.ZIP_DEFLATED) as z:
for n in keep:
info = zipfile.ZipInfo(n, date_time=(2026, 1, 1, 0, 0, 0))
info.compress_type = zipfile.ZIP_DEFLATED
z.writestr(info, scripts.read(n))
for name in sorted(os.listdir(exp_script)):
if name.endswith(".lua") and not re.match(r"c\d+\.lua$", name):
shutil.copyfile(os.path.join(exp_script, name), os.path.join(out, "Expansions/script", name))
shutil.copyfile(os.path.join(src, "Data/locales/en-US/bot.conf"), os.path.join(out, "Data/locales/en-US/bot.conf"))
shutil.copyfile(os.path.join(src, "Data/Windbot/Decks", DECK + ".ydk"), os.path.join(out, "Data/Windbot/Decks", DECK + ".ydk"))
print("%d cards, %d scripts -> %s" % (len(want), len(keep), out))
if __name__ == "__main__":
main(*sys.argv[1:4])