aorecomp

Anarchy Online classic in the browser

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


server/plasma_test.go (13995B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"slices"
      8 	"sync"
      9 	"testing"
     10 )
     11 
     12 func newPlasmaWorld(t *testing.T) (*server, *combatTestConn) {
     13 	t.Helper()
     14 	world, conn := newEquipmentWorld(t)
     15 	player := world.characters[1]
     16 	// Ten in each ability contributes two Pharmacy points.
     17 	player.stats.TradeRepair.PharmaceuticalTechnology = 1
     18 	if err := world.saveUser(player); err != nil {
     19 		t.Fatal(err)
     20 	}
     21 	for _, template := range []uint32{154331, 42640} {
     22 		if granted, err := world.grantItem(player, template); err != nil || !granted {
     23 			t.Fatal("plasma input", granted, err)
     24 		}
     25 	}
     26 	conn.packets = nil
     27 	return world, conn
     28 }
     29 
     30 func TestPlasmaCombinationRejectsUnsafeInputsWithoutConsumingTool(t *testing.T) {
     31 	for _, scenario := range []string{"pharmacy", "reverse", "pelted parts", "ivory parts", "tool quality", "part quality", "tool count", "part count", "tool type"} {
     32 		t.Run(scenario, func(t *testing.T) {
     33 			world, conn := newPlasmaWorld(t)
     34 			player := world.characters[1]
     35 			request := combinationRequest(1, 65, 66)
     36 			switch scenario {
     37 			case "pharmacy":
     38 				player.stats.TradeRepair.PharmaceuticalTechnology = 0
     39 			case "reverse":
     40 				request = combinationRequest(1, 66, 65)
     41 			case "pelted parts":
     42 				player.inventory.Items[2].Template = 42639
     43 			case "ivory parts":
     44 				player.inventory.Items[2].Template = 42643
     45 			case "tool quality":
     46 				player.inventory.Items[1].Quality = 2
     47 			case "part quality":
     48 				player.inventory.Items[2].Quality = 2
     49 			case "tool count":
     50 				player.inventory.Items[1].Count = 2
     51 			case "part count":
     52 				player.inventory.Items[2].Count = 2
     53 			case "tool type":
     54 				player.inventory.Items[1].Type = 51022
     55 			}
     56 			items := slices.Clone(player.inventory.Items)
     57 			before, err := readTestItemStore(world)
     58 			if err != nil {
     59 				t.Fatal(err)
     60 			}
     61 
     62 			err = world.handleItemCombination(world.clients[1], request)
     63 			after, readErr := readTestItemStore(world)
     64 
     65 			if err != nil || readErr != nil || !bytes.Equal(before, after) || !slices.Equal(player.inventory.Items, items) || world.nextItem != 3 {
     66 				t.Fatal("rejected plasma conversion changed ownership or allocation", err, readErr)
     67 			}
     68 			if scenario == "pharmacy" {
     69 				if len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][33:37]) != 32 ||
     70 					binary.BigEndian.Uint32(conn.packets[0][41:45]) != 159 || binary.BigEndian.Uint32(conn.packets[0][45:49]) != 3 {
     71 					t.Fatal("Pharmacy failure did not report the effective QL1 threshold")
     72 				}
     73 			}
     74 		})
     75 	}
     76 }
     77 
     78 func TestPlasmaCombinationFailureRollsBackAndRetriesWithoutDeletingTool(t *testing.T) {
     79 	for _, failure := range []struct{ name, sql string }{
     80 		{"part", "CREATE TRIGGER fail_plasma BEFORE DELETE ON items WHEN OLD.id=3 BEGIN SELECT RAISE(FAIL,'part'); END"},
     81 		{"ignored part", "CREATE TRIGGER fail_plasma BEFORE DELETE ON items WHEN OLD.id=3 BEGIN SELECT RAISE(IGNORE); END"},
     82 		{"part placement", "CREATE TRIGGER fail_plasma BEFORE DELETE ON item_locations WHEN OLD.item_id=3 BEGIN SELECT RAISE(FAIL,'part placement'); END"},
     83 		{"ignored part placement", "CREATE TRIGGER fail_plasma BEFORE DELETE ON item_locations WHEN OLD.item_id=3 BEGIN SELECT RAISE(IGNORE); END"},
     84 		{"result", "CREATE TRIGGER fail_plasma BEFORE INSERT ON items WHEN NEW.id=4 BEGIN SELECT RAISE(FAIL,'result'); END"},
     85 		{"ignored result", "CREATE TRIGGER fail_plasma BEFORE INSERT ON items WHEN NEW.id=4 BEGIN SELECT RAISE(IGNORE); END"},
     86 		{"result placement", "CREATE TRIGGER fail_plasma BEFORE INSERT ON item_locations WHEN NEW.item_id=4 BEGIN SELECT RAISE(FAIL,'result placement'); END"},
     87 		{"ignored result placement", "CREATE TRIGGER fail_plasma BEFORE INSERT ON item_locations WHEN NEW.item_id=4 BEGIN SELECT RAISE(IGNORE); END"},
     88 		{"allocation", "CREATE TRIGGER fail_plasma BEFORE UPDATE OF last_item_id ON world BEGIN SELECT RAISE(FAIL,'allocation'); END"},
     89 		{"ignored allocation", "CREATE TRIGGER fail_plasma BEFORE UPDATE OF last_item_id ON world BEGIN SELECT RAISE(IGNORE); END"},
     90 		{"commit", "CREATE TABLE pending_plasma (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_plasma AFTER INSERT ON items WHEN NEW.id=4 BEGIN INSERT INTO pending_plasma VALUES(999999); END"},
     91 	} {
     92 		t.Run(failure.name, func(t *testing.T) {
     93 			world, conn := newPlasmaWorld(t)
     94 			if _, err := world.db.Exec("CREATE TRIGGER protect_tool BEFORE DELETE ON items WHEN OLD.id=2 BEGIN SELECT RAISE(FAIL,'reusable tool'); END; " + failure.sql); err != nil {
     95 				t.Fatal(err)
     96 			}
     97 			before, err := readTestItemStore(world)
     98 			if err != nil {
     99 				t.Fatal(err)
    100 			}
    101 			items := slices.Clone(world.characters[1].inventory.Items)
    102 
    103 			failed := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66))
    104 			after, err := readTestItemStore(world)
    105 
    106 			if failed == nil || err != nil || !bytes.Equal(before, after) || !slices.Equal(world.characters[1].inventory.Items, items) || world.nextItem != 3 || len(conn.packets) != 0 {
    107 				t.Fatal("failed plasma conversion leaked an item, consumption, allocation or acknowledgement", failed, err)
    108 			}
    109 			if _, err := world.db.Exec("DROP TRIGGER fail_plasma"); err != nil {
    110 				t.Fatal(err)
    111 			}
    112 			if err := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66)); err != nil {
    113 				t.Fatal("retry tried to delete the protected tool or failed to create plasma", err)
    114 			}
    115 			cold := newTestInventoryServer(t).characters[1].inventory.Items
    116 			if !slices.Contains(cold, ownedItem{ID: 2, Type: 51005, Template: 154331, Quality: 1, Slot: 65, Count: 1}) ||
    117 				!slices.Contains(cold, ownedItem{ID: 5, Type: 51005, Template: 154358, Quality: 1, Slot: 66, Count: 1}) {
    118 				t.Fatal("successful retry did not retain one tool and one plasma")
    119 			}
    120 		})
    121 	}
    122 }
    123 
    124 func TestPlasmaCombinationFullInventoryAndConcurrentReplayCreateOnce(t *testing.T) {
    125 	world, conn := newPlasmaWorld(t)
    126 	for range 27 {
    127 		if granted, err := world.grantItem(world.characters[1], 95577); err != nil || !granted {
    128 			t.Fatal("fill inventory", granted, err)
    129 		}
    130 	}
    131 	conn.packets = nil
    132 
    133 	client := world.clients[1]
    134 	shutdown := startTestService(t, world)
    135 	owner, err := world.world.owner(1)
    136 	if err != nil {
    137 		t.Fatal(err)
    138 	}
    139 
    140 	var requests sync.WaitGroup
    141 	for range 8 {
    142 		requests.Go(func() {
    143 			if err := owner.call(zonePacket{client: client, body: combinationRequest(1, 65, 66)}); err != nil {
    144 				t.Error(err)
    145 			}
    146 		})
    147 	}
    148 	requests.Wait()
    149 	if err := shutdown(); err != nil {
    150 		t.Fatal(err)
    151 	}
    152 	cold := newTestInventoryServer(t)
    153 
    154 	items := cold.characters[1].inventory.Items
    155 	if len(items) != 30 || cold.nextItem != 31 ||
    156 		!slices.Contains(items, ownedItem{ID: 2, Type: 51005, Template: 154331, Quality: 1, Slot: 65, Count: 1}) ||
    157 		!slices.Contains(items, ownedItem{ID: 31, Type: 51005, Template: 154358, Quality: 1, Slot: 66, Count: 1}) {
    158 		t.Fatal("full inventory lost its reusable tool or replay duplicated the plasma", items)
    159 	}
    160 	results, deletions := 0, 0
    161 	for _, packet := range conn.packets {
    162 		if binary.BigEndian.Uint32(packet[16:20]) == 0x052e2f0c {
    163 			results++
    164 		} else if binary.BigEndian.Uint32(packet[29:33]) == 112 {
    165 			deletions++
    166 		}
    167 	}
    168 	if results != 1 || deletions != 1 {
    169 		t.Fatal("concurrent requests published repeated consumption or result delivery", results, deletions)
    170 	}
    171 }
    172 
    173 func TestPlasmaCombinationLostReplyRetainsToolForAnotherPart(t *testing.T) {
    174 	world, _ := newPlasmaWorld(t)
    175 	world.clients[1].conn = &failingCombatConn{}
    176 
    177 	failed := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66))
    178 	cold := newTestInventoryServer(t)
    179 	cold.clients[1] = &worldClient{id: 1, conn: &combatTestConn{}, target: true, ready: true}
    180 	if err := cold.handleItemCombination(cold.clients[1], combinationRequest(1, 65, 66)); err != nil {
    181 		t.Fatal(err)
    182 	}
    183 	if granted, err := cold.grantItem(cold.characters[1], 42640); err != nil || !granted {
    184 		t.Fatal("next part", granted, err)
    185 	}
    186 	if err := cold.handleItemCombination(cold.clients[1], combinationRequest(1, 65, 67)); err != nil {
    187 		t.Fatal(err)
    188 	}
    189 	final := newTestInventoryServer(t).characters[1].inventory.Items
    190 
    191 	expected := []ownedItem{
    192 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
    193 		{ID: 2, Type: 51005, Template: 154331, Quality: 1, Slot: 65, Count: 1},
    194 		{ID: 4, Type: 51005, Template: 154358, Quality: 1, Slot: 66, Count: 1},
    195 		{ID: 6, Type: 51005, Template: 154358, Quality: 1, Slot: 67, Count: 1},
    196 	}
    197 	if failed == nil || !slices.Equal(final, expected) || cold.nextItem != 6 {
    198 		t.Fatal("lost reply or reuse lost the tool, reversed consumption or repeated plasma", failed, final)
    199 	}
    200 }
    201 
    202 func TestPlasmaProductionLootCraftAndSaleRetainToolAndKillRewards(t *testing.T) {
    203 	world, _ := newKillExperienceWorld(t, "unarmed")
    204 	player := world.characters[1]
    205 	world.combatRoll = func(uint32) uint32 { return 0 }
    206 	if granted, err := world.grantItem(player, 154331); err != nil || !granted {
    207 		t.Fatal("tool", granted, err)
    208 	}
    209 
    210 	attackForExperience(t, world, "unarmed")
    211 	if err := world.handleLootUse(world.clients[1], lootUseRequest(1)); err != nil {
    212 		t.Fatal(err)
    213 	}
    214 	if err := world.handleInventory(world.clients[1], lootTakeRequest()); err != nil {
    215 		t.Fatal(err)
    216 	}
    217 	if err := world.handleItemCombination(world.clients[1], combinationRequest(1, 64, 65)); err != nil {
    218 		t.Fatal(err)
    219 	}
    220 	world = newTestInventoryServer(t)
    221 	player = world.characters[1]
    222 	player.appearance, player.position = 0x515, [3]float32{60, 14.75, 53}
    223 	client := &worldClient{id: 1, conn: &combatTestConn{}, target: true, ready: true}
    224 	world.clients[1] = client
    225 	if err := world.handleShopUse(client, shopUseRequest(1)); err != nil {
    226 		t.Fatal(err)
    227 	}
    228 	for _, request := range [][]byte{
    229 		shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:],
    230 		shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:],
    231 	} {
    232 		if err := world.handleTrade(client, request); err != nil {
    233 			t.Fatal(err)
    234 		}
    235 	}
    236 	if !slices.Contains(newTestInventoryServer(t).characters[1].inventory.Items, ownedItem{ID: 5, Type: 51005, Template: 154358, Quality: 1, Slot: 65, Count: 1}) {
    237 		t.Fatal("cancelled sale lost crafted plasma")
    238 	}
    239 	if err := world.handleShopUse(client, shopUseRequest(2)); err != nil {
    240 		t.Fatal(err)
    241 	}
    242 	for _, request := range [][]byte{
    243 		shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:],
    244 		shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:],
    245 		shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:],
    246 	} {
    247 		if err := world.handleTrade(client, request); err != nil {
    248 			t.Fatal(err)
    249 		}
    250 	}
    251 	cold := newTestInventoryServer(t)
    252 
    253 	expected := []ownedItem{{ID: 3, Type: 51005, Template: 154331, Quality: 1, Slot: 64, Count: 1}}
    254 	if !slices.Equal(cold.characters[1].inventory.Items, expected) || cold.characters[1].inventory.Credits != 63 ||
    255 		cold.characters[1].experience != 100 || cold.nextItem != 5 || len(cold.characters[1000000].corpse.items) != 0 {
    256 		t.Fatal("loot/craft/sale lost the reusable tool or changed kill rewards", cold.characters[1].inventory, cold.characters[1].experience, cold.nextItem)
    257 	}
    258 }
    259 
    260 func TestPlasmaToolPurchaseUsesOriginalBasicPharmacyStock(t *testing.T) {
    261 	world, _ := newEquipmentWorld(t)
    262 	player, client := world.characters[1], world.clients[1]
    263 	machine := gamedata.ShopMachines[3225093279]
    264 	player.playfield, player.position = machine.Playfield, machine.Position
    265 	player.inventory.Credits = 420
    266 	if err := world.saveInventory(player, player.inventory); err != nil {
    267 		t.Fatal(err)
    268 	}
    269 	if granted, err := world.grantItem(player, 42640); err != nil || !granted {
    270 		t.Fatal("part", granted, err)
    271 	}
    272 	request := shopUseRequest(1)
    273 	binary.BigEndian.PutUint32(request[44:48], 3225093279)
    274 	if err := world.handleShopUse(client, request); err != nil {
    275 		t.Fatal(err)
    276 	}
    277 
    278 	for _, request := range [][]byte{
    279 		shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3225093279}, [2]uint32{111, 0})[20:],
    280 		shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:],
    281 	} {
    282 		if err := world.handleTrade(client, request); err != nil {
    283 			t.Fatal(err)
    284 		}
    285 	}
    286 	if err := world.handleItemCombination(client, combinationRequest(1, 66, 65)); err != nil {
    287 		t.Fatal(err)
    288 	}
    289 	cold := newTestInventoryServer(t)
    290 
    291 	if cold.characters[1].inventory.Credits != 0 || !slices.Contains(cold.characters[1].inventory.Items, ownedItem{ID: 3, Type: 51005, Template: 154331, Quality: 1, Slot: 66, Count: 1}) ||
    292 		!slices.Contains(cold.characters[1].inventory.Items, ownedItem{ID: 4, Type: 51005, Template: 154358, Quality: 1, Slot: 65, Count: 1}) {
    293 		t.Fatal("purchased tool did not retain its price, identity or reuse behavior", cold.characters[1].inventory)
    294 	}
    295 	for _, template := range []uint32{155288, 155289, 100034, 100035, 99481} {
    296 		if slices.Contains(gamedata.ShopStock[template], gamedata.ShopItem{Template: 154331, Quality: 1}) {
    297 			t.Fatal("basic pharmacy subset leaked into another stock family", template)
    298 		}
    299 	}
    300 }
    301 
    302 func TestPlasmaCombinationRetainsReusableToolAndOneColdResult(t *testing.T) {
    303 	world, conn := newPlasmaWorld(t)
    304 
    305 	if err := world.handleItemCombination(world.clients[1], combinationRequest(1, 65, 66)); err != nil {
    306 		t.Fatal(err)
    307 	}
    308 	cold := newTestInventoryServer(t)
    309 
    310 	expected := []ownedItem{
    311 		{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
    312 		{ID: 2, Type: 51005, Template: 154331, Quality: 1, Slot: 65, Count: 1},
    313 		{ID: 4, Type: 51005, Template: 154358, Quality: 1, Slot: 66, Count: 1},
    314 	}
    315 	for _, current := range []*server{world, cold} {
    316 		if !slices.Equal(current.characters[1].inventory.Items, expected) || current.nextItem != 4 || len(current.items) != 3 {
    317 			t.Fatal("plasma conversion lost the tool, unrelated item or durable result", current.characters[1].inventory.Items)
    318 		}
    319 	}
    320 	if len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 112 ||
    321 		binary.BigEndian.Uint32(conn.packets[0][37:41]) != 50 || binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x052e2f0c {
    322 		t.Fatal("native conversion deleted the reusable tool or omitted first-free result delivery")
    323 	}
    324 }