server/shop_expansion_test.go (3948B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "slices" 9 "testing" 10 ) 11 12 func TestExpandedShopCoverageAndMerchantBindings(t *testing.T) { 13 for id, machine := range gamedata.ShopMachines { 14 // Original scene dependency markers are not usable shops. 15 if machine.Template == 41325 { 16 continue 17 } 18 if len(gamedata.ShopStock[machine.Template]) == 0 { 19 t.Fatalf("unstocked original shop %d: %s", id, gamedata.ItemTemplates[machine.Template].Name) 20 } 21 } 22 for template, stock := range gamedata.ShopStock { 23 seen := map[gamedata.ShopItem]bool{} 24 for _, item := range stock { 25 if seen[item] { 26 t.Fatalf("duplicate stock row in %d: %+v", template, item) 27 } 28 seen[item] = true 29 } 30 } 31 for id, group := range gamedata.NPCGroups { 32 if group.ShopTemplate != 0 && itemStat(gamedata.ItemTemplates[group.ShopTemplate], 30)&8 == 0 { 33 t.Fatal("merchant binding selects a template that cannot be used", id) 34 } 35 } 36 for id, template := range map[string]uint32{ 37 "127/district/0/fixed/0": 99637, "127/district/0/fixed/1": 99572, 38 "127/district/0/fixed/2": 99570, "127/district/0/fixed/3": 99574, 39 "127/district/0/fixed/4": 99601, "127/district/0/fixed/5": 99634, 40 "610/district/1/fixed/46": 164104, "560/district/18/fixed/4": 205737, 41 "570/district/15/fixed/0": 162374, 42 } { 43 if gamedata.NPCGroups[id].ShopTemplate != template || len(gamedata.ShopStock[template]) < 5 { 44 t.Fatal("merchant lacks its role-specific catalog", id) 45 } 46 } 47 } 48 49 func TestExpandedNPCShopPurchasesKeepOriginalTypesAndColdOwnership(t *testing.T) { 50 for _, scenario := range []struct { 51 route string 52 item gamedata.ShopItem 53 kind uint32 54 }{ 55 {"127/district/0/fixed/5", gamedata.ShopItem{Template: 99228, Quality: 1}, 51017}, 56 {"610/district/1/fixed/46", gamedata.ShopItem{Template: 163993, Quality: 1}, 51018}, 57 {"560/district/18/fixed/4", gamedata.ShopItem{Template: 205738, Quality: 20}, 51022}, 58 } { 59 t.Run(fmt.Sprint(scenario.item.Template), func(t *testing.T) { 60 world, conn := newEquipmentWorld(t) 61 player, client := world.characters[1], world.clients[1] 62 player.inventory.Credits = 100000000 63 if scenario.route == "610/district/1/fixed/46" { 64 // Smith's original shop requires level 50. 65 player.level = 50 66 } 67 npc := makeNPCSpawn(gamedata.NPCGroups[scenario.route], 1, player.position) 68 npc.id, npc.playfield = 1000001, player.playfield 69 world.characters[npc.id] = &npc 70 client.npcs = map[uint32]bool{npc.id: true} 71 if err := world.saveInventory(player, player.inventory); err != nil { 72 t.Fatal(err) 73 } 74 75 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 0, [2]uint32{50000, npc.id}, [2]uint32{})[20:]); err != nil || client.shop == nil { 76 t.Fatal("new YAML-bound merchant did not open", err) 77 } 78 index := slices.Index(client.shop.stock, scenario.item) 79 if index < 0 { 80 t.Fatal("documented specialty item absent", scenario.item) 81 } 82 conn.packets = nil 83 for _, request := range [][]byte{ 84 shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, npc.id}, [2]uint32{111, uint32(index)}), 85 shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{}), 86 } { 87 if err := world.handleTrade(client, request[20:]); err != nil { 88 t.Fatal(err) 89 } 90 } 91 cold := newTestInventoryServer(t).characters[1].inventory 92 93 want := ownedItem{ID: 2, Type: scenario.kind, Template: scenario.item.Template, Quality: scenario.item.Quality, Slot: 65, Count: 1} 94 if len(cold.Items) != 2 || cold.Items[1] != want || !reflect.DeepEqual(cold, player.inventory) || cold.Credits >= 100000000 || client.shop != nil { 95 t.Fatal("expanded stock purchase lost its type, ownership or payment", cold) 96 } 97 if scenario.kind == 51017 && !slices.ContainsFunc(conn.packets, func(packet []byte) bool { return binary.BigEndian.Uint32(packet[16:20]) == 0x465a5d73 }) { 98 t.Fatal("purchased backpack did not use portable-container delivery") 99 } 100 }) 101 } 102 }