server/graft_armor_test.go (10607B)
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 TestGraftGeneratedPickupKeepsSelectedQuality(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 = 277, 252 16 if err := world.saveUser(player); err != nil { 17 t.Fatal(err) 18 } 19 // Controlled membership tests delivery, not historical Graft creature drops. 20 profile := *world.characters[1000000].combatProfile 21 profile.Loot = []gamedata.NPCLoot{{Template: 142683, 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, 120581, 142683, 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: 142683, 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 TestGraftArmorQualitiesWearRemoveAndRecover(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 {120579, 142679, 27, 1, 1093, 9, 9, [8]int32{3, 3, 3, 3, 3, 1, 3, 3}}, 72 {120579, 142679, 27, 2, 1119, 12, 11, [8]int32{6, 6, 6, 5, 5, 2, 6, 6}}, 73 {120579, 142679, 27, 20, 10572, 60, 55, [8]int32{55, 62, 62, 47, 40, 13, 55, 55}}, 74 {120579, 142679, 27, 100, 258450, 276, 251, [8]int32{273, 312, 312, 234, 196, 63, 273, 273}}, 75 {120579, 142679, 27, 200, 1040946, 545, 495, [8]int32{546, 625, 625, 468, 390, 125, 546, 546}}, 76 {120565, 142681, 18, 1, 1312, 9, 9, [8]int32{3, 3, 3, 3, 3, 1, 3, 3}}, 77 {120565, 142681, 18, 2, 1343, 12, 11, [8]int32{6, 7, 7, 6, 5, 2, 6, 6}}, 78 {120565, 142681, 18, 20, 12687, 60, 55, [8]int32{65, 74, 74, 56, 47, 15, 65, 65}}, 79 {120565, 142681, 18, 100, 310140, 276, 251, [8]int32{328, 375, 375, 281, 234, 75, 328, 328}}, 80 {120565, 142681, 18, 200, 1249135, 545, 495, [8]int32{656, 750, 750, 562, 468, 150, 656, 656}}, 81 {120581, 142683, 24, 1, 437, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 82 {120581, 142683, 24, 2, 447, 12, 11, [8]int32{2, 2, 2, 2, 2, 1, 2, 2}}, 83 {120581, 142683, 24, 20, 4228, 60, 55, [8]int32{22, 25, 25, 19, 16, 6, 22, 22}}, 84 {120581, 142683, 24, 100, 103379, 276, 251, [8]int32{109, 125, 125, 94, 78, 25, 109, 109}}, 85 {120581, 142683, 24, 101, 105470, 278, 253, [8]int32{110, 126, 126, 94, 79, 26, 110, 110}}, 86 {120581, 142683, 24, 199, 412208, 542, 493, [8]int32{217, 249, 249, 186, 155, 50, 217, 217}}, 87 {120581, 142683, 24, 200, 416378, 545, 495, [8]int32{218, 250, 250, 187, 156, 50, 218, 218}}, 88 {120582, 142685, 30, 1, 656, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 89 {120582, 142685, 30, 2, 671, 12, 11, [8]int32{3, 3, 3, 2, 2, 1, 3, 3}}, 90 {120582, 142685, 30, 20, 6343, 60, 55, [8]int32{32, 37, 37, 28, 23, 8, 32, 32}}, 91 {120582, 142685, 30, 100, 155070, 276, 251, [8]int32{164, 187, 187, 140, 117, 38, 164, 164}}, 92 {120582, 142685, 30, 200, 624567, 545, 495, [8]int32{328, 375, 375, 281, 234, 75, 328, 328}}, 93 {120583, 142687, 21, 1, 1750, 9, 9, [8]int32{5, 5, 5, 5, 5, 1, 5, 5}}, 94 {120583, 142687, 21, 2, 1792, 12, 11, [8]int32{9, 10, 10, 9, 8, 2, 9, 9}}, 95 {120583, 142687, 21, 20, 16916, 60, 55, [8]int32{83, 100, 100, 76, 64, 20, 83, 83}}, 96 {120583, 142687, 21, 100, 413521, 276, 251, [8]int32{413, 500, 500, 376, 313, 100, 413, 413}}, 97 {120583, 142687, 21, 200, 1665514, 545, 495, [8]int32{825, 1000, 1000, 750, 625, 200, 825, 825}}, 98 {120584, 142689, 25, 1, 437, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 99 {120584, 142689, 25, 2, 447, 12, 11, [8]int32{2, 2, 2, 2, 2, 1, 2, 2}}, 100 {120584, 142689, 23, 2, 447, 12, 11, [8]int32{2, 2, 2, 2, 2, 1, 2, 2}}, 101 {120584, 142689, 25, 20, 4228, 60, 55, [8]int32{22, 25, 25, 19, 16, 6, 22, 22}}, 102 {120584, 142689, 25, 100, 103379, 276, 251, [8]int32{109, 125, 125, 94, 78, 25, 109, 109}}, 103 {120584, 142689, 25, 200, 416378, 545, 495, [8]int32{218, 250, 250, 187, 156, 50, 218, 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 TestGraftSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) { 173 for _, scenario := range []struct{ quality, sale uint32 }{ 174 {2, 22}, {100, 5168}, {101, 5273}, {200, 20818}, 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, 120581); 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 }