server/organic_armor_test.go (10598B)
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 TestOrganicGeneratedPickupKeepsSelectedQuality(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 = 272, 247 16 if err := world.saveUser(player); err != nil { 17 t.Fatal(err) 18 } 19 // Controlled membership tests delivery, not historical Organic creature drops. 20 profile := *world.characters[1000000].combatProfile 21 profile.Loot = []gamedata.NPCLoot{{Template: 142684, 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, 120587, 142684, 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: 142684, 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 TestOrganicArmorQualitiesWearRemoveAndRecover(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 {120585, 142680, 27, 1, 968, 9, 9, [8]int32{3, 3, 3, 3, 1, 1, 3, 3}}, 72 {120585, 142680, 27, 2, 991, 12, 11, [8]int32{6, 6, 6, 5, 2, 2, 6, 6}}, 73 {120585, 142680, 27, 20, 9363, 59, 54, [8]int32{55, 62, 62, 47, 13, 13, 55, 55}}, 74 {120585, 142680, 27, 100, 228912, 271, 246, [8]int32{273, 312, 312, 234, 63, 63, 273, 273}}, 75 {120585, 142680, 27, 200, 921981, 535, 485, [8]int32{546, 625, 625, 468, 125, 125, 546, 546}}, 76 {120586, 142682, 18, 1, 1162, 9, 9, [8]int32{3, 3, 3, 3, 1, 1, 3, 3}}, 77 {120586, 142682, 18, 2, 1189, 12, 11, [8]int32{6, 7, 7, 6, 2, 2, 6, 6}}, 78 {120586, 142682, 18, 20, 11237, 59, 54, [8]int32{65, 74, 74, 56, 15, 15, 65, 65}}, 79 {120586, 142682, 18, 100, 274696, 271, 246, [8]int32{328, 375, 375, 281, 75, 75, 328, 328}}, 80 {120586, 142682, 18, 200, 1106377, 535, 485, [8]int32{656, 750, 750, 562, 150, 150, 656, 656}}, 81 {120587, 142684, 24, 1, 387, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 82 {120587, 142684, 24, 2, 396, 12, 11, [8]int32{2, 2, 2, 2, 1, 1, 2, 2}}, 83 {120587, 142684, 24, 20, 3745, 59, 54, [8]int32{22, 25, 25, 19, 6, 6, 22, 22}}, 84 {120587, 142684, 24, 100, 91565, 271, 246, [8]int32{109, 125, 125, 94, 25, 25, 109, 109}}, 85 {120587, 142684, 24, 101, 93416, 273, 248, [8]int32{110, 126, 126, 94, 26, 26, 110, 110}}, 86 {120587, 142684, 24, 199, 365099, 532, 483, [8]int32{217, 249, 249, 186, 50, 50, 217, 217}}, 87 {120587, 142684, 24, 200, 368792, 535, 485, [8]int32{218, 250, 250, 187, 50, 50, 218, 218}}, 88 {120588, 142686, 30, 1, 581, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 89 {120588, 142686, 30, 2, 594, 12, 11, [8]int32{3, 3, 3, 2, 1, 1, 3, 3}}, 90 {120588, 142686, 30, 20, 5618, 59, 54, [8]int32{32, 37, 37, 28, 8, 8, 32, 32}}, 91 {120588, 142686, 30, 100, 137347, 271, 246, [8]int32{164, 187, 187, 140, 38, 38, 164, 164}}, 92 {120588, 142686, 30, 200, 553188, 535, 485, [8]int32{328, 375, 375, 281, 75, 75, 328, 328}}, 93 {120589, 142688, 21, 1, 1550, 9, 9, [8]int32{5, 5, 5, 5, 1, 1, 5, 5}}, 94 {120589, 142688, 21, 2, 1587, 12, 11, [8]int32{9, 10, 10, 9, 2, 2, 9, 9}}, 95 {120589, 142688, 21, 20, 14983, 59, 54, [8]int32{88, 100, 100, 76, 20, 20, 88, 88}}, 96 {120589, 142688, 21, 100, 366261, 271, 246, [8]int32{438, 500, 500, 376, 100, 100, 438, 438}}, 97 {120589, 142688, 21, 200, 1475169, 535, 485, [8]int32{875, 1000, 1000, 750, 200, 200, 875, 875}}, 98 {120590, 142690, 25, 1, 387, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 99 {120590, 142690, 25, 2, 396, 12, 11, [8]int32{2, 2, 2, 2, 1, 1, 2, 2}}, 100 {120590, 142690, 23, 2, 396, 12, 11, [8]int32{2, 2, 2, 2, 1, 1, 2, 2}}, 101 {120590, 142690, 25, 20, 3745, 59, 54, [8]int32{22, 25, 25, 19, 6, 6, 22, 22}}, 102 {120590, 142690, 25, 100, 91565, 271, 246, [8]int32{109, 125, 125, 94, 25, 25, 109, 109}}, 103 {120590, 142690, 25, 200, 368792, 535, 485, [8]int32{218, 250, 250, 187, 50, 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 TestOrganicSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) { 173 for _, scenario := range []struct{ quality, sale uint32 }{ 174 {2, 19}, {100, 4578}, {101, 4670}, {200, 18439}, 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, 120587); 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 }