aorecomp

Anarchy Online classic in the browser

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


server/npc_shops_test.go (7065B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"reflect"
      7 	"testing"
      8 )
      9 
     10 func TestNPCShopRoutesBuySellAndReload(t *testing.T) {
     11 	for _, route := range []string{"710/district/0/fixed/18", "710/district/0/fixed/19", "950/district/1/fixed/0", "952/district/3/fixed/0", "954/district/1/fixed/0"} {
     12 		t.Run(route, func(t *testing.T) {
     13 			world, conn := newEquipmentWorld(t)
     14 			player, client := world.characters[1], world.clients[1]
     15 			player.appearance = 0x515
     16 			npc := makeNPCSpawn(gamedata.NPCGroups[route], 1, player.position)
     17 			npc.id, npc.playfield = 1000001, player.playfield
     18 			world.characters[npc.id] = &npc
     19 			client.npcs = map[uint32]bool{npc.id: true}
     20 			player.inventory.Credits = 1000
     21 			if err := world.saveInventory(player, player.inventory); err != nil {
     22 				t.Fatal(err)
     23 			}
     24 
     25 			for _, request := range [][]byte{
     26 				shopTradePacket([2]uint32{50000, 1}, 0, [2]uint32{50000, npc.id}, [2]uint32{}),
     27 				shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64}),
     28 				shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, npc.id}, [2]uint32{111, 0}),
     29 				shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{}),
     30 			} {
     31 				if err := world.handleTrade(client, request[20:]); err != nil {
     32 					t.Fatal(err)
     33 				}
     34 			}
     35 
     36 			want := ownedItem{ID: 2, Type: 51005, Template: 25812, Quality: 1, Slot: 64, Count: 50}
     37 			credits := uint32(1327)
     38 			if gamedata.NPCGroups[route].ShopTemplate == 120973 {
     39 				want.Type, want.Template, want.Count = 51018, 130581, 1
     40 				credits = 1702
     41 			}
     42 			cold := newTestInventoryServer(t).characters[1].inventory
     43 			if client.shop != nil || !reflect.DeepEqual(player.inventory.Items, []ownedItem{want}) || player.inventory.Credits != credits || !reflect.DeepEqual(cold, player.inventory) {
     44 				t.Fatalf("NPC transaction or cold reload differs: live=%+v cold=%+v", player.inventory, cold)
     45 			}
     46 			if len(conn.packets) < 4 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x7f544905 ||
     47 				binary.BigEndian.Uint32(conn.packets[0][20:24]) != 51035 || binary.BigEndian.Uint32(conn.packets[0][24:28]) != npc.id ||
     48 				binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x58362220 || binary.BigEndian.Uint32(conn.packets[1][24:28]) != npc.id {
     49 				t.Fatal("NPC trade did not create its private vending catalog")
     50 			}
     51 		})
     52 	}
     53 }
     54 
     55 func TestNPCShopRevalidatesSellerOnStartAndAccept(t *testing.T) {
     56 	for _, stage := range []string{"start", "accept"} {
     57 		for _, invalid := range []string{"unseen", "dead", "other zone", "outside range", "unbound", "removed", "player"} {
     58 			t.Run(stage+"/"+invalid, func(t *testing.T) {
     59 				world, _ := newEquipmentWorld(t)
     60 				player, client := world.characters[1], world.clients[1]
     61 				npc := makeNPCSpawn(gamedata.NPCGroups["950/district/1/fixed/0"], 1, player.position)
     62 				npc.id, npc.playfield = 1000001, player.playfield
     63 				world.characters[npc.id] = &npc
     64 				client.npcs = map[uint32]bool{npc.id: true}
     65 				before := player.inventory
     66 				request := shopTradePacket([2]uint32{50000, 1}, 0, [2]uint32{50000, npc.id}, [2]uint32{})
     67 				if stage == "accept" {
     68 					if err := world.handleTrade(client, request[20:]); err != nil || client.shop == nil {
     69 						t.Fatalf("open NPC shop: %v", err)
     70 					}
     71 					if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]); err != nil {
     72 						t.Fatal(err)
     73 					}
     74 					request = shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})
     75 				}
     76 				switch invalid {
     77 				case "unseen":
     78 					delete(client.npcs, npc.id)
     79 				case "dead":
     80 					npc.health = 0
     81 				case "other zone":
     82 					npc.playfield++
     83 				case "outside range":
     84 					npc.position[0] += 6
     85 				case "unbound":
     86 					npc.npcDefinitionID = "950/district/2/group/0/authored_point"
     87 				case "removed":
     88 					delete(world.characters, npc.id)
     89 				case "player":
     90 					npc.npc = false
     91 				}
     92 
     93 				if err := world.handleTrade(client, request[20:]); err != nil {
     94 					t.Fatal(err)
     95 				}
     96 
     97 				if client.shop != nil || client.trade != nil || !reflect.DeepEqual(player.inventory, before) ||
     98 					!reflect.DeepEqual(newTestInventoryServer(t).characters[1].inventory, before) {
     99 					t.Fatal("unavailable NPC accepted a trade or changed ownership")
    100 				}
    101 			})
    102 		}
    103 	}
    104 }
    105 
    106 func TestNPCShopKeyboardUseAndCancellation(t *testing.T) {
    107 	world, conn := newEquipmentWorld(t)
    108 	player, client := world.characters[1], world.clients[1]
    109 	npc := makeNPCSpawn(gamedata.NPCGroups["950/district/1/fixed/0"], 1, player.position)
    110 	npc.id, npc.playfield = 1000001, player.playfield
    111 	world.characters[npc.id] = &npc
    112 	client.npcs = map[uint32]bool{npc.id: true}
    113 	before := player.inventory
    114 	request := shopUseRequest(1)
    115 	binary.BigEndian.PutUint32(request[28:32], 0)
    116 	binary.BigEndian.PutUint32(request[40:44], 50000)
    117 	binary.BigEndian.PutUint32(request[44:48], npc.id)
    118 
    119 	if err := world.handleShopUse(client, request); err != nil || client.shop == nil {
    120 		t.Fatalf("native E Use did not open NPC catalog: %v", err)
    121 	}
    122 	if binary.BigEndian.Uint32(conn.packets[0][29:33]) != 1 {
    123 		t.Fatal("NPC Use was not acknowledged")
    124 	}
    125 	for _, packet := range [][]byte{
    126 		shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64}),
    127 		shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{}),
    128 	} {
    129 		if err := world.handleTrade(client, packet[20:]); err != nil {
    130 			t.Fatal(err)
    131 		}
    132 	}
    133 
    134 	if client.shop != nil || !reflect.DeepEqual(player.inventory, before) ||
    135 		!reflect.DeepEqual(newTestInventoryServer(t).characters[1].inventory, before) {
    136 		t.Fatal("NPC shop cancellation changed the offered item's identity or credits")
    137 	}
    138 }
    139 
    140 func TestNPCShopMovementCancelsOutsideRange(t *testing.T) {
    141 	world, _ := newEquipmentWorld(t)
    142 	player, client := world.characters[1], world.clients[1]
    143 	npc := makeNPCSpawn(gamedata.NPCGroups["950/district/1/fixed/0"], 1, player.position)
    144 	npc.id, npc.playfield = 1000001, player.playfield
    145 	world.characters[npc.id] = &npc
    146 	client.npcs = map[uint32]bool{1000001: true}
    147 	before := player.inventory
    148 	for _, packet := range [][]byte{
    149 		shopTradePacket([2]uint32{50000, 1}, 0, [2]uint32{50000, 1000001}, [2]uint32{}),
    150 		shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64}),
    151 	} {
    152 		if err := world.handleTrade(client, packet[20:]); err != nil {
    153 			t.Fatal(err)
    154 		}
    155 	}
    156 	if client.shop == nil || len(client.shop.items) != 1 {
    157 		t.Fatal("NPC sale draft was not prepared")
    158 	}
    159 
    160 	if err := world.handleMovement(client, playerMovementRequest(&character{id: 1, position: [3]float32{4, 0, 0}, rotation: [4]float32{0, 0, 0, 1}}, 4)); err != nil || client.shop == nil {
    161 		t.Fatalf("movement within range cancelled the shop: %v", err)
    162 	}
    163 	if err := world.handleMovement(client, playerMovementRequest(&character{id: 1, position: [3]float32{6, 0, 0}, rotation: [4]float32{0, 0, 0, 1}}, 4)); err != nil {
    164 		t.Fatal(err)
    165 	}
    166 
    167 	if client.shop != nil || !reflect.DeepEqual(player.inventory, before) ||
    168 		!reflect.DeepEqual(newTestInventoryServer(t).characters[1].inventory, before) {
    169 		t.Fatal("movement retained an unreachable draft or lost its offered item")
    170 	}
    171 }