F6: Headless server entry point and smoke test
`tools/godot.sh -- --server` runs a headless dedicated server with --seed, --port, --players, --bots, --ticks, --unthrottled and --log-every. ServerLoop drives the simulation and times every tick without a scene tree, so tests run it directly. A placeholder WanderBot gives smoke runs realistic load until the v0.4 bots land. Logs are JSON lines that all carry the seed. An ErrorCounter logger makes any engine or script error exit 1; a bad command line exits 2. `make smoke` (now part of `make check`) runs 1,000 ticks with 24 bots, prints min/mean/p50/p95/p99/max tick time against the 12 ms budget, reruns to confirm the state hash reproduces, and proves a deliberate script error and a bad config both exit non-zero. Closes #6 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
## Command-line configuration for a headless server.
|
||||
##
|
||||
## tools/godot.sh -- --server [--seed=N] [--port=N] [--players=N] [--bots=N]
|
||||
## [--ticks=N] [--unthrottled] [--log-every=N]
|
||||
##
|
||||
## Unknown or malformed arguments are errors, so a typo can't silently run the wrong
|
||||
## match. See `parse()`.
|
||||
class_name ServerConfig
|
||||
extends RefCounted
|
||||
|
||||
const DEFAULT_PORT: int = 24650
|
||||
## The `--fault` names `HeadlessServer` understands. They exist so tools/smoke.sh can
|
||||
## prove a script error really produces a non-zero exit.
|
||||
const FAULTS: Array[String] = ["", "script-error"]
|
||||
|
||||
## True when `--server` was given.
|
||||
var server: bool = false
|
||||
## The match seed. `has_seed` is false when none was given and the server must pick one.
|
||||
var match_seed: int = 0
|
||||
var has_seed: bool = false
|
||||
## UDP port the server will listen on once networking lands (v0.5).
|
||||
var port: int = DEFAULT_PORT
|
||||
## Player slots for human clients. Until v0.5 they join as idle players.
|
||||
var players: int = 0
|
||||
var bots: int = 0
|
||||
## Ticks to run before exiting. 0 runs until the process is stopped.
|
||||
var ticks: int = 0
|
||||
## Runs ticks back to back instead of at the real tick rate. For tests and smoke runs.
|
||||
var unthrottled: bool = false
|
||||
## Logs a progress line every this many ticks. 0 turns progress lines off.
|
||||
var log_every: int = 0
|
||||
var fault: String = ""
|
||||
## Human-readable problems found while parsing. Empty when the config is usable.
|
||||
var errors: Array[String] = []
|
||||
|
||||
|
||||
## Parses arguments. Arguments before `--server` are ignored unless `--server` is absent,
|
||||
## in which case nothing is parsed and `server` stays false.
|
||||
static func parse(args: PackedStringArray) -> ServerConfig:
|
||||
var config: ServerConfig = ServerConfig.new()
|
||||
var start: int = args.find("--server")
|
||||
if start < 0:
|
||||
return config
|
||||
config.server = true
|
||||
for index: int in range(start + 1, args.size()):
|
||||
config._parse_one(args[index])
|
||||
config._validate()
|
||||
return config
|
||||
|
||||
|
||||
func is_valid() -> bool:
|
||||
return errors.is_empty()
|
||||
|
||||
|
||||
## The values that matter for reproducing a run, for the start log line.
|
||||
func to_dictionary() -> Dictionary:
|
||||
return {
|
||||
"seed": match_seed,
|
||||
"port": port,
|
||||
"players": players,
|
||||
"bots": bots,
|
||||
"ticks": ticks,
|
||||
"unthrottled": unthrottled,
|
||||
}
|
||||
|
||||
|
||||
func _parse_one(arg: String) -> void:
|
||||
if arg == "--unthrottled":
|
||||
unthrottled = true
|
||||
return
|
||||
var parts: PackedStringArray = arg.split("=", true, 1)
|
||||
if parts.size() != 2 or not arg.begins_with("--"):
|
||||
errors.append("unknown argument '%s'" % arg)
|
||||
return
|
||||
var key: String = parts[0]
|
||||
var value: String = parts[1]
|
||||
match key:
|
||||
"--seed":
|
||||
if _check_int(key, value):
|
||||
match_seed = value.to_int()
|
||||
has_seed = true
|
||||
"--port":
|
||||
if _check_int(key, value):
|
||||
port = value.to_int()
|
||||
"--players":
|
||||
if _check_int(key, value):
|
||||
players = value.to_int()
|
||||
"--bots":
|
||||
if _check_int(key, value):
|
||||
bots = value.to_int()
|
||||
"--ticks":
|
||||
if _check_int(key, value):
|
||||
ticks = value.to_int()
|
||||
"--log-every":
|
||||
if _check_int(key, value):
|
||||
log_every = value.to_int()
|
||||
"--fault":
|
||||
fault = value
|
||||
_:
|
||||
errors.append("unknown argument '%s'" % arg)
|
||||
|
||||
|
||||
func _check_int(key: String, value: String) -> bool:
|
||||
if value.is_valid_int():
|
||||
return true
|
||||
errors.append("%s needs a whole number, got '%s'" % [key, value])
|
||||
return false
|
||||
|
||||
|
||||
func _validate() -> void:
|
||||
if port < 1 or port > 65535:
|
||||
errors.append("--port must be between 1 and 65535, got %d" % port)
|
||||
if players < 0 or bots < 0:
|
||||
errors.append("--players and --bots can't be negative")
|
||||
if players + bots > Tuning.SOLO_PLAYERS:
|
||||
errors.append(
|
||||
"--players plus --bots is %d, more than the match size of %d"
|
||||
% [players + bots, Tuning.SOLO_PLAYERS]
|
||||
)
|
||||
if ticks < 0:
|
||||
errors.append("--ticks can't be negative")
|
||||
if log_every < 0:
|
||||
errors.append("--log-every can't be negative")
|
||||
if fault not in FAULTS:
|
||||
errors.append("unknown --fault '%s'" % fault)
|
||||
Reference in New Issue
Block a user