aorecomp

Anarchy Online classic in the browser

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


server/nomad_armor_test.go (10510B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"slices"
      9 	"testing"
     10 )
     11 
     12 func TestNomadGeneratedPickupKeepsSelectedQuality(t *testing.T) {
     13 	world, output := newKillExperienceWorld(t, "unarmed")
     14 	player, client := world.characters[1], world.clients[1]
     15 	player.stats.Abilities.Stamina, player.stats.Abilities.Strength = 264, 239
     16 	if err := world.saveUser(player); err != nil {
     17 		t.Fatal(err)
     18 	}
     19 	// Controlled membership tests delivery, not historical Nomad creature drops.
     20 	profile := *world.characters[1000000].combatProfile
     21 	profile.Loot = []gamedata.NPCLoot{{Template: 85592, Quality: [2]uint32{100, 100}, Chance: 100}}
     22 	world.characters[1000000].combatProfile = &profile
     23 	world.combatRoll = func(uint32) uint32 { return 0 }
     24 	identity := world.nextItem + 1
     25 	var wire []byte
     26 	for _, value := range []uint32{identity, 85593, 85592, 100, 0} {
     27 		wire = binary.BigEndian.AppendUint32(wire, value)
     28 	}
     29 
     30 	attackForExperience(t, world, "unarmed")
     31 	if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	shown := slices.ContainsFunc(output.packets, func(packet []byte) bool { return bytes.Contains(packet, wire) })
     35 	for range 2 {
     36 		if err := world.handleInventory(client, lootTakeRequest()); err != nil {
     37 			t.Fatal(err)
     38 		}
     39 	}
     40 	world = newTestInventoryServer(t)
     41 	client = &worldClient{id: 1, conn: output, target: true, ready: true}
     42 	world.clients[1] = client
     43 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 24)[20:]); err != nil || client.equipment == nil {
     44 		t.Fatal("picked-up gloves did not start wear", err)
     45 	}
     46 	world.advanceEquipment(client.equipment.readyAt)
     47 	worn := newTestInventoryServer(t)
     48 	if err := world.handleInventory(client, itemMovePacket(1, 24, 64)[20:]); err != nil || client.equipment == nil {
     49 		t.Fatal("picked-up gloves did not start removal", err)
     50 	}
     51 	world.advanceEquipment(client.equipment.readyAt)
     52 	removed := newTestInventoryServer(t)
     53 
     54 	expected := ownedItem{ID: identity, Type: 51022, Template: 85592, Quality: 100, Slot: 24, Count: 1}
     55 	if !shown || !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{expected}) || worn.characters[1].equipmentBonus(91) != 125 {
     56 		t.Fatal("generated pickup lost its native endpoints, identity, selected quality or worn AC")
     57 	}
     58 	expected.Slot = 64
     59 	if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{expected}) || removed.characters[1].equipmentBonus(91) != 0 ||
     60 		len(removed.characters[1000000].corpse.items) != 0 || removed.nextItem != identity {
     61 		t.Fatal("duplicate pickup, removal or cold recovery lost or duplicated the gloves")
     62 	}
     63 }
     64 
     65 func TestNomadArmorQualitiesWearRemoveAndRecover(t *testing.T) {
     66 	for _, scenario := range []struct {
     67 		low, high, slot, quality, value uint32
     68 		stamina, strength               uint16
     69 		armor                           [8]int32
     70 	}{
     71 		{85507, 85506, 27, 1, 843, 8, 8, [8]int32{3, 3, 3, 1, 1, 3, 1, 3}},
     72 		{85507, 85506, 27, 2, 863, 11, 10, [8]int32{6, 6, 6, 2, 2, 6, 2, 6}},
     73 		{85507, 85506, 27, 20, 8155, 57, 52, [8]int32{62, 62, 55, 16, 16, 55, 16, 55}},
     74 		{85507, 85506, 27, 100, 199375, 263, 238, [8]int32{312, 312, 273, 78, 78, 273, 78, 273}},
     75 		{85507, 85506, 27, 200, 803015, 520, 470, [8]int32{625, 625, 546, 156, 156, 546, 156, 546}},
     76 		{85554, 85553, 18, 1, 1012, 8, 8, [8]int32{3, 3, 3, 1, 1, 3, 1, 3}},
     77 		{85554, 85553, 18, 2, 1036, 11, 10, [8]int32{7, 7, 6, 2, 2, 6, 2, 6}},
     78 		{85554, 85553, 18, 20, 9787, 57, 52, [8]int32{74, 74, 65, 19, 19, 65, 19, 65}},
     79 		{85554, 85553, 18, 100, 239251, 263, 238, [8]int32{375, 375, 328, 94, 94, 328, 94, 328}},
     80 		{85554, 85553, 18, 200, 963619, 520, 470, [8]int32{750, 750, 656, 187, 187, 656, 187, 656}},
     81 		{85593, 85592, 24, 1, 337, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     82 		{85593, 85592, 24, 2, 345, 11, 10, [8]int32{2, 2, 2, 1, 1, 2, 1, 2}},
     83 		{85593, 85592, 24, 20, 3262, 57, 52, [8]int32{25, 25, 22, 7, 7, 22, 7, 22}},
     84 		{85593, 85592, 24, 100, 79749, 263, 238, [8]int32{125, 125, 109, 31, 31, 109, 31, 109}},
     85 		{85593, 85592, 24, 101, 81362, 265, 240, [8]int32{126, 126, 110, 32, 32, 110, 32, 110}},
     86 		{85593, 85592, 24, 199, 317990, 517, 468, [8]int32{249, 249, 217, 62, 62, 217, 62, 217}},
     87 		{85593, 85592, 24, 200, 321206, 520, 470, [8]int32{250, 250, 218, 62, 62, 218, 62, 218}},
     88 		{85633, 85632, 30, 1, 506, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     89 		{85633, 85632, 30, 2, 518, 11, 10, [8]int32{3, 3, 3, 1, 1, 3, 1, 3}},
     90 		{85633, 85632, 30, 20, 4893, 57, 52, [8]int32{37, 37, 32, 10, 10, 32, 10, 32}},
     91 		{85633, 85632, 30, 100, 119625, 263, 238, [8]int32{187, 187, 164, 47, 47, 164, 47, 164}},
     92 		{85633, 85632, 30, 200, 481809, 520, 470, [8]int32{375, 375, 328, 93, 93, 328, 93, 328}},
     93 		{85681, 85680, 21, 1, 1350, 8, 8, [8]int32{5, 5, 5, 1, 1, 5, 1, 5}},
     94 		{85681, 85680, 21, 2, 1382, 11, 10, [8]int32{10, 10, 9, 2, 2, 9, 2, 9}},
     95 		{85681, 85680, 21, 20, 13050, 57, 52, [8]int32{100, 100, 88, 25, 25, 88, 25, 88}},
     96 		{85681, 85680, 21, 100, 319002, 263, 238, [8]int32{500, 500, 438, 125, 125, 438, 125, 438}},
     97 		{85681, 85680, 21, 200, 1284825, 520, 470, [8]int32{1000, 1000, 875, 250, 250, 875, 250, 875}},
     98 		{85737, 85736, 25, 1, 337, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     99 		{85737, 85736, 25, 2, 345, 11, 10, [8]int32{2, 2, 2, 1, 1, 2, 1, 2}},
    100 		{85737, 85736, 23, 2, 345, 11, 10, [8]int32{2, 2, 2, 1, 1, 2, 1, 2}},
    101 		{85737, 85736, 25, 20, 3262, 57, 52, [8]int32{25, 25, 22, 7, 7, 22, 7, 22}},
    102 		{85737, 85736, 25, 100, 79749, 263, 238, [8]int32{125, 125, 109, 31, 31, 109, 31, 109}},
    103 		{85737, 85736, 25, 200, 321206, 520, 470, [8]int32{250, 250, 218, 62, 62, 218, 62, 218}},
    104 	} {
    105 		for _, anchor := range []uint32{scenario.low, scenario.high} {
    106 			t.Run(fmt.Sprintf("%d/QL%d/slot%d", anchor, scenario.quality, scenario.slot), func(t *testing.T) {
    107 				world, output := newEquipmentWorld(t)
    108 				player, client := world.characters[1], world.clients[1]
    109 				player.stats.Abilities.Stamina, player.stats.Abilities.Strength = scenario.stamina+1, scenario.strength+1
    110 				if err := world.saveUser(player); err != nil {
    111 					t.Fatal(err)
    112 				}
    113 				if granted, err := world.grantItem(player, anchor); err != nil || !granted {
    114 					t.Fatal("grant armor", granted, err)
    115 				}
    116 				player.inventory.Items[1].Quality = scenario.quality
    117 				if err := world.saveInventory(player, player.inventory); err != nil {
    118 					t.Fatal(err)
    119 				}
    120 				weapon := player.inventory.Items[0]
    121 
    122 				for _, ability := range []*uint16{&player.stats.Abilities.Stamina, &player.stats.Abilities.Strength} {
    123 					*ability -= 1
    124 					if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 {
    125 						t.Fatal("threshold equality equipped or lost the armor", err)
    126 					}
    127 					*ability += 1
    128 				}
    129 				output.packets = nil
    130 				if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment == nil {
    131 					t.Fatal("qualified armor did not start wear", err)
    132 				}
    133 				world.advanceEquipment(client.equipment.readyAt)
    134 				worn := newTestInventoryServer(t)
    135 				low, high := scenario.low, scenario.high
    136 				if scenario.quality == 1 {
    137 					high = low
    138 				} else if scenario.quality == 200 {
    139 					low = high
    140 				}
    141 				var action []byte
    142 				for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, scenario.slot, 0, 0} {
    143 					action = binary.BigEndian.AppendUint32(action, word)
    144 				}
    145 				if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) {
    146 					t.Fatal("native wear lost the selected quality or endpoint pair")
    147 				}
    148 				if err := world.handleInventory(client, itemMovePacket(1, scenario.slot, 111)[20:]); err != nil || client.equipment == nil {
    149 					t.Fatal("armor did not start removal", err)
    150 				}
    151 				world.advanceEquipment(client.equipment.readyAt)
    152 				removed := newTestInventoryServer(t)
    153 
    154 				expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: scenario.slot, Count: 1}
    155 				if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, expected}) || worn.characters[1].inventory.Items[1].shopPrice() != scenario.value {
    156 					t.Fatal("cold armor identity, quality, value or unrelated item changed")
    157 				}
    158 				expected.Slot = 65
    159 				if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 {
    160 					t.Fatal("removal lost or replaced an instance")
    161 				}
    162 				for index, bonus := range scenario.armor {
    163 					if worn.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 {
    164 						t.Fatal("selected armor effects or their removal changed", index)
    165 					}
    166 				}
    167 			})
    168 		}
    169 	}
    170 }
    171 
    172 func TestNomadSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) {
    173 	for _, scenario := range []struct{ quality, sale uint32 }{
    174 		{2, 17}, {100, 3987}, {101, 4068}, {200, 16060},
    175 	} {
    176 		t.Run(fmt.Sprint(scenario.quality), func(t *testing.T) {
    177 			world, _ := newShopWorld(t)
    178 			player, client := world.characters[1], world.clients[1]
    179 			if granted, err := world.grantItem(player, 85593); err != nil || !granted {
    180 				t.Fatal(granted, err)
    181 			}
    182 			player.inventory.Items[1].Quality = scenario.quality
    183 			if err := world.saveInventory(player, player.inventory); err != nil {
    184 				t.Fatal(err)
    185 			}
    186 			before := append([]ownedItem(nil), player.inventory.Items...)
    187 			offer := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:]
    188 
    189 			if err := world.handleTrade(client, offer); err != nil {
    190 				t.Fatal(err)
    191 			}
    192 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    193 				t.Fatal(err)
    194 			}
    195 			cancelled := newTestInventoryServer(t).characters[1].inventory
    196 			if err := world.handleShopUse(client, shopUseRequest(2)); err != nil {
    197 				t.Fatal(err)
    198 			}
    199 			if err := world.handleTrade(client, offer); err != nil {
    200 				t.Fatal(err)
    201 			}
    202 			for range 2 {
    203 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    204 					t.Fatal(err)
    205 				}
    206 			}
    207 			sold := newTestInventoryServer(t).characters[1].inventory
    208 
    209 			if !slices.Equal(cancelled.Items, before) || cancelled.Credits != 100 {
    210 				t.Fatal("cancelled sale changed the selected instance or wallet", cancelled)
    211 			}
    212 			if !slices.Equal(sold.Items, before[:1]) || sold.Credits != 100+scenario.sale {
    213 				t.Fatal("accepted or repeated sale changed another item or its selected-quality price", sold)
    214 			}
    215 		})
    216 	}
    217 }