aorecomp

Anarchy Online classic in the browser

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


server/resource_kit_quality_test.go (11747B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"slices"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func newQualityKitWorld(t *testing.T, template, quality uint32) (*server, *combatTestConn) {
     14 	t.Helper()
     15 	world, output := newEquipmentWorld(t)
     16 	player := world.characters[1]
     17 	player.level = 200
     18 	player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     19 	player.stats.Body.BodyDevelopment, player.stats.Body.NanoPool = 6000, 6000
     20 	player.stats.NanoAiding.Treatment, player.stats.NanoAiding.FirstAid, player.stats.NanoAiding.TimeAndSpace = 1500, 1500, 1500
     21 	player.health, player.nano, player.sitting = 1, 1, true
     22 	if err := world.saveUser(player); err != nil {
     23 		t.Fatal(err)
     24 	}
     25 	if granted, err := world.grantItem(player, template); !granted || err != nil {
     26 		t.Fatal(granted, err)
     27 	}
     28 	player.inventory.Items[1].Quality, player.inventory.Items[1].Count = quality, 3
     29 	if err := world.saveInventory(player, player.inventory); err != nil {
     30 		t.Fatal(err)
     31 	}
     32 	world.clients[1].selectedTarget = [2]uint32{50000, 1}
     33 	output.packets = nil
     34 	return world, output
     35 }
     36 
     37 func TestResourceKitSelectedQualityRestorationAndColdOwnership(t *testing.T) {
     38 	for _, scenario := range []struct {
     39 		low, high, quality uint32
     40 		health, nano       [2]uint32
     41 		price, skill       uint32
     42 		seconds            time.Duration
     43 	}{
     44 		{25812, 25811, 1, [2]uint32{30, 30}, [2]uint32{}, 150, 124, 15},
     45 		{25812, 25811, 2, [2]uint32{37, 37}, [2]uint32{}, 151, 124, 15},
     46 		{25812, 25811, 100, [2]uint32{761, 761}, [2]uint32{}, 12487, 124, 15},
     47 		{25812, 25811, 101, [2]uint32{769, 769}, [2]uint32{}, 12737, 124, 15},
     48 		{25812, 25811, 199, [2]uint32{1493, 1493}, [2]uint32{}, 49499, 124, 15},
     49 		{25812, 25811, 200, [2]uint32{1500, 1500}, [2]uint32{}, 50000, 124, 15},
     50 		{85366, 85367, 1, [2]uint32{}, [2]uint32{10, 12}, 20, 123, 40},
     51 		{85366, 85367, 2, [2]uint32{}, [2]uint32{15, 17}, 20, 123, 40},
     52 		{85366, 85367, 100, [2]uint32{}, [2]uint32{503, 504}, 2490, 123, 40},
     53 		{85366, 85367, 101, [2]uint32{}, [2]uint32{507, 508}, 2540, 123, 40},
     54 		{85366, 85367, 199, [2]uint32{}, [2]uint32{995, 995}, 9900, 123, 40},
     55 		{85366, 85367, 200, [2]uint32{}, [2]uint32{1000, 1000}, 10000, 123, 40},
     56 		{25820, 25821, 1, [2]uint32{}, [2]uint32{30, 30}, 250, 131, 20},
     57 		{25820, 25821, 2, [2]uint32{}, [2]uint32{40, 40}, 252, 131, 20},
     58 		{25820, 25821, 100, [2]uint32{}, [2]uint32{1010, 1010}, 24937, 131, 20},
     59 		{25820, 25821, 101, [2]uint32{}, [2]uint32{1020, 1020}, 25439, 131, 20},
     60 		{25820, 25821, 199, [2]uint32{}, [2]uint32{1990, 1990}, 99000, 131, 20},
     61 		{25820, 25821, 200, [2]uint32{}, [2]uint32{2000, 2000}, 100000, 131, 20},
     62 	} {
     63 		for _, anchor := range []uint32{scenario.low, scenario.high} {
     64 			t.Run(fmt.Sprintf("%d/QL%d", anchor, scenario.quality), func(t *testing.T) {
     65 				world, output := newQualityKitWorld(t, anchor, scenario.quality)
     66 				player := world.characters[1]
     67 				weapon := player.inventory.Items[0]
     68 				started := time.Now()
     69 
     70 				if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 65, 1)[20:]); err != nil {
     71 					t.Fatal(err)
     72 				}
     73 				cold := newTestInventoryServer(t)
     74 
     75 				expected := []ownedItem{weapon, {ID: 2, Type: 51005, Template: anchor, Quality: scenario.quality, Slot: 65, Count: 2}}
     76 				if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) ||
     77 					player.health < 1+scenario.health[0] || player.health > 1+scenario.health[1] ||
     78 					player.nano < 1+scenario.nano[0] || player.nano > 1+scenario.nano[1] ||
     79 					cold.characters[1].health != player.health || cold.characters[1].nano != player.nano ||
     80 					player.inventory.Items[1].shopPrice() != scenario.price || world.nextItem != 2 {
     81 					t.Fatal("selected quality lost restoration, price, charges or cold ownership", player.inventory, player.health, player.nano)
     82 				}
     83 				deadline := player.skillLocks[scenario.skill]
     84 				if deadline.Before(started.Add(scenario.seconds*time.Second)) || deadline.After(time.Now().Add(scenario.seconds*time.Second)) ||
     85 					!cold.characters[1].skillLocks[scenario.skill].Equal(deadline) {
     86 					t.Fatal("selected quality changed the authored durable cooldown")
     87 				}
     88 				low, high := scenario.low, scenario.high
     89 				if scenario.quality == 1 {
     90 					high = low
     91 				} else if scenario.quality == 200 {
     92 					low = high
     93 				}
     94 				if len(output.packets) < 2 || binary.BigEndian.Uint32(output.packets[0][29:33]) != 1 ||
     95 					binary.BigEndian.Uint32(output.packets[1][16:20]) != 0x35505644 ||
     96 					binary.BigEndian.Uint32(output.packets[1][29:33]) != low || binary.BigEndian.Uint32(output.packets[1][33:37]) != high ||
     97 					binary.BigEndian.Uint32(output.packets[1][37:41]) != scenario.quality {
     98 					t.Fatal("native use lost selected quality or endpoint identities", output.packets)
     99 				}
    100 			})
    101 		}
    102 	}
    103 }
    104 
    105 func TestResourceKitSelectedQualityStrictSkillThresholds(t *testing.T) {
    106 	for _, scenario := range []struct {
    107 		template, quality uint32
    108 		field             string
    109 		threshold         uint16
    110 	}{
    111 		{25812, 2, "treatment", 11}, {25812, 100, "treatment", 451}, {25811, 101, "treatment", 456},
    112 		{85366, 2, "first aid", 12}, {85366, 100, "first aid", 501}, {85367, 101, "first aid", 506},
    113 		{25820, 2, "treatment", 10}, {25820, 2, "time space", 10},
    114 		{25821, 101, "treatment", 355}, {25821, 101, "time space", 355},
    115 	} {
    116 		t.Run(fmt.Sprintf("%d/QL%d/%s", scenario.template, scenario.quality, scenario.field), func(t *testing.T) {
    117 			world, output := newQualityKitWorld(t, scenario.template, scenario.quality)
    118 			player := world.characters[1]
    119 			field := &player.stats.NanoAiding.Treatment
    120 			switch scenario.field {
    121 			case "first aid":
    122 				field = &player.stats.NanoAiding.FirstAid
    123 			case "time space":
    124 				field = &player.stats.NanoAiding.TimeAndSpace
    125 			}
    126 			*field = scenario.threshold
    127 			if err := world.saveUser(player); err != nil {
    128 				t.Fatal(err)
    129 			}
    130 			before, err := readTestItemStore(world)
    131 			if err != nil {
    132 				t.Fatal(err)
    133 			}
    134 
    135 			err = world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 65, 1)[20:])
    136 			after, readErr := readTestItemStore(world)
    137 
    138 			if err != nil || readErr != nil || !bytes.Equal(before, after) || player.health != 1 || player.nano != 1 ||
    139 				player.inventory.Items[1].Count != 3 || len(player.skillLocks) != 0 || len(output.packets) != 1 ||
    140 				binary.BigEndian.Uint32(output.packets[0][29:33]) != 2 {
    141 				t.Fatal("threshold equality restored resources, consumed a charge or acquired a lock", err, readErr)
    142 			}
    143 
    144 			*field++
    145 			output.packets = nil
    146 			if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 65, 2)[20:]); err != nil {
    147 				t.Fatal(err)
    148 			}
    149 
    150 			if player.inventory.Items[1].Count != 2 || len(output.packets) < 2 || binary.BigEndian.Uint32(output.packets[0][29:33]) != 1 {
    151 				t.Fatal("one point above the selected-quality threshold did not permit use")
    152 			}
    153 		})
    154 	}
    155 }
    156 
    157 func TestResourceKitSelectedQualityRollbackAndReplay(t *testing.T) {
    158 	for _, failure := range []struct{ name, sql string }{
    159 		{"charge error", "CREATE TRIGGER reject_kit BEFORE UPDATE OF count ON items BEGIN SELECT RAISE(FAIL, 'charge'); END"},
    160 		{"ignored charge", "CREATE TRIGGER reject_kit BEFORE UPDATE OF count ON items BEGIN SELECT RAISE(IGNORE); END"},
    161 		{"lock error", "CREATE TRIGGER reject_kit BEFORE INSERT ON skill_locks BEGIN SELECT RAISE(FAIL, 'lock'); END"},
    162 		{"ignored health", "CREATE TRIGGER reject_kit BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(IGNORE); END"},
    163 		{"commit error", "CREATE TABLE pending_kit (id INTEGER REFERENCES items(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_kit AFTER UPDATE OF count ON items BEGIN INSERT INTO pending_kit VALUES (999999); END"},
    164 	} {
    165 		t.Run(failure.name, func(t *testing.T) {
    166 			world, output := newQualityKitWorld(t, 25812, 101)
    167 			player := world.characters[1]
    168 			if _, err := world.db.Exec(failure.sql); err != nil {
    169 				t.Fatal(err)
    170 			}
    171 			before, err := readTestItemStore(world)
    172 			if err != nil {
    173 				t.Fatal(err)
    174 			}
    175 
    176 			failure := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 65, 1)[20:])
    177 			after, readErr := readTestItemStore(world)
    178 
    179 			if failure == nil || readErr != nil || !bytes.Equal(before, after) || len(output.packets) != 0 ||
    180 				player.health != 1 || player.nano != 1 || player.inventory.Items[1].Count != 3 || len(player.skillLocks) != 0 {
    181 				t.Fatal("failed selected-quality use published a partial restoration or debit", failure, readErr)
    182 			}
    183 
    184 			if _, err := world.db.Exec("DROP TRIGGER reject_kit"); err != nil {
    185 				t.Fatal(err)
    186 			}
    187 			for _, sequence := range []uint32{2, 3} {
    188 				if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 65, sequence)[20:]); err != nil {
    189 					t.Fatal(err)
    190 				}
    191 			}
    192 			cold := newTestInventoryServer(t)
    193 
    194 			if player.health != 770 || player.inventory.Items[1].Count != 2 || cold.characters[1].health != 770 ||
    195 				!slices.Equal(cold.characters[1].inventory.Items, player.inventory.Items) || world.nextItem != 2 || len(player.skillLocks) != 1 {
    196 				t.Fatal("retry or immediate locked replay lost or repeated the selected-quality effect")
    197 			}
    198 		})
    199 	}
    200 }
    201 
    202 func TestResourceKitSelectedQualityRejectsUnsupportedUpperContract(t *testing.T) {
    203 	world, output := newQualityKitWorld(t, 25812, 2)
    204 	original := gamedata.ItemTemplates[25811]
    205 	definition := *original
    206 	definition.Sections = slices.Clone(original.Sections)
    207 	functions := slices.Clone(*definition.Sections[0].Functions)
    208 	functions[0].ID = 53051
    209 	definition.Sections[0].Functions = &functions
    210 	gamedata.ItemTemplates[25811] = &definition
    211 	t.Cleanup(func() { gamedata.ItemTemplates[25811] = original })
    212 	before, err := readTestItemStore(world)
    213 	if err != nil {
    214 		t.Fatal(err)
    215 	}
    216 
    217 	err = world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 65, 1)[20:])
    218 	after, readErr := readTestItemStore(world)
    219 
    220 	if err != nil || readErr != nil || !bytes.Equal(before, after) || world.characters[1].health != 1 ||
    221 		world.characters[1].inventory.Items[1].Count != 3 || len(output.packets) != 1 || binary.BigEndian.Uint32(output.packets[0][29:33]) != 2 {
    222 		t.Fatal("a supported lower endpoint enabled part of an unsupported upper contract", err, readErr)
    223 	}
    224 }
    225 
    226 func TestResourceKitSelectedQualityLastChargeRemovesItsNativeEndpoints(t *testing.T) {
    227 	for _, scenario := range []struct{ template, quality, low, high uint32 }{
    228 		{25812, 2, 25812, 25811}, {85367, 2, 85366, 85367}, {25820, 100, 25820, 25821},
    229 	} {
    230 		t.Run(fmt.Sprint(scenario.template), func(t *testing.T) {
    231 			world, output := newQualityKitWorld(t, scenario.template, scenario.quality)
    232 			player := world.characters[1]
    233 			player.inventory.Items[1].Count = 1
    234 			if err := world.saveInventory(player, player.inventory); err != nil {
    235 				t.Fatal(err)
    236 			}
    237 			weapon := player.inventory.Items[0]
    238 
    239 			if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 65, 1)[20:]); err != nil {
    240 				t.Fatal(err)
    241 			}
    242 			cold := newTestInventoryServer(t)
    243 
    244 			var body []byte
    245 			// Native inventory slot 49 represents the server's ordinary slot 65.
    246 			for _, value := range []uint32{112, 104, 49, scenario.low, scenario.high} {
    247 				body = binary.BigEndian.AppendUint32(body, value)
    248 			}
    249 			removed := slices.ContainsFunc(output.packets, func(packet []byte) bool {
    250 				return binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && bytes.Equal(packet[29:49], body)
    251 			})
    252 			if !removed || !slices.Equal(player.inventory.Items, []ownedItem{weapon}) ||
    253 				!slices.Equal(cold.characters[1].inventory.Items, []ownedItem{weapon}) || len(cold.items) != 1 || cold.nextItem != 2 {
    254 				t.Fatalf("last selected-quality charge: removal=%t live=%+v cold=%+v items=%d next=%d packets=%x", removed, player.inventory.Items, cold.characters[1].inventory.Items, len(cold.items), cold.nextItem, output.packets)
    255 			}
    256 		})
    257 	}
    258 }