aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 5a25d667713742620bbb5a445f040d9387fde096
parent 9879ecc39251c9222d94738e0d6403fa8e36744c
Author: anon <anon@example.com>
Date:   Sat, 24 Oct 2026 19:23:21 +0000

Resume pending player death through native resurrection on reconnect

Diffstat:
MTODO.md | 15++++++++++-----
Mdocs/player-resurrection.md | 48+++++++++++++++++++++++++++++++++++++++++++-----
Mserver/combat_test.go | 8++++----
Mserver/main.go | 4----
Aserver/resurrection_reconnect_test.go | 212+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/world_actions.go | 14+++++++++++++-
6 files changed, 282 insertions(+), 19 deletions(-)

diff --git a/TODO.md b/TODO.md @@ -300,6 +300,12 @@ Historical outdoor give-up timing and obstacle navigation remain open. before clients see the replacement; failed saves retry without duplication. - [x] Resurrect a player through the native flow. Check: death leads to a valid resurrection location and usable character without reloading the browser. +- [x] Resume a pending death after reconnect or server restart. Check: the + loaded client receives one death notification and completes the native + scanner return, retaining committed XP loss, removed effects and reclaim. + Repeated ready/return requests cannot restore resources twice; failed or + ignored persistence and failed COMMIT leave the return retryable. See + [player resurrection](docs/player-resurrection.md). - [x] Save a resurrection location. Check: using one save terminal persists the location and a later death returns the character there. - [x] Reclaim items after death. Check: the applicable items move to reclaim and @@ -1127,11 +1133,10 @@ and server restarts. Turn every reproduced bug into a focused regression test. - [ ] Audit death, resurrection, insurance and reclaim. Cover each supported item policy, saved/unsaved locations, repeated return/retrieval, full inventory, failed persistence and restart at each transition. - Replace the world-entry shortcut in `server/main.go` that revives a zero-health - player at full health without completing the scanner return. Cover disconnect - and restart between the killing hit and native action 152; retain the already - committed XP loss, removed effects and reclaim state while completing exactly - one legitimate resurrection at the saved location. + Pending-death reconnects now resume the native scanner return instead of + restoring full health at the death location. Regression cases cover saved + and default arrivals, database reopen, premature/repeated return requests, + loading/ready boundaries, retained outcomes and persistence rollback/retry. Ability-buff deaths now cover nonlethal retention, zero-health durability, pending-cast refunds, resource clamps, reclaim, ignored/failed SQL and COMMIT rollback, shared ranged outcomes and resurrection without replay. Two native diff --git a/docs/player-resurrection.md b/docs/player-resurrection.md @@ -65,11 +65,15 @@ take effect. The replacement world session initializes the same character, equipment and inventory at the saved return point. Death and resurrection persist their resources immediately; ordinary simulation -uses periodic checkpoints. Startup restores the durable state, but world entry -still revives a pending dead player at full health in `server/main.go` instead -of completing the scanner return. That shortcut remains an explicit audit gap; -it does not restore lost XP. The supported scanner also protects saved -XP, with classic uninsured loss committed alongside death; see +uses periodic checkpoints. Reconnecting after the killing hit preserves zero +health, the committed XP loss, removed effects and pending reclaim. After the +local actor finishes loading, the server acknowledges CharacterReady and +replays one native death notification to the player and nearby observers. The +client then sends its normal action 152 and completes the same scanner return. +Repeated ready messages do not repeat the notification, and reconnecting after +the return preserves resources and location while ordinary regeneration resumes. +The supported scanner also protects saved XP, with classic uninsured loss +committed alongside death; see [XP protection](insurance.md#classic-xp-protection). Other scanners, insurance fees and resurrection shock stat penalties remain unimplemented. Ordinary inventory items now enter [private reclaim](reclaim.md) from level 10; equipment and NoDrop items remain @@ -82,6 +86,40 @@ arrival before reconnect, retained inventory after restart, movement after the new world handshake, concurrent repeated requests, both observers, stale and invalid sessions/identities, combat cleanup and save failure. +`server/resurrection_reconnect_test.go` covers pending deaths across reconnect +and database reopen, saved and default return locations, disconnect before +ready, premature/repeated resurrection, repeated ready, observer presentation, +living arrival reconnect, retained XP/buffs/reclaim and a single durable +resource restoration. Failed and ignored UPDATEs and a failed COMMIT leave +the player dead and retryable without publishing success. The five reconnect +regressions fail against the previous world-entry shortcut; see +`local/resurrection-reconnect-research/red-tests.log`. + +Two GPU WASM clients verified the interrupted transition with real Leet +killing hits. Closing the player tab immediately after action 99 left zero +health and the committed XP/reclaim state in SQLite. A fresh tab automatically +sent action 152 and returned to the saved scanner with 1 HP/1 nano. A second +interrupted death survived a clean server restart and automatically returned +to the unsaved starter default with the same resources. Both retained progress +1000, insured cumulative XP 140850, IP 1500 and the same reclaim item. A later +living reload retained 7 HP/13 nano; walking afterward reached SQLite position +`[955.625, 16.375, 505.625]` without another death or return. + +Independent stream decoding confirms one death replay and one native return +per interrupted transition, no XP-loss replay and no private XP updates to the +observer. There were no browser errors. Evidence and the verifier are under +`local/resurrection-reconnect-smoke/`; `verification.json` records the results. +Cleanup removed only the two owned test players, their one item and encounter +contribution, preserving unrelated rows and passing SQLite integrity checks. +Both test browsers were closed; the single shared preview remains on its +normal HTTPS/game ports. + +The final `go test ./server/... -count=1` passed (server 459.858s, gamedata +9.298s). Repeated race checks passed for resurrection/reconnect (110.794s) +and the wider death/insurance/session-replacement selection (120.675s). +`go vet -stdversion=false ./server/...` passed. Logs are under +`local/resurrection-reconnect-research/`. + Two published WASM clients exercised a full-health 37 HP player's defeat by the profiled 100 HP Leet. The native client sent action 152 automatically and returned with 1 HP/1 nano at `[960, 16.5, 510]`, without a page reload. The diff --git a/server/combat_test.go b/server/combat_test.go @@ -423,16 +423,16 @@ func TestCombatDeadPlayerCannotCrossZoneTriggers(t *testing.T) { } } -func TestCombatReconnectRevivesDefeatedPlayer(t *testing.T) { +func TestCombatReconnectKeepsDefeatedPlayerDead(t *testing.T) { useNPCData(t, testNPCData()) world := newTestServer(t) world.newCharacter().health = 0 - connectZoningClient(t, world, true) + reconnectResurrectionPlayer(t, world, n3Packet(0x570c2039, 50000, 1, nil)) world.mutex.Lock() defer world.mutex.Unlock() - if world.characters[1].health != 34 || world.characters[1].fighting != 0 { - t.Fatal("reconnecting did not revive the defeated player") + if world.characters[1].health != 0 || world.characters[1].fighting != 0 { + t.Fatal("reconnecting bypassed the pending resurrection") } } diff --git a/server/main.go b/server/main.go @@ -352,10 +352,6 @@ func (server *server) serveWorldClient(client *worldClient) error { character.sitting = false client.regenerationAt = time.Now() client.regenerationSeconds = 0 - // Health is session state; reconnecting revives a defeated player. - if character.health == 0 { - character.health = character.maximumHealth() - } client.id = character.id client.npcs = make(map[uint32]bool) client.lootCorpse = 0 diff --git a/server/resurrection_reconnect_test.go b/server/resurrection_reconnect_test.go @@ -0,0 +1,212 @@ +package main + +import ( + "bytes" + "encoding/binary" + "fmt" + "net" + "reflect" + "testing" +) + +func pendingResurrectionWorld(t *testing.T, saved bool) *server { + t.Helper() + world, _ := newBuffedDeathWorld(t) + t.Cleanup(func() { world.db.Close() }) + player := world.characters[1] + player.level, player.experience, player.insuredExperience = 20, 1500, 140850 + player.playfield, player.position = 950, [3]float32{178, 15, 57} + if saved { + player.resurrectionTerminal = 0xc00202c1 + } + if err := world.saveUser_locked(player); err != nil { + t.Fatal(err) + } + if err := world.savePosition_locked(player); err != nil { + t.Fatal(err) + } + defeatReclaimPlayer(t, world) + delete(world.clients, 1) + return world +} + +func reconnectResurrectionPlayer(t *testing.T, world *server, messages ...[]byte) [][]byte { + t.Helper() + host, peer := net.Pipe() + defer host.Close() + defer peer.Close() + cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}) + binary.BigEndian.PutUint16(cookie, 0xdfdf) + for _, message := range messages { + cookie = append(cookie, targetPacket(append([]byte(nil), message...))...) + } + conn := &combatInterestConn{combatTestConn: combatTestConn{Conn: host}, input: bytes.NewReader(cookie)} + if err := world.serveWorldClient(&worldClient{conn: conn, loading: true}); err != nil { + t.Fatal(err) + } + return conn.packets +} + +func TestResurrectionReconnectResumesPendingDeath(t *testing.T) { + for _, restart := range []bool{false, true} { + for _, saved := range []bool{false, true} { + t.Run(fmt.Sprintf("restart=%t/saved=%t", restart, saved), func(t *testing.T) { + world := pendingResurrectionWorld(t, saved) + if restart { + if err := world.db.Close(); err != nil { + t.Fatal(err) + } + world = newTestInventoryServer(t) + } + player := world.characters[1] + belongings := player.inventory + durableBelongings := newTestInventoryServer(t).characters[1].inventory + observer := &combatTestConn{} + world.characters[2] = &character{id: 2, playfield: 950, health: 100} + world.clients[2] = &worldClient{id: 2, conn: observer, target: true, ready: true} + if _, err := world.db.Exec("CREATE TABLE resurrection_writes (health INTEGER); CREATE TRIGGER record_resurrection AFTER UPDATE OF health ON characters WHEN NEW.id=1 AND OLD.health=0 AND NEW.health>0 BEGIN INSERT INTO resurrection_writes VALUES (NEW.health); END"); err != nil { + t.Fatal(err) + } + ready := n3Packet(0x570c2039, 50000, 1, nil) + movement := *player + movement.position[0] = 999 + + packets := reconnectResurrectionPlayer(t, world, resurrectionRequest(1), ready, ready, npcMovementPacket(&movement, 2)) + restored := newTestInventoryServer(t).characters[1] + + if player.health != 0 || restored.health != 0 || player.playfield != 950 || player.position != [3]float32{178, 15, 57} || + player.experience != 1000 || restored.experience != 1000 || player.insuredExperience != 140850 || + len(player.activeNanos) != 0 || !reflect.DeepEqual(player.inventory, belongings) { + t.Fatal("reconnect bypassed death or changed its committed outcome") + } + death, seenReady, seenFull := 0, false, false + for _, packet := range packets { + switch binary.BigEndian.Uint32(packet[16:20]) { + case 0x5f52412e: + seenFull = true + case 0x570c2039: + seenReady = true + case 0x5e477770: + if binary.BigEndian.Uint32(packet[29:33]) == 99 { + if !seenFull || !seenReady { + t.Fatal("death replay preceded the loaded local actor") + } + death++ + } + case 0x445f2a0b, 0x43197d22, 0x2b333d6e: + t.Fatalf("pending reconnect replayed resurrection or a stat outcome: %x", packet) + } + } + if death != 1 { + t.Fatalf("repeated ready produced %d death notifications", death) + } + observerDeaths := 0 + for _, packet := range observer.packets { + if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 99 { + observerDeaths++ + } + } + if observerDeaths != 1 { + t.Fatalf("observer saw %d pending-death replays", observerDeaths) + } + + packets = reconnectResurrectionPlayer(t, world, ready, resurrectionRequest(1), resurrectionRequest(1)) + restored = newTestInventoryServer(t).characters[1] + var writes int + if err := world.db.QueryRow("SELECT COUNT(*) FROM resurrection_writes").Scan(&writes); err != nil { + t.Fatal(err) + } + + position := [3]float32{960, 16.5, 510} + if saved { + position = [3]float32{918, 20.7, 578} + } + if writes != 1 || player.health != 1 || restored.health != 1 || restored.nano != 1 || restored.playfield != 705 || restored.position != position || + restored.experience != 1000 || restored.insuredExperience != 140850 || restored.stats.ImprovementPoints != 1500 || + len(restored.activeNanos) != 0 || !reflect.DeepEqual(restored.inventory, durableBelongings) { + t.Fatal("resurrection lost durable state or restored resources more than once") + } + var transition []uint32 + for _, packet := range packets { + key := binary.BigEndian.Uint32(packet[16:20]) + if key == 0x445f2a0b || key == 0x43197d22 || key == 0x3c { + transition = append(transition, key) + } + } + if !reflect.DeepEqual(transition, []uint32{0x445f2a0b, 0x43197d22, 0x3c}) { + t.Fatalf("resurrection transition missing or repeated: %x", transition) + } + + packets = reconnectResurrectionPlayer(t, world, ready, resurrectionRequest(1)) + + if player.health != 1 || player.position != position { + t.Fatal("arrival reconnect restored full health or changed the return point") + } + for _, packet := range packets { + key := binary.BigEndian.Uint32(packet[16:20]) + if key == 0x445f2a0b || key == 0x43197d22 || key == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 99 { + t.Fatal("living reconnect replayed death or resurrection") + } + } + }) + } + } +} + +func TestResurrectionReconnectBeforeReadyKeepsPendingDeath(t *testing.T) { + world := pendingResurrectionWorld(t, true) + + packets := reconnectResurrectionPlayer(t, world, resurrectionRequest(1)) + restored := newTestInventoryServer(t).characters[1] + + if world.characters[1].health != 0 || restored.health != 0 || restored.playfield != 950 || restored.experience != 1000 { + t.Fatal("unfinished login bypassed death") + } + for _, packet := range packets { + key := binary.BigEndian.Uint32(packet[16:20]) + if key == 0x445f2a0b || key == 0x43197d22 || key == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 99 { + t.Fatal("unfinished login resumed presentation before ready") + } + } +} + +func TestResurrectionPendingDeathPersistenceFailureCanRetry(t *testing.T) { + for _, failure := range []struct{ name, statement string }{ + {"update", "CREATE TRIGGER reject_return BEFORE UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN SELECT RAISE(ABORT, 'return'); END"}, + {"ignored update", "CREATE TRIGGER reject_return BEFORE UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN SELECT RAISE(IGNORE); END"}, + {"commit", "CREATE TABLE pending_return (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_return AFTER UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN INSERT INTO pending_return VALUES (999999); END"}, + } { + t.Run(failure.name, func(t *testing.T) { + pendingResurrectionWorld(t, true) + world := newTestInventoryServer(t) + player := world.characters[1] + belongings := player.inventory + conn := &combatTestConn{} + client := &worldClient{id: 1, ready: true, target: true, conn: conn} + world.clients[1] = client + if _, err := world.db.Exec(failure.statement); err != nil { + t.Fatal(err) + } + + err := world.handleResurrection(client, resurrectionRequest(1)[20:]) + restored := newTestInventoryServer(t).characters[1] + + if err == nil || player.health != 0 || restored.health != 0 || restored.playfield != 950 || restored.position != [3]float32{178, 15, 57} || + restored.experience != 1000 || restored.insuredExperience != 140850 || len(restored.activeNanos) != 0 || + !reflect.DeepEqual(restored.inventory, belongings) || world.clients[1] != client || !client.ready || len(conn.packets) != 0 { + t.Fatal("failed return changed the pending death or published success", err) + } + if _, err := world.db.Exec("DROP TRIGGER reject_return"); err != nil { + t.Fatal(err) + } + + err = world.handleResurrection(client, resurrectionRequest(1)[20:]) + restored = newTestInventoryServer(t).characters[1] + + if err != nil || restored.health != 1 || restored.nano != 1 || restored.playfield != 705 || restored.position != [3]float32{918, 20.7, 578} || + restored.experience != 1000 || !reflect.DeepEqual(restored.inventory, belongings) || len(conn.packets) != 3 { + t.Fatal("retry did not complete exactly one durable scanner return", err) + } + }) + } +} diff --git a/server/world_actions.go b/server/world_actions.go @@ -38,12 +38,17 @@ func (server *server) handleCharacterReady(client *worldClient) error { // Repeated ready messages acknowledge the actor without applying effects twice. var equipment [][]byte var team [][]byte + var death []byte resources := make(map[*worldClient][][]byte) if client.loading { equipment = equippedItemPackets(server.characters[client.id]) equipment = append(equipment, activeNanoPackets(server.characters[client.id], time.Now())...) team = server.teamPackets_locked(client.id) server.queueTeamResources_locked(resources, server.characters[client.id]) + // Resume the native return request after a disconnect during death. + if server.characters[client.id].health == 0 { + death = deathPacket(server.characters[client.id]) + } } client.loading = false playfield := server.characters[client.id].playfield @@ -60,7 +65,14 @@ func (server *server) handleCharacterReady(client *worldClient) error { return err } } - return client.send(response) + if err := client.send(response); err != nil { + return err + } + if death != nil { + server.broadcast(playfield, client.id, death) + return client.send(death) + } + return nil } func (server *server) handleMovement(client *worldClient, body []byte) error {