aorecomp

Anarchy Online classic in the browser

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


server/armor_quality_test.go (9143B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"net/http"
      8 	"net/http/httptest"
      9 	"net/url"
     10 	"reflect"
     11 	"strings"
     12 	"testing"
     13 )
     14 
     15 var metaplastQualityCases = []struct {
     16 	low, high, slot, quality uint32
     17 	strength, agility        uint16
     18 	armor                    [8]uint32
     19 }{
     20 	{85512, 85511, 27, 2, 10, 11, [8]uint32{6, 6, 6, 5, 1, 1, 1, 1}},
     21 	{85557, 85556, 18, 2, 10, 11, [8]uint32{7, 6, 7, 6, 2, 2, 2, 2}},
     22 	{85596, 85595, 24, 2, 10, 11, [8]uint32{2, 2, 2, 2, 1, 1, 1, 1}},
     23 	{85638, 85637, 30, 2, 10, 11, [8]uint32{3, 3, 3, 2, 1, 1, 1, 1}},
     24 	{85686, 85685, 21, 2, 10, 11, [8]uint32{10, 9, 10, 9, 2, 2, 2, 2}},
     25 	{85742, 85741, 23, 2, 10, 11, [8]uint32{2, 2, 2, 2, 1, 1, 1, 1}},
     26 	{85742, 85741, 25, 2, 10, 11, [8]uint32{2, 2, 2, 2, 1, 1, 1, 1}},
     27 	{85512, 85511, 27, 50, 120, 132, [8]uint32{156, 137, 156, 117, 24, 24, 24, 24}},
     28 	{85557, 85556, 18, 50, 120, 132, [8]uint32{187, 164, 187, 141, 28, 28, 28, 28}},
     29 	{85596, 85595, 24, 50, 120, 132, [8]uint32{62, 54, 62, 47, 10, 10, 10, 10}},
     30 	{85638, 85637, 30, 50, 120, 132, [8]uint32{93, 82, 93, 70, 15, 15, 15, 15}},
     31 	{85686, 85685, 21, 50, 120, 132, [8]uint32{250, 219, 250, 188, 38, 38, 38, 38}},
     32 	{85742, 85741, 23, 50, 120, 132, [8]uint32{62, 54, 62, 47, 10, 10, 10, 10}},
     33 	{85742, 85741, 25, 50, 120, 132, [8]uint32{62, 54, 62, 47, 10, 10, 10, 10}},
     34 	{85512, 85511, 27, 100, 233, 258, [8]uint32{312, 273, 312, 234, 47, 47, 47, 47}},
     35 	{85557, 85556, 18, 100, 233, 258, [8]uint32{375, 328, 375, 281, 56, 56, 56, 56}},
     36 	{85596, 85595, 24, 100, 233, 258, [8]uint32{125, 109, 125, 94, 19, 19, 19, 19}},
     37 	{85638, 85637, 30, 100, 233, 258, [8]uint32{187, 164, 187, 140, 28, 28, 28, 28}},
     38 	{85686, 85685, 21, 100, 233, 258, [8]uint32{500, 438, 500, 376, 75, 75, 75, 75}},
     39 	{85742, 85741, 23, 100, 233, 258, [8]uint32{125, 109, 125, 94, 19, 19, 19, 19}},
     40 	{85742, 85741, 25, 100, 233, 258, [8]uint32{125, 109, 125, 94, 19, 19, 19, 19}},
     41 	{85512, 85511, 27, 101, 236, 261, [8]uint32{316, 276, 316, 237, 47, 47, 47, 47}},
     42 	{85557, 85556, 18, 101, 236, 261, [8]uint32{378, 331, 378, 284, 57, 57, 57, 57}},
     43 	{85596, 85595, 24, 101, 236, 261, [8]uint32{126, 110, 126, 94, 19, 19, 19, 19}},
     44 	{85638, 85637, 30, 101, 236, 261, [8]uint32{189, 165, 189, 142, 29, 29, 29, 29}},
     45 	{85686, 85685, 21, 101, 236, 261, [8]uint32{505, 442, 505, 379, 76, 76, 76, 76}},
     46 	{85742, 85741, 23, 101, 236, 261, [8]uint32{126, 110, 126, 94, 19, 19, 19, 19}},
     47 	{85742, 85741, 25, 101, 236, 261, [8]uint32{126, 110, 126, 94, 19, 19, 19, 19}},
     48 	{85512, 85511, 27, 150, 347, 385, [8]uint32{469, 410, 469, 351, 70, 70, 70, 70}},
     49 	{85557, 85556, 18, 150, 347, 385, [8]uint32{562, 492, 562, 422, 84, 84, 84, 84}},
     50 	{85596, 85595, 24, 150, 347, 385, [8]uint32{187, 163, 187, 140, 28, 28, 28, 28}},
     51 	{85638, 85637, 30, 150, 347, 385, [8]uint32{281, 246, 281, 211, 42, 42, 42, 42}},
     52 	{85686, 85685, 21, 150, 347, 385, [8]uint32{750, 656, 750, 563, 113, 113, 113, 113}},
     53 	{85742, 85741, 23, 150, 347, 385, [8]uint32{187, 163, 187, 140, 28, 28, 28, 28}},
     54 	{85742, 85741, 25, 150, 347, 385, [8]uint32{187, 163, 187, 140, 28, 28, 28, 28}},
     55 	{85512, 85511, 27, 199, 459, 508, [8]uint32{622, 543, 622, 466, 93, 93, 93, 93}},
     56 	{85557, 85556, 18, 199, 459, 508, [8]uint32{746, 653, 746, 559, 111, 111, 111, 111}},
     57 	{85596, 85595, 24, 199, 459, 508, [8]uint32{249, 217, 249, 186, 37, 37, 37, 37}},
     58 	{85638, 85637, 30, 199, 459, 508, [8]uint32{373, 326, 373, 280, 56, 56, 56, 56}},
     59 	{85686, 85685, 21, 199, 459, 508, [8]uint32{995, 871, 995, 746, 149, 149, 149, 149}},
     60 	{85742, 85741, 23, 199, 459, 508, [8]uint32{249, 217, 249, 186, 37, 37, 37, 37}},
     61 	{85742, 85741, 25, 199, 459, 508, [8]uint32{249, 217, 249, 186, 37, 37, 37, 37}},
     62 }
     63 
     64 func TestMetaplastIntermediateWearRemoveAndRestart(t *testing.T) {
     65 	for _, piece := range metaplastQualityCases {
     66 		t.Run(fmt.Sprintf("%d/QL%d/slot%d", piece.low, piece.quality, piece.slot), func(t *testing.T) {
     67 			world, conn := newEquipmentWorld(t)
     68 			player, client := world.characters[1], world.clients[1]
     69 			player.stats.Abilities.Strength, player.stats.Abilities.Agility = piece.strength, piece.agility
     70 			if granted, err := world.grantItem(player, piece.low); err != nil || !granted {
     71 				t.Fatal(granted, err)
     72 			}
     73 			player.inventory.Items[1].Quality = piece.quality
     74 			if err := world.saveInventory(player, player.inventory); err != nil {
     75 				t.Fatal(err)
     76 			}
     77 
     78 			if err := world.handleInventory(client, inventoryMovePacket(1, 65, piece.slot)[20:]); err != nil {
     79 				t.Fatal(err)
     80 			}
     81 			if client.equipment == nil {
     82 				t.Fatal("intermediate quality was rejected")
     83 			}
     84 			world.advanceEquipment(client.equipment.readyAt)
     85 			restarted := newTestInventoryServer(t)
     86 			snapshot := targetPacket(fullCharacterPacket(restarted.characters[1]))
     87 
     88 			expected := ownedItem{ID: 2, Type: 51022, Template: piece.low, Quality: piece.quality, Slot: piece.slot, Count: 1}
     89 			if !reflect.DeepEqual(restarted.characters[1].inventory.Items[1], expected) {
     90 				t.Fatal("quality or identity did not persist")
     91 			}
     92 			for i, armor := range piece.armor {
     93 				if got := restarted.characters[1].inventory.equipmentBonus(90 + uint32(i)); got != int32(armor) {
     94 					t.Fatalf("AC%d: got %d, want %d", 90+i, got, armor)
     95 				}
     96 			}
     97 			var action, entry []byte
     98 			for _, v := range []uint32{piece.low, piece.high, piece.quality, 1, 6, 102, piece.slot, 0, 0} {
     99 				action = binary.BigEndian.AppendUint32(action, v)
    100 			}
    101 			for _, v := range []uint32{2, piece.low, piece.high, piece.quality, 0} {
    102 				entry = binary.BigEndian.AppendUint32(entry, v)
    103 			}
    104 			if len(conn.packets) != 2 || !bytes.Equal(conn.packets[1][29:65], action) || !bytes.Contains(snapshot, entry) {
    105 				t.Fatal("native wear or initialization lost the endpoint pair")
    106 			}
    107 
    108 			if err := world.handleInventory(client, itemMovePacket(1, piece.slot, 111)[20:]); err != nil {
    109 				t.Fatal(err)
    110 			}
    111 			if client.equipment == nil {
    112 				t.Fatal("intermediate removal rejected")
    113 			}
    114 			world.advanceEquipment(client.equipment.readyAt)
    115 			removed := newTestInventoryServer(t)
    116 
    117 			expected.Slot = 65
    118 			if removed.characters[1].inventory.Items[1] != expected {
    119 				t.Fatal("removal changed instance or quality")
    120 			}
    121 			for stat := uint32(90); stat < 98; stat++ {
    122 				if removed.characters[1].inventory.equipmentBonus(stat) != 0 {
    123 					t.Fatal("removal retained an interpolated bonus")
    124 				}
    125 			}
    126 		})
    127 	}
    128 }
    129 
    130 func TestMetaplastIntermediateRequirementBoundaries(t *testing.T) {
    131 	for _, piece := range metaplastQualityCases {
    132 		if piece.low != 85557 {
    133 			continue
    134 		}
    135 		for _, ability := range []string{"strength", "agility"} {
    136 			t.Run(fmt.Sprintf("QL%d/%s", piece.quality, ability), func(t *testing.T) {
    137 				world, conn := newEquipmentWorld(t)
    138 				player, client := world.characters[1], world.clients[1]
    139 				player.stats.Abilities.Strength, player.stats.Abilities.Agility = piece.strength, piece.agility
    140 				if ability == "strength" {
    141 					player.stats.Abilities.Strength--
    142 				} else {
    143 					player.stats.Abilities.Agility--
    144 				}
    145 				world.grantItem(player, piece.low)
    146 				player.inventory.Items[1].Quality = piece.quality
    147 				world.saveInventory(player, player.inventory)
    148 				before, err := readTestItemStore(world)
    149 				if err != nil {
    150 					t.Fatal(err)
    151 				}
    152 
    153 				err = world.handleInventory(client, inventoryMovePacket(1, 65, 18)[20:])
    154 				after, readErr := readTestItemStore(world)
    155 
    156 				if err != nil || readErr != nil || client.equipment != nil || !bytes.Equal(before, after) || len(conn.packets) != 2 {
    157 					t.Fatal("requirement boundary accepted or lost its source", err, readErr)
    158 				}
    159 			})
    160 		}
    161 	}
    162 }
    163 
    164 func TestControlMetaplastIntermediateQualityAndNames(t *testing.T) {
    165 	for _, scenario := range []struct {
    166 		template, quality uint32
    167 		status            int
    168 		name              string
    169 	}{
    170 		{85557, 100, 303, "Worn Metaplast Armor Helmet"},
    171 		{85557, 101, 303, "High-Quality Metaplast Armor Helmet"},
    172 		{85556, 100, 303, "Worn Metaplast Armor Helmet"},
    173 		{85556, 101, 303, "High-Quality Metaplast Armor Helmet"},
    174 		{85557, 0, 400, ""}, {85557, 201, 400, ""}, {85556, 511, 400, ""},
    175 		{85535, 100, 400, ""},
    176 	} {
    177 		t.Run(fmt.Sprintf("%d/QL%d", scenario.template, scenario.quality), func(t *testing.T) {
    178 			useNPCData(t, testNPCData())
    179 			world := newTestServer(t)
    180 			postControlForm(world, "/admin/items", url.Values{"template": {fmt.Sprint(scenario.template)}})
    181 			fields := url.Values{"quality": {fmt.Sprint(scenario.quality)}}
    182 			before, _ := readTestItemStore(world)
    183 
    184 			response := postControlForm(world, "/admin/items/1", fields)
    185 			after, err := readTestItemStore(world)
    186 			page := httptest.NewRecorder()
    187 			adminTestHandler(world).ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/1", nil))
    188 
    189 			if response.Code != scenario.status || err != nil {
    190 				t.Fatal(response.Code, response.Body.String(), err)
    191 			}
    192 			if response.Code == http.StatusBadRequest {
    193 				if !bytes.Equal(before, after) {
    194 					t.Fatal("rejected quality changed the store")
    195 				}
    196 				return
    197 			}
    198 			restarted := newTestInventoryServer(t)
    199 			if restarted.items[1].Quality != scenario.quality || !strings.Contains(withoutItemIcons(page.Body.String()), "<h2>"+scenario.name+"</h2>") {
    200 				t.Fatal("interpolated quality or nearest endpoint name was lost")
    201 			}
    202 		})
    203 	}
    204 }