# 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

# Local patch: custom game values and elements (docker/patch-custom-game.js).
# Hooks the loader into main.js and the game server, makes the e.g. Lone Wolf rates
# data-driven, and merges phrase overrides into the language catalogs. The
# loader itself is copied to /app/docker/ in the runtime stage; the custom file
# is mounted at runtime from ./files/custom.
COPY docker/patch-custom-game.js /tmp/patch-custom-game.js
RUN node /tmp/patch-custom-game.js /app

# Local patch: optional email auto-verification (docker/patch-auto-verify-email.js).
# A self-hosted server without SES cannot deliver verification links, so every
# new account would stay "Not Verified". The patch marks accounts verified at
# signup and login when options.auto_verify_email is enabled; without the
# option the upstream behaviour is unchanged.
COPY docker/patch-auto-verify-email.js /tmp/patch-auto-verify-email.js
RUN node /tmp/patch-auto-verify-email.js /app

# Local patch: make the Steam/MAS auth-id requirement for tutorial and class
# rewards optional (docker/patch-tutorial-reward-auth.js). Controlled at runtime
# by options.tutorial_reward_requires_auth_id.
COPY docker/patch-tutorial-reward-auth.js /tmp/patch-tutorial-reward-auth.js
RUN node /tmp/patch-tutorial-reward-auth.js /app

# Local patch: make the merchant and non-merchant multi-account limits
# configurable (docker/patch-multi-account-limits.js). Upstream hard-codes one
# merchant per IP/owner and three non-merchant characters per IP/owner in the
# backend enforcement cron, while login uses options.ip_limit/character_limit.
# The patch makes both places use options.merchant_ip_limit,
# options.merchant_owner_limit, options.ip_limit and options.character_limit.
COPY docker/patch-multi-account-limits.js /tmp/patch-multi-account-limits.js
RUN node /tmp/patch-multi-account-limits.js /app


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"]
