diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..56f1f13 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +**/bin/ +**/obj/ +out/ +.engine/ +.git/ diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..3133777 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,73 @@ +# Every push and pull request: build the library and both arenas and run the tests, all in Docker (Dockerfile, +# target dist, which runs the test stage first). A v* tag adds the release: one Gitea release named after the +# tag, the CHANGELOG section for that version as the body, and +# +# bots-lib.zip YGOLegacy.Bots.dll, YGOLegacy.Bots.pdb, bots.json (what the game references) +# bot-arena-linux-x64.tar.gz bot-arena-linux-x64/: BotArena (self-contained), libygol-engine.so, the binding, bots.json +# bot-arena-win-x64.zip bot-arena-win-x64/: BotArena.exe (self-contained), ygol-engine.dll, the binding, bots.json +# bots.json { version, engine }: this release and the engine release it was built and tested on +# +# A failing build or test means no release. The token is the run's own GITHUB_TOKEN; the release is in this repo. +name: CI + +on: + push: + branches: ['**'] + tags: ['v*'] + pull_request: + workflow_dispatch: + +concurrency: + group: ${{ github.ref }} + cancel-in-progress: false + +env: + API: https://gitea.dev.blueoceanswim.com/api/v1/repos/donny/yugioh-legacy-bots + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Build, test and pack (library, linux-x64 and win-x64 arenas, the tests) + run: | + set -eu + version=local + case "$GITHUB_REF" in refs/tags/v*) version="$GITHUB_REF_NAME";; esac + DOCKER_BUILDKIT=1 docker build --progress=plain --target release --build-arg "BOTS_VERSION=$version" -o dist . + ls -l dist + cat dist/bots.json + + - name: The CHANGELOG section for this version is the release body + if: startsWith(github.ref, 'refs/tags/v') + run: | + set -eu + tag="$GITHUB_REF_NAME" + awk -v tag="$tag" ' + /^## / { on = ($2 == tag); next } + on { print }' CHANGELOG.md > dist/body.md + [ -s dist/body.md ] || { echo "::error::CHANGELOG.md has no '## $tag' section"; exit 1; } + cat dist/body.md + + - name: Create the release (refused if it already exists) and attach the assets + if: startsWith(github.ref, 'refs/tags/v') + env: + TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -eu + tag="$GITHUB_REF_NAME" + code=$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: token $TOKEN" "$API/releases/tags/$tag") + [ "$code" = "404" ] || { echo "::error::release $tag already exists (http $code); a bad release gets a new patch tag, never a re-upload"; exit 1; } + jq -n --arg tag "$tag" --rawfile body dist/body.md \ + '{tag_name: $tag, target_commitish: env.GITHUB_SHA, name: $tag, body: $body, draft: false, prerelease: false}' > dist/release.json + curl -sS -f -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/json" \ + --data-binary @dist/release.json "$API/releases" > dist/created.json + id=$(jq -r .id dist/created.json) + echo "release id $id" + for f in dist/bots-lib.zip dist/bot-arena-linux-x64.tar.gz dist/bot-arena-win-x64.zip dist/bots.json; do + name=$(basename "$f") + echo "uploading $name" + curl -sS -f -o /dev/null -X POST -H "Authorization: token $TOKEN" -F "attachment=@$f" "$API/releases/$id/assets?name=$name" + done + echo "https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-bots/releases/tag/$tag" diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..59abe6a --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,24 @@ +# yugioh-legacy-bots + +Instructions for any coding agent. `README.md` has the layout, the contract, the build and the release. + +- **The game runs this code.** Solo duels in the Unity game play these bots, so a change here is a change to the + game. Keep game behaviour identical unless the owner asked for the change; say in the commit what a player would + see differ. +- **The library stays what Unity compiles:** `netstandard2.1`, `LangVersion 9`, no Unity, Mono or + Microsoft.Data reference, no package it would ship (Newtonsoft is compile-time only). Anything it needs from the + game goes through `BotHost` (`src/YGOLegacy.Bots/Host/BotHost.cs`); a new need is a new member there, written + in `README.md`'s contract table and filed for the Unity repo. +- **Keep the namespaces the game uses** (`WindBot.*`, `YGOSharp.*`, `YGOLegacy.Bot`, `YGOLegacy.Characters`, + `YGOLegacy.Services`) and WindBot's `[YGOL]` markers on fork changes. +- **Everything builds in Docker** (`Dockerfile`); there is no `dotnet` on the hosts. `docker build --target test .` + before every push, and confirm the Gitea Actions run is green after it. +- **The engine is a release, never built here**: `engine.json` is the pin, `build/fetch-engine.sh` fetches it. +- **Releases are tags** (`vX.Y.Z`, with its `CHANGELOG.md` section). Never re-upload a release; tag a patch. + Tag only when asked. +- **The Unity repo** (`donny/yugioh-legacy-unity`) is never changed from here: work for it is a ticket. It is huge: + never a full or blob-less clone, never `git grep` over it; read single files through the Gitea API or a depth-1, + tree-less fetch of only the paths needed. +- **Test data** is `tests/fixture/game`, built by `tests/fixture/make-fixture.py` from the Unity repo's `Data/` at a + named commit. Never read or write the live Syncthing shares (`/mnt/nvme/syncthing/`). +- Git remote: Gitea (`donny`), never GitHub. Default branch `main`. diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2b0715d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +One section per release; the section is the release's body. Each names the engine release it was built and +tested against (`bots.json`'s `engine`). + +## v0.1.0 — 2026-10-19 + +The bots leave the Unity repo. Engine `v1.4.1`. + +- `YGOLegacy.Bots.dll` (.NET Standard 2.1, C# 9): the Unity repo's WindBot (`Assets/Scripts/Windbot/**` minus + `BotWrapper.cs`), the YGOSharp files WindBot uses, `Core/Bot/` (SmartExecutor, BoardEvaluator, BattlePredictor, + BrainRules, EffectSimulator, DuelStateBus, LocalDuelHost, LoopbackBotSeat, Lookahead, ArenaReport), the + character model and registry and `BotRoster`, as of Unity `dev` `19c6158`. No change to how the bots play. +- What came from the game now comes through `BotHost`: `Log`, `Cards` (the SQLite connection), `Files` (locale, + the expansions' `.cdb`), `DeckLimits` (`#ruleset` → caps) and `BotConf` (bot.conf). `CharacterRegistry` keeps + `Scan`/`Publish`/`LoadFromDisk`; the game's UniTask `Init`/`Loaded` stay in the game. +- `BotArena` for `linux-x64` and `win-x64`, same options and report as the Unity repo's `Tools/BotArena`. + `--host local` on both; `--host server` on Windows only (on Linux the engine's server finds no cards). +- Tests: 100 Smart-vs-Smart `AI_OldSchool` duels on Linux with 0 errors; the same `--seed` gives the same report. diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..43c994c --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1 @@ +@AGENTS.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cad3114 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,69 @@ +# yugioh-legacy-bots: the library, both arenas and the tests, on the engine release engine.json pins. There is no +# dotnet on the build hosts; everything builds here. Build context: the repo root. +# docker build --target test . build, then the tests (the Linux arena plays the fixture duels) +# docker build --target dist -o out . the same, then out/{lib,bot-arena-linux-x64,bot-arena-win-x64} +# docker build --target release -o dist . the same, packed as the release assets (.gitea/workflows/ci.yml) +# docker build --build-arg BOTS_VERSION=v0.1.0 ... stamps the version (default: local) + +ARG ENGINE_RELEASES=https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-engine/releases/download + +# The engine is never built here: build/fetch-engine.sh downloads the pinned release and refuses a bundle whose +# engine.json is not the pin. +FROM debian:bookworm-slim@sha256:3783cc01769c7b2b1b83a5c5ad96c815348e28ed7da68e2e3687004faa906251 AS engine +ARG ENGINE_RELEASES +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates curl jq unzip zip \ + && rm -rf /var/lib/apt/lists/* +COPY engine.json /repo/engine.json +COPY build/fetch-engine.sh /repo/build/fetch-engine.sh +RUN ENGINE_RELEASES=$ENGINE_RELEASES /repo/build/fetch-engine.sh /engine + +FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:78235e09001f52b6592c458ac010775ebac6725422e80cd0c1650590f67b2743 AS build +ARG BOTS_VERSION=local +ENV DOTNET_CLI_TELEMETRY_OPTOUT=1 DOTNET_NOLOGO=1 +WORKDIR /src +COPY --from=engine /engine/ .engine/ +COPY Directory.Build.props engine.json ./ +COPY src/YGOLegacy.Bots/YGOLegacy.Bots.csproj src/YGOLegacy.Bots/ +COPY src/BotArena/BotArena.csproj src/BotArena/ +COPY tests/YGOLegacy.Bots.Tests/YGOLegacy.Bots.Tests.csproj tests/YGOLegacy.Bots.Tests/ +RUN for rid in linux-x64 win-x64; do dotnet restore src/BotArena -r $rid -p:SelfContained=true -p:RestoreUseStaticGraphEvaluation=false || exit 1; done \ + && dotnet restore tests/YGOLegacy.Bots.Tests +COPY src/ src/ +COPY tests/ tests/ +RUN dotnet build src/YGOLegacy.Bots -c Release --no-restore -p:BotsVersion=$BOTS_VERSION -warnaserror:CS8032 \ + && mkdir -p /out/lib \ + && cp src/YGOLegacy.Bots/bin/Release/netstandard2.1/YGOLegacy.Bots.dll src/YGOLegacy.Bots/bin/Release/netstandard2.1/YGOLegacy.Bots.pdb /out/lib/ \ + && for rid in linux-x64 win-x64; do \ + dotnet publish src/BotArena -c Release -r $rid --self-contained -p:BotsVersion=$BOTS_VERSION \ + -o /out/bot-arena-$rid || exit 1; \ + done \ + && ls -l /out/lib /out/bot-arena-linux-x64/BotArena /out/bot-arena-linux-x64/libygol-engine.so /out/bot-arena-win-x64/BotArena.exe /out/bot-arena-win-x64/ygol-engine.dll + +# The tests: the library through BotHost, and the published Linux arena on tests/fixture/game. +FROM build AS test +ARG BOTS_VERSION=local +ENV BOTARENA_EXE=/out/bot-arena-linux-x64/BotArena BOTS_VERSION=$BOTS_VERSION +RUN dotnet test tests/YGOLegacy.Bots.Tests -c Release --no-restore -p:BotsVersion=$BOTS_VERSION --logger "console;verbosity=normal" + +FROM scratch AS dist +COPY --from=test /out/ / + +# The release assets: each bundle carries bots.json { version, engine }, and so does the release itself. +FROM engine AS release-pack +ARG BOTS_VERSION=local +COPY --from=dist / /out/ +COPY engine.json /repo/engine.json +RUN set -eu; cd /out \ + && jq -n --arg version "$BOTS_VERSION" --arg engine "$(jq -r .version /repo/engine.json)" \ + '{version: $version, engine: $engine}' > bots.json \ + && for d in lib bot-arena-linux-x64 bot-arena-win-x64; do cp bots.json "$d/"; done \ + && mkdir /dist \ + && (cd lib && zip -q -X /dist/bots-lib.zip YGOLegacy.Bots.dll YGOLegacy.Bots.pdb bots.json) \ + && tar czf /dist/bot-arena-linux-x64.tar.gz bot-arena-linux-x64 \ + && zip -q -r -X /dist/bot-arena-win-x64.zip bot-arena-win-x64 \ + && cp bots.json /dist/ \ + && cat /dist/bots.json && ls -l /dist + +FROM scratch AS release +COPY --from=release-pack /dist/ / diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c4fb0d --- /dev/null +++ b/README.md @@ -0,0 +1,108 @@ +# yugioh-legacy-bots + +The bots of Yu-Gi-Oh! Legacy, as one library both the game and the server take as a release, the way both take +the engine (`donny/yugioh-legacy-engine`): WindBot, the parts of YGOSharp it uses, the SmartExecutor brain, the +in-process duel host, the character registry and the roster (`YGOLegacy.Bots.dll`), and the headless Bot Arena +on top of it (`BotArena`, Linux and Windows). The game plays these bots in solo duels; the server runs the arena. + +The code came from the Unity repo (`donny/yugioh-legacy-unity`, branch `dev`, commit +`19c6158197a0a8f0817dd0924bb9a6a987c2bfc5`); the first commit here is that code, verbatim, in this layout. + +## Layout + +| Path | What | +|---|---| +| `src/YGOLegacy.Bots/` | The library: .NET Standard 2.1, C# 9, no Unity reference (Unity compiles against it). `WindBot/`, `YGOSharp/` (the client, the card managers, the enums), `Bot/` (SmartExecutor, BoardEvaluator, BrainRules, EffectSimulator, BattlePredictor, DuelStateBus, LocalDuelHost, LoopbackBotSeat, Lookahead, ArenaReport), `Characters/`, `Services/BotRoster.cs`, and `Host/BotHost.cs`, the contract. | +| `src/BotArena/` | The headless arena, .NET 8, `linux-x64` and `win-x64`. Its own `README.md` has the options, the report and how it works. | +| `tests/YGOLegacy.Bots.Tests/` | NUnit: the library through its contract, and the Linux arena end to end. | +| `tests/fixture/` | `game/`, the smallest game root the tests duel on, and `make-fixture.py`, which builds it (see its `README.md`). | +| `engine.json` | The pin: the engine release this repo builds and tests against (that release's `engine.json`, verbatim). | +| `build/fetch-engine.sh` | Downloads the pinned release into `.engine/` and refuses one whose `engine.json` is not the pin. | +| `Dockerfile` | Every build and test. There is no `dotnet` on the build hosts. | +| `.gitea/workflows/ci.yml` | CI on every push; the release on a `vX.Y.Z` tag. | +| `CHANGELOG.md` | One section per release; it is the release's body. | + +## The contract: what the game implements + +The library reaches the program that runs it only through `YGOLegacy.Bots.BotHost`. The host sets it once at +start-up, before the first bot is built and before the character registry loads. Every path the bots read is +relative to the current directory, which must be the game root (`Data/locales//cards.cdb`, +`Expansions/*.cdb`, `Data/Windbot/Decks/`, `Deck/`, `Data/Characters/`). + +| `BotHost.` | Interface | The game (YGO-156) | The arena (`src/BotArena/ArenaHost.cs`) | +|---|---|---|---| +| `Log` | `IBotLog`: `Info`, `Warning`, `Error` | `UnityEngine.Debug.Log` / `LogWarning` / `LogError` | stderr, into the worker log | +| `Cards` | `ICardDatabase`: `IDbConnection Connect(string path)`, not yet opened | `new Mono.Data.Sqlite.SqliteConnection("Data Source=" + path)` | `Microsoft.Data.Sqlite`, read-only, shared | +| `Files` | `IGameFiles`: `Locale`, `ExpansionCardDatabases()` | `Language.GetConfig()`, `ResourceManager.GetAllCdbInZipPaths()` | `--locale`; the `.cdb` it extracts from `Expansions/*.ypk` | +| `DeckLimits` | `IDeckLimits`: `DeckCaps For(string rulesetId)` (the deck file's `#ruleset`, or null) | `DeckLimits.For(id)` → `new DeckCaps(r.MainMax, r.ExtraLimit(), r.SideMax)`, read at each call | the default, 60 / 99 / 15 | +| `BotConf` | `IBotConf`: `Entries()` (`BotConfEntry` name, command, desc, flags), `Reload()` | `SoloLoader.Catalog` (initialised if not ready), `SoloLoader.Reload()` | `Data/locales//bot.conf` | + +Defaults when a host sets nothing: `Log` the console, `Files` en-US with no expansion databases, `DeckLimits` +60 / 99 / 15, `BotConf` empty (the roster then uses its built-in Dovphin command). `Cards` has no default: loading +the card databases without it throws. + +Two more things the game does on its side: + +- **Characters.** `CharacterRegistry` is in the library without its UniTask surface. The game keeps its own + `Init()` / `Loaded`: `CharacterRegistry.Scan(CharacterRegistry.Root)` on the thread pool, then + `CharacterRegistry.Publish(scan)` on the main thread (exactly what `Init` did). Hosts without a player loop call + `CharacterRegistry.LoadFromDisk()`. +- **Internals.** The game calls internal members of WindBot (`WindBot.Program.YGOL_Main`, ...). The library keeps + them internal and lets `Assembly-CSharp`, `Assembly-CSharp-Editor`, `BotArena` and the tests see them + (`Properties/AssemblyInfo.cs`). + +Namespaces are the ones the game already uses (`WindBot.*`, `YGOSharp.*`, `YGOLegacy.Bot`, `YGOLegacy.Characters`, +`YGOLegacy.Services` for `BotRoster` / `BotDef`), so the game's callers do not change. What stays in the game: +`GamePaths`, `DeckLimits` / `RulesetDeck` / `DeckPool` and the rulesets, `SoloLoader`, `CharacterLoader`, +`VoiceLineStore`, the in-editor arena (`Core/Bot/BotArena.cs`) and YGOSharp's server side (`YGOSharp/YGOSharp/*`, +`Api.cs`, `Duel.cs`, `MtRandom.cs`, the `Async*` and `NetworkServer` files). + +The game references `YGOLegacy.Bots.dll` beside the engine's `YGOLegacy.Engine.dll` (same release as +`bots.json`'s `engine`) and its own Newtonsoft.Json (Unity's `com.unity.nuget.newtonsoft-json`, 13.x). Neither is +in `bots-lib.zip`. + +## Build and test + +```bash +docker build --target test . # build, then every test (the Linux arena plays the fixture duels) +docker build --target dist -o out . # out/lib, out/bot-arena-linux-x64, out/bot-arena-win-x64 +docker build --target release -o dist . # the release assets +``` + +With a dotnet 8 SDK instead: `build/fetch-engine.sh`, then `dotnet build src/YGOLegacy.Bots`, `dotnet publish +src/BotArena -r linux-x64 --self-contained`, `BOTARENA_EXE=/BotArena dotnet test tests/YGOLegacy.Bots.Tests`. + +The tests: the library through `BotHost` (cards, deck caps and `#ruleset`, the roster's deck → character → brain +and its Dovphin fallback, a broken character logged, the game's scan-then-publish), and the published Linux arena +on the fixture: 100 duels of `AI_OldSchool` against itself (both seats the Smart brain) with 0 errors, and the +same `--seed` giving the same report at `--parallel 4` and `--parallel 2` (clocks aside). + +## Engine pin + +`engine.json` is the engine release's own `engine.json`. `build/fetch-engine.sh` downloads that version's +`ygol-engine-binding.zip`, `ygol-engine-linux-x64.tar.gz` and `ygol-engine-win64.zip` and refuses a bundle that +disagrees with the pin. The library compiles against the binding; each arena ships its platform's native library +and the binding next to the exe. **Moving to a new engine release:** copy that release's `engine.json` over this +one, run `docker build --target test .`, commit, release. The game and the server should be on the same engine +release as the bots they take (`bots.json`'s `engine`). + +## Release + +```bash +# CHANGELOG.md: add "## vX.Y.Z — " (the section is the release body), commit +git tag vX.Y.Z && git push origin main vX.Y.Z +``` + +The run (Actions tab) builds, tests and packs; the release named after the tag appears only when all of that +passed. A tag whose release already exists is refused: a bad release gets a new patch tag, never a re-upload. + +`https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-bots/releases/download//`: + +| Asset | Contents | +|---|---| +| `bots-lib.zip` | `YGOLegacy.Bots.dll`, `YGOLegacy.Bots.pdb`, `bots.json` | +| `bot-arena-linux-x64.tar.gz` | `bot-arena-linux-x64/`: `BotArena` (self-contained), `libygol-engine.so`, `YGOLegacy.Engine.dll`, `engine.json`, `bots.json` | +| `bot-arena-win-x64.zip` | `bot-arena-win-x64/`: `BotArena.exe` (self-contained), `ygol-engine.dll`, `YGOLegacy.Engine.dll`, `engine.json`, `bots.json` | +| `bots.json` | `{ version, engine }`: this release and the engine release it was built and tested against | + +`YGOLegacy.Bots.BotsBuild.Version` and `.Engine` carry the same two strings inside the library. diff --git a/build/fetch-engine.sh b/build/fetch-engine.sh new file mode 100755 index 0000000..4f31fec --- /dev/null +++ b/build/fetch-engine.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# Fetches the engine release engine.json pins into (default .engine/) and refuses a bundle whose +# engine.json is not the pin: +# /YGOLegacy.Engine.dll the C# binding (ygol-engine-binding.zip) +# /linux-x64/libygol-engine.so ygol-engine-linux-x64.tar.gz +# /win-x64/ygol-engine.dll ygol-engine-win64.zip +# Needs curl, jq, tar, unzip. +set -euo pipefail +repo=$(cd "$(dirname "$0")/.." && pwd) +out=${1:-$repo/.engine} +releases=${ENGINE_RELEASES:-https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-engine/releases/download} +pin=$repo/engine.json +version=$(jq -r .version "$pin") +[ "$(jq -r .binding "$pin")" = "$version" ] || { echo "the pin's binding $(jq -r .binding "$pin") is not its version $version" >&2; exit 1; } + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +for asset in ygol-engine-linux-x64.tar.gz ygol-engine-win64.zip ygol-engine-binding.zip; do + curl -fsSL -o "$tmp/$asset" "$releases/$version/$asset" +done +mkdir -p "$tmp/linux" "$tmp/win" +tar xzf "$tmp/ygol-engine-linux-x64.tar.gz" -C "$tmp/linux" libygol-engine.so engine.json +unzip -q "$tmp/ygol-engine-win64.zip" ygol-engine.dll engine.json -d "$tmp/win" +unzip -q "$tmp/ygol-engine-binding.zip" YGOLegacy.Engine.dll -d "$tmp" +for bundle in linux win; do + # exports is per platform; everything else must be the pin's + got=$(jq -S 'del(.exports)' "$tmp/$bundle/engine.json") + want=$(jq -S 'del(.exports)' "$pin") + [ "$got" = "$want" ] || { echo "the $version $bundle bundle's engine.json differs from the pin:" >&2; cat "$tmp/$bundle/engine.json" >&2; exit 1; } +done +[ "$(jq -S . "$tmp/linux/engine.json")" = "$(jq -S . "$pin")" ] \ + || { echo "the pin is the Linux bundle's engine.json, verbatim; it differs:" >&2; cat "$tmp/linux/engine.json" >&2; exit 1; } + +mkdir -p "$out/linux-x64" "$out/win-x64" +cp "$tmp/YGOLegacy.Engine.dll" "$out/" +cp "$tmp/linux/libygol-engine.so" "$out/linux-x64/" +cp "$tmp/win/ygol-engine.dll" "$out/win-x64/" +cp "$pin" "$out/engine.json" +echo "ygol-engine $version -> $out"