aorecomp

Anarchy Online classic in the browser

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


server/low_tech_set_test.go (12735B)


      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 TestLowTechArmorQualityFamiliesWearAndRecover(t *testing.T) {
     13 	for _, scenario := range []struct {
     14 		low, high, slot, quality, value uint32
     15 		strength, agility               uint16
     16 		armor                           [8]int32
     17 	}{
     18 		{70558, 85640, 30, 1, 142, 7, 7, [8]int32{3, 3, 1, 1, 1, 1, 1, 1}},
     19 		{70558, 85640, 30, 2, 146, 9, 9, [8]int32{5, 5, 2, 1, 1, 1, 1, 1}},
     20 		{70558, 85640, 30, 100, 44270, 227, 252, [8]int32{165, 165, 140, 28, 28, 28, 28, 28}},
     21 		{70558, 85640, 30, 200, 178447, 450, 500, [8]int32{328, 328, 281, 56, 56, 56, 56, 56}},
     22 		{70559, 85689, 21, 1, 500, 7, 7, [8]int32{5, 5, 5, 1, 1, 1, 1, 1}},
     23 		{70559, 85689, 21, 2, 512, 9, 9, [8]int32{9, 9, 9, 2, 2, 2, 2, 2}},
     24 		{70559, 85689, 21, 100, 118148, 227, 252, [8]int32{438, 438, 376, 75, 75, 75, 75, 75}},
     25 		{70559, 85689, 21, 200, 475861, 450, 500, [8]int32{875, 875, 750, 150, 150, 150, 150, 150}},
     26 		{70560, 85688, 21, 1, 500, 7, 7, [8]int32{5, 5, 5, 1, 1, 1, 1, 1}},
     27 		{70560, 85688, 21, 2, 512, 9, 9, [8]int32{9, 9, 9, 2, 2, 2, 2, 2}},
     28 		{70560, 85688, 21, 100, 118148, 227, 252, [8]int32{438, 438, 376, 75, 75, 75, 75, 75}},
     29 		{70560, 85688, 21, 200, 475861, 450, 500, [8]int32{875, 875, 750, 150, 150, 150, 150, 150}},
     30 		{70561, 85744, 23, 1, 125, 7, 7, [8]int32{3, 3, 1, 1, 1, 1, 1, 1}},
     31 		{70561, 85744, 23, 2, 127, 9, 9, [8]int32{4, 4, 2, 1, 1, 1, 1, 1}},
     32 		{70561, 85744, 23, 100, 29536, 227, 252, [8]int32{110, 110, 94, 19, 19, 19, 19, 19}},
     33 		{70561, 85744, 23, 200, 118965, 450, 500, [8]int32{218, 218, 187, 37, 37, 37, 37, 37}},
     34 		{70563, 85558, 18, 1, 375, 7, 7, [8]int32{3, 3, 3, 1, 1, 1, 1, 1}},
     35 		{70563, 85558, 18, 2, 384, 9, 9, [8]int32{6, 6, 6, 2, 2, 2, 2, 2}},
     36 		{70563, 85558, 18, 100, 88611, 227, 252, [8]int32{328, 328, 281, 56, 56, 56, 56, 56}},
     37 		{70563, 85558, 18, 200, 356895, 450, 500, [8]int32{656, 656, 562, 112, 112, 112, 112, 112}},
     38 		{70564, 85515, 27, 1, 312, 7, 7, [8]int32{3, 3, 3, 1, 1, 1, 1, 1}},
     39 		{70564, 85515, 27, 2, 319, 9, 9, [8]int32{6, 6, 5, 1, 1, 1, 1, 1}},
     40 		{70564, 85515, 27, 100, 73842, 227, 252, [8]int32{273, 273, 234, 47, 47, 47, 47, 47}},
     41 		{70564, 85515, 27, 200, 297413, 450, 500, [8]int32{546, 546, 468, 93, 93, 93, 93, 93}},
     42 		{70565, 85514, 27, 1, 312, 7, 7, [8]int32{3, 3, 3, 1, 1, 1, 1, 1}},
     43 		{70565, 85514, 27, 2, 319, 9, 9, [8]int32{6, 6, 5, 1, 1, 1, 1, 1}},
     44 		{70565, 85514, 27, 100, 73842, 227, 252, [8]int32{273, 273, 234, 47, 47, 47, 47, 47}},
     45 		{70565, 85514, 27, 200, 297413, 450, 500, [8]int32{546, 546, 468, 93, 93, 93, 93, 93}},
     46 	} {
     47 		for _, anchor := range []uint32{scenario.low, scenario.high} {
     48 			t.Run(fmt.Sprintf("%d/QL%d", anchor, scenario.quality), func(t *testing.T) {
     49 				world, output := newEquipmentWorld(t)
     50 				player, client := world.characters[1], world.clients[1]
     51 				player.stats.Abilities.Strength, player.stats.Abilities.Agility = scenario.strength+1, scenario.agility+1
     52 				if err := world.saveUser(player); err != nil {
     53 					t.Fatal(err)
     54 				}
     55 				if granted, err := world.grantItem(player, anchor); err != nil || !granted {
     56 					t.Fatal("grant armor", granted, err)
     57 				}
     58 				player.inventory.Items[1].Quality = scenario.quality
     59 				if err := world.saveInventory(player, player.inventory); err != nil {
     60 					t.Fatal(err)
     61 				}
     62 				weapon := player.inventory.Items[0]
     63 
     64 				for _, ability := range []*uint16{&player.stats.Abilities.Strength, &player.stats.Abilities.Agility} {
     65 					*ability -= 1
     66 					if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 {
     67 						t.Fatal("threshold equality equipped or lost the armor", err)
     68 					}
     69 					*ability += 1
     70 				}
     71 				output.packets = nil
     72 				if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment == nil {
     73 					t.Fatal("qualified armor did not start wear", err)
     74 				}
     75 				world.advanceEquipment(client.equipment.readyAt)
     76 				worn := newTestInventoryServer(t)
     77 				low, high := scenario.low, scenario.high
     78 				if scenario.quality == 1 {
     79 					high = low
     80 				} else if scenario.quality == 200 {
     81 					low = high
     82 				}
     83 				var action []byte
     84 				for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, scenario.slot, 0, 0} {
     85 					action = binary.BigEndian.AppendUint32(action, word)
     86 				}
     87 				if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) {
     88 					t.Fatal("native wear lost the selected quality or endpoint pair")
     89 				}
     90 				if err := world.handleInventory(client, itemMovePacket(1, scenario.slot, 111)[20:]); err != nil || client.equipment == nil {
     91 					t.Fatal("armor did not start removal", err)
     92 				}
     93 				world.advanceEquipment(client.equipment.readyAt)
     94 				removed := newTestInventoryServer(t)
     95 
     96 				expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: scenario.slot, Count: 1}
     97 				if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, expected}) || worn.characters[1].inventory.Items[1].shopPrice() != scenario.value {
     98 					t.Fatal("cold armor identity, quality, value or unrelated item changed")
     99 				}
    100 				expected.Slot = 65
    101 				if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 {
    102 					t.Fatal("removal lost or replaced an instance")
    103 				}
    104 				for index, bonus := range scenario.armor {
    105 					if worn.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 {
    106 						t.Fatal("selected armor effects or their removal changed", index)
    107 					}
    108 				}
    109 			})
    110 		}
    111 	}
    112 }
    113 
    114 func TestLowTechCompleteShopSetKeepsBothSleevesAndExactPrice(t *testing.T) {
    115 	for _, terminal := range []struct {
    116 		id     uint32
    117 		side   uint16
    118 		prices [2]uint32
    119 	}{
    120 		{3221226655, 2, [2]uint32{1704, 1742}},
    121 		{3223389340, 1, [2]uint32{1787, 1826}},
    122 		{3222078626, 0, [2]uint32{1787, 1826}},
    123 	} {
    124 		for row, quality := range []uint32{1, 2} {
    125 			t.Run(fmt.Sprintf("%d/QL%d", terminal.id, quality), func(t *testing.T) {
    126 				world, _ := newPurchaseWorld(t)
    127 				player, client := world.characters[1], world.clients[1]
    128 				machine := gamedata.ShopMachines[terminal.id]
    129 				player.playfield, player.position, player.appearance = machine.Playfield, machine.Position, 0x115|terminal.side<<9
    130 				player.stats.Abilities.Strength, player.stats.Abilities.Agility = 10, 10
    131 				player.stats.TradeRepair.ComputerLiteracy = 5
    132 				player.inventory.Credits = terminal.prices[row]
    133 				if err := world.saveUser(player); err != nil {
    134 					t.Fatal(err)
    135 				}
    136 				if err := world.savePosition(player); err != nil {
    137 					t.Fatal(err)
    138 				}
    139 				if err := world.saveInventory(player, player.inventory); err != nil {
    140 					t.Fatal(err)
    141 				}
    142 				weapon := player.inventory.Items[0]
    143 				request := shopUseRequest(2)
    144 				binary.BigEndian.PutUint32(request[44:48], terminal.id)
    145 
    146 				if err := world.handleShopUse(client, request); err != nil {
    147 					t.Fatal(err)
    148 				}
    149 				for _, index := range []uint32{8, 10, 14, 14, 6, 16, 18} {
    150 					if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, terminal.id}, [2]uint32{111, index + uint32(row)})[20:]); err != nil {
    151 						t.Fatal(err)
    152 					}
    153 				}
    154 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    155 					t.Fatal(err)
    156 				}
    157 				// Purchased instances materialize in catalog order, not selection order.
    158 				for index, slot := range []uint32{24, 30, 21, 23, 25, 18, 27} {
    159 					if err := world.handleInventory(client, inventoryMovePacket(1, 65+uint32(index), slot)[20:]); err != nil || client.equipment == nil {
    160 						t.Fatal("set piece did not start wear", index, err)
    161 					}
    162 					world.advanceEquipment(client.equipment.readyAt)
    163 				}
    164 				worn := newTestInventoryServer(t)
    165 				if err := world.handleInventory(client, itemMovePacket(1, 25, 111)[20:]); err != nil || client.equipment == nil {
    166 					t.Fatal("left sleeve did not start removal", err)
    167 				}
    168 				world.advanceEquipment(client.equipment.readyAt)
    169 				removed := newTestInventoryServer(t)
    170 
    171 				expected := []ownedItem{weapon,
    172 					{ID: 2, Type: 51022, Template: 70562, Quality: quality, Slot: 24, Count: 1},
    173 					{ID: 3, Type: 51022, Template: 70558, Quality: quality, Slot: 30, Count: 1},
    174 					{ID: 4, Type: 51022, Template: 70559, Quality: quality, Slot: 21, Count: 1},
    175 					{ID: 5, Type: 51022, Template: 70561, Quality: quality, Slot: 23, Count: 1},
    176 					{ID: 6, Type: 51022, Template: 70561, Quality: quality, Slot: 25, Count: 1},
    177 					{ID: 7, Type: 51022, Template: 70563, Quality: quality, Slot: 18, Count: 1},
    178 					{ID: 8, Type: 51022, Template: 70564, Quality: quality, Slot: 27, Count: 1},
    179 				}
    180 				if !slices.Equal(worn.characters[1].inventory.Items, expected) || worn.characters[1].inventory.Credits != 0 {
    181 					t.Fatal("complete set lost an instance or charged the wrong price", worn.characters[1].inventory)
    182 				}
    183 				expected[5].Slot = 65
    184 				if !slices.Equal(removed.characters[1].inventory.Items, expected) || removed.nextItem != 8 {
    185 					t.Fatal("removing one sleeve changed its partner or another item")
    186 				}
    187 				fullArmor := [][8]int32{{23, 23, 15, 7, 7, 7, 7, 7}, {38, 38, 28, 9, 9, 9, 9, 9}}[row]
    188 				oneSleeve := [][8]int32{{20, 20, 14, 6, 6, 6, 6, 6}, {34, 34, 26, 8, 8, 8, 8, 8}}[row]
    189 				for index := range fullArmor {
    190 					if worn.characters[1].equipmentBonus(90+uint32(index)) != fullArmor[index] || removed.characters[1].equipmentBonus(90+uint32(index)) != oneSleeve[index] {
    191 						t.Fatal("complete-set bonuses or single-sleeve removal changed", index)
    192 					}
    193 				}
    194 			})
    195 		}
    196 	}
    197 }
    198 
    199 func TestLowTechSetSecondSleeveFailureRollsBackTheWholeBasket(t *testing.T) {
    200 	for _, failure := range []struct{ name, sql string }{
    201 		{"item", "CREATE TRIGGER reject_set BEFORE INSERT ON items WHEN NEW.id=6 BEGIN SELECT RAISE(FAIL,'second sleeve'); END"},
    202 		{"ignored placement", "CREATE TRIGGER reject_set BEFORE INSERT ON item_locations WHEN NEW.item_id=6 BEGIN SELECT RAISE(IGNORE); END"},
    203 		{"commit", "CREATE TABLE set_commit_failure (id INTEGER REFERENCES items(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_set AFTER INSERT ON items WHEN NEW.id=6 BEGIN INSERT INTO set_commit_failure VALUES(9999); END"},
    204 	} {
    205 		t.Run(failure.name, func(t *testing.T) {
    206 			world, output := newPurchaseWorld(t)
    207 			player, client := world.characters[1], world.clients[1]
    208 			player.position = gamedata.ShopMachines[3221226655].Position
    209 			player.stats.TradeRepair.ComputerLiteracy = 5
    210 			player.inventory.Credits = 5000
    211 			if err := world.saveUser(player); err != nil {
    212 				t.Fatal(err)
    213 			}
    214 			if err := world.saveInventory(player, player.inventory); err != nil {
    215 				t.Fatal(err)
    216 			}
    217 			request := shopUseRequest(2)
    218 			binary.BigEndian.PutUint32(request[44:48], 3221226655)
    219 			if err := world.handleShopUse(client, request); err != nil {
    220 				t.Fatal(err)
    221 			}
    222 			for _, index := range []uint32{6, 8, 10, 14, 14, 16, 18} {
    223 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221226655}, [2]uint32{111, index})[20:]); err != nil {
    224 					t.Fatal(err)
    225 				}
    226 			}
    227 			before, err := readTestItemStore(world)
    228 			if err != nil {
    229 				t.Fatal(err)
    230 			}
    231 			if _, err := world.db.Exec(failure.sql); err != nil {
    232 				t.Fatal(err)
    233 			}
    234 			output.packets = nil
    235 			accept := shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]
    236 
    237 			failed := world.handleTrade(client, accept)
    238 			after, err := readTestItemStore(world)
    239 			cold := newTestInventoryServer(t)
    240 
    241 			if failed == nil || err != nil || !bytes.Equal(before, after) || len(output.packets) != 0 ||
    242 				player.inventory.Credits != 5000 || len(player.inventory.Items) != 1 || world.nextItem != 1 ||
    243 				cold.characters[1].inventory.Credits != 5000 || len(cold.characters[1].inventory.Items) != 1 || cold.nextItem != 1 {
    244 				t.Fatal("partial set purchase spent credits, leaked an instance or announced success", failed, err)
    245 			}
    246 
    247 			if _, err := world.db.Exec("DROP TRIGGER reject_set"); err != nil {
    248 				t.Fatal(err)
    249 			}
    250 			if err := world.handleTrade(client, accept); err != nil {
    251 				t.Fatal(err)
    252 			}
    253 			retried := newTestInventoryServer(t)
    254 
    255 			if retried.characters[1].inventory.Credits != 3296 || retried.nextItem != 15 || len(retried.characters[1].inventory.Items) != 8 ||
    256 				retried.characters[1].inventory.Items[4] != (ownedItem{ID: 12, Type: 51022, Template: 70561, Quality: 1, Slot: 68, Count: 1}) ||
    257 				retried.characters[1].inventory.Items[5] != (ownedItem{ID: 13, Type: 51022, Template: 70561, Quality: 1, Slot: 69, Count: 1}) {
    258 				t.Fatal("whole-basket retry lost a sleeve, changed its identity or charged twice")
    259 			}
    260 		})
    261 	}
    262 }