commit 7f77b6062439193081fbf79e687e46e4de1b4255
parent 9957da6f651052e3500e219157c97bbefec2a812
Author: anon <anon@example.com>
Date: Sat, 7 Nov 2026 05:34:18 +0000
Publish zone presence only for changed identities
Diffstat:
9 files changed, 151 insertions(+), 25 deletions(-)
diff --git a/server/browser_account_owner_test.go b/server/browser_account_owner_test.go
@@ -131,6 +131,9 @@ func TestBrowserAccountRoutedChangesCommitAndPublishWithoutGlobalLocks(t *testin
if query.name != "Alice" || query.savedName != "Alice" || query.credential != credential || query.savedCredential != credential || query.items != 7 {
t.Fatal("committed account was not published by its owner", query)
}
+ if person := fixture.world.person(target); person.name != "Alice" || person.side != 2 {
+ t.Fatal("account publication lost roster identity", person)
+ }
id, err := fixture.browserPlayerID(credential)
if err != nil || id != target {
t.Fatal("cookie did not identify committed player", id, err)
diff --git a/server/chat_runtime.go b/server/chat_runtime.go
@@ -191,6 +191,7 @@ func (update chatWorldPresence) execute(state *chatState) error {
}
func (server *server) publishChatPerson_locked(player *character) {
+ server.markPresence_locked(player.id)
if server.chatService != nil {
server.chatService.post(chatWorldPresence{player: player.id, name: player.name,
credential: player.browserTokenHash, online: server.clients[player.id] != nil})
diff --git a/server/combat.go b/server/combat.go
@@ -365,6 +365,7 @@ func (server *server) startAttack_locked(attacker, target *character, now time.T
attacker.movement.action(attacker, 33)
}
}
+ server.combatPresence_locked(attacker, target.id)
attacker.fighting = target.id
attacker.nextAttack = now
if attacker.nextAttack.Before(attacker.rechargeUntil) {
@@ -384,6 +385,7 @@ func (server *server) stopAttack_locked(attacker *character, updates map[*worldC
delete(server.fighting, attacker.id)
return
}
+ server.combatPresence_locked(attacker, 0)
attacker.fighting = 0
attacker.nextAttack = time.Time{}
delete(server.fighting, attacker.id)
diff --git a/server/control.go b/server/control.go
@@ -356,6 +356,7 @@ func (server *server) replaceUser_locked(character, updated *character, updates
updated.fighting = 0
updated.nextAttack = time.Time{}
*character = *updated
+ server.markPresence_locked(character.id)
server.cancelPlayerTrade_locked(server.clients[character.id])
// Team side is derived from the durable roster and each member's affiliation.
if character.team != nil && previousSide != character.appearance>>9&3 {
diff --git a/server/team_messages_test.go b/server/team_messages_test.go
@@ -185,14 +185,19 @@ func TestWorldRuntimeTeamReplayAndAffiliationUseRemotePresence(t *testing.T) {
type zoneTeamCombat struct{ target uint32 }
func (command zoneTeamCombat) execute(state *server) error {
- state.characters[2].fighting = command.target
+ if command.target == 0 {
+ state.stopAttack_locked(state.characters[2], make(map[*worldClient][][]byte))
+ } else {
+ state.startAttack_locked(state.characters[2], state.characters[command.target], time.Now(), make(map[*worldClient][][]byte))
+ state.characters[2].nextAttack = time.Now().Add(time.Hour)
+ }
return nil
}
func TestWorldRuntimeTeamInvitationChecksRemoteCombat(t *testing.T) {
fixture, _ := newTeamWorld(t)
formTestTeam(t, fixture, 1, 2)
- fixture.characters[2].playfield = 800
+ fixture.characters[2].playfield, fixture.characters[3].playfield = 800, 800
world := newRoutedTestWorld(t, fixture)
defer world.stop()
first, _ := world.owner(1)
diff --git a/server/world_presence.go b/server/world_presence.go
@@ -28,29 +28,28 @@ func (world *worldRuntime) person(id uint32) playerPresence {
func (zone *zoneRuntime) publishPresence(state *server) {
if state.world == nil {
+ zone.presenceDirty = zone.presenceDirty[:0]
return
}
- var incoming map[uint32]bool
- if len(state.fighting) != 0 {
- incoming = make(map[uint32]bool, len(state.fighting))
- for _, attacker := range state.fighting {
- incoming[attacker.fighting] = true
- }
+ if len(zone.presenceDirty) == 0 {
+ return
}
+
var changed []playerPresence
- for id, player := range zone.players {
+ for _, id := range zone.presenceDirty {
+ player := zone.players[id]
+ if player == nil {
+ delete(zone.presence, id)
+ continue
+ }
person := playerPresence{id: id, name: player.name, side: player.appearance >> 9 & 3,
- combat: player.fighting != 0 || incoming[id]}
+ combat: player.fighting != 0 || zone.incomingAttacks[id] != 0}
if zone.presence[id] != person {
changed = append(changed, person)
zone.presence[id] = person
}
}
- for id := range zone.presence {
- if zone.players[id] == nil {
- delete(zone.presence, id)
- }
- }
+ zone.presenceDirty = zone.presenceDirty[:0]
if len(changed) != 0 {
update := presenceUpdate{zone: zone, players: changed, done: make(chan struct{})}
state.world.presence <- update
@@ -58,6 +57,34 @@ func (zone *zoneRuntime) publishPresence(state *server) {
}
}
+func (server *server) markPresence_locked(id uint32) {
+ if server.zone != nil && id != 0 && id < 1000000 {
+ server.zone.presenceDirty = append(server.zone.presenceDirty, id)
+ }
+}
+
+// Maintain incoming combat counts at the same boundary as the fighting index.
+// Roster publication never scans unrelated players or attackers.
+func (server *server) combatPresence_locked(attacker *character, target uint32) {
+ if server.zone == nil {
+ return
+ }
+ server.markPresence_locked(attacker.id)
+ if previous := server.fighting[attacker.id]; previous != nil && previous.fighting != 0 && previous.fighting < 1000000 {
+ id := previous.fighting
+ if server.zone.incomingAttacks[id] > 1 {
+ server.zone.incomingAttacks[id]--
+ } else {
+ delete(server.zone.incomingAttacks, id)
+ }
+ server.markPresence_locked(id)
+ }
+ if target != 0 && target < 1000000 {
+ server.zone.incomingAttacks[target]++
+ server.markPresence_locked(target)
+ }
+}
+
func (server *server) playerPresence_locked(id uint32) playerPresence {
if player := server.characters[id]; player != nil {
return playerPresence{id: id, name: player.name, side: player.appearance >> 9 & 3,
diff --git a/server/world_presence_test.go b/server/world_presence_test.go
@@ -0,0 +1,76 @@
+package main
+
+import (
+ "fmt"
+ "testing"
+ "time"
+)
+
+// Movement commands do not change roster metadata. Their publication cost
+// must not depend on how many other players share the zone.
+func BenchmarkZoneUnchangedPresence(b *testing.B) {
+ for _, count := range []int{100, 1000, 20000} {
+ b.Run(fmt.Sprint(count), func(b *testing.B) {
+ zone := &zoneRuntime{players: make(map[uint32]*character), presence: make(map[uint32]playerPresence)}
+ for id := uint32(1); id <= uint32(count); id++ {
+ zone.players[id] = &character{id: id, name: "Player"}
+ zone.presence[id] = playerPresence{id: id, name: "Player"}
+ }
+ state := &server{world: &worldRuntime{}}
+ b.ReportAllocs()
+ b.ResetTimer()
+ for index := 0; index < b.N; index++ {
+ zone.publishPresence(state)
+ }
+ })
+ }
+}
+
+type zonePresenceAttack struct{ attacker, target uint32 }
+
+func (command zonePresenceAttack) execute(state *server) error {
+ attacker := state.characters[command.attacker]
+ updates := make(map[*worldClient][][]byte)
+ if command.target == 0 {
+ state.stopAttack_locked(attacker, updates)
+ } else {
+ state.startAttack_locked(attacker, state.characters[command.target], time.Now(), updates)
+ attacker.nextAttack = time.Now().Add(time.Hour)
+ }
+ return nil
+}
+
+func TestWorldPresenceTracksIncomingAttackersAndRetargets(t *testing.T) {
+ fixture, _ := newTeamWorld(t)
+ for _, id := range []uint32{2, 3, 5, 6} {
+ fixture.characters[id].playfield = 800
+ fixture.characters[id].position = [3]float32{52, 14, 56}
+ }
+ world := newRoutedTestWorld(t, fixture)
+ defer world.stop()
+ owner, err := world.owner(2)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Stopping one of two attackers must preserve the target's combat status;
+ // retargeting the remaining attacker must update both affected players.
+ for _, step := range []struct {
+ attacker, target uint32
+ second, fifth bool
+ }{
+ {3, 2, true, false},
+ {6, 2, true, false},
+ {3, 0, true, false},
+ {6, 5, false, true},
+ {6, 0, false, false},
+ } {
+ if err := owner.call(zonePresenceAttack{step.attacker, step.target}); err != nil {
+ t.Fatal(err)
+ }
+ second, fifth := world.person(2), world.person(5)
+ if second.combat != step.second || fifth.combat != step.fifth {
+ t.Fatal("cross-zone combat presence did not follow the fighting index", step, second, fifth)
+ }
+ }
+}
diff --git a/server/zone_runtime.go b/server/zone_runtime.go
@@ -10,16 +10,18 @@ import (
// A zone receives commands and advances its own state on one goroutine. The
// caller transfers the state at construction and must subsequently use commands.
type zoneRuntime struct {
- commands chan zoneRequest
- quiescing chan struct{}
- quiesced chan struct{}
- stopping chan struct{}
- done chan struct{}
- players map[uint32]*character
- presence map[uint32]playerPresence
- checkpoint *simulationCheckpoint
- checkpointDone <-chan error
- stopError error
+ commands chan zoneRequest
+ quiescing chan struct{}
+ quiesced chan struct{}
+ stopping chan struct{}
+ done chan struct{}
+ players map[uint32]*character
+ presence map[uint32]playerPresence
+ presenceDirty []uint32
+ incomingAttacks map[uint32]uint32
+ checkpoint *simulationCheckpoint
+ checkpointDone <-chan error
+ stopError error
}
type zoneCommand interface {
@@ -44,9 +46,16 @@ func newZoneRuntime(state *server) *zoneRuntime {
zone := &zoneRuntime{commands: make(chan zoneRequest), quiescing: make(chan struct{}), quiesced: make(chan struct{}), stopping: make(chan struct{}), done: make(chan struct{})}
zone.players = make(map[uint32]*character)
zone.presence = make(map[uint32]playerPresence)
+ zone.incomingAttacks = make(map[uint32]uint32)
for id, player := range state.characters {
if !player.npc {
zone.players[id] = player
+ zone.presenceDirty = append(zone.presenceDirty, id)
+ }
+ }
+ for _, attacker := range state.fighting {
+ if attacker.fighting != 0 && attacker.fighting < 1000000 {
+ zone.incomingAttacks[attacker.fighting]++
}
}
state.zone = zone
diff --git a/server/zone_transfer.go b/server/zone_transfer.go
@@ -38,6 +38,7 @@ func (arrival playerTransfer) execute(state *server) error {
}
state.characters[arrival.player.id] = arrival.player
state.zone.players[arrival.player.id] = arrival.player
+ state.markPresence_locked(arrival.player.id)
for id, item := range arrival.items {
state.items[id] = item
state.itemPlacements[id] = arrival.placements[id]
@@ -70,6 +71,7 @@ func (server *server) transferPlayer_locked(player *character) error {
}
delete(server.characters, player.id)
delete(server.zone.players, player.id)
+ server.markPresence_locked(player.id)
if err := server.world.transfer(server.zone, arrival); err != nil {
// A rejected routing change never transferred ownership of the payload.
server.characters[player.id] = player