server/shop_quality_test.go (10485B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "testing" 9 ) 10 11 func TestShopBasicArmorProductionQualityPurchase(t *testing.T) { 12 for _, scenario := range []struct { 13 machine uint32 14 side uint16 15 credits uint32 16 }{ 17 {3221226655, 2, 657}, {3223389340, 1, 640}, {3222078626, 0, 640}, 18 } { 19 t.Run(fmt.Sprint(scenario.machine), func(t *testing.T) { 20 world, conn := newPurchaseWorld(t) 21 client, player := world.clients[1], world.characters[1] 22 machine := gamedata.ShopMachines[scenario.machine] 23 player.playfield, player.position = machine.Playfield, machine.Position 24 player.appearance = 0x115 | scenario.side<<9 25 request := shopUseRequest(2) 26 binary.BigEndian.PutUint32(request[44:48], scenario.machine) 27 28 if err := world.handleShopUse(client, request); err != nil { 29 t.Fatal(err) 30 } 31 32 if client.shop == nil || len(client.shop.stock) < 22 || !reflect.DeepEqual(client.shop.stock[:22], []gamedata.ShopItem{ 33 {Template: 85596, Quality: 5}, {Template: 85742, Quality: 5}, {Template: 85638, Quality: 5}, 34 {Template: 85512, Quality: 5}, {Template: 85557, Quality: 5}, {Template: 85686, Quality: 5}, 35 {Template: 70562, Quality: 1}, {Template: 70562, Quality: 2}, {Template: 70558, Quality: 1}, {Template: 70558, Quality: 2}, 36 {Template: 70559, Quality: 1}, {Template: 70559, Quality: 2}, {Template: 70560, Quality: 1}, {Template: 70560, Quality: 2}, 37 {Template: 70561, Quality: 1}, {Template: 70561, Quality: 2}, {Template: 70563, Quality: 1}, {Template: 70563, Quality: 2}, 38 {Template: 70564, Quality: 1}, {Template: 70564, Quality: 2}, {Template: 70565, Quality: 1}, {Template: 70565, Quality: 2}, 39 }) { 40 t.Fatal("basic armor lost its explicit selected-quality subset") 41 } 42 43 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, scenario.machine}, [2]uint32{111, 0})[20:]); err != nil { 44 t.Fatal(err) 45 } 46 conn.packets = nil 47 err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]) 48 49 if err != nil || player.inventory.Credits != scenario.credits || len(conn.packets) != 3 || 50 !reflect.DeepEqual(player.inventory.Items[1:], []ownedItem{{ID: 2, Type: 51022, Template: 85596, Quality: 5, Count: 1, Slot: 65}}) { 51 t.Fatal("production armor quote or delivery changed", err, player.inventory) 52 } 53 }) 54 } 55 } 56 57 func TestShopQualityCatalogResolvesBothStoredAnchors(t *testing.T) { 58 for _, anchor := range []uint32{85596, 85595} { 59 if !t.Run(fmt.Sprint(anchor), func(t *testing.T) { 60 world, conn := newPurchaseWorld(t) 61 original := gamedata.ShopStock[99491] 62 gamedata.ShopStock[99491] = []gamedata.ShopItem{{Template: anchor, Quality: 1}, {Template: anchor, Quality: 5}, {Template: anchor, Quality: 200}} 63 request := shopUseRequest(2) 64 binary.BigEndian.PutUint32(request[44:48], 3221488799) 65 conn.packets = nil 66 67 err := world.handleShopUse(world.clients[1], request) 68 gamedata.ShopStock[99491] = original 69 70 if err != nil || len(conn.packets) != 6 { 71 t.Fatal("quality catalog failed", err, len(conn.packets)) 72 } 73 catalog := conn.packets[3] 74 for index, expected := range []uint32{uint32(len(world.clients[1].shop.stock)+1) * 1009, 85596, 85596, 1, 85596, 85595, 5} { 75 if binary.BigEndian.Uint32(catalog[29+4*index:]) != expected { 76 t.Fatalf("anchor %d catalog field %d changed", anchor, index) 77 } 78 } 79 // The highest row may rotate out or vary within the original endpoint range. 80 if len(world.clients[1].shop.stock) == 3 { 81 quality := world.clients[1].shop.stock[2].Quality 82 low := uint32(85596) 83 if quality == 200 { 84 low = 85595 85 } 86 if quality < 180 || quality > 200 || binary.BigEndian.Uint32(catalog[57:61]) != low || binary.BigEndian.Uint32(catalog[61:65]) != 85595 || binary.BigEndian.Uint32(catalog[65:69]) != quality { 87 t.Fatal("rotated catalog lost its selected quality or original endpoints") 88 } 89 } 90 }) { 91 return 92 } 93 } 94 } 95 96 func TestShopQualityRowsKeepDistinctPricesAndDelivery(t *testing.T) { 97 world, conn := newPurchaseWorld(t) 98 client, player := world.clients[1], world.characters[1] 99 client.shop.stock = []gamedata.ShopItem{{Template: 85595, Quality: 1}, {Template: 85596, Quality: 5}, {Template: 85595, Quality: 6}} 100 player.inventory.Credits = 2000 101 if err := world.saveInventory(player, player.inventory); err != nil { 102 t.Fatal(err) 103 } 104 for _, row := range []uint32{2, 1, 0, 1} { 105 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221488799}, [2]uint32{111, row})[20:]); err != nil { 106 t.Fatal(err) 107 } 108 } 109 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 5, [2]uint32{51035, 3221488799}, [2]uint32{111, 1})[20:]); err != nil { 110 t.Fatal(err) 111 } 112 conn.packets = nil 113 114 err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]) 115 restored := newTestInventoryServer(t) 116 117 want := []ownedItem{ 118 {ID: 2, Type: 51022, Template: 85595, Quality: 1, Count: 1, Slot: 65}, 119 {ID: 3, Type: 51022, Template: 85596, Quality: 5, Count: 1, Slot: 66}, 120 {ID: 4, Type: 51022, Template: 85595, Quality: 6, Count: 1, Slot: 67}, 121 } 122 if err != nil || !reflect.DeepEqual(player.inventory.Items[1:], want) || !reflect.DeepEqual(restored.characters[1].inventory, player.inventory) || player.inventory.Credits != 1012 || world.nextItem != 4 { 123 t.Fatalf("quality rows lost price or ownership: %v %+v", err, player.inventory) 124 } 125 if len(conn.packets) != 5 { 126 t.Fatal("wrong delivery count", len(conn.packets)) 127 } 128 for index, fields := range [][4]uint32{{85596, 85596, 1, 1}, {85596, 85595, 5, 1}, {85596, 85595, 6, 1}} { 129 packet := conn.packets[index+1] 130 if binary.BigEndian.Uint32(packet[16:20]) != 0x052e2f0c { 131 t.Fatal("missing native template delivery") 132 } 133 for field, expected := range fields { 134 if binary.BigEndian.Uint32(packet[29+4*field:]) != expected { 135 t.Fatalf("delivery %d field %d changed", index, field) 136 } 137 } 138 } 139 } 140 141 func TestShopQualityBasketCannotSpendEndpointPrice(t *testing.T) { 142 world, conn := newPurchaseWorld(t) 143 client, player := world.clients[1], world.characters[1] 144 client.shop.stock = []gamedata.ShopItem{{Template: 85596, Quality: 5}} 145 player.inventory.Credits = 685 146 if err := world.saveInventory(player, player.inventory); err != nil { 147 t.Fatal(err) 148 } 149 conn.packets = nil 150 add := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221488799}, [2]uint32{111, 0})[20:] 151 152 for _, request := range [][]byte{add, add, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]} { 153 if err := world.handleTrade(client, request); err != nil { 154 t.Fatal(err) 155 } 156 } 157 restored := newTestInventoryServer(t) 158 159 if client.shop != nil || len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[1][33:37]) != 2 || player.inventory.Credits != 685 || len(player.inventory.Items) != 1 || world.nextItem != 1 || !reflect.DeepEqual(restored.characters[1].inventory, player.inventory) { 160 t.Fatal("basket used endpoint price or retained a rejected purchase") 161 } 162 } 163 164 func TestShopQualityPurchaseRollbackAndWearLifetime(t *testing.T) { 165 world, conn := newPurchaseWorld(t) 166 client, player := world.clients[1], world.characters[1] 167 client.shop.stock = []gamedata.ShopItem{{Template: 85596, Quality: 5}} 168 add := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221488799}, [2]uint32{111, 0})[20:] 169 if err := world.handleTrade(client, add); err != nil { 170 t.Fatal(err) 171 } 172 if _, err := world.db.Exec("CREATE TRIGGER fail_quality_purchase BEFORE INSERT ON items WHEN NEW.quality=5 BEGIN SELECT RAISE(ABORT, 'quality purchase failure'); END"); err != nil { 173 t.Fatal(err) 174 } 175 conn.packets = nil 176 accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:] 177 178 failed := world.handleTrade(client, accept) 179 180 if failed == nil || len(conn.packets) != 0 || len(player.inventory.Items) != 1 || player.inventory.Credits != 1000 || world.nextItem != 1 || client.shop == nil { 181 t.Fatal("failed quality purchase published a partial result") 182 } 183 184 if _, err := world.db.Exec("DROP TRIGGER fail_quality_purchase"); err != nil { 185 t.Fatal(err) 186 } 187 if err := world.handleTrade(client, accept); err != nil { 188 t.Fatal(err) 189 } 190 player.stats.Abilities.Strength, player.stats.Abilities.Agility = 16, 18 191 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 24)[20:]); err != nil { 192 t.Fatal(err) 193 } 194 195 if client.equipment != nil || player.inventory.Items[1].Slot != 65 || player.inventory.Credits != 657 { 196 t.Fatal("purchase bypassed interpolated requirements or lost its rejected item") 197 } 198 199 player.stats.Abilities.Strength = 17 200 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 24)[20:]); err != nil { 201 t.Fatal(err) 202 } 203 if client.equipment == nil { 204 t.Fatal("qualified purchased armor rejected") 205 } 206 world.advanceEquipment(client.equipment.readyAt) 207 restored := newTestInventoryServer(t) 208 209 want := ownedItem{ID: 3, Type: 51022, Template: 85596, Quality: 5, Count: 1, Slot: 24} 210 if restored.characters[1].inventory.Items[1] != want || restored.characters[1].inventory.Credits != 657 || world.nextItem != 3 { 211 t.Fatal("purchased quality or worn ownership did not survive restoration") 212 } 213 for index, expected := range []int32{6, 5, 6, 5, 2, 2, 2, 2} { 214 if restored.characters[1].inventory.equipmentBonus(90+uint32(index)) != expected { 215 t.Fatal("purchased armor lost its selected quality effects", index) 216 } 217 } 218 219 if err := world.handleInventory(client, itemMovePacket(1, 24, 111)[20:]); err != nil { 220 t.Fatal(err) 221 } 222 if client.equipment == nil { 223 t.Fatal("purchased armor removal rejected") 224 } 225 world.advanceEquipment(client.equipment.readyAt) 226 request := shopUseRequest(2) 227 binary.BigEndian.PutUint32(request[44:48], 3221488799) 228 if err := world.handleShopUse(client, request); err != nil { 229 t.Fatal(err) 230 } 231 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:]); err != nil { 232 t.Fatal(err) 233 } 234 if err := world.handleTrade(client, accept); err != nil { 235 t.Fatal(err) 236 } 237 sold := newTestInventoryServer(t) 238 239 if len(sold.characters[1].inventory.Items) != 1 || sold.characters[1].inventory.Credits != 677 || world.nextItem != 3 { 240 t.Fatal("sale lost selected-quality price or retained sold ownership") 241 } 242 for stat := uint32(90); stat < 98; stat++ { 243 if sold.characters[1].inventory.equipmentBonus(stat) != 0 { 244 t.Fatal("sold armor retained its bonus") 245 } 246 } 247 }