server/world_presence_test.go (2299B)
1 package main 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 ) 8 9 // Movement commands do not change roster metadata. Their publication cost 10 // must not depend on how many other players share the zone. 11 func BenchmarkZoneUnchangedPresence(b *testing.B) { 12 for _, count := range []int{100, 1000, 20000} { 13 b.Run(fmt.Sprint(count), func(b *testing.B) { 14 zone := &zoneRuntime{players: make(map[uint32]*character), presence: make(map[uint32]playerPresence)} 15 for id := uint32(1); id <= uint32(count); id++ { 16 zone.players[id] = &character{id: id, name: "Player"} 17 zone.presence[id] = playerPresence{id: id, name: "Player"} 18 } 19 state := &server{world: &worldRuntime{}} 20 b.ReportAllocs() 21 b.ResetTimer() 22 for index := 0; index < b.N; index++ { 23 zone.publishPresence(state) 24 } 25 }) 26 } 27 } 28 29 type zonePresenceAttack struct{ attacker, target uint32 } 30 31 func (command zonePresenceAttack) execute(state *server) error { 32 attacker := state.characters[command.attacker] 33 updates := make(map[*worldClient][][]byte) 34 if command.target == 0 { 35 state.stopAttack(attacker, updates) 36 } else { 37 state.startAttack(attacker, state.characters[command.target], time.Now(), updates) 38 attacker.nextAttack = time.Now().Add(time.Hour) 39 } 40 return nil 41 } 42 43 func TestWorldPresenceTracksIncomingAttackersAndRetargets(t *testing.T) { 44 fixture, _ := newTeamWorld(t) 45 for _, id := range []uint32{2, 3, 5, 6} { 46 fixture.characters[id].playfield = 800 47 fixture.characters[id].position = [3]float32{52, 14, 56} 48 } 49 world := newRoutedTestWorld(t, fixture) 50 defer world.stop() 51 owner, err := world.owner(2) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 // Stopping one of two attackers must preserve the target's combat status; 57 // retargeting the remaining attacker must update both affected players. 58 for _, step := range []struct { 59 attacker, target uint32 60 second, fifth bool 61 }{ 62 {3, 2, true, false}, 63 {6, 2, true, false}, 64 {3, 0, true, false}, 65 {6, 5, false, true}, 66 {6, 0, false, false}, 67 } { 68 if err := owner.call(zonePresenceAttack{step.attacker, step.target}); err != nil { 69 t.Fatal(err) 70 } 71 second, fifth := world.person(2), world.person(5) 72 if second.combat != step.second || fifth.combat != step.fifth { 73 t.Fatal("cross-zone combat presence did not follow the fighting index", step, second, fifth) 74 } 75 } 76 }