aorecomp

Anarchy Online classic in the browser

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


server/kevlar_contracts_test.go (10519B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"slices"
      9 	"testing"
     10 )
     11 
     12 func TestKevlarGeneratedPickupKeepsSelectedQuality(t *testing.T) {
     13 	world, output := newKillExperienceWorld(t, "unarmed")
     14 	player, client := world.characters[1], world.clients[1]
     15 
     16 	player.stats.Abilities.Agility, player.stats.Abilities.Strength = 266, 241
     17 	if err := world.saveUser(player); err != nil {
     18 		t.Fatal(err)
     19 	}
     20 	// Controlled membership tests delivery, not historical Kevlar creature drops.
     21 	profile := *world.characters[1000000].combatProfile
     22 	profile.Loot = []gamedata.NPCLoot{{Template: 22178, Quality: [2]uint32{100, 100}, Chance: 100}}
     23 	world.characters[1000000].combatProfile = &profile
     24 	world.combatRoll = func(uint32) uint32 { return 0 }
     25 	identity := world.nextItem + 1
     26 	var wire []byte
     27 	for _, value := range []uint32{identity, 85598, 22178, 100, 0} {
     28 		wire = binary.BigEndian.AppendUint32(wire, value)
     29 	}
     30 
     31 	attackForExperience(t, world, "unarmed")
     32 	if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
     33 		t.Fatal(err)
     34 	}
     35 	shown := slices.ContainsFunc(output.packets, func(packet []byte) bool { return bytes.Contains(packet, wire) })
     36 	for range 2 {
     37 		if err := world.handleInventory(client, lootTakeRequest()); err != nil {
     38 			t.Fatal(err)
     39 		}
     40 	}
     41 	world = newTestInventoryServer(t)
     42 	client = &worldClient{id: 1, conn: output, target: true, ready: true}
     43 	world.clients[1] = client
     44 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 24)[20:]); err != nil || client.equipment == nil {
     45 		t.Fatal("picked-up gloves did not start wear", err)
     46 	}
     47 	world.advanceEquipment(client.equipment.readyAt)
     48 	worn := newTestInventoryServer(t)
     49 	if err := world.handleInventory(client, itemMovePacket(1, 24, 64)[20:]); err != nil || client.equipment == nil {
     50 		t.Fatal("picked-up gloves did not start removal", err)
     51 	}
     52 	world.advanceEquipment(client.equipment.readyAt)
     53 	removed := newTestInventoryServer(t)
     54 
     55 	expected := ownedItem{ID: identity, Type: 51022, Template: 22178, Quality: 100, Slot: 24, Count: 1}
     56 	if !shown || !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{expected}) || worn.characters[1].equipmentBonus(91) != 125 {
     57 		t.Fatal("generated pickup lost its native endpoints, identity, selected quality or worn AC")
     58 	}
     59 	expected.Slot = 64
     60 	if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{expected}) || removed.characters[1].equipmentBonus(91) != 0 ||
     61 		len(removed.characters[1000000].corpse.items) != 0 || removed.nextItem != identity {
     62 		t.Fatal("duplicate pickup, removal or cold recovery lost or duplicated the gloves")
     63 	}
     64 }
     65 
     66 func TestKevlarArmorQualitiesWearRemoveAndRecover(t *testing.T) {
     67 	for _, scenario := range []struct {
     68 		low, high, slot, quality, value uint32
     69 		agility, strength               uint16
     70 		armor                           [8]int32
     71 	}{
     72 		{85516, 22301, 27, 1, 875, 8, 8, [8]int32{3, 3, 3, 3, 1, 3, 3, 1}},
     73 		{85516, 22301, 27, 2, 896, 11, 10, [8]int32{6, 6, 6, 5, 2, 5, 5, 2}},
     74 		{85516, 22301, 27, 20, 8458, 57, 53, [8]int32{62, 62, 55, 40, 13, 40, 40, 13}},
     75 		{85516, 22301, 27, 100, 206760, 265, 240, [8]int32{312, 312, 273, 196, 63, 196, 196, 63}},
     76 		{85516, 22301, 27, 200, 832757, 525, 475, [8]int32{625, 625, 546, 390, 125, 390, 390, 125}},
     77 		{85598, 22178, 24, 1, 350, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     78 		{85598, 22178, 24, 2, 358, 11, 10, [8]int32{2, 2, 2, 2, 1, 2, 2, 1}},
     79 		{85598, 22178, 24, 20, 3383, 57, 53, [8]int32{25, 25, 22, 16, 6, 16, 16, 6}},
     80 		{85598, 22178, 24, 100, 82704, 265, 240, [8]int32{125, 125, 109, 78, 25, 78, 78, 25}},
     81 		{85598, 22178, 24, 101, 84376, 268, 243, [8]int32{126, 126, 110, 79, 26, 79, 79, 26}},
     82 		{85598, 22178, 24, 199, 329766, 522, 473, [8]int32{249, 249, 217, 155, 50, 155, 155, 50}},
     83 		{85598, 22178, 24, 200, 333102, 525, 475, [8]int32{250, 250, 218, 156, 50, 156, 156, 50}},
     84 		{85641, 22116, 30, 1, 525, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     85 		{85641, 22116, 30, 2, 537, 11, 10, [8]int32{3, 3, 3, 2, 1, 2, 2, 1}},
     86 		{85641, 22116, 30, 20, 5075, 57, 53, [8]int32{37, 37, 32, 23, 8, 23, 23, 8}},
     87 		{85641, 22116, 30, 100, 124055, 265, 240, [8]int32{187, 187, 164, 117, 38, 117, 117, 38}},
     88 		{85641, 22116, 30, 200, 499654, 525, 475, [8]int32{375, 375, 328, 234, 75, 234, 234, 75}},
     89 		{85691, 22004, 21, 1, 1400, 8, 8, [8]int32{5, 5, 5, 5, 1, 5, 5, 1}},
     90 		{85691, 22004, 21, 2, 1433, 11, 10, [8]int32{9, 9, 9, 7, 2, 7, 7, 2}},
     91 		{85691, 22004, 21, 20, 13533, 57, 53, [8]int32{88, 88, 76, 52, 18, 52, 52, 18}},
     92 		{85691, 22004, 21, 100, 330816, 265, 240, [8]int32{438, 438, 376, 251, 88, 251, 251, 88}},
     93 		{85691, 22004, 21, 200, 1332411, 525, 475, [8]int32{875, 875, 750, 500, 175, 500, 500, 175}},
     94 		{85692, 22026, 21, 1, 1400, 8, 8, [8]int32{5, 5, 5, 5, 1, 5, 5, 1}},
     95 		{85692, 22026, 21, 2, 1433, 11, 10, [8]int32{10, 10, 9, 8, 2, 8, 8, 2}},
     96 		{85692, 22026, 21, 20, 13533, 57, 53, [8]int32{100, 100, 88, 64, 20, 64, 64, 20}},
     97 		{85692, 22026, 21, 100, 330816, 265, 240, [8]int32{500, 500, 438, 313, 100, 313, 313, 100}},
     98 		{85692, 22026, 21, 200, 1332411, 525, 475, [8]int32{1000, 1000, 875, 625, 200, 625, 625, 200}},
     99 		{85745, 21929, 25, 1, 350, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
    100 		{85745, 21929, 25, 2, 358, 11, 10, [8]int32{2, 2, 2, 2, 1, 2, 2, 1}},
    101 		{85745, 21929, 23, 2, 358, 11, 10, [8]int32{2, 2, 2, 2, 1, 2, 2, 1}},
    102 		{85745, 21929, 25, 20, 3383, 57, 53, [8]int32{25, 25, 22, 16, 6, 16, 16, 6}},
    103 		{85745, 21929, 25, 100, 82704, 265, 240, [8]int32{125, 125, 109, 78, 25, 78, 78, 25}},
    104 		{85745, 21929, 25, 200, 333102, 525, 475, [8]int32{250, 250, 218, 156, 50, 156, 156, 50}},
    105 	} {
    106 		for _, anchor := range []uint32{scenario.low, scenario.high} {
    107 			t.Run(fmt.Sprintf("%d/QL%d/slot%d", anchor, scenario.quality, scenario.slot), func(t *testing.T) {
    108 				world, output := newEquipmentWorld(t)
    109 				player, client := world.characters[1], world.clients[1]
    110 
    111 				player.stats.Abilities.Agility, player.stats.Abilities.Strength = scenario.agility+1, scenario.strength+1
    112 				if err := world.saveUser(player); err != nil {
    113 					t.Fatal(err)
    114 				}
    115 				if granted, err := world.grantItem(player, anchor); err != nil || !granted {
    116 					t.Fatal("grant armor", granted, err)
    117 				}
    118 				player.inventory.Items[1].Quality = scenario.quality
    119 				if err := world.saveInventory(player, player.inventory); err != nil {
    120 					t.Fatal(err)
    121 				}
    122 				weapon := player.inventory.Items[0]
    123 
    124 				for _, ability := range []*uint16{&player.stats.Abilities.Agility, &player.stats.Abilities.Strength} {
    125 					*ability -= 1
    126 					if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 {
    127 						t.Fatal("threshold equality equipped or lost the armor", err)
    128 					}
    129 					*ability += 1
    130 				}
    131 				output.packets = nil
    132 				if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment == nil {
    133 					t.Fatal("qualified armor did not start wear", err)
    134 				}
    135 				world.advanceEquipment(client.equipment.readyAt)
    136 				worn := newTestInventoryServer(t)
    137 				low, high := scenario.low, scenario.high
    138 				if scenario.quality == 1 {
    139 					high = low
    140 				} else if scenario.quality == 200 {
    141 					low = high
    142 				}
    143 				var action []byte
    144 				for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, scenario.slot, 0, 0} {
    145 					action = binary.BigEndian.AppendUint32(action, word)
    146 				}
    147 				if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) {
    148 					t.Fatal("native wear lost the selected quality or endpoint pair")
    149 				}
    150 				if err := world.handleInventory(client, itemMovePacket(1, scenario.slot, 111)[20:]); err != nil || client.equipment == nil {
    151 					t.Fatal("armor did not start removal", err)
    152 				}
    153 				world.advanceEquipment(client.equipment.readyAt)
    154 				removed := newTestInventoryServer(t)
    155 
    156 				expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: scenario.slot, Count: 1}
    157 				if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, expected}) || worn.characters[1].inventory.Items[1].shopPrice() != scenario.value {
    158 					t.Fatal("cold armor identity, quality, value or unrelated item changed")
    159 				}
    160 				expected.Slot = 65
    161 				if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 {
    162 					t.Fatal("removal lost or replaced an instance")
    163 				}
    164 				for index, bonus := range scenario.armor {
    165 					if worn.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 {
    166 						t.Fatal("selected armor effects or their removal changed", index)
    167 					}
    168 				}
    169 			})
    170 		}
    171 	}
    172 }
    173 
    174 func TestKevlarSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) {
    175 	for _, scenario := range []struct{ quality, sale uint32 }{
    176 		{2, 17}, {100, 4135}, {101, 4218}, {200, 16655},
    177 	} {
    178 		t.Run(fmt.Sprint(scenario.quality), func(t *testing.T) {
    179 			world, _ := newShopWorld(t)
    180 			player, client := world.characters[1], world.clients[1]
    181 			if granted, err := world.grantItem(player, 85598); err != nil || !granted {
    182 				t.Fatal(granted, err)
    183 			}
    184 			player.inventory.Items[1].Quality = scenario.quality
    185 			if err := world.saveInventory(player, player.inventory); err != nil {
    186 				t.Fatal(err)
    187 			}
    188 			before := append([]ownedItem(nil), player.inventory.Items...)
    189 			offer := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:]
    190 
    191 			if err := world.handleTrade(client, offer); err != nil {
    192 				t.Fatal(err)
    193 			}
    194 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    195 				t.Fatal(err)
    196 			}
    197 			cancelled := newTestInventoryServer(t).characters[1].inventory
    198 			if err := world.handleShopUse(client, shopUseRequest(2)); err != nil {
    199 				t.Fatal(err)
    200 			}
    201 			if err := world.handleTrade(client, offer); err != nil {
    202 				t.Fatal(err)
    203 			}
    204 			for range 2 {
    205 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    206 					t.Fatal(err)
    207 				}
    208 			}
    209 			sold := newTestInventoryServer(t).characters[1].inventory
    210 
    211 			if !slices.Equal(cancelled.Items, before) || cancelled.Credits != 100 {
    212 				t.Fatal("cancelled sale changed the selected instance or wallet", cancelled)
    213 			}
    214 			if !slices.Equal(sold.Items, before[:1]) || sold.Credits != 100+scenario.sale {
    215 				t.Fatal("accepted or repeated sale changed another item or its selected-quality price", sold)
    216 			}
    217 		})
    218 	}
    219 }