# syntax=docker/dockerfile:1

# Adventure Land (MongoDB Edition) - image build
#
# The game is split across three upstream repositories:
#   adventureland_mongodb           -> /app
#   common_engine                   -> /app/common
#   adventureland_secretsandconfig  -> /app/secretsandconfig (template only,
#                                     replaced at runtime by ./config)
#
# All three are cloned at build time, so `docker compose build --pull` is the
# way to update the game.

ARG NODE_IMAGE=node:22-bookworm-slim

FROM ${NODE_IMAGE} AS build

RUN apt-get update \
	&& apt-get install -y --no-install-recommends git ca-certificates python3 make g++ \
	&& rm -rf /var/lib/apt/lists/*

ENV GIT_TERMINAL_PROMPT=0

WORKDIR /app

ARG AL_REPO=https://github.com/kaansoral/adventureland_mongodb.git
ARG AL_REF=main
ARG COMMON_REPO=https://github.com/kaansoral/common_engine.git
ARG COMMON_REF=main
ARG CONFIG_REPO=https://github.com/kaansoral/adventureland_secretsandconfig.git
ARG CONFIG_REF=main

RUN git clone --depth 1 --single-branch --branch "${AL_REF}" "${AL_REPO}" . \
	&& git clone --depth 1 --single-branch --branch "${COMMON_REF}" "${COMMON_REPO}" common \
	&& git clone --depth 1 --single-branch --branch "${CONFIG_REF}" "${CONFIG_REPO}" secretsandconfig \
	&& rm -rf .git common/.git secretsandconfig/.git
	
# Backend (Express) and game server (Socket.IO) have separate dependency trees.
RUN npm install --omit=dev --no-audit --no-fund \
	&& cd node && npm install --omit=dev --no-audit --no-fund \
	&& rm -rf /app/node/node_modules/geoip-lite \
	&& rm -rf /app/node_modules/geoip-lite

# Local patch: make the DRM login check switchable (docker/patch-drm-check.js).
# A self-hosted server cannot produce Steam/MAS auth ids, so the check would
# otherwise flag every account with the "authfail" condition. Kept after the
# npm install so the dependency layers stay cached across patch changes.
COPY docker/patch-drm-check.js /tmp/patch-drm-check.js
RUN node /tmp/patch-drm-check.js /app/node/server.js

# Local patch: self-host attribution (docker/patch-attribution.js).
# The license requires a crawlable link back to https://adventure.land, and
# the original project should not appear to support this instance. The phrase
# ships as docker/attribution/{en.js,de.json}; other languages fall back to
# English. Kept after the npm install so dependency layers stay cached.
COPY --chown=node:node docker/attribution/en.js /app/languages/en/attribution.js
COPY --chown=node:node docker/attribution/de.json /app/languages/de/attribution.json
COPY docker/patch-attribution.js /tmp/patch-attribution.js
RUN node /tmp/patch-attribution.js /app/htmls/contents/account_help.html


FROM ${NODE_IMAGE} AS runtime

RUN apt-get update \
	&& apt-get install -y --no-install-recommends ca-certificates socat \
	&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY --from=build --chown=node:node /app /app
COPY --chown=node:node docker/ /app/docker/

# Local/dev mode (see config/options.js). The seed script refuses to run when
# NODE_ENV=production, so keep this out of the way for the seeding container.
ENV NODE_ENV=development

USER node

EXPOSE 8090 7192

ENTRYPOINT ["/app/docker/entrypoint.sh"]

CMD ["node", "main.js"]
