aorecomp

Anarchy Online classic in the browser

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


server/team_loot_test.go (18001B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"sync"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func teamLootUse(owner, sequence uint32) []byte {
     12 	request := lootUseRequest(sequence)
     13 	binary.BigEndian.PutUint32(request[8:12], owner)
     14 	binary.BigEndian.PutUint32(request[36:40], owner)
     15 	return request
     16 }
     17 
     18 func TestTeamLootNanoAndFlingUseTheSameDurableTeamAward(t *testing.T) {
     19 	for _, attack := range []string{"nano", "Fling"} {
     20 		t.Run(attack, func(t *testing.T) {
     21 			t.Chdir(t.TempDir())
     22 			world := newLootRightsWorld(t)
     23 			formTestTeam(t, world, 1, 2)
     24 			player, npc := world.characters[1], world.characters[1000000]
     25 			player.profession, player.nano = 8, 30
     26 			player.stats = testCharacterStats()
     27 			player.stats.NanoAiding.PsychologicalModification, player.stats.NanoAiding.MatterCreation = 7, 6
     28 			player.inventory.LearnedNanos = []uint32{29618}
     29 			if attack == "Fling" {
     30 				player.inventory.Items = []ownedItem{{ID: 1, Type: 51018, Template: 123742, Quality: 1, Slot: 6, Count: 1, Ammo: 3}}
     31 			}
     32 			npc.health, npc.level = 1, 1
     33 			if err := world.flushWorld(); err != nil {
     34 				t.Fatal(err)
     35 			}
     36 
     37 			if attack == "nano" {
     38 				if err := world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 29618})[20:]); err != nil {
     39 					t.Fatal(err)
     40 				}
     41 				world.advanceNanos(world.clients[1].nanoCast.readyAt.Add(time.Nanosecond))
     42 			} else if err := world.handleWeaponSpecial(world.clients[1], flingShotRequest(1, npc.id)[20:]); err != nil {
     43 				t.Fatal(err)
     44 			}
     45 			restored := newTestInventoryServer(t)
     46 			world.handleLootUse(world.clients[2], teamLootUse(2, 1))
     47 
     48 			if npc.health != 0 || npc.corpse == nil || npc.corpse.team != 1 || world.clients[2].lootCorpse != npc.id ||
     49 				restored.characters[1000000].corpse == nil || restored.characters[1000000].corpse.team != 1 ||
     50 				len(world.npcRespawns) != 1 || len(world.corpseExpiry) != 1 {
     51 				t.Fatal("alternate attack lost durable team ownership or shared native Use")
     52 			}
     53 		})
     54 	}
     55 }
     56 
     57 func TestTeamLootHitTimeCreditDoesNotMoveWhenMemberLeaves(t *testing.T) {
     58 	t.Chdir(t.TempDir())
     59 	world := newLootRightsWorld(t)
     60 	fourth := world.newCharacter()
     61 	fourth.position = [3]float32{}
     62 	if err := world.saveUser(fourth); err != nil {
     63 		t.Fatal(err)
     64 	}
     65 	world.clients[4] = &worldClient{id: 4, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     66 	formTestTeam(t, world, 1, 2)
     67 	formTestTeam(t, world, 1, 4)
     68 	npc := world.characters[1000000]
     69 	npc.health = 13
     70 	for _, id := range []uint32{1, 2} {
     71 		world.handleCombat(world.clients[id], attackPacket(id, npc.id)[20:])
     72 		world.advanceCombat(world.characters[id].nextAttack)
     73 		world.handleCombat(world.clients[id], stopFightPacket(id)[20:])
     74 	}
     75 
     76 	if err := world.handleTeamAction(world.clients[2], teamRequest(2, 24, 0, 0)); err != nil {
     77 		t.Fatal(err)
     78 	}
     79 	for _, id := range []uint32{2, 3, 3} {
     80 		world.handleCombat(world.clients[id], attackPacket(id, npc.id)[20:])
     81 		world.advanceCombat(world.characters[id].nextAttack)
     82 		world.handleCombat(world.clients[id], stopFightPacket(id)[20:])
     83 	}
     84 	world.handleLootUse(world.clients[2], teamLootUse(2, 1))
     85 	world.handleLootUse(world.clients[4], teamLootUse(4, 1))
     86 
     87 	if npc.corpse == nil || npc.corpse.team != 1 || world.clients[2].lootCorpse != 0 || world.clients[4].lootCorpse != npc.id {
     88 		t.Fatal("departure retroactively moved team damage into solo credit or retained team access")
     89 	}
     90 }
     91 
     92 func TestTeamLootFailedCashUseRetainsMoneyAndPublishesNoSuccess(t *testing.T) {
     93 	world := newTeamLootWorld(t)
     94 	for _, client := range world.clients {
     95 		client.conn.(*combatTestConn).packets = nil
     96 	}
     97 	failDatabaseWrites(t, world)
     98 
     99 	err := world.handleLootUse(world.clients[2], teamLootUse(2, 1))
    100 
    101 	if err == nil || world.characters[1000000].corpse.credits != 123 || world.characters[2].inventory.Credits != 0 || world.clients[2].lootCorpse != 0 {
    102 		t.Fatal("failed cash transaction changed corpse or player ownership")
    103 	}
    104 	for _, client := range world.clients {
    105 		if len(client.conn.(*combatTestConn).packets) != 0 {
    106 			t.Fatal("failed cash transaction published success or a cash reset")
    107 		}
    108 	}
    109 }
    110 
    111 func TestTeamLootFailedNanoDeathRetainsReservationUntilRefundCommits(t *testing.T) {
    112 	t.Chdir(t.TempDir())
    113 	world := newLootRightsWorld(t)
    114 	formTestTeam(t, world, 1, 2)
    115 	player, npc := world.characters[1], world.characters[1000000]
    116 	player.profession, player.nano = 8, 30
    117 	player.stats = testCharacterStats()
    118 	player.stats.NanoAiding.PsychologicalModification, player.stats.NanoAiding.MatterCreation = 7, 6
    119 	player.inventory.LearnedNanos = []uint32{29618}
    120 	npc.health = 1
    121 	if err := world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 29618})[20:]); err != nil {
    122 		t.Fatal(err)
    123 	}
    124 	deadline := world.clients[1].nanoCast.readyAt
    125 	failDatabaseWrites(t, world)
    126 
    127 	world.advanceNanos(deadline.Add(time.Nanosecond))
    128 
    129 	if npc.health != 1 || npc.corpse != nil || len(npc.damageContributors) != 0 || player.nano != 21 || player.nanoReserved != 9 ||
    130 		world.clients[1].nanoCast == nil || !world.clients[1].nanoCast.cancelled ||
    131 		len(world.npcRespawns)+len(world.corpseExpiry) != 0 {
    132 		t.Fatal("failed nano death lost its refundable reservation or published a team award")
    133 	}
    134 	if _, err := world.db.Exec("PRAGMA query_only=OFF"); err != nil {
    135 		t.Fatal(err)
    136 	}
    137 	world.advanceNanos(world.clients[1].nanoCast.readyAt.Add(time.Nanosecond))
    138 	var nano, reserved uint32
    139 	if err := world.db.QueryRow("SELECT nano,nano_reserved FROM characters WHERE id=1").Scan(&nano, &reserved); err != nil {
    140 		t.Fatal(err)
    141 	}
    142 	if nano != 30 || reserved != 0 || player.nano != 30 || player.nanoReserved != 0 || world.clients[1].nanoCast != nil || npc.health != 1 || npc.corpse != nil {
    143 		t.Fatal("restored writes did not durably refund the cancelled cast without a team award")
    144 	}
    145 }
    146 
    147 func TestTeamLootFilledBackpackRetainsContentsForTheTakingMember(t *testing.T) {
    148 	world := newFilledLootContainerWorld(t, 143832)
    149 	member := world.newCharacter()
    150 	member.position = [3]float32{}
    151 	if err := world.saveUser(member); err != nil {
    152 		t.Fatal(err)
    153 	}
    154 	world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
    155 	formTestTeam(t, world, 1, 2)
    156 	corpse := world.characters[1000000]
    157 	corpse.corpse.looter, corpse.corpse.team = 0, world.characters[1].team.id
    158 	if err := world.flushWorld(); err != nil {
    159 		t.Fatal(err)
    160 	}
    161 
    162 	if err := world.handleLootUse(world.clients[2], teamLootUse(2, 1)); err != nil {
    163 		t.Fatal(err)
    164 	}
    165 	if err := world.handleInventory(world.clients[2], teamLootTake(2)); err != nil {
    166 		t.Fatal(err)
    167 	}
    168 	restored := newTestInventoryServer(t)
    169 
    170 	if len(corpse.corpse.items) != 0 || len(restored.characters[1].inventory.Items) != 0 || len(restored.characters[2].inventory.Items) != 1 ||
    171 		restored.characters[2].inventory.Items[0] != (ownedItem{ID: 17, Type: 51017, Template: 143832, Quality: 1, Slot: 64, Count: 1}) ||
    172 		len(restored.characters[2].inventory.Containers[17]) != 1 ||
    173 		restored.characters[2].inventory.Containers[17][0] != (ownedItem{ID: 18, Type: 51005, Template: 152030, Quality: 42, Count: 3}) ||
    174 		len(restored.items) != 2 || restored.nextItem != 18 {
    175 		t.Fatal("team pickup detached a filled backpack, duplicated its contents or changed item metadata")
    176 	}
    177 }
    178 
    179 func teamLootTake(owner uint32) []byte {
    180 	request := lootTakeRequest()
    181 	binary.BigEndian.PutUint32(request[8:12], owner)
    182 	binary.BigEndian.PutUint32(request[28:32], owner)
    183 	return request
    184 }
    185 
    186 func newTeamLootWorld(t *testing.T) *server {
    187 	t.Helper()
    188 	t.Chdir(t.TempDir())
    189 	world := newLootRightsWorld(t)
    190 	formTestTeam(t, world, 1, 2)
    191 	npc := world.characters[1000000]
    192 	npc.health, npc.level = 1, 1
    193 	if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil {
    194 		t.Fatal(err)
    195 	}
    196 	world.advanceCombat(world.characters[1].nextAttack)
    197 	npc.corpse.items = []ownedItem{{ID: 17, Type: 51005, Template: 42640, Quality: 1, Count: 1}}
    198 	npc.corpse.credits = 123
    199 	if err := world.flushWorld(); err != nil {
    200 		t.Fatal(err)
    201 	}
    202 	return world
    203 }
    204 
    205 func TestTeamLootAggregatesDamageAndAllowsBothMembers(t *testing.T) {
    206 	t.Chdir(t.TempDir())
    207 	world := newLootRightsWorld(t)
    208 	formTestTeam(t, world, 1, 2)
    209 	npc := world.characters[1000000]
    210 	npc.health = 19
    211 
    212 	for _, id := range []uint32{1, 3, 3, 2, 3, 2, 1} {
    213 		if err := world.handleCombat(world.clients[id], attackPacket(id, npc.id)[20:]); err != nil {
    214 			t.Fatal(err)
    215 		}
    216 		world.advanceCombat(world.characters[id].nextAttack)
    217 		world.handleCombat(world.clients[id], stopFightPacket(id)[20:])
    218 	}
    219 	for _, id := range []uint32{1, 2, 3} {
    220 		request := lootUseRequest(1)
    221 		binary.BigEndian.PutUint32(request[8:12], id)
    222 		binary.BigEndian.PutUint32(request[36:40], id)
    223 		if err := world.handleLootUse(world.clients[id], request); err != nil {
    224 			t.Fatal(err)
    225 		}
    226 	}
    227 
    228 	if npc.health != 0 || world.clients[1].lootCorpse != npc.id || world.clients[2].lootCorpse != npc.id || world.clients[3].lootCorpse != 0 {
    229 		t.Fatal("team's ten damage did not beat the solo player's nine, or All rights were not shared")
    230 	}
    231 	packet := corpsePacket(npc)
    232 	length := int(binary.BigEndian.Uint16(packet[6:8]))
    233 	if binary.BigEndian.Uint32(packet[length-20:]) != world.characters[1].team.id || binary.BigEndian.Uint32(packet[length-16:]) != 3 ||
    234 		binary.BigEndian.Uint64(packet[length-12:length-4]) != 0 {
    235 		t.Fatal("native corpse did not announce team All rights with a null designated looter")
    236 	}
    237 }
    238 
    239 func TestTeamLootCompetingBeneficiariesAndTies(t *testing.T) {
    240 	for _, scenario := range []struct {
    241 		name   string
    242 		teams  [][2]uint32
    243 		health uint32
    244 		swings []uint32
    245 		player uint32
    246 		team   uint32
    247 	}{
    248 		{"overlapping player and team IDs", [][2]uint32{{2, 3}}, 19, []uint32{2, 1, 1, 3, 1, 3, 2}, 0, 1},
    249 		{"stronger solo wins despite team final hit", [][2]uint32{{1, 2}}, 19, []uint32{3, 1, 3, 2, 3, 3, 1}, 3, 0},
    250 		{"team reaches tied total first", [][2]uint32{{1, 2}}, 12, []uint32{1, 3, 2, 3}, 0, 1},
    251 		{"solo reaches tied total first", [][2]uint32{{1, 2}}, 12, []uint32{3, 1, 3, 2}, 3, 0},
    252 		{"stronger second team wins", [][2]uint32{{1, 2}, {3, 4}}, 19, []uint32{1, 3, 4, 2, 3, 4, 1}, 0, 2},
    253 	} {
    254 		t.Run(scenario.name, func(t *testing.T) {
    255 			t.Chdir(t.TempDir())
    256 			world := newLootRightsWorld(t)
    257 			fourth := world.newCharacter()
    258 			if err := world.saveUser(fourth); err != nil {
    259 				t.Fatal(err)
    260 			}
    261 			fourth.position = [3]float32{}
    262 			world.clients[4] = &worldClient{id: 4, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
    263 			for _, pair := range scenario.teams {
    264 				formTestTeam(t, world, pair[0], pair[1])
    265 			}
    266 			npc := world.characters[1000000]
    267 			npc.health = scenario.health
    268 
    269 			for _, id := range scenario.swings {
    270 				world.handleCombat(world.clients[id], attackPacket(id, npc.id)[20:])
    271 				world.advanceCombat(world.characters[id].nextAttack)
    272 				world.handleCombat(world.clients[id], stopFightPacket(id)[20:])
    273 			}
    274 
    275 			if npc.health != 0 || npc.corpse.looter != scenario.player || npc.corpse.team != scenario.team {
    276 				t.Fatalf("wrong winning beneficiary: %+v", npc.corpse)
    277 			}
    278 		})
    279 	}
    280 }
    281 
    282 func TestTeamLootConcurrentUseAndPickupHaveOneDurableOwner(t *testing.T) {
    283 	world := newTeamLootWorld(t)
    284 	npc := world.characters[1000000]
    285 	clients := []*worldClient{world.clients[1], world.clients[2], world.clients[3]}
    286 	zone, stop := startTestZone(t, world)
    287 	var workers sync.WaitGroup
    288 
    289 	for _, client := range clients {
    290 		workers.Go(func() {
    291 			if err := zone.call(zonePacket{client, teamLootUse(client.id, 1)}); err != nil {
    292 				t.Error(err)
    293 			}
    294 		})
    295 	}
    296 	workers.Wait()
    297 	var windowsCorrect bool
    298 	if err := zone.call(zoneTestCommand(func(state *server) error {
    299 		windowsCorrect = state.clients[1].lootCorpse == npc.id && state.clients[2].lootCorpse == npc.id && state.clients[3].lootCorpse == 0
    300 		state.clients[3].lootCorpse = npc.id
    301 		return nil
    302 	})); err != nil {
    303 		t.Fatal(err)
    304 	}
    305 	if !windowsCorrect {
    306 		t.Fatal("All windows or outsider exclusion failed")
    307 	}
    308 	for _, client := range clients {
    309 		workers.Go(func() {
    310 			if err := zone.call(zonePacket{client, teamLootTake(client.id)}); err != nil {
    311 				t.Error(err)
    312 			}
    313 		})
    314 	}
    315 	workers.Wait()
    316 	if err := stop(); err != nil {
    317 		t.Fatal(err)
    318 	}
    319 	restored := newTestInventoryServer(t)
    320 
    321 	if len(npc.corpse.items) != 0 || len(restored.characters[1000000].corpse.items) != 0 ||
    322 		len(restored.characters[1].inventory.Items)+len(restored.characters[2].inventory.Items) != 1 ||
    323 		len(restored.characters[3].inventory.Items) != 0 || restored.characters[3].inventory.Credits != 0 ||
    324 		restored.characters[1].inventory.Credits+restored.characters[2].inventory.Credits != 123 || restored.characters[1000000].corpse.credits != 0 {
    325 		t.Fatal("concurrent members duplicated an item or cash, or stale outsider handle authorized pickup")
    326 	}
    327 	for _, id := range []uint32{1, 2} {
    328 		packets := world.clients[id].conn.(*combatTestConn).packets
    329 		refreshed := false
    330 		for _, packet := range packets {
    331 			refreshed = refreshed || bytes.Equal(packet[16:], targetPacket(lootInventoryPacket(id, npc, false))[16:])
    332 		}
    333 		if len(world.characters[id].inventory.Items) == 0 && !refreshed {
    334 			t.Fatal("other open team window retained a taken icon")
    335 		}
    336 	}
    337 }
    338 
    339 func TestTeamLootDepartureRevokesOpenHandlesAndNewTeamCannotInherit(t *testing.T) {
    340 	for _, action := range []uint32{22, 24} {
    341 		t.Run(map[uint32]string{22: "kick", 24: "leave"}[action], func(t *testing.T) {
    342 			world := newTeamLootWorld(t)
    343 			previous := world.characters[1].team.id
    344 			for _, id := range []uint32{1, 2} {
    345 				world.handleLootUse(world.clients[id], teamLootUse(id, 1))
    346 			}
    347 
    348 			owner, target := uint32(1), uint32(2)
    349 			if action == 24 {
    350 				owner, target = 2, 0
    351 			}
    352 			if err := world.handleTeamAction(world.clients[owner], teamRequest(owner, action, target, 0)); err != nil {
    353 				t.Fatal(err)
    354 			}
    355 			if world.clients[1].lootCorpse != 0 || world.clients[2].lootCorpse != 0 {
    356 				t.Fatal("dissolving a pair retained open corpse handles")
    357 			}
    358 			formTestTeam(t, world, 1, 2)
    359 			for _, id := range []uint32{1, 2} {
    360 				world.clients[id].lootCorpse = 1000000
    361 				world.handleInventory(world.clients[id], teamLootTake(id))
    362 				world.handleLootUse(world.clients[id], teamLootUse(id, 2))
    363 			}
    364 
    365 			if world.characters[1].team.id == previous || len(world.characters[1000000].corpse.items) != 1 ||
    366 				len(world.characters[1].inventory.Items)+len(world.characters[2].inventory.Items) != 0 {
    367 				t.Fatal("replacement team inherited historical corpse rights")
    368 			}
    369 		})
    370 	}
    371 }
    372 
    373 func TestTeamLootFailedPickupAndDeparturePreserveRightsAndContents(t *testing.T) {
    374 	world := newTeamLootWorld(t)
    375 	world.handleLootUse(world.clients[2], teamLootUse(2, 1))
    376 	before := len(world.clients[2].conn.(*combatTestConn).packets)
    377 	failDatabaseWrites(t, world)
    378 
    379 	errTake := world.handleInventory(world.clients[2], teamLootTake(2))
    380 	errLeave := world.handleTeamAction(world.clients[2], teamRequest(2, 24, 0, 0))
    381 
    382 	if errTake == nil || errLeave == nil || world.clients[2].lootCorpse != 1000000 || world.characters[2].team == nil ||
    383 		len(world.clients[2].conn.(*combatTestConn).packets) != before || len(world.characters[1000000].corpse.items) != 1 ||
    384 		len(world.characters[2].inventory.Items) != 0 {
    385 		t.Fatal("failed writes changed live rights, contents or acknowledgements")
    386 	}
    387 }
    388 
    389 func TestTeamLootContributionsAndAwardSurviveRestart(t *testing.T) {
    390 	t.Chdir(t.TempDir())
    391 	world := newLootRightsWorld(t)
    392 	formTestTeam(t, world, 2, 3)
    393 	npc := world.characters[1000000]
    394 	npc.health, npc.level = 7, 1
    395 	npc.npcDefinitionID = "950/district/2/group/0/authored_point"
    396 	world.handleCombat(world.clients[2], attackPacket(2, npc.id)[20:])
    397 	world.advanceCombat(world.characters[2].nextAttack)
    398 	world.handleCombat(world.clients[2], stopFightPacket(2)[20:])
    399 	if err := world.flushWorld(); err != nil {
    400 		t.Fatal(err)
    401 	}
    402 	restored := newTestInventoryServer(t)
    403 	for _, id := range []uint32{1, 2, 3} {
    404 		restored.clients[id] = &worldClient{id: id, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
    405 	}
    406 
    407 	for _, id := range []uint32{3, 1} {
    408 		restored.handleCombat(restored.clients[id], attackPacket(id, npc.id)[20:])
    409 		restored.advanceCombat(restored.characters[id].nextAttack)
    410 		restored.handleCombat(restored.clients[id], stopFightPacket(id)[20:])
    411 	}
    412 	again := newTestInventoryServer(t)
    413 
    414 	if npc.damageContributors[damageContributor{team: 1}] != 3 || restored.characters[1000000].health != 0 ||
    415 		again.characters[1000000].corpse == nil || again.characters[1000000].corpse.team != 1 || again.characters[1000000].corpse.looter != 0 ||
    416 		again.characters[2].team.id != 1 || again.characters[3].team.id != 1 || len(again.characters[1000000].damageContributors) != 0 ||
    417 		len(again.npcRespawns) != 1 || len(again.corpseExpiry) != 1 {
    418 		t.Fatalf("restart lost hit-time team credit or the immediate durable death award: old=%+v HP=%d corpse=%+v team=%+v respawns=%d expiry=%d", npc.damageContributors, restored.characters[1000000].health, again.characters[1000000].corpse, again.characters[2].team, len(again.npcRespawns), len(again.corpseExpiry))
    419 	}
    420 }
    421 
    422 func TestTeamLootFailedKillingBlowPublishesNoDeath(t *testing.T) {
    423 	t.Chdir(t.TempDir())
    424 	world := newLootRightsWorld(t)
    425 	formTestTeam(t, world, 1, 2)
    426 	npc := world.characters[1000000]
    427 	npc.health = 1
    428 	if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil {
    429 		t.Fatal(err)
    430 	}
    431 	world.clients[1].conn.(*combatTestConn).packets = nil
    432 	failDatabaseWrites(t, world)
    433 
    434 	world.advanceCombat(world.characters[1].nextAttack)
    435 	world.advanceCombat(time.Now().Add(time.Hour))
    436 
    437 	if npc.health != 1 || npc.corpse != nil || len(npc.damageContributors) != 0 || len(world.npcRespawns)+len(world.corpseExpiry) != 0 {
    438 		t.Fatal("failed killing-blow transaction changed HP, rights or lifecycle")
    439 	}
    440 	for _, packet := range world.clients[1].conn.(*combatTestConn).packets {
    441 		if key := binary.BigEndian.Uint32(packet[16:20]); key == 0x4f474e05 || key == 0x46002f16 ||
    442 			key == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 99 {
    443 			t.Fatal("failed death was published")
    444 		}
    445 	}
    446 }