aorecomp

Anarchy Online classic in the browser

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


server/cyber_armor_test.go (10424B)


      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 TestCyberGeneratedPickupKeepsSelectedQuality(t *testing.T) {
     13 	world, output := newKillExperienceWorld(t, "unarmed")
     14 	player, client := world.characters[1], world.clients[1]
     15 	player.stats.Abilities.Intelligence, player.stats.Abilities.Psychic = 253, 228
     16 	if err := world.saveUser(player); err != nil {
     17 		t.Fatal(err)
     18 	}
     19 	// Controlled membership tests delivery, not historical Cyber creature drops.
     20 	profile := *world.characters[1000000].combatProfile
     21 	profile.Loot = []gamedata.NPCLoot{{Template: 22166, Quality: [2]uint32{100, 100}, Chance: 100}}
     22 	world.characters[1000000].combatProfile = &profile
     23 	world.combatRoll = func(uint32) uint32 { return 0 }
     24 	identity := world.nextItem + 1
     25 	var wire []byte
     26 	for _, value := range []uint32{identity, 85612, 22166, 100, 0} {
     27 		wire = binary.BigEndian.AppendUint32(wire, value)
     28 	}
     29 
     30 	attackForExperience(t, world, "unarmed")
     31 	if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	shown := slices.ContainsFunc(output.packets, func(packet []byte) bool { return bytes.Contains(packet, wire) })
     35 	for range 2 {
     36 		if err := world.handleInventory(client, lootTakeRequest()); err != nil {
     37 			t.Fatal(err)
     38 		}
     39 	}
     40 	world = newTestInventoryServer(t)
     41 	client = &worldClient{id: 1, conn: output, target: true, ready: true}
     42 	world.clients[1] = client
     43 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 24)[20:]); err != nil || client.equipment == nil {
     44 		t.Fatal("picked-up gloves did not start wear", err)
     45 	}
     46 	world.advanceEquipment(client.equipment.readyAt)
     47 	worn := newTestInventoryServer(t)
     48 	if err := world.handleInventory(client, itemMovePacket(1, 24, 64)[20:]); err != nil || client.equipment == nil {
     49 		t.Fatal("picked-up gloves did not start removal", err)
     50 	}
     51 	world.advanceEquipment(client.equipment.readyAt)
     52 	removed := newTestInventoryServer(t)
     53 
     54 	expected := ownedItem{ID: identity, Type: 51022, Template: 22166, Quality: 100, Slot: 24, Count: 1}
     55 	if !shown || !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{expected}) || worn.characters[1].equipmentBonus(91) != 125 {
     56 		t.Fatal("generated pickup lost its native endpoints, identity, selected quality or worn AC")
     57 	}
     58 	expected.Slot = 64
     59 	if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{expected}) || removed.characters[1].equipmentBonus(91) != 0 ||
     60 		len(removed.characters[1000000].corpse.items) != 0 || removed.nextItem != identity {
     61 		t.Fatal("duplicate pickup, removal or cold recovery lost or duplicated the gloves")
     62 	}
     63 }
     64 
     65 func TestCyberArmorQualitiesWearRemoveAndRecover(t *testing.T) {
     66 	for _, scenario := range []struct {
     67 		low, high, slot, quality, value uint32
     68 		intelligence, psychic           uint16
     69 		armor                           [8]int32
     70 	}{
     71 		{85477, 21917, 25, 1, 300, 7, 7, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     72 		{85477, 21917, 25, 2, 307, 9, 9, [8]int32{2, 2, 2, 1, 1, 2, 1, 2}},
     73 		{85477, 21917, 23, 2, 307, 9, 9, [8]int32{2, 2, 2, 1, 1, 2, 1, 2}},
     74 		{85477, 21917, 25, 100, 70888, 252, 227, [8]int32{125, 125, 109, 25, 25, 78, 25, 78}},
     75 		{85477, 21917, 25, 200, 285516, 500, 450, [8]int32{250, 250, 218, 50, 50, 156, 50, 156}},
     76 		{85531, 22289, 27, 1, 750, 7, 7, [8]int32{3, 3, 3, 1, 1, 3, 1, 3}},
     77 		{85531, 22289, 27, 2, 768, 9, 9, [8]int32{6, 6, 6, 2, 2, 5, 2, 5}},
     78 		{85531, 22289, 27, 100, 177223, 252, 227, [8]int32{312, 312, 273, 63, 63, 196, 63, 196}},
     79 		{85531, 22289, 27, 200, 713791, 500, 450, [8]int32{625, 625, 546, 125, 125, 390, 125, 390}},
     80 		{85572, 22219, 18, 1, 900, 7, 7, [8]int32{3, 3, 3, 1, 1, 3, 1, 3}},
     81 		{85572, 22219, 18, 2, 921, 9, 9, [8]int32{7, 7, 6, 2, 2, 5, 2, 5}},
     82 		{85572, 22219, 18, 100, 212667, 252, 227, [8]int32{375, 375, 328, 75, 75, 234, 75, 234}},
     83 		{85572, 22219, 18, 200, 856550, 500, 450, [8]int32{750, 750, 656, 150, 150, 468, 150, 468}},
     84 		{85612, 22166, 24, 1, 300, 7, 7, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     85 		{85612, 22166, 24, 2, 307, 9, 9, [8]int32{2, 2, 2, 1, 1, 2, 1, 2}},
     86 		{85612, 22166, 24, 5, 415, 17, 16, [8]int32{6, 6, 5, 2, 2, 4, 2, 4}},
     87 		{85612, 22166, 24, 100, 70888, 252, 227, [8]int32{125, 125, 109, 25, 25, 78, 25, 78}},
     88 		{85612, 22166, 24, 101, 72322, 255, 230, [8]int32{126, 126, 110, 26, 26, 79, 26, 79}},
     89 		{85612, 22166, 24, 199, 282657, 498, 448, [8]int32{249, 249, 217, 50, 50, 155, 50, 155}},
     90 		{85612, 22166, 24, 200, 285516, 500, 450, [8]int32{250, 250, 218, 50, 50, 156, 50, 156}},
     91 		{85655, 22104, 30, 1, 450, 7, 7, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     92 		{85655, 22104, 30, 2, 460, 9, 9, [8]int32{3, 3, 3, 1, 1, 2, 1, 2}},
     93 		{85655, 22104, 30, 100, 106334, 252, 227, [8]int32{187, 187, 164, 38, 38, 117, 38, 117}},
     94 		{85655, 22104, 30, 200, 428275, 500, 450, [8]int32{375, 375, 328, 75, 75, 234, 75, 234}},
     95 		{85711, 22014, 21, 1, 1200, 7, 7, [8]int32{5, 5, 5, 1, 1, 5, 1, 5}},
     96 		{85711, 22014, 21, 2, 1228, 9, 9, [8]int32{10, 10, 9, 2, 2, 8, 2, 8}},
     97 		{85711, 22014, 21, 100, 283557, 252, 227, [8]int32{500, 500, 438, 100, 100, 313, 100, 313}},
     98 		{85711, 22014, 21, 200, 1142067, 500, 450, [8]int32{1000, 1000, 875, 200, 200, 625, 200, 625}},
     99 		{85712, 22010, 21, 1, 1200, 7, 7, [8]int32{5, 5, 5, 1, 1, 5, 1, 5}},
    100 		{85712, 22010, 21, 2, 1228, 9, 9, [8]int32{10, 10, 9, 2, 2, 8, 2, 8}},
    101 		{85712, 22010, 21, 100, 283557, 252, 227, [8]int32{500, 500, 438, 100, 100, 313, 100, 313}},
    102 		{85712, 22010, 21, 200, 1142067, 500, 450, [8]int32{1000, 1000, 875, 200, 200, 625, 200, 625}},
    103 	} {
    104 		for _, anchor := range []uint32{scenario.low, scenario.high} {
    105 			t.Run(fmt.Sprintf("%d/QL%d/slot%d", anchor, scenario.quality, scenario.slot), func(t *testing.T) {
    106 				world, output := newEquipmentWorld(t)
    107 				player, client := world.characters[1], world.clients[1]
    108 				player.stats.Abilities.Intelligence, player.stats.Abilities.Psychic = scenario.intelligence+1, scenario.psychic+1
    109 				if err := world.saveUser(player); err != nil {
    110 					t.Fatal(err)
    111 				}
    112 				if granted, err := world.grantItem(player, anchor); err != nil || !granted {
    113 					t.Fatal("grant armor", granted, err)
    114 				}
    115 				player.inventory.Items[1].Quality = scenario.quality
    116 				if err := world.saveInventory(player, player.inventory); err != nil {
    117 					t.Fatal(err)
    118 				}
    119 				weapon := player.inventory.Items[0]
    120 
    121 				for _, ability := range []*uint16{&player.stats.Abilities.Intelligence, &player.stats.Abilities.Psychic} {
    122 					*ability -= 1
    123 					if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 {
    124 						t.Fatal("threshold equality equipped or lost the armor", err)
    125 					}
    126 					*ability += 1
    127 				}
    128 				output.packets = nil
    129 				if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment == nil {
    130 					t.Fatal("qualified armor did not start wear", err)
    131 				}
    132 				world.advanceEquipment(client.equipment.readyAt)
    133 				worn := newTestInventoryServer(t)
    134 				low, high := scenario.low, scenario.high
    135 				if scenario.quality == 1 {
    136 					high = low
    137 				} else if scenario.quality == 200 {
    138 					low = high
    139 				}
    140 				var action []byte
    141 				for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, scenario.slot, 0, 0} {
    142 					action = binary.BigEndian.AppendUint32(action, word)
    143 				}
    144 				if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) {
    145 					t.Fatal("native wear lost the selected quality or endpoint pair")
    146 				}
    147 				if err := world.handleInventory(client, itemMovePacket(1, scenario.slot, 111)[20:]); err != nil || client.equipment == nil {
    148 					t.Fatal("armor did not start removal", err)
    149 				}
    150 				world.advanceEquipment(client.equipment.readyAt)
    151 				removed := newTestInventoryServer(t)
    152 
    153 				expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: scenario.slot, Count: 1}
    154 				if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, expected}) || worn.characters[1].inventory.Items[1].shopPrice() != scenario.value {
    155 					t.Fatal("cold armor identity, quality, value or unrelated item changed")
    156 				}
    157 				expected.Slot = 65
    158 				if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 {
    159 					t.Fatal("removal lost or replaced an instance")
    160 				}
    161 				for index, bonus := range scenario.armor {
    162 					if worn.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 {
    163 						t.Fatal("selected armor effects or their removal changed", index)
    164 					}
    165 				}
    166 			})
    167 		}
    168 	}
    169 }
    170 
    171 func TestCyberSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) {
    172 	for _, scenario := range []struct{ quality, sale uint32 }{
    173 		{2, 15}, {100, 3544}, {101, 3616}, {200, 14275},
    174 	} {
    175 		t.Run(fmt.Sprint(scenario.quality), func(t *testing.T) {
    176 			world, _ := newShopWorld(t)
    177 			player, client := world.characters[1], world.clients[1]
    178 			if granted, err := world.grantItem(player, 85612); err != nil || !granted {
    179 				t.Fatal(granted, err)
    180 			}
    181 			player.inventory.Items[1].Quality = scenario.quality
    182 			if err := world.saveInventory(player, player.inventory); err != nil {
    183 				t.Fatal(err)
    184 			}
    185 			before := append([]ownedItem(nil), player.inventory.Items...)
    186 			offer := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:]
    187 
    188 			if err := world.handleTrade(client, offer); err != nil {
    189 				t.Fatal(err)
    190 			}
    191 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    192 				t.Fatal(err)
    193 			}
    194 			cancelled := newTestInventoryServer(t).characters[1].inventory
    195 			if err := world.handleShopUse(client, shopUseRequest(2)); err != nil {
    196 				t.Fatal(err)
    197 			}
    198 			if err := world.handleTrade(client, offer); err != nil {
    199 				t.Fatal(err)
    200 			}
    201 			for range 2 {
    202 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    203 					t.Fatal(err)
    204 				}
    205 			}
    206 			sold := newTestInventoryServer(t).characters[1].inventory
    207 
    208 			if !slices.Equal(cancelled.Items, before) || cancelled.Credits != 100 {
    209 				t.Fatal("cancelled sale changed the selected instance or wallet", cancelled)
    210 			}
    211 			if !slices.Equal(sold.Items, before[:1]) || sold.Credits != 100+scenario.sale {
    212 				t.Fatal("accepted or repeated sale changed another item or its selected-quality price", sold)
    213 			}
    214 		})
    215 	}
    216 }