mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-05 12:24:25 +01:00
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0112LuqYi6ChYujek3cdoCs2
135 lines
6.7 KiB
Python
135 lines
6.7 KiB
Python
#!/usr/bin/env python3
|
||
"""U-Jagd – Fregatten-U-Jagd-Strategiespiel für die uConsole."""
|
||
|
||
import argparse
|
||
import ipaddress
|
||
import os
|
||
import random
|
||
from pathlib import Path
|
||
from urllib.parse import urlsplit
|
||
|
||
from src.core.crashlog import run_logged
|
||
from src.core.game import Game
|
||
from src.core.preferences import load_preferences
|
||
from src.core.version import APP_VERSION
|
||
from src.commander.web_auth import WebHostAuth
|
||
|
||
|
||
def main(argv=None) -> int:
|
||
parser = argparse.ArgumentParser(description="ASW-Simulation fuer die uConsole")
|
||
parser.add_argument("seed", nargs="?", type=int)
|
||
parser.add_argument("--windowed", action="store_true")
|
||
parser.add_argument("--no-audio", action="store_true")
|
||
parser.add_argument("--solo-crew", action="store_true",
|
||
help="start Remote Crew in solo mode for this launch: one "
|
||
"paired browser operates every station and the game "
|
||
"controls (never persisted)")
|
||
parser.add_argument("--remote-crew", action="store_true",
|
||
help="start Remote Crew in crew mode on the first private "
|
||
"LAN address for this launch (as F9 would)")
|
||
parser.add_argument("--status-file", metavar="PATH",
|
||
help="write the Remote Crew address and join code as JSON "
|
||
"to PATH whenever they change (used by the Windows "
|
||
"starter)")
|
||
parser.add_argument("--play-sub", action="store_true",
|
||
help="the uConsole plays the hostile submarine for this "
|
||
"launch; the frigate is crewed through Remote Crew "
|
||
"(F9) or runs on autocrew (never persisted)")
|
||
parser.add_argument("--web-host", action="store_true",
|
||
help="run one browser-only room behind a local HTTPS reverse proxy")
|
||
parser.add_argument("--public-origin", metavar="HTTPS_ORIGIN",
|
||
help="exact public HTTPS origin used by the reverse proxy; "
|
||
"without --web-host, Remote Crew (F9, crew or solo) "
|
||
"answers on this proxy origin and on its LAN address")
|
||
parser.add_argument("--web-port", type=int, default=8765)
|
||
parser.add_argument("--web-bind", default="127.0.0.1", metavar="PRIVATE_IP",
|
||
help="IPv4 address to listen on; use the device's private LAN IP "
|
||
"when the HTTPS proxy runs on another trusted LAN host")
|
||
parser.add_argument("--reset-web-host-password", action="store_true",
|
||
help="reset the web host password during this web-host launch")
|
||
parser.add_argument("--version", action="version", version=APP_VERSION)
|
||
args = parser.parse_args(argv)
|
||
if args.web_host and (args.solo_crew or not args.public_origin):
|
||
parser.error("--web-host requires --public-origin and excludes --solo-crew")
|
||
if args.remote_crew and (args.web_host or args.solo_crew):
|
||
parser.error("--remote-crew excludes --web-host and --solo-crew")
|
||
if args.web_bind != "127.0.0.1" and not args.web_host:
|
||
parser.error("--web-bind requires --web-host")
|
||
if args.reset_web_host_password and not args.web_host:
|
||
parser.error("--reset-web-host-password requires --web-host")
|
||
if args.public_origin is not None:
|
||
try:
|
||
origin = urlsplit(args.public_origin)
|
||
valid_origin = (origin.scheme == "https" and bool(origin.hostname)
|
||
and not origin.path and not origin.query and not origin.fragment
|
||
and not origin.username and not origin.password
|
||
and not args.public_origin.endswith("/")
|
||
and origin.port != 0)
|
||
except ValueError:
|
||
valid_origin = False
|
||
if not valid_origin:
|
||
parser.error("--public-origin must be an exact HTTPS origin")
|
||
if not 1024 <= args.web_port <= 65535:
|
||
parser.error("--web-port must be between 1024 and 65535")
|
||
if args.web_host:
|
||
try:
|
||
bind = ipaddress.IPv4Address(args.web_bind)
|
||
except ipaddress.AddressValueError:
|
||
parser.error("--web-bind must be a private or loopback IPv4 address")
|
||
if not (bind.is_loopback or any(bind in ipaddress.IPv4Network(network)
|
||
for network in ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"))):
|
||
parser.error("--web-bind must be a private or loopback IPv4 address")
|
||
os.environ["SDL_VIDEODRIVER"] = "dummy"
|
||
os.environ["SDL_AUDIODRIVER"] = "dummy"
|
||
# Without a terminal (uConsole menu entry) a crash would leave no trace:
|
||
# ~/.u-jagd/crash.log records start, end and any traceback or fatal signal.
|
||
return run_logged(lambda: _start(args))
|
||
|
||
|
||
def _start(args) -> int:
|
||
auth = WebHostAuth(Path.home() / ".u-jagd" / "web-host.json") if args.web_host else None
|
||
if args.reset_web_host_password:
|
||
auth.reset_local()
|
||
preferences = load_preferences()
|
||
seed = args.seed if args.seed is not None else random.SystemRandom().randrange(
|
||
1, 1_000_000_000)
|
||
game = Game(seed=seed, start_menu=True,
|
||
preferences=preferences,
|
||
fullscreen=preferences.fullscreen and not args.windowed and not args.web_host,
|
||
show_splash=not args.web_host,
|
||
audio_enabled=preferences.audio and not args.no_audio and not args.web_host,
|
||
web_mode=args.web_host)
|
||
if args.web_host:
|
||
try:
|
||
game.commander.start_web(auth, args.public_origin, args.web_port,
|
||
args.web_bind)
|
||
except Exception:
|
||
game.commander.stop()
|
||
game.audio.shutdown()
|
||
import pygame
|
||
pygame.quit()
|
||
raise
|
||
if not auth.configured:
|
||
print(f"Web host setup code (15 minutes): {auth.setup_code}", flush=True)
|
||
print(f"Web room: {args.public_origin}/admin", flush=True)
|
||
print(f"Proxy upstream: http://{args.web_bind}:{args.web_port}", flush=True)
|
||
elif args.public_origin is not None:
|
||
# Local game: Remote Crew (F9) also answers behind this HTTPS proxy.
|
||
game.commander.public_origin = args.public_origin
|
||
if args.status_file:
|
||
game.commander.status_path = os.path.abspath(args.status_file)
|
||
if (args.solo_crew or args.remote_crew) and args.web_port != 8765:
|
||
game.commander.port = args.web_port
|
||
if args.solo_crew:
|
||
game.commander.autostart_solo()
|
||
elif args.remote_crew:
|
||
game.commander.autostart()
|
||
if getattr(args, "play_sub", False):
|
||
game.local_side = "uboot"
|
||
game.run()
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|