server/shop_stock_test.go (4864B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "testing" 9 ) 10 11 func TestShopDocumentedStockUsesTerminalBindings(t *testing.T) { 12 for _, scenario := range []struct { 13 machine uint32 14 stock []gamedata.ShopItem 15 }{ 16 {3221488799, []gamedata.ShopItem{{Template: 95577, Quality: 1}, {Template: 28564, Quality: 1}}}, 17 {3221816479, []gamedata.ShopItem{{Template: 161699, Quality: 1}}}, 18 {3225158815, []gamedata.ShopItem{{Template: 156054, Quality: 1}, {Template: 156339, Quality: 1}}}, 19 {3225093279, []gamedata.ShopItem{{Template: 154331, Quality: 1}}}, 20 } { 21 t.Run(fmt.Sprint(scenario.machine), func(t *testing.T) { 22 world, conn := newPurchaseWorld(t) 23 player, client := world.characters[1], world.clients[1] 24 machine := gamedata.ShopMachines[scenario.machine] 25 player.playfield, player.position = machine.Playfield, machine.Position 26 request := shopUseRequest(1) 27 binary.BigEndian.PutUint32(request[20:24], 2) 28 binary.BigEndian.PutUint32(request[44:48], scenario.machine) 29 conn.packets = nil 30 31 err := world.handleShopUse(client, request) 32 33 if err != nil || client.shop == nil || len(client.shop.stock) < len(scenario.stock) || !reflect.DeepEqual(client.shop.stock[:len(scenario.stock)], scenario.stock) { 34 t.Fatalf("wrong documented catalog: %v %+v", err, client.shop) 35 } 36 catalogs := 0 37 for _, packet := range conn.packets { 38 if binary.BigEndian.Uint32(packet[16:20]) != 0x58362220 { 39 continue 40 } 41 catalogs++ 42 if binary.BigEndian.Uint32(packet[24:28]) != scenario.machine || binary.BigEndian.Uint32(packet[29:33]) != uint32(len(client.shop.stock)+1)*1009 { 43 t.Fatal("catalog owner or row count changed") 44 } 45 for index, entry := range scenario.stock { 46 for field, expected := range []uint32{entry.Template, entry.Template, entry.Quality} { 47 if binary.BigEndian.Uint32(packet[33+12*index+4*field:]) != expected { 48 t.Fatal("catalog endpoint or quality changed") 49 } 50 } 51 } 52 } 53 if (len(scenario.stock) == 0 && catalogs != 0) || (len(scenario.stock) != 0 && catalogs != 1) { 54 t.Fatal("unknown stock was inferred or a known catalog was omitted") 55 } 56 }) 57 } 58 } 59 60 func TestShopDevicesPurchasePersistsUniqueInterface(t *testing.T) { 61 world, _ := newPurchaseWorld(t) 62 client, player := world.clients[1], world.characters[1] 63 player.position = gamedata.ShopMachines[3221816479].Position 64 request := shopUseRequest(1) 65 binary.BigEndian.PutUint32(request[20:24], 2) 66 binary.BigEndian.PutUint32(request[44:48], 3221816479) 67 if err := world.handleShopUse(client, request); err != nil { 68 t.Fatal(err) 69 } 70 add := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221816479}, [2]uint32{111, 0})[20:] 71 if err := world.handleTrade(client, add); err != nil { 72 t.Fatal(err) 73 } 74 75 err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]) 76 restored := newTestInventoryServer(t).characters[1] 77 binary.BigEndian.PutUint32(request[20:24], 3) 78 world.handleShopUse(client, request) 79 world.handleTrade(client, add) 80 world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]) 81 82 if err != nil || restored.inventory.Credits != 108 || len(restored.inventory.Items) != 2 || 83 restored.inventory.Items[1] != (ownedItem{ID: 2, Type: 51022, Template: 161699, Quality: 1, Slot: 65, Count: 1}) || 84 client.shop != nil || world.nextItem != 2 || !reflect.DeepEqual(restored.inventory, player.inventory) { 85 t.Fatal("interface purchase lost its type, price or Unique ownership") 86 } 87 } 88 89 func TestShopGeneralToolsPurchasesKeepDistinctRowsAndIdentities(t *testing.T) { 90 world, _ := newPurchaseWorld(t) 91 client, player := world.clients[1], world.characters[1] 92 player.position = gamedata.ShopMachines[3225158815].Position 93 player.inventory.Credits = 20000 94 if err := world.saveInventory(player, player.inventory); err != nil { 95 t.Fatal(err) 96 } 97 request := shopUseRequest(1) 98 binary.BigEndian.PutUint32(request[20:24], 2) 99 binary.BigEndian.PutUint32(request[44:48], 3225158815) 100 if err := world.handleShopUse(client, request); err != nil { 101 t.Fatal(err) 102 } 103 for _, index := range []uint32{1, 0} { 104 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3225158815}, [2]uint32{111, index})[20:]); err != nil { 105 t.Fatal(err) 106 } 107 } 108 109 err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]) 110 restored := newTestInventoryServer(t).characters[1].inventory 111 112 if err != nil || restored.Credits != 9306 || !reflect.DeepEqual(restored.Items[1:], []ownedItem{ 113 {ID: 2, Type: 51005, Template: 156054, Quality: 1, Slot: 65, Count: 1}, 114 {ID: 3, Type: 51005, Template: 156339, Quality: 1, Slot: 66, Count: 1}, 115 }) { 116 t.Fatalf("different catalog rows lost their prices or identities: %v %+v", err, restored) 117 } 118 }