server/low_tech_armor_test.go (9923B)
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 TestLowTechGloveQualityWearAndRecovery(t *testing.T) { 13 for _, anchor := range []uint32{70562, 85597} { 14 for _, scenario := range []struct { 15 quality, value uint32 16 strength, agility uint16 17 armor [8]int32 18 }{ 19 {1, 125, 7, 7, [8]int32{3, 3, 1, 1, 1, 1, 1, 1}}, 20 {2, 127, 9, 9, [8]int32{4, 4, 2, 1, 1, 1, 1, 1}}, 21 {100, 29536, 227, 252, [8]int32{110, 110, 94, 19, 19, 19, 19, 19}}, 22 {101, 30134, 230, 255, [8]int32{111, 111, 94, 19, 19, 19, 19, 19}}, 23 {199, 117773, 448, 498, [8]int32{217, 217, 186, 37, 37, 37, 37, 37}}, 24 {200, 118965, 450, 500, [8]int32{218, 218, 187, 37, 37, 37, 37, 37}}, 25 } { 26 t.Run(fmt.Sprintf("%d/QL%d", anchor, scenario.quality), func(t *testing.T) { 27 world, output := newEquipmentWorld(t) 28 player, client := world.characters[1], world.clients[1] 29 player.stats.Abilities.Strength, player.stats.Abilities.Agility = scenario.strength+1, scenario.agility+1 30 if err := world.saveUser(player); err != nil { 31 t.Fatal(err) 32 } 33 if granted, err := world.grantItem(player, anchor); err != nil || !granted { 34 t.Fatal(granted, err) 35 } 36 player.inventory.Items[1].Quality = scenario.quality 37 if err := world.saveInventory(player, player.inventory); err != nil { 38 t.Fatal(err) 39 } 40 weapon := player.inventory.Items[0] 41 42 // Each authored threshold is strict; rejection must retain the item. 43 for _, ability := range []*uint16{&player.stats.Abilities.Strength, &player.stats.Abilities.Agility} { 44 *ability -= 1 45 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 24)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 { 46 t.Fatal("threshold equality equipped or lost the gloves", err) 47 } 48 *ability += 1 49 } 50 output.packets = nil 51 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 24)[20:]); err != nil || client.equipment == nil { 52 t.Fatal("qualified selected-quality gloves did not start wear", err) 53 } 54 world.advanceEquipment(client.equipment.readyAt) 55 cold := newTestInventoryServer(t) 56 low, high := uint32(70562), uint32(85597) 57 if scenario.quality == 1 { 58 high = low 59 } else if scenario.quality == 200 { 60 low = high 61 } 62 var action []byte 63 for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, 24, 0, 0} { 64 action = binary.BigEndian.AppendUint32(action, word) 65 } 66 if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) { 67 t.Fatal("native wear lost the selected quality or endpoints") 68 } 69 if err := world.handleInventory(client, itemMovePacket(1, 24, 111)[20:]); err != nil || client.equipment == nil { 70 t.Fatal("selected-quality gloves could not be removed", err) 71 } 72 world.advanceEquipment(client.equipment.readyAt) 73 removed := newTestInventoryServer(t) 74 75 expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: 24, Count: 1} 76 if !slices.Equal(cold.characters[1].inventory.Items, []ownedItem{weapon, expected}) || cold.characters[1].inventory.Items[1].shopPrice() != scenario.value { 77 t.Fatal("cold worn quality, identity, value or unrelated item changed", cold.characters[1].inventory) 78 } 79 expected.Slot = 65 80 if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 { 81 t.Fatal("removal lost or replaced the selected instance") 82 } 83 for index, bonus := range scenario.armor { 84 if cold.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 { 85 t.Fatal("selected armor effects or removal changed", index) 86 } 87 } 88 }) 89 } 90 } 91 } 92 93 func TestLowTechBasicArmorPurchaseWearAndSale(t *testing.T) { 94 for _, terminal := range []struct { 95 id uint32 96 side uint16 97 prices, sales [2]uint32 98 }{ 99 {3221226655, 2, [2]uint32{124, 126}, [2]uint32{7, 7}}, 100 {3223389340, 1, [2]uint32{130, 132}, [2]uint32{5, 5}}, 101 {3222078626, 0, [2]uint32{130, 132}, [2]uint32{5, 5}}, 102 } { 103 for row, quality := range []uint32{1, 2} { 104 t.Run(fmt.Sprintf("%d/QL%d", terminal.id, quality), func(t *testing.T) { 105 world, _ := newPurchaseWorld(t) 106 player, client := world.characters[1], world.clients[1] 107 machine := gamedata.ShopMachines[terminal.id] 108 player.playfield, player.position, player.appearance = machine.Playfield, machine.Position, 0x115|terminal.side<<9 109 player.stats.Abilities.Strength, player.stats.Abilities.Agility = 10, 10 110 player.inventory.Credits = terminal.prices[row] 111 if err := world.saveUser(player); err != nil { 112 t.Fatal(err) 113 } 114 if err := world.savePosition(player); err != nil { 115 t.Fatal(err) 116 } 117 if err := world.saveInventory(player, player.inventory); err != nil { 118 t.Fatal(err) 119 } 120 weapon := player.inventory.Items[0] 121 request := shopUseRequest(2) 122 binary.BigEndian.PutUint32(request[44:48], terminal.id) 123 124 if err := world.handleShopUse(client, request); err != nil { 125 t.Fatal(err) 126 } 127 if client.shop == nil || len(client.shop.stock) <= 6+row || client.shop.stock[6+row] != (gamedata.ShopItem{Template: 70562, Quality: quality}) { 128 t.Fatal("basic armor lacks its low-quality glove selection") 129 } 130 for _, request := range [][]byte{ 131 shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, terminal.id}, [2]uint32{111, uint32(6 + row)})[20:], 132 shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:], 133 } { 134 if err := world.handleTrade(client, request); err != nil { 135 t.Fatal(err) 136 } 137 } 138 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 24)[20:]); err != nil || client.equipment == nil { 139 t.Fatal("purchased gloves did not start wear", err) 140 } 141 world.advanceEquipment(client.equipment.readyAt) 142 worn := newTestInventoryServer(t) 143 if err := world.handleInventory(client, itemMovePacket(1, 24, 111)[20:]); err != nil || client.equipment == nil { 144 t.Fatal("purchased gloves did not start removal", err) 145 } 146 world.advanceEquipment(client.equipment.readyAt) 147 request = shopUseRequest(3) 148 binary.BigEndian.PutUint32(request[44:48], terminal.id) 149 if err := world.handleShopUse(client, request); err != nil { 150 t.Fatal(err) 151 } 152 for _, request := range [][]byte{ 153 shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:], 154 shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:], 155 shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:], 156 } { 157 if err := world.handleTrade(client, request); err != nil { 158 t.Fatal(err) 159 } 160 } 161 sold := newTestInventoryServer(t) 162 163 if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, {ID: 2, Type: 51022, Template: 70562, Quality: quality, Slot: 24, Count: 1}}) || worn.characters[1].inventory.Credits != 0 || worn.characters[1].equipmentBonus(91) != int32(quality+2) { 164 t.Fatal("purchased armor or its exact wallet did not survive cold load", worn.characters[1].inventory) 165 } 166 if !slices.Equal(sold.characters[1].inventory.Items, []ownedItem{weapon}) || sold.characters[1].inventory.Credits != terminal.sales[row] || sold.nextItem != 2 || sold.characters[1].equipmentBonus(91) != 0 { 167 t.Fatal("accepted/replayed sale lost another item or credited more than once", sold.characters[1].inventory) 168 } 169 }) 170 } 171 } 172 } 173 174 func TestLowTechPurchaseFailureRetainsWalletAndRetries(t *testing.T) { 175 for _, failure := range []struct{ name, sql string }{ 176 {"item", "CREATE TRIGGER reject_glove BEFORE INSERT ON items BEGIN SELECT RAISE(FAIL,'glove'); END"}, 177 {"ignored placement", "CREATE TRIGGER reject_glove BEFORE INSERT ON item_locations BEGIN SELECT RAISE(IGNORE); END"}, 178 {"commit", "CREATE TABLE glove_commit_failure (id INTEGER REFERENCES items(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_glove AFTER INSERT ON items BEGIN INSERT INTO glove_commit_failure VALUES(9999); END"}, 179 } { 180 t.Run(failure.name, func(t *testing.T) { 181 world, _ := newPurchaseWorld(t) 182 player, client := world.characters[1], world.clients[1] 183 player.position = gamedata.ShopMachines[3221226655].Position 184 request := shopUseRequest(2) 185 binary.BigEndian.PutUint32(request[44:48], 3221226655) 186 if err := world.handleShopUse(client, request); err != nil { 187 t.Fatal(err) 188 } 189 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221226655}, [2]uint32{111, 7})[20:]); err != nil { 190 t.Fatal(err) 191 } 192 if _, err := world.db.Exec(failure.sql); err != nil { 193 t.Fatal(err) 194 } 195 accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:] 196 197 if err := world.handleTrade(client, accept); err == nil { 198 t.Fatal("failed purchase did not report its database failure") 199 } 200 cold := newTestInventoryServer(t) 201 if player.inventory.Credits != 1000 || len(player.inventory.Items) != 1 || world.nextItem != 1 || cold.characters[1].inventory.Credits != 1000 || len(cold.characters[1].inventory.Items) != 1 || cold.nextItem != 1 { 202 t.Fatal("failed purchase spent cash or leaked an instance") 203 } 204 if _, err := world.db.Exec("DROP TRIGGER reject_glove"); err != nil { 205 t.Fatal(err) 206 } 207 if err := world.handleTrade(client, accept); err != nil { 208 t.Fatal(err) 209 } 210 retried := newTestInventoryServer(t) 211 212 if retried.characters[1].inventory.Credits != 874 || retried.nextItem != 3 || len(retried.characters[1].inventory.Items) != 2 || retried.characters[1].inventory.Items[1] != (ownedItem{ID: 3, Type: 51022, Template: 70562, Quality: 2, Slot: 65, Count: 1}) { 213 t.Fatal("purchase retry lost selected quality or charged twice", retried.characters[1].inventory) 214 } 215 }) 216 } 217 }