Files
transcience/tests/unit/test_emergency_escape.gd
T
claudeandClaude Opus 5 39d857c4d1 Bound what one packet can cost, and stop the hub eating its own loot
Selecting a character rewrote the entire store to disk, re-placed the
player, re-streamed its map chunks and broadcast a roster to everyone --
off one small reliable packet, at whatever rate a client cared to send.
Repeating a selection you already hold now answers yes and stops, and
select/create are paced per peer. Item moves and experience grants, the
two paths a client can trigger every tick, mark the store dirty and the
server flushes once per tick instead of once per event.

That flush also fixes experience quietly not being saved: grant_xp only
wrote when a level was crossed, and level is DERIVED from lifetime xp, so
the number nobody was keeping was the only number there is. A restart
rolled a character back to its last level-up.

The emergency escape ran in the hub, where completing it means
transferring into the instance you are already in: teleported to spawn,
healed to full, instanced loot dropped, every map chunk re-streamed, once
a second for as long as the key is held. The hub now opts out.

Ground loot evicts the oldest item when the floor fills. That is right
for a dungeon, which closes and takes its litter with it, and wrong for
the hub, which never closes -- a player dropping into a full floor
destroyed whatever someone else had left there longest, and drop is
edge-triggered per tick. A player's drop is refused instead; the world's
own drops still make room.

Also: a linkdead body is not in inst.peers, so closing a CLEARED dungeon
walked past it and left that peer's account and name in the dictionaries
forever -- with the new one-account guard, enough to lock someone out of
their own session. And several encoders wrote mini(size, 255) as a count
and then looped the whole array, which puts records after the count on
the wire; MAX_CLIENTS makes it unreachable, which is why it would have
survived to the day it was not.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 17:09:43 +02:00

145 lines
5.6 KiB
GDScript

