aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/shops_test.go (19918B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"reflect"
      9 	"sync"
     10 	"testing"
     11 )
     12 
     13 func TestShopSalePricesUseNativeQualityChargesAndLiteracyRounding(t *testing.T) {
     14 	for _, scenario := range []struct {
     15 		template uint32
     16 		quality  uint32
     17 		count    uint16
     18 		literacy uint16
     19 		credits  uint32
     20 	}{
     21 		{209283, 1, 1, 39, 605},
     22 		{209283, 1, 1, 40, 611},
     23 		{209283, 1, 1, 80, 617},
     24 		{209283, 1, 1, 4000, 1209},
     25 		{43552, 1, 1, 40, 25},
     26 		{25812, 1, 50, 40, 7},
     27 		{25812, 1, 1, 40, 0},
     28 		{25812, 1, 49, 40, 7},
     29 		{85366, 1, 25, 40, 1},
     30 		{21605, 1, 100, 40, 1},
     31 		{85512, 1, 1, 40, 31},
     32 		{85512, 2, 1, 40, 32},
     33 		{85512, 100, 1, 40, 7457},
     34 		{85512, 101, 1, 40, 7608},
     35 		{85512, 199, 1, 40, 29737},
     36 		{85511, 200, 1, 40, 30038},
     37 	} {
     38 		t.Run(fmt.Sprintf("%d/QL%d/CL%d", scenario.template, scenario.quality, scenario.literacy), func(t *testing.T) {
     39 			world, _ := newShopWorld(t)
     40 			player, client := world.characters[1], world.clients[1]
     41 			player.stats.Abilities.Intelligence = 5
     42 			player.stats.TradeRepair.ComputerLiteracy = scenario.literacy - 1
     43 			player.inventory.Items[0] = ownedItem{ID: 1, Type: gamedata.ItemTemplates[scenario.template].HeaderWords[0], Template: scenario.template, Quality: scenario.quality, Slot: 64, Count: scenario.count}
     44 			if err := world.saveInventory(player, player.inventory); err != nil {
     45 				t.Fatal(err)
     46 			}
     47 
     48 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
     49 				t.Fatal(err)
     50 			}
     51 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
     52 				t.Fatal(err)
     53 			}
     54 
     55 			if player.inventory.Credits != 100+scenario.credits || len(player.inventory.Items) != 0 {
     56 				t.Fatalf("sale credited %d, want %d", player.inventory.Credits-100, scenario.credits)
     57 			}
     58 		})
     59 	}
     60 }
     61 
     62 func shopUseRequest(sequence uint32) []byte {
     63 	var body []byte
     64 	for _, value := range []uint32{0, sequence, 3, 1, 50000, 1, 51035, 0xc00003b6} {
     65 		body = binary.BigEndian.AppendUint32(body, value)
     66 	}
     67 	return n3Packet(0x52526858, 50000, 1, body)[20:]
     68 }
     69 
     70 func newShopWorld(t *testing.T) (*server, *combatTestConn) {
     71 	t.Helper()
     72 	world, conn := newEquipmentWorld(t)
     73 	player := world.characters[1]
     74 	player.position, player.appearance = [3]float32{60, 14.75, 53}, 0x515
     75 	player.inventory.Credits = 100
     76 	if err := world.saveUser(player); err != nil {
     77 		t.Fatal(err)
     78 	}
     79 	if err := world.savePosition(player); err != nil {
     80 		t.Fatal(err)
     81 	}
     82 	if err := world.saveInventory(player, player.inventory); err != nil {
     83 		t.Fatal(err)
     84 	}
     85 	if err := world.handleShopUse(world.clients[1], shopUseRequest(1)); err != nil {
     86 		t.Fatal(err)
     87 	}
     88 	conn.packets = nil
     89 	return world, conn
     90 }
     91 
     92 func TestShopNativeSocketUseAndSale(t *testing.T) {
     93 	newShopWorld(t)
     94 	world := newTestInventoryServer(t)
     95 	startTestService(t, world)
     96 	connection := connectZoningClient(t, world, true)
     97 	defer connection.Close()
     98 
     99 	request := shopUseRequest(1)
    100 	connection.Write(targetPacket(n3Packet(0x52526858, 50000, 1, request[16:])))
    101 	for index, key := range []uint32{0x52526858, 0x7f544905, 0x58362220, 0x36284f6e, 0x36284f6e} {
    102 		packet, err := readPacket(connection)
    103 		if err != nil {
    104 			t.Fatal(err)
    105 		}
    106 		if binary.BigEndian.Uint32(packet[16:20]) != key {
    107 			t.Fatalf("native shop opening sent %x, want key %x", packet, key)
    108 		}
    109 		if key == 0x52526858 && binary.BigEndian.Uint32(packet[29:33]) != 1 {
    110 			t.Fatalf("native shop use was rejected: %x", packet)
    111 		}
    112 		if key == 0x58362220 && (binary.BigEndian.Uint32(packet[20:24]) != 51035 || binary.BigEndian.Uint32(packet[24:28]) != 0xc00003b6 ||
    113 			len(packet) != 33+12*len(gamedata.ShopStock[100035]) || binary.BigEndian.Uint32(packet[29:33]) != uint32(len(gamedata.ShopStock[100035])+1)*1009) {
    114 			t.Fatalf("starter shop catalog has the wrong owner or stock count: %x", packet)
    115 		}
    116 		if index == 3 && (binary.BigEndian.Uint32(packet[20:24]) != 51035 || binary.BigEndian.Uint32(packet[24:28]) != 0xc00003b6 ||
    117 			binary.BigEndian.Uint32(packet[37:41]) != 50000 || binary.BigEndian.Uint32(packet[41:45]) != 1) {
    118 			t.Fatalf("machine trade record was not initialized before the window: %x", packet)
    119 		}
    120 		if index == 4 && (binary.BigEndian.Uint32(packet[20:24]) != 50000 || binary.BigEndian.Uint32(packet[24:28]) != 1 ||
    121 			binary.BigEndian.Uint32(packet[37:41]) != 51035 || binary.BigEndian.Uint32(packet[41:45]) != 0xc00003b6 ||
    122 			binary.BigEndian.Uint32(packet[45:49]) != 111 || binary.BigEndian.Uint32(packet[49:53]) != 0) {
    123 			t.Fatalf("player trade window has the wrong partner: %x", packet)
    124 		}
    125 	}
    126 	connection.Write(targetPacket(shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})))
    127 	if _, err := readPacket(connection); err != nil {
    128 		t.Fatal(err)
    129 	}
    130 	connection.Write(targetPacket(shopTradePacket([2]uint32{50000, 1}, 6, [2]uint32{}, [2]uint32{})))
    131 	reset, err := readPacket(connection)
    132 	if err != nil || binary.BigEndian.Uint32(reset[33:37]) != 6 {
    133 		t.Fatalf("native zero-cash reset aborted the shop: %x %v", reset, err)
    134 	}
    135 	connection.Write(targetPacket(shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})))
    136 	complete, err := readPacket(connection)
    137 	if err != nil {
    138 		t.Fatal(err)
    139 	}
    140 	cash, err := readPacket(connection)
    141 	if err != nil {
    142 		t.Fatal(err)
    143 	}
    144 	restored := newTestInventoryServer(t)
    145 
    146 	if binary.BigEndian.Uint32(complete[16:20]) != 0x36284f6e || binary.BigEndian.Uint32(complete[33:37]) != 3 ||
    147 		binary.BigEndian.Uint32(cash[16:20]) != 0x2b333d6e || binary.BigEndian.Uint32(cash[33:37]) != 705 ||
    148 		restored.characters[1].inventory.Credits != 705 || len(restored.characters[1].inventory.Items) != 0 {
    149 		t.Fatalf("native socket sale did not commit before acknowledgement: complete=%x cash=%x", complete, cash)
    150 	}
    151 }
    152 
    153 func TestShopInvalidOffersLeaveCashAndInventoryUnchanged(t *testing.T) {
    154 	for _, scenario := range []string{"NoDrop", "backpack", "foreign owner", "equipped", "wrong page", "missing item", "purchase", "cash offer", "unsolicited complete", "wrong version", "wrong subject"} {
    155 		t.Run(scenario, func(t *testing.T) {
    156 			world, _ := newShopWorld(t)
    157 			client, player := world.clients[1], world.characters[1]
    158 			body := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]
    159 			switch scenario {
    160 			case "NoDrop":
    161 				player.inventory.Items[0] = ownedItem{ID: 1, Type: 51018, Template: 121567, Quality: 1, Slot: 64, Count: 1}
    162 			case "backpack":
    163 				player.inventory.Items[0] = ownedItem{ID: 1, Type: 51017, Template: 99228, Quality: 1, Slot: 64, Count: 1}
    164 			case "foreign owner":
    165 				binary.BigEndian.PutUint32(body[28:32], 2)
    166 			case "equipped":
    167 				binary.BigEndian.PutUint32(body[36:40], 6)
    168 			case "wrong page":
    169 				binary.BigEndian.PutUint32(body[32:36], 102)
    170 			case "missing item":
    171 				binary.BigEndian.PutUint32(body[36:40], 65)
    172 			case "purchase":
    173 				binary.BigEndian.PutUint32(body[24:28], 51035)
    174 				binary.BigEndian.PutUint32(body[28:32], 0xc00003b6)
    175 				binary.BigEndian.PutUint32(body[32:36], 111)
    176 			case "cash offer":
    177 				binary.BigEndian.PutUint32(body[20:24], 6)
    178 				binary.BigEndian.PutUint32(body[24:28], 0)
    179 				binary.BigEndian.PutUint32(body[28:32], 1)
    180 				binary.BigEndian.PutUint32(body[32:36], 0)
    181 				binary.BigEndian.PutUint32(body[36:40], 0)
    182 			case "unsolicited complete":
    183 				binary.BigEndian.PutUint32(body[20:24], 3)
    184 			case "wrong version":
    185 				binary.BigEndian.PutUint32(body[16:20], 2)
    186 			case "wrong subject":
    187 				binary.BigEndian.PutUint32(body[8:12], 2)
    188 			}
    189 			if err := world.saveInventory(player, player.inventory); err != nil {
    190 				t.Fatal(err)
    191 			}
    192 			before, err := readTestItemStore(world)
    193 			if err != nil {
    194 				t.Fatal(err)
    195 			}
    196 
    197 			err = world.handleTrade(client, body)
    198 			after, readErr := readTestItemStore(world)
    199 
    200 			if err != nil || readErr != nil || !bytes.Equal(before, after) || client.shop != nil && len(client.shop.items) != 0 {
    201 				t.Fatal("invalid offer changed ownership, credits or basket")
    202 			}
    203 		})
    204 	}
    205 }
    206 
    207 func TestShopNativeSalePersistsAndCannotBeReplayed(t *testing.T) {
    208 	world, conn := newShopWorld(t)
    209 	client := world.clients[1]
    210 	add := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]
    211 	accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]
    212 
    213 	if err := world.handleTrade(client, add); err != nil {
    214 		t.Fatal(err)
    215 	}
    216 	world.handleTrade(client, add)
    217 	staged := newTestInventoryServer(t).characters[1].inventory
    218 	if err := world.handleTrade(client, accept); err != nil {
    219 		t.Fatal(err)
    220 	}
    221 	world.handleTrade(client, accept)
    222 	world.handleTrade(client, add)
    223 	restored := newTestInventoryServer(t)
    224 
    225 	if staged.Credits != 100 || len(staged.Items) != 1 {
    226 		t.Fatal("staging changed durable ownership or cash")
    227 	}
    228 	if restored.characters[1].inventory.Credits != 705 || len(restored.characters[1].inventory.Items) != 0 || len(restored.items) != 0 ||
    229 		world.characters[1].inventory.Credits != 705 || client.shop != nil {
    230 		t.Fatal("sale did not remove exactly one item and award 605 credits once")
    231 	}
    232 	if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[1][33:37]) != 3 ||
    233 		binary.BigEndian.Uint32(conn.packets[2][29:33]) != 61 || binary.BigEndian.Uint32(conn.packets[2][33:37]) != 705 {
    234 		t.Fatalf("wrong native sale acknowledgement or absolute credit update: %x", conn.packets)
    235 	}
    236 }
    237 
    238 func TestShopCancelAndRemovePreserveTheOwnedItem(t *testing.T) {
    239 	for _, action := range []uint32{2, 5} {
    240 		t.Run(fmt.Sprint(action), func(t *testing.T) {
    241 			world, conn := newShopWorld(t)
    242 			client := world.clients[1]
    243 			before := world.characters[1].inventory
    244 			world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:])
    245 
    246 			first, second := [2]uint32{}, [2]uint32{}
    247 			if action == 5 {
    248 				first, second = [2]uint32{50000, 1}, [2]uint32{108, 0}
    249 			}
    250 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, action, first, second)[20:]); err != nil {
    251 				t.Fatal(err)
    252 			}
    253 			if action == 5 {
    254 				world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:])
    255 			}
    256 			restored := newTestInventoryServer(t).characters[1].inventory
    257 
    258 			if before.Credits != restored.Credits || !reflect.DeepEqual(before.Items, restored.Items) || !reflect.DeepEqual(before, world.characters[1].inventory) || client.shop != nil || len(conn.packets) < 2 {
    259 				t.Fatal("cancelled or removed offer changed ownership or credits")
    260 			}
    261 		})
    262 	}
    263 }
    264 
    265 func TestShopRemovingAnOfferKeepsTheOtherTradeSlotsStable(t *testing.T) {
    266 	world, _ := newShopWorld(t)
    267 	client := world.clients[1]
    268 	if granted, err := world.grantItem(world.characters[1], 43552); err != nil || !granted {
    269 		t.Fatalf("grant clinic: %v %v", granted, err)
    270 	}
    271 	if granted, err := world.grantItem(world.characters[1], 43552); err != nil || !granted {
    272 		t.Fatalf("grant second clinic: %v %v", granted, err)
    273 	}
    274 	for _, slot := range []uint32{64, 65, 66} {
    275 		if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, slot})[20:]); err != nil {
    276 			t.Fatal(err)
    277 		}
    278 	}
    279 
    280 	for _, slot := range []uint32{0, 2} {
    281 		if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 5, [2]uint32{50000, 1}, [2]uint32{108, slot})[20:]); err != nil {
    282 			t.Fatal(err)
    283 		}
    284 	}
    285 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    286 		t.Fatal(err)
    287 	}
    288 	restored := newTestInventoryServer(t).characters[1].inventory
    289 
    290 	if restored.Credits != 125 || !reflect.DeepEqual(restored.Items, []ownedItem{
    291 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
    292 		{ID: 3, Type: 51005, Template: 43552, Quality: 1, Slot: 65, Count: 1},
    293 	}) {
    294 		t.Fatal("removing offers shifted the remaining trade slot or sold the wrong item")
    295 	}
    296 }
    297 
    298 func TestShopLostCompletionReplyDoesNotReplayTheSale(t *testing.T) {
    299 	world, _ := newShopWorld(t)
    300 	client := world.clients[1]
    301 	world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:])
    302 	client.conn = &failingCombatConn{}
    303 	accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]
    304 
    305 	err := world.handleTrade(client, accept)
    306 	world.handleTrade(client, accept)
    307 	restored := newTestInventoryServer(t).characters[1].inventory
    308 
    309 	if err == nil || restored.Credits != 705 || len(restored.Items) != 0 || client.shop != nil {
    310 		t.Fatal("lost acknowledgement lost or duplicated a committed sale")
    311 	}
    312 }
    313 
    314 func TestShopConcurrentAcceptAwardsCreditsOnce(t *testing.T) {
    315 	world, _ := newShopWorld(t)
    316 	client := world.clients[1]
    317 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
    318 		t.Fatal(err)
    319 	}
    320 	accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]
    321 	errors := make(chan error, 2)
    322 	shutdown := startTestService(t, world)
    323 	owner, err := world.world.owner(1)
    324 	if err != nil {
    325 		t.Fatal(err)
    326 	}
    327 
    328 	var requests sync.WaitGroup
    329 
    330 	for range 2 {
    331 		requests.Go(func() { errors <- owner.call(zonePacket{client: client, body: accept}) })
    332 	}
    333 	requests.Wait()
    334 	close(errors)
    335 	for err := range errors {
    336 		if err != nil {
    337 			t.Fatal(err)
    338 		}
    339 	}
    340 	if err := shutdown(); err != nil {
    341 		t.Fatal(err)
    342 	}
    343 	restored := newTestInventoryServer(t).characters[1].inventory
    344 
    345 	if restored.Credits != 705 || len(restored.Items) != 0 {
    346 		t.Fatal("concurrent accept duplicated or lost the sale")
    347 	}
    348 }
    349 
    350 func TestShopClanTerminalUsesItsAuthoredSaleModifier(t *testing.T) {
    351 	world, _ := newShopWorld(t)
    352 	player, client := world.characters[1], world.clients[1]
    353 	player.appearance, player.playfield, player.position = 0x315, 952, [3]float32{48, 14, 65}
    354 	if err := world.saveUser(player); err != nil {
    355 		t.Fatal(err)
    356 	}
    357 	if err := world.savePosition(player); err != nil {
    358 		t.Fatal(err)
    359 	}
    360 	request := shopUseRequest(2)
    361 	binary.BigEndian.PutUint32(request[44:48], 0xc00003b8)
    362 
    363 	if err := world.handleShopUse(client, request); err != nil {
    364 		t.Fatal(err)
    365 	}
    366 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
    367 		t.Fatal(err)
    368 	}
    369 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    370 		t.Fatal(err)
    371 	}
    372 	restored := newTestInventoryServer(t).characters[1].inventory
    373 
    374 	if restored.Credits != 584 || len(restored.Items) != 0 {
    375 		t.Fatal("Clan terminal did not award its authored four-percent sale price")
    376 	}
    377 }
    378 
    379 func TestShopEquippingAnOfferedItemCancelsTheSale(t *testing.T) {
    380 	world, conn := newShopWorld(t)
    381 	client := world.clients[1]
    382 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
    383 		t.Fatal(err)
    384 	}
    385 	conn.packets = nil
    386 
    387 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil {
    388 		t.Fatal(err)
    389 	}
    390 	if client.equipment == nil {
    391 		t.Fatal("equip did not start after cancelling the offer")
    392 	}
    393 	world.advanceEquipment(client.equipment.readyAt)
    394 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    395 		t.Fatal(err)
    396 	}
    397 	restored := newTestInventoryServer(t).characters[1].inventory
    398 
    399 	if client.shop != nil || restored.Credits != 100 || !reflect.DeepEqual(restored.Items, []ownedItem{
    400 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 6, Count: 1},
    401 	}) {
    402 		t.Fatal("offered equipment disappeared or was also sold")
    403 	}
    404 	if len(conn.packets) == 0 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x36284f6e || binary.BigEndian.Uint32(conn.packets[0][33:37]) != 2 {
    405 		t.Fatal("native offer was not restored before equipping")
    406 	}
    407 }
    408 
    409 func TestShopCommitFailureRollsBackCashAndOwnership(t *testing.T) {
    410 	for _, trigger := range []string{
    411 		"CREATE TRIGGER fail_sale BEFORE UPDATE OF credits ON players BEGIN SELECT RAISE(ABORT, 'cash failure'); END",
    412 		"CREATE TRIGGER fail_sale BEFORE DELETE ON items BEGIN SELECT RAISE(ABORT, 'item failure'); END",
    413 	} {
    414 		t.Run(trigger, func(t *testing.T) {
    415 			world, conn := newShopWorld(t)
    416 			client := world.clients[1]
    417 			world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:])
    418 			before, err := readTestItemStore(world)
    419 			if err != nil {
    420 				t.Fatal(err)
    421 			}
    422 			if _, err := world.db.Exec(trigger); err != nil {
    423 				t.Fatal(err)
    424 			}
    425 			conn.packets = nil
    426 
    427 			err = world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:])
    428 			after, readErr := readTestItemStore(world)
    429 
    430 			if err == nil || readErr != nil || !bytes.Equal(before, after) || len(conn.packets) != 0 ||
    431 				world.characters[1].inventory.Credits != 100 || len(world.characters[1].inventory.Items) != 1 {
    432 				t.Fatalf("failed sale changed RAM, disk or sent success: save=%v read=%v", err, readErr)
    433 			}
    434 		})
    435 	}
    436 }
    437 
    438 func TestShopUseRejectsIneligibleAndForgedRequests(t *testing.T) {
    439 	for _, scenario := range []string{"Clan", "Neutral", "wrong zone", "too far", "dead", "loading", "unready", "replaced", "wrong owner", "wrong caller", "wrong machine", "wrong type", "static item type", "reply", "wrong action", "11.0", "replay"} {
    440 		t.Run(scenario, func(t *testing.T) {
    441 			world, conn := newCombatWorld(t)
    442 			client, player := world.clients[1], world.characters[1]
    443 			player.position, player.appearance = [3]float32{60, 14.75, 53}, 0x515
    444 			body := shopUseRequest(1)
    445 			switch scenario {
    446 			case "Clan":
    447 				player.appearance = 0x315
    448 			case "Neutral":
    449 				player.appearance = 0x115
    450 			case "wrong zone":
    451 				player.playfield = 952
    452 			case "too far":
    453 				player.position[0] = 66
    454 			case "dead":
    455 				player.health = 0
    456 			case "loading":
    457 				client.loading = true
    458 			case "unready":
    459 				client.ready = false
    460 			case "replaced":
    461 				world.clients[1] = &worldClient{id: 1}
    462 			case "wrong owner":
    463 				binary.BigEndian.PutUint32(body[8:12], 2)
    464 			case "wrong caller":
    465 				binary.BigEndian.PutUint32(body[36:40], 2)
    466 			case "wrong machine":
    467 				binary.BigEndian.PutUint32(body[44:48], 0xc00003b8)
    468 			case "wrong type":
    469 				binary.BigEndian.PutUint32(body[40:44], 51059)
    470 			case "static item type":
    471 				binary.BigEndian.PutUint32(body[40:44], 51005)
    472 			case "reply":
    473 				binary.BigEndian.PutUint32(body[16:20], 1)
    474 			case "wrong action":
    475 				binary.BigEndian.PutUint32(body[24:28], 4)
    476 			case "11.0":
    477 				client.target = false
    478 			case "replay":
    479 				client.useSequence = 1
    480 			}
    481 
    482 			err := world.handleShopUse(client, body)
    483 
    484 			if err != nil || client.shop != nil || len(conn.packets) > 1 || len(conn.packets) == 1 && binary.BigEndian.Uint32(conn.packets[0][29:33]) != 2 {
    485 				t.Fatal("invalid use opened a shop")
    486 			}
    487 		})
    488 	}
    489 }
    490 
    491 func TestShopRejectsChangedOffersAndCreditOverflow(t *testing.T) {
    492 	for _, scenario := range []string{"moved", "changed count", "credit overflow", "too far", "dead", "faction changed"} {
    493 		t.Run(scenario, func(t *testing.T) {
    494 			world, _ := newShopWorld(t)
    495 			client, player := world.clients[1], world.characters[1]
    496 			world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:])
    497 			switch scenario {
    498 			case "moved":
    499 				player.inventory.Items[0].Slot = 65
    500 			case "changed count":
    501 				player.inventory.Items[0].Count = 2
    502 			case "credit overflow":
    503 				player.inventory.Credits = 999999999
    504 			case "too far":
    505 				player.position[0] = 70
    506 			case "dead":
    507 				player.health = 0
    508 			case "faction changed":
    509 				player.appearance = 0x315
    510 			}
    511 			before := player.inventory
    512 			before.Items = append([]ownedItem(nil), before.Items...)
    513 			if scenario == "moved" {
    514 				before.Items[0].Slot = 64
    515 			}
    516 
    517 			err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:])
    518 
    519 			if err != nil || !reflect.DeepEqual(before, player.inventory) || client.shop != nil {
    520 				t.Fatal("invalidated offer was committed")
    521 			}
    522 		})
    523 	}
    524 }