server/steel_ribbed_armor_test.go (10524B)
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 TestSteelRibbedGeneratedPickupKeepsSelectedQuality(t *testing.T) { 13 world, output := newKillExperienceWorld(t, "unarmed") 14 player, client := world.characters[1], world.clients[1] 15 player.stats.Abilities.Sense, player.stats.Abilities.Agility = 269, 244 16 if err := world.saveUser(player); err != nil { 17 t.Fatal(err) 18 } 19 // Controlled membership tests delivery, not historical SteelRibbed creature drops. 20 profile := *world.characters[1000000].combatProfile 21 profile.Loot = []gamedata.NPCLoot{{Template: 22212, 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, 85580, 22212, 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: 22212, Quality: 100, Slot: 24, Count: 1} 55 if !shown || !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{expected}) || worn.characters[1].equipmentBonus(91) != 109 { 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 TestSteelRibbedArmorQualitiesWearRemoveAndRecover(t *testing.T) { 66 for _, scenario := range []struct { 67 low, high, slot, quality, value uint32 68 sense, agility uint16 69 armor [8]int32 70 }{ 71 {85494, 22347, 27, 1, 906, 8, 8, [8]int32{3, 3, 3, 3, 1, 1, 3, 1}}, 72 {85494, 22347, 27, 2, 927, 11, 10, [8]int32{6, 6, 6, 6, 2, 2, 6, 2}}, 73 {85494, 22347, 27, 20, 8760, 58, 53, [8]int32{62, 55, 62, 62, 13, 13, 55, 13}}, 74 {85494, 22347, 27, 100, 214144, 268, 243, [8]int32{312, 273, 312, 312, 63, 63, 273, 63}}, 75 {85494, 22347, 27, 200, 862498, 530, 480, [8]int32{625, 546, 625, 625, 125, 125, 546, 125}}, 76 {85541, 22262, 18, 1, 1087, 8, 8, [8]int32{3, 3, 3, 3, 1, 1, 3, 1}}, 77 {85541, 22262, 18, 2, 1113, 11, 10, [8]int32{7, 6, 7, 7, 2, 2, 6, 2}}, 78 {85541, 22262, 18, 20, 10512, 58, 53, [8]int32{74, 65, 74, 74, 15, 15, 65, 15}}, 79 {85541, 22262, 18, 100, 256973, 268, 243, [8]int32{375, 328, 375, 375, 75, 75, 328, 75}}, 80 {85541, 22262, 18, 200, 1034998, 530, 480, [8]int32{750, 656, 750, 750, 150, 150, 656, 150}}, 81 {85580, 22212, 24, 1, 362, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 82 {85580, 22212, 24, 2, 370, 11, 10, [8]int32{2, 2, 2, 2, 1, 1, 2, 1}}, 83 {85580, 22212, 24, 20, 3503, 58, 53, [8]int32{25, 22, 25, 25, 6, 6, 22, 6}}, 84 {85580, 22212, 24, 100, 85657, 268, 243, [8]int32{125, 109, 125, 125, 25, 25, 109, 25}}, 85 {85580, 22212, 24, 101, 87388, 270, 245, [8]int32{126, 110, 126, 126, 26, 26, 110, 26}}, 86 {85580, 22212, 24, 199, 341543, 527, 478, [8]int32{249, 217, 249, 249, 50, 50, 217, 50}}, 87 {85580, 22212, 24, 200, 344999, 530, 480, [8]int32{250, 218, 250, 250, 50, 50, 218, 50}}, 88 {85620, 22158, 30, 1, 543, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 89 {85620, 22158, 30, 2, 556, 11, 10, [8]int32{3, 3, 3, 3, 1, 1, 3, 1}}, 90 {85620, 22158, 30, 20, 5255, 58, 53, [8]int32{37, 32, 37, 37, 8, 8, 32, 8}}, 91 {85620, 22158, 30, 100, 128486, 268, 243, [8]int32{187, 164, 187, 187, 38, 38, 164, 38}}, 92 {85620, 22158, 30, 200, 517499, 530, 480, [8]int32{375, 328, 375, 375, 75, 75, 328, 75}}, 93 {85668, 22080, 21, 1, 1450, 8, 8, [8]int32{5, 5, 5, 5, 1, 1, 5, 1}}, 94 {85668, 22080, 21, 2, 1484, 11, 10, [8]int32{10, 9, 10, 10, 2, 2, 9, 2}}, 95 {85668, 22080, 21, 20, 14016, 58, 53, [8]int32{100, 88, 100, 100, 20, 20, 88, 20}}, 96 {85668, 22080, 21, 100, 342631, 268, 243, [8]int32{500, 438, 500, 500, 100, 100, 438, 100}}, 97 {85668, 22080, 21, 200, 1379997, 530, 480, [8]int32{1000, 875, 1000, 1000, 200, 200, 875, 200}}, 98 {85724, 21971, 25, 1, 362, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 99 {85724, 21971, 25, 2, 370, 11, 10, [8]int32{2, 2, 2, 2, 1, 1, 2, 1}}, 100 {85724, 21971, 23, 2, 370, 11, 10, [8]int32{2, 2, 2, 2, 1, 1, 2, 1}}, 101 {85724, 21971, 25, 20, 3503, 58, 53, [8]int32{25, 22, 25, 25, 6, 6, 22, 6}}, 102 {85724, 21971, 25, 100, 85657, 268, 243, [8]int32{125, 109, 125, 125, 25, 25, 109, 25}}, 103 {85724, 21971, 25, 200, 344999, 530, 480, [8]int32{250, 218, 250, 250, 50, 50, 218, 50}}, 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.Sense, player.stats.Abilities.Agility = scenario.sense+1, scenario.agility+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.Sense, &player.stats.Abilities.Agility} { 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 TestSteelRibbedSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) { 173 for _, scenario := range []struct{ quality, sale uint32 }{ 174 {2, 18}, {100, 4282}, {101, 4369}, {200, 17249}, 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, 85580); 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 }