aorecomp

Anarchy Online classic in the browser

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


server/metaplast_test.go (9122B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"reflect"
      9 	"testing"
     10 )
     11 
     12 func TestMetaplastNativeWearRemoveAndRestart(t *testing.T) {
     13 	for _, piece := range []struct {
     14 		template uint32
     15 		slot     uint32
     16 		armor    [8]uint32
     17 	}{
     18 		{85512, 27, [8]uint32{3, 3, 3, 3, 1, 1, 1, 1}},
     19 		{85557, 18, [8]uint32{3, 3, 3, 3, 1, 1, 1, 1}},
     20 		{85596, 24, [8]uint32{1, 1, 1, 1, 1, 1, 1, 1}},
     21 		{85638, 30, [8]uint32{1, 1, 1, 1, 1, 1, 1, 1}},
     22 		{85686, 21, [8]uint32{5, 5, 5, 5, 1, 1, 1, 1}},
     23 		{85742, 23, [8]uint32{1, 1, 1, 1, 1, 1, 1, 1}},
     24 		{85742, 25, [8]uint32{1, 1, 1, 1, 1, 1, 1, 1}},
     25 		{85511, 27, [8]uint32{625, 546, 625, 468, 93, 93, 93, 93}},
     26 		{85556, 18, [8]uint32{750, 656, 750, 562, 112, 112, 112, 112}},
     27 		{85595, 24, [8]uint32{250, 218, 250, 187, 37, 37, 37, 37}},
     28 		{85637, 30, [8]uint32{375, 328, 375, 281, 56, 56, 56, 56}},
     29 		{85685, 21, [8]uint32{1000, 875, 1000, 750, 150, 150, 150, 150}},
     30 		{85741, 23, [8]uint32{250, 218, 250, 187, 37, 37, 37, 37}},
     31 		{85741, 25, [8]uint32{250, 218, 250, 187, 37, 37, 37, 37}},
     32 	} {
     33 		t.Run(fmt.Sprintf("%s/slot%d", gamedata.ItemTemplates[piece.template].Name, piece.slot), func(t *testing.T) {
     34 			world, conn := newEquipmentWorld(t)
     35 			player := world.characters[1]
     36 			player.stats.Abilities.Strength, player.stats.Abilities.Agility = 8, 8
     37 			if *gamedata.ItemTemplates[piece.template].Quality == 200 {
     38 				player.stats.Abilities.Strength, player.stats.Abilities.Agility = 461, 511
     39 			}
     40 			if err := world.saveUser(player); err != nil {
     41 				t.Fatal(err)
     42 			}
     43 			if granted, err := world.grantItem(player, piece.template); err != nil || !granted {
     44 				t.Fatal("grant armor", granted, err)
     45 			}
     46 			observer := &combatTestConn{}
     47 			world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     48 			world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true}
     49 			original := player.inventory.Items[1]
     50 
     51 			request := inventoryMovePacket(1, 49, piece.slot)[20:]
     52 			decodeInventory1491(request)
     53 			if err := world.handleInventory(world.clients[1], request); err != nil {
     54 				t.Fatal(err)
     55 			}
     56 			if world.clients[1].equipment == nil {
     57 				t.Fatal("catalog armor was rejected")
     58 			}
     59 			world.advanceEquipment(world.clients[1].equipment.readyAt)
     60 			worn := newTestInventoryServer(t).characters[1]
     61 
     62 			expected := original
     63 			expected.Slot = piece.slot
     64 			if !reflect.DeepEqual(worn.inventory.Items, []ownedItem{player.inventory.Items[0], expected}) ||
     65 				len(conn.packets) != 2 || len(observer.packets) != 1 ||
     66 				!bytes.Equal(conn.packets[1][16:], observer.packets[0][16:]) {
     67 				t.Fatalf("wear changed identity or failed to replicate: %+v", worn.inventory.Items)
     68 			}
     69 			initial := equippedItemPackets(worn)
     70 			if len(initial) != 1 || !bytes.Equal(targetPacket(initial[0])[16:], conn.packets[1][16:]) {
     71 				t.Fatal("observer reconnect did not restore exactly one wear action")
     72 			}
     73 			for index, armor := range piece.armor {
     74 				if got := worn.inventory.equipmentBonus(90 + uint32(index)); got != int32(armor) {
     75 					t.Fatalf("AC %d = %d, want %d", 90+index, got, armor)
     76 				}
     77 			}
     78 			forged := itemMovePacket(1, piece.slot, 111)[20:]
     79 			binary.BigEndian.PutUint32(forged[16:20], 104)
     80 			world.handleInventory(world.clients[1], forged)
     81 			if world.clients[1].equipment != nil || len(conn.packets) != 2 {
     82 				t.Fatal("forged inventory page accepted an armor removal")
     83 			}
     84 
     85 			// Remove through the native armor page, including a repeated stale request.
     86 			conn.packets, observer.packets = nil, nil
     87 			remove := itemMovePacket(1, piece.slot, 111)[20:]
     88 			if binary.BigEndian.Uint32(remove[16:20]) != 102 {
     89 				t.Fatal("armor removal used the wrong native page")
     90 			}
     91 			world.handleInventory(world.clients[1], remove)
     92 			if world.clients[1].equipment == nil {
     93 				t.Fatal("armor removal was rejected")
     94 			}
     95 			world.advanceEquipment(world.clients[1].equipment.readyAt)
     96 			world.handleInventory(world.clients[1], remove)
     97 			removed := newTestInventoryServer(t).characters[1]
     98 			if !reflect.DeepEqual(removed.inventory.Items, []ownedItem{player.inventory.Items[0], original}) ||
     99 				len(conn.packets) != 4 || len(observer.packets) != 1 ||
    100 				binary.BigEndian.Uint32(conn.packets[0][45:49]) != 7 ||
    101 				binary.BigEndian.Uint32(conn.packets[0][53:57]) != piece.slot ||
    102 				len(equippedItemPackets(removed)) != 0 {
    103 				t.Fatal("removal lost the armor or repeated its effects")
    104 			}
    105 			for index, action := range []uint32{146, 112} {
    106 				packet := conn.packets[index+2]
    107 				if binary.BigEndian.Uint32(packet[16:20]) != 0x5e477770 || binary.BigEndian.Uint32(packet[29:33]) != action ||
    108 					binary.BigEndian.Uint32(packet[33:37]) != 102 || binary.BigEndian.Uint32(packet[37:41]) != piece.slot {
    109 					t.Fatalf("repeated removal changed more than its empty source: %x", packet)
    110 				}
    111 			}
    112 			for stat := uint32(90); stat <= 97; stat++ {
    113 				if removed.inventory.equipmentBonus(stat) != 0 {
    114 					t.Fatal("removed armor retained AC")
    115 				}
    116 			}
    117 		})
    118 	}
    119 }
    120 
    121 func TestMetaplastRejectedWearPreservesOwnedItem(t *testing.T) {
    122 	for _, template := range []uint32{85511, 85512, 85556, 85557, 85595, 85596, 85637, 85638, 85685, 85686, 85741, 85742} {
    123 		for _, scenario := range []string{"strength", "agility", "quality", "identity", "wrong slot", "foreign owner", "changed requirement", "failed save"} {
    124 			t.Run(gamedata.ItemTemplates[template].Name+"/"+scenario, func(t *testing.T) {
    125 				world, _ := newEquipmentWorld(t)
    126 				player := world.characters[1]
    127 				player.stats.Abilities.Strength, player.stats.Abilities.Agility = 8, 8
    128 				strength, agility := uint16(7), uint16(7)
    129 				if *gamedata.ItemTemplates[template].Quality == 200 {
    130 					player.stats.Abilities.Strength, player.stats.Abilities.Agility = 461, 511
    131 					strength, agility = 460, 510
    132 				}
    133 				if granted, err := world.grantItem(player, template); err != nil || !granted {
    134 					t.Fatal("grant armor", granted, err)
    135 				}
    136 				destination := 16 + itemStat(gamedata.ItemTemplates[template], 88)
    137 				owner := uint32(1)
    138 				switch scenario {
    139 				case "strength":
    140 					player.stats.Abilities.Strength = strength
    141 				case "agility":
    142 					player.stats.Abilities.Agility = agility
    143 				case "quality":
    144 					player.inventory.Items[1].Quality = 201
    145 				case "identity":
    146 					player.inventory.Items[1].Type = 51018
    147 				case "wrong slot":
    148 					destination = 31
    149 				case "foreign owner":
    150 					owner = 2
    151 				}
    152 				before := append([]ownedItem(nil), player.inventory.Items...)
    153 				stored, err := readTestItemStore(world)
    154 				if err != nil {
    155 					t.Fatal(err)
    156 				}
    157 
    158 				if err := world.handleInventory(world.clients[1], inventoryMovePacket(owner, 65, destination)[20:]); err != nil {
    159 					t.Fatal(err)
    160 				}
    161 				if scenario == "changed requirement" || scenario == "failed save" {
    162 					if world.clients[1].equipment == nil {
    163 						t.Fatal("qualifying armor was rejected before interruption")
    164 					}
    165 					if scenario == "changed requirement" {
    166 						player.stats.Abilities.Agility = agility
    167 					} else {
    168 						failDatabaseWrites(t, world)
    169 					}
    170 					world.advanceEquipment(world.clients[1].equipment.readyAt)
    171 				}
    172 				after, err := readTestItemStore(world)
    173 
    174 				if err != nil || !bytes.Equal(stored, after) || world.clients[1].equipment != nil || !reflect.DeepEqual(before, player.inventory.Items) {
    175 					t.Fatal("rejected wear changed an owned item", err)
    176 				}
    177 			})
    178 		}
    179 	}
    180 }
    181 
    182 func TestMetaplastFullSuitKeepsSeparateSleeveContributions(t *testing.T) {
    183 	world, _ := newEquipmentWorld(t)
    184 	player := world.characters[1]
    185 	player.stats.Abilities.Strength, player.stats.Abilities.Agility = 461, 511
    186 	for _, piece := range [][2]uint32{{85556, 18}, {85685, 21}, {85741, 23}, {85741, 25}, {85595, 24}, {85511, 27}, {85637, 30}} {
    187 		if granted, err := world.grantItem(player, piece[0]); err != nil || !granted {
    188 			t.Fatal("grant armor", granted, err)
    189 		}
    190 		if err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 65, piece[1])[20:]); err != nil {
    191 			t.Fatal(err)
    192 		}
    193 		if world.clients[1].equipment == nil {
    194 			t.Fatal("suit piece rejected")
    195 		}
    196 		world.advanceEquipment(world.clients[1].equipment.readyAt)
    197 	}
    198 	var before [8]uint32
    199 	for index := range before {
    200 		before[index] = uint32(player.inventory.equipmentBonus(90 + uint32(index)))
    201 	}
    202 
    203 	world.handleInventory(world.clients[1], itemMovePacket(1, 23, 111)[20:])
    204 	world.advanceEquipment(world.clients[1].equipment.readyAt)
    205 	restarted := newTestInventoryServer(t).characters[1]
    206 	var after [8]uint32
    207 	for index := range after {
    208 		after[index] = uint32(restarted.inventory.equipmentBonus(90 + uint32(index)))
    209 	}
    210 
    211 	if before != [8]uint32{3500, 3059, 3500, 2622, 522, 522, 522, 522} ||
    212 		after != [8]uint32{3250, 2841, 3250, 2435, 485, 485, 485, 485} {
    213 		t.Fatalf("sleeve removal changed other contributions: before=%v after=%v", before, after)
    214 	}
    215 	if len(restarted.inventory.Items) != 8 || len(equippedItemPackets(restarted)) != 6 ||
    216 		restarted.inventory.Items[3].ID != 4 || restarted.inventory.Items[3].Slot != 65 ||
    217 		restarted.inventory.Items[4].ID != 5 || restarted.inventory.Items[4].Slot != 25 {
    218 		t.Fatalf("sleeve instances were lost or interchanged: %+v", restarted.inventory.Items)
    219 	}
    220 }