server/backpack_quality_test.go (3766B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "net/http/httptest" 8 "net/url" 9 "testing" 10 ) 11 12 func TestBackpackOfSurvivalInterpolatesEffectsAndNativeEndpoints(t *testing.T) { 13 for _, scenario := range []struct { 14 quality, armor, energy, cold, perception uint32 15 }{ 16 {51, 255, 204, 306, 11}, 17 {100, 500, 400, 600, 22}, 18 {101, 505, 404, 606, 22}, 19 {149, 745, 596, 894, 33}, 20 } { 21 for _, template := range []uint32{206730, 206731} { 22 t.Run(fmt.Sprintf("%d/QL%d", template, scenario.quality), func(t *testing.T) { 23 world, conn := newEquipmentWorld(t) 24 player, client := world.characters[1], world.clients[1] 25 player.stats.Body.Adventuring, player.level, player.profession = 1000, 200, 6 26 world.saveUser(player) 27 world.grantItem(player, template) 28 player.inventory.Items[1].Quality = scenario.quality 29 world.saveInventory(player, player.inventory) 30 31 err := world.handleInventory(client, inventoryMovePacket(1, 65, 19)[20:]) 32 if client.equipment == nil { 33 t.Fatal("intermediate quality wear rejected", err) 34 } 35 world.advanceEquipment(client.equipment.readyAt) 36 restarted := newTestInventoryServer(t).characters[1] 37 snapshot := targetPacket(fullCharacterPacket(restarted)) 38 chest := backpackPacket(1, player.playfield, player.inventory.Items[1], 104) 39 40 expected := [8]uint32{scenario.armor, scenario.armor, scenario.energy, scenario.armor, scenario.energy, scenario.cold, scenario.armor, scenario.cold} 41 for index, amount := range expected { 42 if restarted.inventory.equipmentBonus(90+uint32(index)) != int32(amount) { 43 t.Fatal("interpolated AC differs from the native rounding rule", index) 44 } 45 } 46 if restarted.inventory.equipmentBonus(136) != int32(scenario.perception) || len(conn.packets) != 2 || 47 restarted.inventory.Items[1].ID != 2 || restarted.inventory.Items[1].Quality != scenario.quality { 48 t.Fatal("interpolated skill, identity or quality changed") 49 } 50 var entry, action []byte 51 for _, value := range []uint32{2, 206730, 206731, scenario.quality, 0} { 52 entry = binary.BigEndian.AppendUint32(entry, value) 53 } 54 for _, value := range []uint32{206730, 206731, scenario.quality, 1, 6, 102, 19, 0, 0} { 55 action = binary.BigEndian.AppendUint32(action, value) 56 } 57 if !bytes.Contains(snapshot, entry) || !bytes.Equal(conn.packets[1][29:65], action) || 58 !bytes.Contains(chest, binary.BigEndian.AppendUint32(binary.BigEndian.AppendUint32(nil, 702), 206730)) || 59 !bytes.Contains(chest, binary.BigEndian.AppendUint32(binary.BigEndian.AppendUint32(nil, 703), 206731)) { 60 t.Fatal("one native path lost the interpolated endpoint pair") 61 } 62 }) 63 } 64 } 65 } 66 67 func TestControlBackpackOfSurvivalQualityBounds(t *testing.T) { 68 for _, quality := range []uint32{49, 50, 51, 100, 149, 150, 151} { 69 t.Run(fmt.Sprint(quality), func(t *testing.T) { 70 useNPCData(t, testNPCData()) 71 world := newTestServer(t) 72 postControlForm(world, "/admin/items", url.Values{"template": {"206730"}}) 73 before, _ := readTestItemStore(world) 74 fields := url.Values{"quality": {fmt.Sprint(quality)}} 75 76 response := postControlForm(world, "/admin/items/1", fields) 77 after, err := readTestItemStore(world) 78 page := httptest.NewRecorder() 79 adminTestHandler(world).ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/1", nil)) 80 81 if err != nil || page.Code != 200 { 82 t.Fatal(err, page.Code) 83 } 84 if quality == 49 || quality == 151 { 85 if response.Code != 400 || !bytes.Equal(before, after) { 86 t.Fatal("unsupported quality changed the stored container") 87 } 88 } else if response.Code != 303 || newTestInventoryServer(t).items[1].Quality != quality { 89 t.Fatal("supported container quality did not persist", response.Code) 90 } 91 }) 92 } 93 }