# openra-arena: OpenRA (Red Alert) prebuilt with the decision-ra LLM arena bridge, ready to play and record games.
# Used as the Jetty sandbox snapshot `openra-arena`.
#   docker build --platform linux/amd64 -t openra-arena .

# ---- build the engine ----------------------------------------------------------------------------------------------
FROM mcr.microsoft.com/dotnet/sdk:8.0-bookworm-slim AS engine
RUN apt-get update && apt-get install -y --no-install-recommends git make ca-certificates && rm -rf /var/lib/apt/lists/*
# OpenRA-RL's engine fork, pinned; our arena bridge + replay capture is applied as a patch.
ARG OPENRA_REPO=https://github.com/yxc20089/OpenRA.git
ARG OPENRA_COMMIT=9b271c1a5562a07deeb1c3f81d9a556cd563a676
WORKDIR /src/OpenRA
RUN git init -q && git remote add origin ${OPENRA_REPO} && git fetch -q --depth 1 origin ${OPENRA_COMMIT} && git checkout -q FETCH_HEAD
COPY patches/openra-arena.patch /tmp/openra-arena.patch
RUN git apply /tmp/openra-arena.patch && make TARGETPLATFORM=linux-x64 \
 && rm -rf OpenRA.*/obj .git

# ---- runtime ---------------------------------------------------------------------------------------------------------
FROM mcr.microsoft.com/dotnet/aspnet:8.0-bookworm-slim
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates curl unzip git sudo procps jq \
      xvfb xauth libgl1 libgl1-mesa-dri libegl1 libgles2 libsdl2-2.0-0 libopenal1 libfreetype6 liblua5.1-0 \
      ffmpeg python3 python3-venv python3-pip \
 && curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
 && apt-get install -y --no-install-recommends nodejs \
 && npm install -g @anthropic-ai/claude-code@2.1.282 \
 && rm -rf /var/lib/apt/lists/* /root/.npm \
 && ln -sf /usr/bin/python3 /usr/local/bin/python \
 && curl -LsSf https://astral.sh/uv/0.9.5/install.sh | env UV_INSTALL_DIR=/usr/local/bin sh \
 && mkdir -p /logs/agent/sessions /app && chmod -R 0777 /logs /app

COPY --from=engine /src/OpenRA /opt/openra

# Red Alert freeware game data (the same quick-install bundle OpenRA's content installer uses).
RUN mkdir -p /opt/openra-content/ra/v2/expand /opt/openra-content/ra/v2/cnc \
 && curl -fsSL --retry 3 -o /tmp/ra.zip https://openra.baxxster.no/openra/ra-quickinstall.zip \
 && unzip -q /tmp/ra.zip -d /tmp/ra \
 && cp /tmp/ra/*.mix /opt/openra-content/ra/v2/ && cp /tmp/ra/expand/* /opt/openra-content/ra/v2/expand/ \
 && cp /tmp/ra/cnc/* /opt/openra-content/ra/v2/cnc/ && rm -rf /tmp/ra /tmp/ra.zip

# Arena orchestrator (Python).
COPY pyproject.toml README.md /opt/decision-ra/
COPY arena /opt/decision-ra/arena
COPY bin /opt/decision-ra/bin
COPY patches /opt/decision-ra/patches
COPY Dockerfile /opt/decision-ra/Dockerfile
RUN python3 -m venv /opt/decision-ra/.venv && /opt/decision-ra/.venv/bin/pip install -q "httpx>=0.27" \
 && chmod +x /opt/decision-ra/bin/* && ln -s /opt/decision-ra/bin/run-match /usr/local/bin/run-match \
 && chmod -R a+rX /opt/openra /opt/openra-content /opt/decision-ra

ENV ARENA_ENGINE_DIR=/opt/openra \
    ARENA_CONTENT_DIR=/opt/openra-content \
    DOTNET_ROLL_FORWARD=LatestMajor \
    DOTNET_CLI_TELEMETRY_OPTOUT=1 \
    LIBGL_ALWAYS_SOFTWARE=1 \
    SDL_AUDIODRIVER=dummy
WORKDIR /app
