aorecomp

Anarchy Online classic in the browser

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


server/stack_family_test.go (3323B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"slices"
      7 	"testing"
      8 )
      9 
     10 func TestStackJoinUsesReviewedSelectedEndpoints(t *testing.T) {
     11 	for _, family := range [][2]uint32{{25812, 25811}, {85366, 85367}, {25820, 25821}} {
     12 		for _, quality := range []uint32{1, 2, 101, 200} {
     13 			for _, location := range []string{"carried", "bank", "backpack"} {
     14 				for _, kept := range []int{0, 1} {
     15 					t.Run(fmt.Sprintf("%d/QL%d/%s/keep%d", family[0], quality, location, kept), func(t *testing.T) {
     16 						world, conn := newStackWorld(t)
     17 						player, client := world.characters[1], world.clients[1]
     18 						player.inventory.Items[0].Template, player.inventory.Items[1].Template = family[0], family[1]
     19 						player.inventory.Items[0].Quality, player.inventory.Items[1].Quality = quality, quality
     20 						player.inventory.Items[0].Count, player.inventory.Items[1].Count = 3, 5
     21 						items := slices.Clone(player.inventory.Items)
     22 						page, packed := uint32(104), uint32(0)
     23 						switch location {
     24 						case "bank":
     25 							page = 105
     26 							items[0].Slot, items[1].Slot = 0, 1
     27 							player.inventory.Items, player.inventory.Bank = nil, items
     28 							client.bank = &bankSession{player.playfield, player.position}
     29 						case "backpack":
     30 							if granted, err := world.grantItem(player, 99228); err != nil || !granted {
     31 								t.Fatal(granted, err)
     32 							}
     33 							page, packed = 107, 1<<16
     34 							items[0].Slot, items[1].Slot = 0, 1
     35 							player.inventory.Items = player.inventory.Items[2:]
     36 							player.inventory.Containers = map[uint32][]ownedItem{3: items}
     37 							client.backpacks = map[uint32]backpackHandle{1: {3, true}}
     38 						}
     39 						if err := world.saveInventory(player, player.inventory); err != nil {
     40 							t.Fatal(err)
     41 						}
     42 						removed := 1 - kept
     43 						request := characterActionPacket(1, [5]uint32{53, page, packed | items[kept].Slot, page, packed | items[removed].Slot})[20:]
     44 						expected := items[kept]
     45 						expected.Count = 8
     46 						conn.packets = nil
     47 
     48 						for range 2 {
     49 							if err := world.handleInventory(client, request); err != nil {
     50 								t.Fatal(err)
     51 							}
     52 						}
     53 						cold := newTestInventoryServer(t)
     54 
     55 						for _, current := range []*server{world, cold} {
     56 							actual := current.characters[1].inventory.Items
     57 							switch location {
     58 							case "bank":
     59 								actual = current.characters[1].inventory.Bank
     60 							case "backpack":
     61 								actual = current.characters[1].inventory.Containers[3]
     62 							}
     63 							if !slices.Equal(actual, []ownedItem{expected}) || current.nextItem != world.nextItem {
     64 								t.Fatal("reviewed-anchor join lost selected quantities or identity", actual)
     65 							}
     66 							if _, exists := current.items[items[removed].ID]; exists {
     67 								t.Fatal("joined source retained a duplicate physical instance")
     68 							}
     69 						}
     70 						acknowledgements, rebuilds := 0, 0
     71 						for _, packet := range conn.packets {
     72 							if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 53 {
     73 								acknowledgements++
     74 							}
     75 							if binary.BigEndian.Uint32(packet[16:20]) == 0x052e2f0c {
     76 								rebuilds++
     77 							}
     78 						}
     79 						if acknowledgements != 1 || (rebuilds == 1) != (location == "carried") || rebuilds > 1 {
     80 							t.Fatal("reviewed-anchor join lost its single native acknowledgement")
     81 						}
     82 					})
     83 				}
     84 			}
     85 		}
     86 	}
     87 }