server/loot_rights_test.go (6684B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "sync" 7 "testing" 8 "time" 9 ) 10 11 func newLootRightsWorld(t *testing.T) *server { 12 t.Helper() 13 world, _ := newCombatWorld(t) 14 for range 3 { 15 player := world.newCharacter() 16 player.position = [3]float32{} 17 if err := world.saveUser(player); err != nil { 18 t.Fatal(err) 19 } 20 world.clients[player.id] = &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 21 } 22 // High melee AC makes ordinary fists deal exactly three damage. 23 npc := world.characters[1000000] 24 npc.monsterData = 17655 25 npc.inventory.Items = []ownedItem{{ID: 99, Type: 51022, Template: 85685, Quality: 200, Slot: 21, Count: 1}} 26 npc.combatProfile = &gamedata.NPCCombatProfile{Health: 100, RespawnSeconds: 100, CorpseSeconds: 180} 27 world.nextNPC = 1000001 28 return world 29 } 30 31 func TestLootRightsFollowAccumulatedDamageAcrossStoppedAttacks(t *testing.T) { 32 for _, scenario := range []struct { 33 name string 34 health uint32 35 swings []uint32 36 owner uint32 37 }{ 38 {"earlier contributor", 13, []uint32{2, 2, 2, 1, 1}, 2}, 39 {"leader changes", 13, []uint32{2, 1, 1, 1, 2}, 1}, 40 {"tie retains first leader", 6, []uint32{2, 1}, 2}, 41 {"three contributors without majority", 19, []uint32{3, 3, 2, 1, 2, 1, 3}, 3}, 42 {"overkill counts remaining health", 7, []uint32{2, 2, 1}, 2}, 43 } { 44 t.Run(scenario.name, func(t *testing.T) { 45 t.Chdir(t.TempDir()) 46 world := newLootRightsWorld(t) 47 npc := world.characters[1000000] 48 npc.health = scenario.health 49 if scenario.name == "overkill counts remaining health" { 50 world.characters[1].stats.Body.MartialArts = 60000 51 } 52 53 for _, id := range scenario.swings { 54 if err := world.handleCombat(world.clients[id], attackPacket(id, npc.id)[20:]); err != nil { 55 t.Fatal(err) 56 } 57 world.advanceCombat(world.characters[id].nextAttack) 58 if err := world.handleCombat(world.clients[id], stopFightPacket(id)[20:]); err != nil { 59 t.Fatal(err) 60 } 61 } 62 63 if npc.health != 0 || npc.corpse == nil || npc.corpse.looter != scenario.owner { 64 t.Fatalf("damage contribution did not determine loot rights: %+v", npc.corpse) 65 } 66 packet := corpsePacket(npc) 67 length := int(binary.BigEndian.Uint16(packet[6:8])) 68 if binary.BigEndian.Uint32(packet[length-8:]) != scenario.owner { 69 t.Fatal("native corpse announcement identified a different looter") 70 } 71 }) 72 } 73 } 74 75 func TestLootRightsRejectFinalHitterAndSurviveCloseUntilExpiry(t *testing.T) { 76 t.Chdir(t.TempDir()) 77 world := newLootRightsWorld(t) 78 npc := world.characters[1000000] 79 npc.health = 7 80 for _, id := range []uint32{2, 2, 1} { 81 world.handleCombat(world.clients[id], attackPacket(id, npc.id)[20:]) 82 world.advanceCombat(world.characters[id].nextAttack) 83 world.handleCombat(world.clients[id], stopFightPacket(id)[20:]) 84 } 85 npc.corpse.items = []ownedItem{{ID: 17, Type: 51005, Template: 42640, Quality: 1, Count: 1}} 86 npc.corpse.credits = 123 87 ownerUse := lootUseRequest(1) 88 binary.BigEndian.PutUint32(ownerUse[8:12], 2) 89 binary.BigEndian.PutUint32(ownerUse[36:40], 2) 90 91 world.handleLootUse(world.clients[1], lootUseRequest(1)) 92 world.clients[1].lootCorpse = npc.id // A stale window cannot authorize pickup. 93 world.handleInventory(world.clients[1], lootTakeRequest()) 94 world.handleLootUse(world.clients[2], ownerUse) 95 if world.clients[2].lootCorpse != npc.id || world.characters[2].inventory.Credits != 123 { 96 t.Fatal("entitled contributor could not open the corpse or collect credits") 97 } 98 binary.BigEndian.PutUint32(ownerUse[20:24], 2) 99 world.handleLootUse(world.clients[2], ownerUse) 100 world.handleLootUse(world.clients[1], lootUseRequest(2)) 101 if world.characters[1].inventory.Credits != 0 || len(world.characters[1].inventory.Items) != 0 || len(npc.corpse.items) != 1 { 102 t.Fatal("closing released loot to the final hitter") 103 } 104 binary.BigEndian.PutUint32(ownerUse[20:24], 3) 105 world.handleLootUse(world.clients[2], ownerUse) 106 ownerTake := lootTakeRequest() 107 binary.BigEndian.PutUint32(ownerTake[8:12], 2) 108 binary.BigEndian.PutUint32(ownerTake[28:32], 2) 109 if err := world.handleInventory(world.clients[2], ownerTake); err != nil { 110 t.Fatal(err) 111 } 112 world.advanceCorpseExpiry(npc.corpse.expiresAt) 113 binary.BigEndian.PutUint32(ownerUse[20:24], 4) 114 world.handleLootUse(world.clients[2], ownerUse) 115 116 if world.clients[1].lootCorpse != 0 || world.clients[2].lootCorpse != 0 || world.characters[npc.id] != nil || 117 len(world.characters[1].inventory.Items) != 0 || world.characters[1].inventory.Credits != 0 || npc.corpse != nil || 118 len(world.characters[2].inventory.Items) != 1 || world.characters[2].inventory.Items[0].ID != 17 { 119 t.Fatal("final hit, stale handle, closing or expiry transferred loot rights") 120 } 121 } 122 123 func TestLootRightsConcurrentSwingsKeepOneDeathAndOwner(t *testing.T) { 124 t.Chdir(t.TempDir()) 125 world := newLootRightsWorld(t) 126 npc := world.characters[1000000] 127 npc.health = 7 128 world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]) 129 world.handleCombat(world.clients[2], attackPacket(2, npc.id)[20:]) 130 first := world.characters[2].nextAttack 131 zone, stop := startTestZone(t, world) 132 var workers sync.WaitGroup 133 134 for range 8 { 135 workers.Go(func() { 136 if err := zone.call(zoneTestCommand(func(state *server) error { state.advanceCombat(first); return nil })); err != nil { 137 t.Error(err) 138 } 139 }) 140 } 141 workers.Wait() 142 if err := stop(); err != nil { 143 t.Fatal(err) 144 } 145 world.advanceCombat(world.characters[1].nextAttack) 146 147 if npc.health != 0 || npc.corpse == nil || npc.corpse.looter != 1 || len(world.npcRespawns) != 1 || len(world.corpseExpiry) != 1 { 148 t.Fatal("concurrent swings duplicated death or changed contribution ownership") 149 } 150 } 151 152 func TestLootRightsDoNotTransferWhenContributorLeavesAndRespawnStartsFresh(t *testing.T) { 153 t.Chdir(t.TempDir()) 154 world := newLootRightsWorld(t) 155 npc := world.characters[1000000] 156 npc.health = 7 157 world.handleCombat(world.clients[2], attackPacket(2, npc.id)[20:]) 158 world.advanceCombat(world.characters[2].nextAttack) 159 world.advanceCombat(world.characters[2].nextAttack) 160 world.clients[2].ready = false 161 world.characters[2].playfield = 951 162 163 world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]) 164 killed := world.characters[1].nextAttack 165 world.advanceCombat(killed) 166 world.advanceNPCRespawns(killed.Add(100 * time.Second)) 167 replacement := world.characters[1000001] 168 replacement.health = 1 169 world.handleCombat(world.clients[1], attackPacket(1, replacement.id)[20:]) 170 world.advanceCombat(world.characters[1].nextAttack) 171 172 if npc.corpse.looter != 2 || replacement.corpse == nil || replacement.corpse.looter != 1 { 173 t.Fatal("departure reassigned loot or respawn inherited an old contribution") 174 } 175 }