N6: Joining games: direct connect, LAN and server list

- LAN discovery: a listening server answers QUERY packets on its game port
  plus one (ServerBeacon, off with --no-lan). ServerBrowser scans eight game
  ports from 24650 by broadcast.
- A self-hostable list server: --list-server [--port=N] (ListServer,
  default UDP 24660). Game servers register with --list=HOST[:PORT] and
  --name=NAME; they are listed under their source address only after
  echoing a CHALLENGE token. Entries expire after 45 s, 8 per address,
  256 in all; every source is rate-limited (SourceLimiter).
- NetDiscovery messages: every QUERY is padded to 512 bytes and no answer
  is bigger, so neither service amplifies spoofed requests.
- tidewall --browse [--list=...] [--lan-target=IP] [--lan-port=N] prints
  the servers found as JSON until menus (A10) show them.
- People who connect mid-match or during results watch a living player
  (Match.watch) and take places ahead of bots in the next lobby, instead
  of being told the server is full. Bots fill only the places left when
  the match starts.
- Tests: codec round trips, size limits and a 40,000-message fuzz; list
  server with fake servers (challenge, stolen and wrong tokens, renewal,
  expiry, caps, flood); LAN scan over loopback; watchers over UDP; lobby
  backfill. make discovery-check runs it all as real processes.

Closes #37

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 17:33:32 -05:00
co-authored by Claude Opus 5.5
parent 53e81bebe1
commit f795ee3fa5
31 changed files with 1524 additions and 17 deletions
+36 -1
View File
@@ -36,6 +36,9 @@ var sim: Simulation
## Participant handles in join order, and which of them are bots.
var participants: Array[int] = []
var bot_handles: Dictionary = {}
## People who arrived after the match started: they spectate, and become
## participants when the next lobby opens. See watch().
var watchers: Array[int] = []
## Handles playing in the current match, and their entity IDs both ways.
var entity_of: Dictionary = {}
var handle_of: Dictionary = {}
@@ -103,7 +106,10 @@ func in_match() -> bool:
## Adds a participant. Returns false if the handle is already here or the lobby is
## full. During a match they wait for the next one.
func join(handle: int, bot: bool = false) -> bool:
if participants.has(handle) or participants.size() >= rules.max_players:
# Once the barge has left, newcomers watch (see watch()) until the next lobby.
if in_match() or phase == RESULTS:
return false
if participants.has(handle) or watchers.has(handle) or participants.size() >= rules.max_players:
return false
participants.append(handle)
if bot:
@@ -115,6 +121,11 @@ func join(handle: int, bot: bool = false) -> bool:
## Removes a participant. In a match, they're placed as if eliminated and their player
## leaves the simulation. Returns false if the handle isn't here.
func leave(handle: int) -> bool:
if watchers.has(handle):
watchers.erase(handle)
spectating.erase(handle)
events.append({"type": "left", "handle": handle})
return true
if not participants.has(handle):
return false
participants.erase(handle)
@@ -134,6 +145,23 @@ func leave(handle: int) -> bool:
return true
## Lets someone who arrives while a match is running (or showing results) watch it.
## They spectate a living player, or a winner during results, and join the next
## lobby as a participant ahead of any bots. Returns false outside a match, for a
## handle already here, and when the server already has a full match's worth of
## people waiting.
func watch(handle: int) -> bool:
if not (in_match() or phase == RESULTS):
return false
if participants.has(handle) or watchers.has(handle) or watchers.size() >= rules.max_players:
return false
watchers.append(handle)
var living: Array[int] = winners.duplicate() if phase == RESULTS and not winners.is_empty() else alive()
spectating[handle] = _retarget(handle, 0, living)
events.append({"type": "watching", "handle": handle, "target": spectating[handle]})
return true
## Passes a participant's command to the simulation. Returns false outside a match, for
## a handle that isn't playing, or if the simulation rejected it.
func submit_command(handle: int, command: PlayerCommand) -> bool:
@@ -372,6 +400,13 @@ func _reset() -> void:
_match_size = 0
sim = Simulation.new(match_seed(matches_started))
_enter(LOBBY)
# People who watched the last match take places first; bots only ever fill
# what is left when the next one starts.
var waiting: Array[int] = watchers.duplicate()
watchers.clear()
for handle: int in waiting:
if not join(handle):
watchers.append(handle)
## The living handle `handle` is watching, or 0 if they aren't spectating or nobody is