extends GutTest
## The escape button is the player's safety valve, and it is also the one input
## with a real consequence attached, so its timing is server-owned end to end.
var world: SimWorld
const PEER := 3
func before_each() -> void:
world = SimWorld.new(1)
world.add_player(PEER, "tester")
func _hold_escape(ticks: int) -> void:
for _i in ticks:
var frames: Array[InputFrame] = [
InputFrame.make(world.tick + 1, Vector2.ZERO, 0.0, InputFrame.BTN_ESCAPE)]
world.queue_input(PEER, frames)
world.step()
func _events_of(type: int) -> Array:
return world.events.filter(func(e: Dictionary) -> bool: return int(e["t"]) == type)
func test_escape_takes_the_full_channel_time() -> void:
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS - 1)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0,
"the escape must not complete early")
_hold_escape(1)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 1)
func test_releasing_the_button_cancels_the_channel() -> void:
# Deliberately short of ESCAPE_CHANNEL_TICKS: hold the full duration and the
# channel completes instead, which is a different test.
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS / 2)
assert_gt(world.players[PEER].escape_ticks, 0)
var frames: Array[InputFrame] = [InputFrame.make(world.tick + 1, Vector2.ZERO, 0.0, 0)]
world.queue_input(PEER, frames)
world.step()
assert_eq(world.players[PEER].escape_ticks, 0)
assert_eq(_events_of(SimEvent.Type.ESCAPE_CANCELLED).size(), 1)
## The inverse of what this asserted originally. Interrupting on damage makes
## quitting the process strictly better than using the button, which turns the
## escape hatch into the exploit -- see SimConfig.ESCAPE_CHANNEL_TICKS.
func test_taking_damage_does_not_cancel_the_channel() -> void:
_hold_escape(20)
var before: int = world.players[PEER].escape_ticks
world.pool.spawn(world.players[PEER].pos, Vector2.ZERO, 6.0, 60, 10,
SimConfig.TEAM_ENEMY, SimConfig.KIND_ORB)
_hold_escape(1)
assert_lt(world.players[PEER].hp, SimConfig.PLAYER_MAX_HP, "setup: should have been hit")
assert_eq(world.players[PEER].escape_ticks, before + 1,
"the channel must keep running while under fire")
assert_eq(_events_of(SimEvent.Type.ESCAPE_CANCELLED).size(), 0)
## Dying is still an interruption -- it is the one thing the escape cannot beat,
## which is what keeps the one-second channel a real risk.
func test_dying_ends_the_channel() -> void:
_hold_escape(20)
var p: SimPlayer = world.players[PEER]
p.hp = 1
world.pool.spawn(p.pos, Vector2.ZERO, 6.0, 60, 999,
SimConfig.TEAM_ENEMY, SimConfig.KIND_ORB)
world.step()
assert_false(p.alive)
assert_eq(p.escape_ticks, 0)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0)
## A dropped connection channels out on exactly the same timer, so pulling the
## plug is never cheaper than pressing the button.
func test_a_linkdead_player_channels_out_without_input() -> void:
var p: SimPlayer = world.players[PEER]
p.linkdead = true
for _i in SimConfig.ESCAPE_CHANNEL_TICKS - 1:
world.step()
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0,
"a disconnect must not be an instant exit")
world.step()
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 1)
func test_a_linkdead_player_is_still_killable_while_channelling() -> void:
var p: SimPlayer = world.players[PEER]
p.linkdead = true
p.hp = 1
world.step()
world.pool.spawn(p.pos, Vector2.ZERO, 6.0, 60, 999,
SimConfig.TEAM_ENEMY, SimConfig.KIND_ORB)
world.step()
assert_false(p.alive, "the whole point: disconnecting does not grant immunity")
func test_a_cancelled_channel_restarts_from_zero() -> void:
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS / 2)
var frames: Array[InputFrame] = [InputFrame.make(world.tick + 1, Vector2.ZERO, 0.0, 0)]
world.queue_input(PEER, frames)
world.step()
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS - 1)
assert_eq(_events_of(SimEvent.Type.ESCAPE_COMPLETED).size(), 0,
"progress must not carry over from an interrupted channel")
func test_escape_completion_never_reaches_the_client_directly() -> void:
_hold_escape(SimConfig.ESCAPE_CHANNEL_TICKS)
var packet := NetCodec.decode_events(NetCodec.encode_events(world.tick, world.events))
for ev: Dictionary in packet["events"]:
assert_ne(int(ev["t"]), SimEvent.Type.ESCAPE_COMPLETED,
"the transfer is the server's decision; clients only see the outcome")
## The hub has nothing to escape from, and completing the channel there routes
## to "send this peer to the lobby" -- a transfer into the instance you are
## already in, which re-places the player: teleported to spawn, healed to full,
## instanced loot dropped, every map chunk re-streamed. Once a second, for as
## long as the key is held.
func test_the_hub_has_no_escape_to_channel() -> void:
var hub := Instance.make_lobby(1)
assert_false(hub.world.escape_enabled,
"the lobby is the one world that opts out")
hub.add_peer(99, "tester")
for i in SimConfig.ESCAPE_CHANNEL_TICKS * 2:
var frames: Array[InputFrame] = [InputFrame.make(hub.world.tick + 1,
Vector2.ZERO, 0.0, InputFrame.BTN_ESCAPE)]
hub.world.queue_input(99, frames)
hub.step()
var p: SimPlayer = hub.world.players[99]
assert_eq(p.escape_ticks, 0, "nothing should have been channelling")
for ev in hub.world.events:
assert_ne(int(ev["t"]), SimEvent.Type.ESCAPE_STARTED,
"holding escape in the hub must not start a channel")
assert_ne(int(ev["t"]), SimEvent.Type.ESCAPE_COMPLETED,
"and must certainly never complete one")
func test_a_dungeon_still_lets_you_out() -> void:
# The guard above must not be a way to disable the escape everywhere.
var run := Instance.make_dungeon(2, 1234)
assert_true(run.world.escape_enabled)